简体   繁体   中英

Calculate bounding box for linestring containing geographic coordinates

I calculated linestring from google maps directions api. I converted the linestring to GEOSGeometry object. I need to another region which covers all the points at a distance of 'd' from the linestring object. The distance is in m, km. GEOS API provides GEOSGeometry.buffer(width, quadsegs=8) to do so which works well in 2-D projection.

But how to do so for spherical model ? Is it related to SRID.

from django.contrib.gis.geos import LineString
from django.contrib.gis.geos import GEOSGeometry

directions = maps_client.directions(source, destination)
overview_polyline = decode_polyline(directions[0]['overview_polyline'])

linestring_obj = LineString(overview_polyline)

# FOR 2-D projection
bounding_box = linestring_obj.buffer(width=100) 

# For spherical model
# ???

For geographical distance in meters to make sense, you will always have to go through a projected coordinate system, so I would suggest that you transform your data to a projected coordinate system, create the buffer and project it back. For instance:

# Specify the original srid of your data
orig_srid = 4326

# Create the linestring with the correct srid
linestring_obj = LineString(overview_polyline, srid=orig_srid)

# Transform (project) the linestring into a projected coorinate system
linestring_obj.transform(3857)

# Compute bbox in in that system
bounding_box = linestring_obj.buffer(width=100)

# Transform bounding box into the original coorinate system of your data
bounding_box.transform(orig_srid)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM