Skip to content
Advertisement

Calculating bounding box from latitude and longitude pairs

Google returns the current lat and lon, but the lower left lat and lon and upper right lat and lon are calculated. I have not been able to figure out how it’s calculated.

Does anyone know if it’s a static distance? a percentage difference? How is it calculated?

Chicago, IL

New York

UPDATE:

Knowing it’s 103.5 miles from center to the left side and right side,and knowing its 103.5 miles from center to the top and bottom boundry.

Using the current lat and lon point, how do I calculate the lower left and upper right points?

I assume PostGIS has a function for this?

Advertisement

Answer

As the data suggests, the BBOX is calculated based on the lower left and upper right coordinates. You can create a BBOX by replicating the x and y coordinates from the existing to the missing corners, that is from lower left and upper right to upper left and lower right, e.g. the y value of the upper left corner is the same as the one at the upper right corner.

Using PostGIS you can pass this data to the ST_Envelope function and it will generate a BBOX automatically.

Chicago BBOX:

enter image description here

If you reverse engineer this polygon with the function ST_Extent you’ll get the same coordinate pairs you provided to generate it:

Creating a BBOX based on a point

An easy approach to crate a BBOX around a point is to draw a buffer with ST_Buffer and use it as a parameter with the ST_Envelope function, e.g. POINT(-87.6297982 41.8781136) – Chicago, IL.

enter image description here

In case you’re wondering why the BBOX does not have the same size in all dimensions: Calculations using GEOMETRY and GEOGRAPHY are made differently, and so are their results. GEOGRAPHY calculates the coordinates over an spherical surface (which can be much slower than GEOMETRY) and uses meters as unit of measurement, while GEOGRAPHY uses a planar projection and uses the SRS unit.

Create a 100 miles (160.934 km) BBOX around a point:

enter image description here

Extracting only the lower left and upper right corners

In case you’re only interested in the lower left and upper right corners of your BBOX, just use ST_Extent as described above.

Further reading:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement