21
21
# the underlying GEOS library.
22
22
from django.contrib.gis.geos import prototypes as capi
24
# Regular expression for recognizing HEXEWKB and WKT. A prophylactic measure
25
# to prevent potentially malicious input from reaching the underlying C
26
# library. Not a substitute for good web security programming practices.
27
hex_regex = re.compile(r'^[0-9A-F]+$', re.I)
28
wkt_regex = re.compile(r'^(SRID=(?P<srid>\d+);)?(?P<wkt>(POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)[ACEGIMLONPSRUTY\d,\.\-\(\) ]+)$', re.I)
24
# These functions provide access to a thread-local instance
25
# of their corresponding GEOS I/O class.
26
from django.contrib.gis.geos.prototypes.io import wkt_r, wkt_w, wkb_r, wkb_w, ewkb_w, ewkb_w3d
28
# For recognizing geometry input.
29
from django.contrib.gis.geometry.regex import hex_regex, wkt_regex, json_regex
30
31
class GEOSGeometry(GEOSBase, ListMixin):
31
32
"A class that, generally, encapsulates a GEOS geometry."
62
63
# Handling WKT input.
63
64
if wkt_m.group('srid'): srid = int(wkt_m.group('srid'))
64
g = wkt_r.read(wkt_m.group('wkt'))
65
g = wkt_r().read(wkt_m.group('wkt'))
65
66
elif hex_regex.match(geo_input):
66
67
# Handling HEXEWKB input.
67
g = wkb_r.read(geo_input)
68
elif gdal.GEOJSON and gdal.geometries.json_regex.match(geo_input):
68
g = wkb_r().read(geo_input)
69
elif gdal.GEOJSON and json_regex.match(geo_input):
69
70
# Handling GeoJSON input.
70
g = wkb_r.read(gdal.OGRGeometry(geo_input).wkb)
71
g = wkb_r().read(gdal.OGRGeometry(geo_input).wkb)
72
73
raise ValueError('String or unicode input unrecognized as WKT EWKT, and HEXEWKB.')
73
74
elif isinstance(geo_input, GEOM_PTR):
76
77
elif isinstance(geo_input, buffer):
77
78
# When the input is a buffer (WKB).
78
g = wkb_r.read(geo_input)
79
g = wkb_r().read(geo_input)
79
80
elif isinstance(geo_input, GEOSGeometry):
80
81
g = capi.geom_clone(geo_input.ptr)
142
143
def __setstate__(self, state):
143
144
# Instantiating from the tuple state that was pickled.
144
145
wkb, srid = state
145
ptr = capi.from_wkb(wkb, len(wkb))
146
ptr = wkb_r().read(buffer(wkb))
146
147
if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.')
148
149
self._post_init(srid)
357
358
#### Output Routines ####
360
"Returns the EWKT (WKT + SRID) of the Geometry."
362
Returns the EWKT (WKT + SRID) of the Geometry. Note that Z values
363
are *not* included in this representation because GEOS does not yet
364
support serializing them.
361
366
if self.get_srid(): return 'SRID=%s;%s' % (self.srid, self.wkt)
362
367
else: return self.wkt
366
"Returns the WKT (Well-Known Text) of the Geometry."
367
return wkt_w.write(self)
371
"Returns the WKT (Well-Known Text) representation of this Geometry."
372
return wkt_w().write(self)
372
Returns the HEX of the Geometry -- please note that the SRID is not
373
included in this representation, because the GEOS C library uses
374
-1 by default, even if the SRID is set.
377
Returns the WKB of this Geometry in hexadecimal form. Please note
378
that the SRID and Z values are not included in this representation
379
because it is not a part of the OGC specification (use the `hexewkb`
376
382
# A possible faster, all-python, implementation:
377
383
# str(self.wkb).encode('hex')
378
return wkb_w.write_hex(self)
384
return wkb_w().write_hex(self)
389
Returns the EWKB of this Geometry in hexadecimal form. This is an
390
extension of the WKB specification that includes SRID and Z values
391
that are a part of this geometry.
395
# See: http://trac.osgeo.org/geos/ticket/216
396
raise GEOSException('Upgrade GEOS to 3.1 to get valid 3D HEXEWKB.')
397
return ewkb_w3d().write_hex(self)
399
return ewkb_w().write_hex(self)
394
"Returns the WKB of the Geometry as a buffer."
395
return wkb_w.write(self)
416
Returns the WKB (Well-Known Binary) representation of this Geometry
417
as a Python buffer. SRID and Z values are not included, use the
418
`ewkb` property instead.
420
return wkb_w().write(self)
425
Return the EWKB representation of this Geometry as a Python buffer.
426
This is an extension of the WKB specification that includes any SRID
427
and Z values that are a part of this geometry.
431
# See: http://trac.osgeo.org/geos/ticket/216
432
raise GEOSException('Upgrade GEOS to 3.1 to get valid 3D EWKB.')
433
return ewkb_w3d().write(self)
435
return ewkb_w().write(self)
454
494
g = gdal.OGRGeometry(self.wkb, srid)
456
496
# Getting a new GEOS pointer
457
ptr = wkb_r.read(g.wkb)
497
ptr = wkb_r().read(g.wkb)
459
499
# User wants a cloned transformed geometry returned.
460
500
return GEOSGeometry(ptr, srid=g.srid)
616
656
7 : GeometryCollection,
619
# Similarly, import the GEOS I/O instances here to avoid conflicts.
620
from django.contrib.gis.geos.io import wkt_r, wkt_w, wkb_r, wkb_w
622
659
# If supported, import the PreparedGeometry class.
624
661
from django.contrib.gis.geos.prepared import PreparedGeometry