计算包含地理坐标的线串的边界框

时间:2015-01-12 12:21:43

标签: python python-3.x geodjango geos

我从谷歌地图方向api计算了线串。 我将线串转换为GEOSGeometry对象。我需要另一个覆盖所有地点的区域,距离“d'来自linestring对象。 距离以m,km为单位。 GEOS API提供了GEOSGeometry.buffer(width,quadsegs = 8)来完成二维投影。

但球形模型怎么办呢?它与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
# ???

1 个答案:

答案 0 :(得分:1)

对于有意义的地理距离(以米为单位),您将始终必须通过投影坐标系,因此我建议您将数据转换为投影坐标系,创建缓冲区并将其投影回来。例如:

# 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)