使用GeoDjango在坐标系之间转换 [英] Convert between coordinate systems with GeoDjango
问题描述
我试图将坐标信息添加到我的数据库,并在我的应用程序中添加 django.contrib.gis
支持。我正在写一个南部
数据迁移,它从数据库中获取地址,并要求Google提供坐标(到目前为止,我认为我最好的选择是使用 geopy
)。
接下来,我需要将返回的坐标从 WGS84:4326
,谷歌的坐标系统,到 WGS84:22186
,我的坐标系。
我迷路了GeoDjango文档试图找到一种方法来做到这一点。到目前为止,我收集我需要这样做:
gcoord = SpatialReference(4326)
mycoord = SpatialReference (22186)
trans = CoordTransform(gcoord,mycoord)
但是,不知道如何使用 CoordTransform
对象..似乎被GDAL的数据对象使用,但是这对我想要做的事情来说是过度的。
如果没有GDAL,CoordTransform将无法正常工作。但其余的很简单:
>>> from django.contrib.gis.gdal import SpatialReference,CoordTransform
>>> from django.contrib.gis.geos import Point
>>> gcoord = SpatialReference(4326)
>>> mycoord = SpatialReference(22186)
>>> trans = CoordTransform(gcoord,mycoord)
>>> pnt = Point(30,50,srid = 4326)
>>>打印'x:%s; y:%s; srid:%s'%(pnt.x,pnt.y,pnt.srid)
x:30.0; y:50.0; srid:4326
>>> pnt.transform(trans)
>>>打印'x:%s; y:%s; srid:%s'%(pnt.x,pnt.y,pnt.srid)
x:11160773.5712; y:19724623.9117; srid:22186
请注意,点已转换到位。
I'm trying to add coordinate information to my database, adding django.contrib.gis
support to my app. I'm writing a south
data migration that takes the addresses from the database, and asks Google for the coordinates (so far I think my best bet is to use geopy
for this).
Next I need to convert the returned coordinates from WGS84:4326
, Google's coordinate system, to WGS84:22186
, my coordinate system.
I'm lost among the GeoDjango docs trying to find a way to do this. This far, I gather I need to do this:
gcoord = SpatialReference("4326")
mycoord = SpatialReference("22186")
trans = CoordTransform(gcoord, mycoord)
but then, I don't know how to use that CoordTransform
object.. seems to be used by GDAL's data objects, but that's overkill for what I want to do..
CoordTransform wouldn't work without GDAL that's true. But the rest of it is simple enough:
>>> from django.contrib.gis.gdal import SpatialReference, CoordTransform
>>> from django.contrib.gis.geos import Point
>>> gcoord = SpatialReference(4326)
>>> mycoord = SpatialReference(22186)
>>> trans = CoordTransform(gcoord, mycoord)
>>> pnt = Point(30, 50, srid=4326)
>>> print 'x: %s; y: %s; srid: %s' % (pnt.x, pnt.y, pnt.srid)
x: 30.0; y: 50.0; srid: 4326
>>> pnt.transform(trans)
>>> print 'x: %s; y: %s; srid: %s' % (pnt.x, pnt.y, pnt.srid)
x: 11160773.5712; y: 19724623.9117; srid: 22186
Note that point is transformed in place.
这篇关于使用GeoDjango在坐标系之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!