mirror of
https://github.com/django/django.git
synced 2025-07-05 02:09:13 +00:00
gis: geos: Added support for EWKT (SRID only), and a transform
routine 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
This commit is contained in:
parent
d2fd4f0dc6
commit
cc0cc9fa08
@ -29,7 +29,7 @@ except:
|
|||||||
# to prevent potentially malicious input from reaching the underlying C
|
# to prevent potentially malicious input from reaching the underlying C
|
||||||
# library. Not a substitute for good web security programming practices.
|
# library. Not a substitute for good web security programming practices.
|
||||||
hex_regex = re.compile(r'^[0-9A-F]+$', re.I)
|
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):
|
class GEOSGeometry(object):
|
||||||
"A class that, generally, encapsulates a GEOS geometry."
|
"A class that, generally, encapsulates a GEOS geometry."
|
||||||
@ -57,11 +57,13 @@ class GEOSGeometry(object):
|
|||||||
if hex_regex.match(geo_input):
|
if hex_regex.match(geo_input):
|
||||||
# If the regex matches, the geometry is in HEX form.
|
# If the regex matches, the geometry is in HEX form.
|
||||||
g = from_hex(geo_input, len(geo_input))
|
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:
|
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):
|
elif isinstance(geo_input, GEOM_PTR):
|
||||||
# When the input is a pointer to a geomtry (GEOM_PTR).
|
# When the input is a pointer to a geomtry (GEOM_PTR).
|
||||||
g = geo_input
|
g = geo_input
|
||||||
@ -303,6 +305,12 @@ class GEOSGeometry(object):
|
|||||||
srid = property(get_srid, set_srid)
|
srid = property(get_srid, set_srid)
|
||||||
|
|
||||||
#### Output Routines ####
|
#### 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
|
@property
|
||||||
def wkt(self):
|
def wkt(self):
|
||||||
"Returns the WKT (Well-Known Text) of the Geometry."
|
"Returns the WKT (Well-Known Text) of the Geometry."
|
||||||
@ -356,6 +364,22 @@ class GEOSGeometry(object):
|
|||||||
"Alias for `srs` property."
|
"Alias for `srs` property."
|
||||||
return self.srs
|
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 ####
|
#### Topology Routines ####
|
||||||
def _topology(self, gptr):
|
def _topology(self, gptr):
|
||||||
"Helper routine to return Geometry from the given pointer."
|
"Helper routine to return Geometry from the given pointer."
|
||||||
|
@ -79,6 +79,16 @@ class GEOSTest(unittest.TestCase):
|
|||||||
geom_t = fromstr(g.wkt)
|
geom_t = fromstr(g.wkt)
|
||||||
self.assertEqual(geom_t.wkt, geom_h.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):
|
def test02a_points(self):
|
||||||
"Testing Point objects."
|
"Testing Point objects."
|
||||||
prev = fromstr('POINT(0 0)')
|
prev = fromstr('POINT(0 0)')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user