Transformation between coordinate systems with GeoDjango

I am trying to add coordinate information to my database by adding django.contrib.gis support to my application. I am writing a south data migration that takes addresses from a database and asks Google for coordinates (so far I believe that it is best 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 got lost among the GeoDjango docs, trying to find a way to do this. As far as I know, I have to do this:

 gcoord = SpatialReference("4326") mycoord = SpatialReference("22186") trans = CoordTransform(gcoord, mycoord) 

but then I don’t know how to use this CoordTransform .. object, it seems to be used by GDAL data objects, but this is an overflow for what I want to do.

+4
source share
2 answers

CoordTransform will not work without GDAL, which is true. But everything else is quite simple:

 >>> 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 the point is converted to a place.

+5
source

If all your libraries are installed correctly, there is no need to use the CoordTransform object, the transform point object method will do your job if you know your desired srid value.

 >>> 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)' 
+5
source

All Articles