~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/contrib/gis/geos/geometry.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
# the underlying GEOS library.
22
22
from django.contrib.gis.geos import prototypes as capi
23
23
 
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
 
27
 
 
28
# For recognizing geometry input.
 
29
from django.contrib.gis.geometry.regex import hex_regex, wkt_regex, json_regex
29
30
 
30
31
class GEOSGeometry(GEOSBase, ListMixin):
31
32
    "A class that, generally, encapsulates a GEOS geometry."
61
62
            if wkt_m:
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)
71
72
            else:
72
73
                raise ValueError('String or unicode input unrecognized as WKT EWKT, and HEXEWKB.')
73
74
        elif isinstance(geo_input, GEOM_PTR):
75
76
            g = geo_input
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)
81
82
        else:
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.')
147
148
        self.ptr = ptr
148
149
        self._post_init(srid)
357
358
    #### Output Routines ####
358
359
    @property
359
360
    def ewkt(self):
360
 
        "Returns the EWKT (WKT + SRID) of the Geometry."
 
361
        """
 
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.
 
365
        """
361
366
        if self.get_srid(): return 'SRID=%s;%s' % (self.srid, self.wkt)
362
367
        else: return self.wkt
363
368
 
364
369
    @property
365
370
    def wkt(self):
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)
368
373
 
369
374
    @property
370
375
    def hex(self):
371
376
        """
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` 
 
380
        property instead).
375
381
        """
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)
 
385
 
 
386
    @property
 
387
    def hexewkb(self):
 
388
        """
 
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.
 
392
        """
 
393
        if self.hasz:
 
394
            if not GEOS_PREPARE:
 
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)
 
398
        else:
 
399
            return ewkb_w().write_hex(self)
379
400
 
380
401
    @property
381
402
    def json(self):
383
404
        Returns GeoJSON representation of this Geometry if GDAL 1.5+
384
405
        is installed.
385
406
        """
386
 
        if gdal.GEOJSON: 
 
407
        if gdal.GEOJSON:
387
408
            return self.ogr.json
388
409
        else:
389
410
            raise GEOSException('GeoJSON output only supported on GDAL 1.5+.')
391
412
 
392
413
    @property
393
414
    def wkb(self):
394
 
        "Returns the WKB of the Geometry as a buffer."
395
 
        return wkb_w.write(self)
 
415
        """
 
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.
 
419
        """
 
420
        return wkb_w().write(self)
 
421
 
 
422
    @property
 
423
    def ewkb(self):
 
424
        """
 
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.
 
428
        """
 
429
        if self.hasz:
 
430
            if not GEOS_PREPARE:
 
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)
 
434
        else:
 
435
            return ewkb_w().write(self)
396
436
 
397
437
    @property
398
438
    def kml(self):
454
494
            g = gdal.OGRGeometry(self.wkb, srid)
455
495
            g.transform(ct)
456
496
            # Getting a new GEOS pointer
457
 
            ptr = wkb_r.read(g.wkb)
 
497
            ptr = wkb_r().read(g.wkb)
458
498
            if clone:
459
499
                # User wants a cloned transformed geometry returned.
460
500
                return GEOSGeometry(ptr, srid=g.srid)
616
656
                7 : GeometryCollection,
617
657
                }
618
658
 
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
621
 
 
622
659
# If supported, import the PreparedGeometry class.
623
660
if GEOS_PREPARE:
624
661
    from django.contrib.gis.geos.prepared import PreparedGeometry