Skip to content

Commit

Permalink
gis: geos: Added support for EWKT (SRID only), and a transform rout…
Browse files Browse the repository at this point in the history
…ine that uses the GDAL facilities to transform the GEOS geometry.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6884 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jbronn committed Dec 4, 2007
1 parent d2fd4f0 commit cc0cc9f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
34 changes: 29 additions & 5 deletions django/contrib/gis/geos/base.py
Expand Up @@ -29,7 +29,7 @@
# to prevent potentially malicious input from reaching the underlying C
# library. Not a substitute for good web security programming practices.
hex_regex = re.compile(r'^[0-9A-F]+$', re.I)
wkt_regex = re.compile(r'^(POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)[ACEGIMLONPSRUTY\d,\.\-\(\) ]+$', re.I)
wkt_regex = re.compile(r'^(SRID=(?P<srid>\d+);)?(?P<wkt>(POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)[ACEGIMLONPSRUTY\d,\.\-\(\) ]+)$', re.I)

class GEOSGeometry(object):
"A class that, generally, encapsulates a GEOS geometry."
Expand Down Expand Up @@ -57,11 +57,13 @@ def __init__(self, geo_input, srid=None):
if hex_regex.match(geo_input):
# If the regex matches, the geometry is in HEX form.
g = from_hex(geo_input, len(geo_input))
elif wkt_regex.match(geo_input):
# Otherwise, the geometry is in WKT form.
g = from_wkt(geo_input)
else:
raise ValueError('String or unicode input unrecognized as WKT or HEXEWKB.')
m = wkt_regex.match(geo_input)
if m:
if m.group('srid'): srid = int(m.group('srid'))
g = from_wkt(m.group('wkt'))
else:
raise ValueError('String or unicode input unrecognized as WKT EWKT, and HEXEWKB.')
elif isinstance(geo_input, GEOM_PTR):
# When the input is a pointer to a geomtry (GEOM_PTR).
g = geo_input
Expand Down Expand Up @@ -303,6 +305,12 @@ def set_srid(self, srid):
srid = property(get_srid, set_srid)

#### Output Routines ####
@property
def ewkt(self):
"Returns the EWKT (WKT + SRID) of the Geometry."
if self.get_srid(): return 'SRID=%s;%s' % (self.srid, self.wkt)
else: return self.wkt

@property
def wkt(self):
"Returns the WKT (Well-Known Text) of the Geometry."
Expand Down Expand Up @@ -356,6 +364,22 @@ def crs(self):
"Alias for `srs` property."
return self.srs

def transform(self, ct):
"Transforms this Geometry; only works with GDAL."
srid = self.srid
if HAS_GDAL and srid:
g = OGRGeometry(self.wkb, srid)
g.transform(ct)
wkb = str(g.wkb)
ptr = from_wkb(wkb, len(wkb))
if ptr:
# Reassigning pointer, and resetting the SRID.
destroy_geom(self._ptr)
self._ptr = ptr
self.srid = g.srid
else:
pass

#### Topology Routines ####
def _topology(self, gptr):
"Helper routine to return Geometry from the given pointer."
Expand Down
10 changes: 10 additions & 0 deletions django/contrib/gis/tests/test_geos.py
Expand Up @@ -78,6 +78,16 @@ def test01g_create_wkb(self):
# we need to do this so decimal places get normalised
geom_t = fromstr(g.wkt)
self.assertEqual(geom_t.wkt, geom_h.wkt)

def test01h_ewkt(self):
"Testing EWKT."
srid = 32140
for p in polygons:
ewkt = 'SRID=%d;%s' % (srid, p.wkt)
poly = fromstr(ewkt)
self.assertEqual(srid, poly.srid)
self.assertEqual(srid, poly.shell.srid)
self.assertEqual(srid, fromstr(poly.ewkt).srid) # Checking export

def test02a_points(self):
"Testing Point objects."
Expand Down

0 comments on commit cc0cc9f

Please sign in to comment.