[英]Convert between coordinate systems with GeoDjango
I'm trying to add coordinate information to my database, adding django.contrib.gis
support to my app.我正在尝试将坐标信息添加到我的数据库中,将django.contrib.gis
支持添加到我的应用程序中。 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).我正在编写一个south
数据迁移,它从数据库中获取地址,并向谷歌询问坐标(到目前为止,我认为我最好的选择是为此使用geopy
)。
Next I need to convert the returned coordinates from WGS84:4326
, Google's coordinate system, to WGS84:22186
, my coordinate system.接下来,我需要将返回的坐标从 Google 的坐标系WGS84:4326
转换为我的坐标系WGS84:22186
。
I'm lost among the GeoDjango docs trying to find a way to do this.我迷失在 GeoDjango 文档中,试图找到一种方法来做到这一点。 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
object .. 似乎被 GDAL 的数据对象使用,但这对于我想要做的事情来说太过分了..
If you have all your libraries installed correctly there is no need to use the CoordTransform
object, the point object's transform
method will do the work for you if you know your desired srid
value.如果您正确安装了所有库,则无需使用CoordTransform
object,如果您知道所需的srid
值,点对象的transform
方法将为您完成工作。
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(30, 50, srid=4326)
>>> desired_srid = 22186
>>> pnt.transform(desired_srid)
>>> pnt.ewkt
u'SRID=22186;POINT (11160773.5712331663817167 19724623.9116888605058193)'
CoordTransform wouldn't work without GDAL that's true.如果没有 GDAL,CoordTransform 将无法工作,这是真的。 But the rest of it is simple enough:但是它的 rest 很简单:
>>> 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.请注意,该点已就地转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.