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

current lat 41.8781136
current lon -87.6297982 
lower left lat  40.3781136
lower left lon  -89.8743648   
upper right lat 43.3781136
upper right lon -85.3852316

New York

current lat 40.7127753
current lon -74.0059728
lower left lat  39.2127753
lower left lon  -76.26396172
upper right lat 42.2127753
upper right lon -71.74798388

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:

SELECT ST_AsText(ST_MakeEnvelope(-89.8743648,40.3781136,-85.3852316,43.3781136,4326));

                                                           st_astext                                                           
-------------------------------------------------------------------------------------------------------------------------------
 POLYGON((-89.8743648 40.3781136,-89.8743648 43.3781136,-85.3852316 43.3781136,-85.3852316 40.3781136,-89.8743648 40.3781136))
(1 Zeile)

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:

SELECT ST_Extent('POLYGON((-89.8743648 40.3781136,-89.8743648 43.3781136,-85.3852316 43.3781136,-85.3852316 40.3781136,-89.8743648 40.3781136))')

                     st_extent                      
----------------------------------------------------
 BOX(-89.8743648 40.3781136,-85.3852316 43.3781136)
(1 Zeile)

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.

SELECT 
  ST_AsText(
   ST_Envelope(
    ST_Buffer(
     ST_GeomFromText('POINT (-87.6297982 41.8781136)',4326),1)));

 st_astext                                                           
-------------------------------------------------------------------------------------------------------------------------------
 POLYGON((-88.6297982 40.8781136,-88.6297982 42.8781136,-86.6297982 42.8781136,-86.6297982 40.8781136,-88.6297982 40.8781136))
(1 Zeile)

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:

SELECT 
  ST_AsText(
  ST_Envelope(
   ST_Rotate(
    ST_Buffer(
     ST_GeomFromText('POINT (-87.6297982 41.8781136)',4326)::GEOGRAPHY,160934)::GEOMETRY,0)));

 st_astext                                                                                        
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 POLYGON((-89.5681600538661 40.4285062983098,-89.5681600538661 43.327349928921,-85.6903925527536 43.327349928921,-85.6903925527536 40.4285062983098,-89.5681600538661 40.4285062983098))
(1 Zeile)

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.

SELECT 
  ST_Extent(
   ST_Envelope(
    ST_Rotate(
     ST_Buffer(
      ST_GeomFromText('POINT (-87.6297982 41.8781136)',4326)::GEOGRAPHY,160934)::GEOMETRY,0)));

 st_extent                                 
---------------------------------------------------------------------------
 BOX(-89.5681600538661 40.4285062983098,-85.6903925527536 43.327349928921)
(1 Zeile)

Further reading:

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