Skip to content

Instantly share code, notes, and snippets.

@slinkp
Created April 26, 2012 16:59
Show Gist options
  • Save slinkp/2500968 to your computer and use it in GitHub Desktop.
Save slinkp/2500968 to your computer and use it in GitHub Desktop.
Geometry conversion test
from ebpub.utils.geodjango import ensure_valid
from ebpub.utils.geodjango import flatten_geomcollection
def convert1(geom, srid=4326):
# Karen's version
geom = geom.transform(srid, True)
if hasattr(geom, 'geos'):
geom = geom.geos
return geom
def convert2(geom, srid=4326):
# Paul's version
if hasattr(geom, 'geos'):
geom = geom.geos
geom = geom.clone()
if geom.srid is None:
geom.srid = srid
elif geom.srid != srid:
geom = geom.transform(srid, True)
geom = ensure_valid(geom)
geom = flatten_geomcollection(geom)
return geom
from django.contrib.gis.geos import Point
from django.contrib.gis.gdal import OGRGeometry
geosp_no_srid = Point((1.0, 2.0))
ogrp_no_srid = OGRGeometry(geosp_no_srid.wkt)
geosp_with_srid = Point((1.0, 2.0), srid=4326)
ogrp_with_srid = OGRGeometry(geosp_with_srid.wkt, srs=4326)
assert geosp_with_srid.srid == 4326
assert geosp_no_srid.srid == None
assert ogrp_with_srid.srid == 4326
assert ogrp_no_srid.srid == None
assert ogrp_with_srid.geos == geosp_with_srid
assert ogrp_no_srid.geos == geosp_no_srid
assert ogrp_with_srid.geos.srid == 4326
assert ogrp_no_srid.geos.srid == None
# assert convert(geosp_with_srid).srid == 4326
# assert convert(geosp_no_srid).srid == 4326
# assert convert(ogrp_with_srid).srid == 4326
# assert convert(ogrp_no_srid).srid == 4326
for name in ('geosp_with_srid', 'geosp_no_srid',
'ogrp_with_srid', 'ogrp_no_srid'):
geom = eval(name)
for func in (convert1, convert2):
print "\n\n==== %s on %s ====\n" % (func.__name__, name)
print "orig SRID: %s, WKT: %s" % (geom.srid, geom.wkt)
try:
result = func(geom)
except Exception as e:
print "Got exception from %s: %s" % (func.__name__, e)
continue
if result is None:
print "%s returned None!" % func.__name__
continue
print "new SRID: %s, WKT: %s" % (result.srid, result.wkt)
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment