Prompt
I need compute near branches and warehouses for local processing
Latest observation
Oct 26, 2026
OpenAI APIWeb search: off
Sure — if you want to compute the nearest branches and warehouses for local processing, the usual approach is:
- Store each branch/warehouse with latitude and longitude
- Store the customer / order / site location with latitude and longitude
- Compute distance between the local point and all candidate branches/warehouses
- Sort by distance and pick the nearest ones
Common methods
1) Haversine formula
Best if you only have GPS coordinates and want straight-line distance.
from math import radians, sin, cos, sqrt, atan2
def haversine(lat1, lon1, lat2, lon2):
R = 6371 # Earth radius in km
dlat = radians(lat2 - lat1)
dlon = radians(lon2 - lon1)
a = sin(dlat/2)**2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon/2)**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
return R * c
Then:
points = [
{"name": "Branch A", "lat": 14.5995, "lon": 120.9842},
{"name": "Warehouse B", "lat": 14.6760, "lon": 121.0437},
]
target_lat = 14.6100
target_lon = 120.9900
distances = []
for p in points:
d = haversine(target_lat, target_lon, p["lat"], p["lon"])
distances.append((p["name"], d))
distances.sort(key=lambda x: x[1])
print(distances)
2) Database query with geospatial support
If you're using PostgreSQL + PostGIS, this is ideal for production.
Example query:
SELECT
name,
ST_Distance(
geography(ST_MakePoint(lon, lat)),
geography(ST_MakePoint(:target_lon, :target_lat))
) AS distance_meters
FROM locations
ORDER BY distance_meters
LIMIT 5;
For local processing
If by “local processing” you mean:
- on-device,
- in a local service,
- or inside a warehouse/branch selection engine,
then the flow is usually:
- fetch local branch/warehouse list
- filter by service area / inventory / capacity
- compute nearest candidates
- choose the best one based on:
- distance
- stock availability
- processing capacity
- service rules
Better selection logic
You may want a scoring formula like:
score = (distance_weight * normalized_distance)
+ (stock_weight * stock_penalty)
+ (capacity_weight * capacity_penalty)
So the nearest location is not always the final choice.
If you want, I can help you with one of these:
- Python code to find nearest branches/warehouses
- SQL query for PostGIS
- Excel formula
- API design for local processing
- A more advanced algorithm with stock + distance + capacity
If you share your data structure, I can write the exact code for you.
Brands