Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored the GEOS interface. Improvements include:

* Geometries now allow list-like manipulation, e.g., can add, insert, delete vertexes (or other geometries in collections) like Python lists.  Thanks, Aryeh Leib Taurog.
* Added support for GEOS prepared geometries via `prepared` property.  Prepared geometries significantly speed up certain operations.
* Added support for GEOS cascaded union as `MultiPolygon.cascaded_union` property.
* Added support for GEOS line merge as `merged` property on `LineString`, and `MultiLineString` geometries.  Thanks, Paul Smith.
* No longer use the deprecated C API for serialization to/from WKB and WKT.  Now use the GEOS I/O classes, which are now exposed as `WKTReader`, `WKTWriter`, `WKBReader`, and `WKBWriter` (which supports 3D and SRID inclusion)
* Moved each type of geometry to their own module, eliminating the cluttered `geometries.py`.
* Internally, all C API methods are explicitly called from a module rather than a star import.

Fixed #9557, #9877, #10222


git-svn-id: http://code.djangoproject.com/svn/django/trunk@10131 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 66e1670efae34d721e374788e4c3f8b5fe5fa481 1 parent 4246c83
@jbronn jbronn authored
Showing with 2,590 additions and 1,224 deletions.
  1. +1 −1  django/contrib/gis/geos/LICENSE
  2. +10 −65 django/contrib/gis/geos/__init__.py
  3. +48 −602 django/contrib/gis/geos/base.py
  4. +72 −49 django/contrib/gis/geos/collections.py
  5. +13 −21 django/contrib/gis/geos/coordseq.py
  6. +23 −0 django/contrib/gis/geos/factory.py
  7. +0 −391 django/contrib/gis/geos/geometries.py
  8. +622 −0 django/contrib/gis/geos/geometry.py
  9. +109 −0 django/contrib/gis/geos/io.py
  10. +20 −18 django/contrib/gis/geos/libgeos.py
  11. +152 −0 django/contrib/gis/geos/linestring.py
  12. +236 −0 django/contrib/gis/geos/mutable_list.py
  13. +138 −0 django/contrib/gis/geos/point.py
  14. +172 −0 django/contrib/gis/geos/polygon.py
  15. +30 −0 django/contrib/gis/geos/prepared.py
  16. +1 −4 django/contrib/gis/geos/prototypes/__init__.py
  17. +14 −6 django/contrib/gis/geos/prototypes/errcheck.py
  18. +4 −12 django/contrib/gis/geos/prototypes/geom.py
  19. +94 −0 django/contrib/gis/geos/prototypes/io.py
  20. +1 −1  django/contrib/gis/geos/prototypes/predicates.py
  21. +24 −0 django/contrib/gis/geos/prototypes/prepared.py
  22. +15 −2 django/contrib/gis/geos/prototypes/topology.py
  23. +24 −0 django/contrib/gis/geos/tests/__init__.py
  24. +184 −0 django/contrib/gis/geos/tests/pymutable_geometries.py
  25. +115 −47 django/contrib/gis/{ → geos}/tests/test_geos.py
  26. +135 −0 django/contrib/gis/geos/tests/test_geos_mutation.py
  27. +329 −0 django/contrib/gis/geos/tests/test_mutable_list.py
  28. +4 −5 django/contrib/gis/tests/__init__.py
View
2  django/contrib/gis/geos/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2007, Justin Bronn
+Copyright (c) 2007-2009 Justin Bronn
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
View
75 django/contrib/gis/geos/__init__.py
@@ -1,69 +1,14 @@
"""
- The goal of this module is to be a ctypes wrapper around the GEOS library
- that will work on both *NIX and Windows systems. Specifically, this uses
- the GEOS C api.
-
- I have several motivations for doing this:
- (1) The GEOS SWIG wrapper is no longer maintained, and requires the
- installation of SWIG.
- (2) The PCL implementation is over 2K+ lines of C and would make
- PCL a requisite package for the GeoDjango application stack.
- (3) Windows and Mac compatibility becomes substantially easier, and does not
- require the additional compilation of PCL or GEOS and SWIG -- all that
- is needed is a Win32 or Mac compiled GEOS C library (dll or dylib)
- in a location that Python can read (e.g. 'C:\Python25').
-
- In summary, I wanted to wrap GEOS in a more maintainable and portable way using
- only Python and the excellent ctypes library (now standard in Python 2.5).
-
- In the spirit of loose coupling, this library does not require Django or
- GeoDjango. Only the GEOS C library and ctypes are needed for the platform
- of your choice.
-
- For more information about GEOS:
- http://geos.refractions.net
-
- For more info about PCL and the discontinuation of the Python GEOS
- library see Sean Gillies' writeup (and subsequent update) at:
- http://zcologia.com/news/150/geometries-for-python/
- http://zcologia.com/news/429/geometries-for-python-update/
+The GeoDjango GEOS module. Please consult the GeoDjango documentation
+for more details:
+ http://geodjango.org/docs/geos.html
"""
-from django.contrib.gis.geos.base import GEOSGeometry, wkt_regex, hex_regex
-from django.contrib.gis.geos.geometries import Point, LineString, LinearRing, Polygon, HAS_NUMPY
+from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex
+from django.contrib.gis.geos.point import Point
+from django.contrib.gis.geos.linestring import LineString, LinearRing
+from django.contrib.gis.geos.polygon import Polygon
from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
-from django.contrib.gis.geos.libgeos import geos_version, geos_version_info
-
-def fromfile(file_name):
- """
- Given a string file name, returns a GEOSGeometry. The file may contain WKB,
- WKT, or HEX.
- """
- fh = open(file_name, 'rb')
- buf = fh.read()
- fh.close()
- if wkt_regex.match(buf) or hex_regex.match(buf):
- return GEOSGeometry(buf)
- else:
- return GEOSGeometry(buffer(buf))
-
-def fromstr(wkt_or_hex, **kwargs):
- "Given a string value (wkt or hex), returns a GEOSGeometry object."
- return GEOSGeometry(wkt_or_hex, **kwargs)
-
-def hex_to_wkt(hex):
- "Converts HEXEWKB into WKT."
- return GEOSGeometry(hex).wkt
-
-def wkt_to_hex(wkt):
- "Converts WKT into HEXEWKB."
- return GEOSGeometry(wkt).hex
-
-def centroid(input):
- "Returns the centroid of the geometry (given in HEXEWKB)."
- return GEOSGeometry(input).centroid.wkt
-
-def area(input):
- "Returns the area of the geometry (given in HEXEWKB)."
- return GEOSGeometry(input).area
-
+from django.contrib.gis.geos.io import WKTReader, WKTWriter, WKBReader, WKBWriter
+from django.contrib.gis.geos.factory import fromfile, fromstr
+from django.contrib.gis.geos.libgeos import geos_version, geos_version_info, GEOS_PREPARE
View
650 django/contrib/gis/geos/base.py
@@ -1,608 +1,54 @@
-"""
- This module contains the 'base' GEOSGeometry object -- all GEOS Geometries
- inherit from this object.
-"""
-# Python, ctypes and types dependencies.
-import re
-from ctypes import addressof, byref, c_double, c_size_t
-from types import UnicodeType
-
-# GEOS-related dependencies.
-from django.contrib.gis.geos.coordseq import GEOSCoordSeq
-from django.contrib.gis.geos.error import GEOSException
-from django.contrib.gis.geos.libgeos import GEOM_PTR
-
-# All other functions in this module come from the ctypes
-# prototypes module -- which handles all interaction with
-# the underlying GEOS library.
-from django.contrib.gis.geos.prototypes import *
+from ctypes import c_void_p
+from types import NoneType
+from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
# Trying to import GDAL libraries, if available. Have to place in
# try/except since this package may be used outside GeoDjango.
try:
- from django.contrib.gis.gdal import OGRGeometry, SpatialReference, GEOJSON
- from django.contrib.gis.gdal.geometries import json_regex
- HAS_GDAL = True
-except:
- HAS_GDAL, GEOJSON = False, False
-
-# Regular expression for recognizing HEXEWKB and WKT. A prophylactic measure
-# 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'^(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."
-
- # Initially, the geometry pointer is NULL
+ from django.contrib.gis import gdal
+except ImportError:
+ # A 'dummy' gdal module.
+ class GDALInfo(object):
+ HAS_GDAL = False
+ GEOJSON = False
+ gdal = GDALInfo()
+
+# NumPy supported?
+try:
+ import numpy
+except ImportError:
+ numpy = False
+
+class GEOSBase(object):
+ """
+ Base object for GEOS objects that has a pointer access property
+ that controls access to the underlying C pointer.
+ """
+ # Initially the pointer is NULL.
_ptr = None
- #### Python 'magic' routines ####
- def __init__(self, geo_input, srid=None):
- """
- The base constructor for GEOS geometry objects, and may take the
- following inputs:
-
- * string: WKT
- * string: HEXEWKB (a PostGIS-specific canonical form)
- * buffer: WKB
-
- The `srid` keyword is used to specify the Source Reference Identifier
- (SRID) number for this Geometry. If not set, the SRID will be None.
- """
- if isinstance(geo_input, basestring):
- if isinstance(geo_input, UnicodeType):
- # Encoding to ASCII, WKT or HEXEWKB doesn't need any more.
- geo_input = geo_input.encode('ascii')
-
- wkt_m = wkt_regex.match(geo_input)
- if wkt_m:
- # Handling WKT input.
- if wkt_m.group('srid'): srid = int(wkt_m.group('srid'))
- g = from_wkt(wkt_m.group('wkt'))
- elif hex_regex.match(geo_input):
- # Handling HEXEWKB input.
- g = from_hex(geo_input, len(geo_input))
- elif GEOJSON and json_regex.match(geo_input):
- # Handling GeoJSON input.
- wkb_input = str(OGRGeometry(geo_input).wkb)
- g = from_wkb(wkb_input, len(wkb_input))
- 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
- elif isinstance(geo_input, buffer):
- # When the input is a buffer (WKB).
- wkb_input = str(geo_input)
- g = from_wkb(wkb_input, len(wkb_input))
- else:
- # Invalid geometry type.
- raise TypeError('Improper geometry input type: %s' % str(type(geo_input)))
-
- if bool(g):
- # Setting the pointer object with a valid pointer.
- self._ptr = g
- else:
- raise GEOSException('Could not initialize GEOS Geometry with given input.')
-
- # Post-initialization setup.
- self._post_init(srid)
-
- def _post_init(self, srid):
- "Helper routine for performing post-initialization setup."
- # Setting the SRID, if given.
- if srid and isinstance(srid, int): self.srid = srid
-
- # Setting the class type (e.g., Point, Polygon, etc.)
- self.__class__ = GEOS_CLASSES[self.geom_typeid]
-
- # Setting the coordinate sequence for the geometry (will be None on
- # geometries that do not have coordinate sequences)
- self._set_cs()
-
- @property
- def ptr(self):
- """
- Property for controlling access to the GEOS geometry pointer. Using
- this raises an exception when the pointer is NULL, thus preventing
- the C library from attempting to access an invalid memory location.
- """
- if self._ptr:
- return self._ptr
- else:
- raise GEOSException('NULL GEOS pointer encountered; was this geometry modified?')
-
- def __del__(self):
- """
- Destroys this Geometry; in other words, frees the memory used by the
- GEOS C++ object.
- """
- if self._ptr: destroy_geom(self._ptr)
-
- def __copy__(self):
- """
- Returns a clone because the copy of a GEOSGeometry may contain an
- invalid pointer location if the original is garbage collected.
- """
- return self.clone()
-
- def __deepcopy__(self, memodict):
- """
- The `deepcopy` routine is used by the `Node` class of django.utils.tree;
- thus, the protocol routine needs to be implemented to return correct
- copies (clones) of these GEOS objects, which use C pointers.
- """
- return self.clone()
-
- def __str__(self):
- "WKT is used for the string representation."
- return self.wkt
-
- def __repr__(self):
- "Short-hand representation because WKT may be very large."
- return '<%s object at %s>' % (self.geom_type, hex(addressof(self.ptr)))
-
- # Pickling support
- def __getstate__(self):
- # The pickled state is simply a tuple of the WKB (in string form)
- # and the SRID.
- return str(self.wkb), self.srid
-
- def __setstate__(self, state):
- # Instantiating from the tuple state that was pickled.
- wkb, srid = state
- ptr = from_wkb(wkb, len(wkb))
- if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.')
- self._ptr = ptr
- self._post_init(srid)
-
- # Comparison operators
- def __eq__(self, other):
- """
- Equivalence testing, a Geometry may be compared with another Geometry
- or a WKT representation.
- """
- if isinstance(other, basestring):
- return self.wkt == other
- elif isinstance(other, GEOSGeometry):
- return self.equals_exact(other)
- else:
- return False
-
- def __ne__(self, other):
- "The not equals operator."
- return not (self == other)
-
- ### Geometry set-like operations ###
- # Thanks to Sean Gillies for inspiration:
- # http://lists.gispython.org/pipermail/community/2007-July/001034.html
- # g = g1 | g2
- def __or__(self, other):
- "Returns the union of this Geometry and the other."
- return self.union(other)
-
- # g = g1 & g2
- def __and__(self, other):
- "Returns the intersection of this Geometry and the other."
- return self.intersection(other)
-
- # g = g1 - g2
- def __sub__(self, other):
- "Return the difference this Geometry and the other."
- return self.difference(other)
-
- # g = g1 ^ g2
- def __xor__(self, other):
- "Return the symmetric difference of this Geometry and the other."
- return self.sym_difference(other)
-
- #### Coordinate Sequence Routines ####
- @property
- def has_cs(self):
- "Returns True if this Geometry has a coordinate sequence, False if not."
- # Only these geometries are allowed to have coordinate sequences.
- if isinstance(self, (Point, LineString, LinearRing)):
- return True
- else:
- return False
-
- def _set_cs(self):
- "Sets the coordinate sequence for this Geometry."
- if self.has_cs:
- self._cs = GEOSCoordSeq(get_cs(self.ptr), self.hasz)
- else:
- self._cs = None
-
- @property
- def coord_seq(self):
- "Returns a clone of the coordinate sequence for this Geometry."
- if self.has_cs:
- return self._cs.clone()
-
- #### Geometry Info ####
- @property
- def geom_type(self):
- "Returns a string representing the Geometry type, e.g. 'Polygon'"
- return geos_type(self.ptr)
-
- @property
- def geom_typeid(self):
- "Returns an integer representing the Geometry type."
- return geos_typeid(self.ptr)
-
- @property
- def num_geom(self):
- "Returns the number of geometries in the Geometry."
- return get_num_geoms(self.ptr)
-
- @property
- def num_coords(self):
- "Returns the number of coordinates in the Geometry."
- return get_num_coords(self.ptr)
-
- @property
- def num_points(self):
- "Returns the number points, or coordinates, in the Geometry."
- return self.num_coords
-
- @property
- def dims(self):
- "Returns the dimension of this Geometry (0=point, 1=line, 2=surface)."
- return get_dims(self.ptr)
-
- def normalize(self):
- "Converts this Geometry to normal form (or canonical form)."
- return geos_normalize(self.ptr)
-
- #### Unary predicates ####
- @property
- def empty(self):
- """
- Returns a boolean indicating whether the set of points in this Geometry
- are empty.
- """
- return geos_isempty(self.ptr)
-
- @property
- def hasz(self):
- "Returns whether the geometry has a 3D dimension."
- return geos_hasz(self.ptr)
-
- @property
- def ring(self):
- "Returns whether or not the geometry is a ring."
- return geos_isring(self.ptr)
-
- @property
- def simple(self):
- "Returns false if the Geometry not simple."
- return geos_issimple(self.ptr)
-
- @property
- def valid(self):
- "This property tests the validity of this Geometry."
- return geos_isvalid(self.ptr)
-
- #### Binary predicates. ####
- def contains(self, other):
- "Returns true if other.within(this) returns true."
- return geos_contains(self.ptr, other.ptr)
-
- def crosses(self, other):
- """
- Returns true if the DE-9IM intersection matrix for the two Geometries
- is T*T****** (for a point and a curve,a point and an area or a line and
- an area) 0******** (for two curves).
- """
- return geos_crosses(self.ptr, other.ptr)
-
- def disjoint(self, other):
- """
- Returns true if the DE-9IM intersection matrix for the two Geometries
- is FF*FF****.
- """
- return geos_disjoint(self.ptr, other.ptr)
-
- def equals(self, other):
- """
- Returns true if the DE-9IM intersection matrix for the two Geometries
- is T*F**FFF*.
- """
- return geos_equals(self.ptr, other.ptr)
-
- def equals_exact(self, other, tolerance=0):
- """
- Returns true if the two Geometries are exactly equal, up to a
- specified tolerance.
- """
- return geos_equalsexact(self.ptr, other.ptr, float(tolerance))
-
- def intersects(self, other):
- "Returns true if disjoint returns false."
- return geos_intersects(self.ptr, other.ptr)
-
- def overlaps(self, other):
- """
- Returns true if the DE-9IM intersection matrix for the two Geometries
- is T*T***T** (for two points or two surfaces) 1*T***T** (for two curves).
- """
- return geos_overlaps(self.ptr, other.ptr)
-
- def relate_pattern(self, other, pattern):
- """
- Returns true if the elements in the DE-9IM intersection matrix for the
- two Geometries match the elements in pattern.
- """
- if not isinstance(pattern, str) or len(pattern) > 9:
- raise GEOSException('invalid intersection matrix pattern')
- return geos_relatepattern(self.ptr, other.ptr, pattern)
-
- def touches(self, other):
- """
- Returns true if the DE-9IM intersection matrix for the two Geometries
- is FT*******, F**T***** or F***T****.
- """
- return geos_touches(self.ptr, other.ptr)
-
- def within(self, other):
- """
- Returns true if the DE-9IM intersection matrix for the two Geometries
- is T*F**F***.
- """
- return geos_within(self.ptr, other.ptr)
-
- #### SRID Routines ####
- def get_srid(self):
- "Gets the SRID for the geometry, returns None if no SRID is set."
- s = geos_get_srid(self.ptr)
- if s == 0: return None
- else: return s
-
- def set_srid(self, srid):
- "Sets the SRID for the geometry."
- geos_set_srid(self.ptr, 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."
- return to_wkt(self.ptr)
-
- @property
- def hex(self):
- """
- Returns the HEX of the Geometry -- please note that the SRID is not
- included in this representation, because the GEOS C library uses
- -1 by default, even if the SRID is set.
- """
- # A possible faster, all-python, implementation:
- # str(self.wkb).encode('hex')
- return to_hex(self.ptr, byref(c_size_t()))
-
- @property
- def json(self):
- """
- Returns GeoJSON representation of this Geometry if GDAL 1.5+
- is installed.
- """
- if GEOJSON: return self.ogr.json
- geojson = json
-
- @property
- def wkb(self):
- "Returns the WKB of the Geometry as a buffer."
- bin = to_wkb(self.ptr, byref(c_size_t()))
- return buffer(bin)
-
- @property
- def kml(self):
- "Returns the KML representation of this Geometry."
- gtype = self.geom_type
- return '<%s>%s</%s>' % (gtype, self.coord_seq.kml, gtype)
-
- #### GDAL-specific output routines ####
- @property
- def ogr(self):
- "Returns the OGR Geometry for this Geometry."
- if HAS_GDAL:
- if self.srid:
- return OGRGeometry(self.wkb, self.srid)
- else:
- return OGRGeometry(self.wkb)
- else:
- return None
-
- @property
- def srs(self):
- "Returns the OSR SpatialReference for SRID of this Geometry."
- if HAS_GDAL and self.srid:
- return SpatialReference(self.srid)
- else:
- return None
-
- @property
- def crs(self):
- "Alias for `srs` property."
- return self.srs
-
- def transform(self, ct, clone=False):
- """
- Requires GDAL. Transforms the geometry according to the given
- transformation object, which may be an integer SRID, and WKT or
- PROJ.4 string. By default, the geometry is transformed in-place and
- nothing is returned. However if the `clone` keyword is set, then this
- geometry will not be modified and a transformed clone will be returned
- instead.
- """
- 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 clone:
- # User wants a cloned transformed geometry returned.
- return GEOSGeometry(ptr, srid=g.srid)
- if ptr:
- # Reassigning pointer, and performing post-initialization setup
- # again due to the reassignment.
- destroy_geom(self.ptr)
- self._ptr = ptr
- self._post_init(g.srid)
- else:
- raise GEOSException('Transformed WKB was invalid.')
-
- #### Topology Routines ####
- def _topology(self, gptr):
- "Helper routine to return Geometry from the given pointer."
- return GEOSGeometry(gptr, srid=self.srid)
-
- @property
- def boundary(self):
- "Returns the boundary as a newly allocated Geometry object."
- return self._topology(geos_boundary(self.ptr))
-
- def buffer(self, width, quadsegs=8):
- """
- Returns a geometry that represents all points whose distance from this
- Geometry is less than or equal to distance. Calculations are in the
- Spatial Reference System of this Geometry. The optional third parameter sets
- the number of segment used to approximate a quarter circle (defaults to 8).
- (Text from PostGIS documentation at ch. 6.1.3)
- """
- return self._topology(geos_buffer(self.ptr, width, quadsegs))
-
- @property
- def centroid(self):
- """
- The centroid is equal to the centroid of the set of component Geometries
- of highest dimension (since the lower-dimension geometries contribute zero
- "weight" to the centroid).
- """
- return self._topology(geos_centroid(self.ptr))
-
- @property
- def convex_hull(self):
- """
- Returns the smallest convex Polygon that contains all the points
- in the Geometry.
- """
- return self._topology(geos_convexhull(self.ptr))
-
- def difference(self, other):
- """
- Returns a Geometry representing the points making up this Geometry
- that do not make up other.
- """
- return self._topology(geos_difference(self.ptr, other.ptr))
-
- @property
- def envelope(self):
- "Return the envelope for this geometry (a polygon)."
- return self._topology(geos_envelope(self.ptr))
-
- def intersection(self, other):
- "Returns a Geometry representing the points shared by this Geometry and other."
- return self._topology(geos_intersection(self.ptr, other.ptr))
-
- @property
- def point_on_surface(self):
- "Computes an interior point of this Geometry."
- return self._topology(geos_pointonsurface(self.ptr))
-
- def relate(self, other):
- "Returns the DE-9IM intersection matrix for this Geometry and the other."
- return geos_relate(self.ptr, other.ptr)
-
- def simplify(self, tolerance=0.0, preserve_topology=False):
- """
- Returns the Geometry, simplified using the Douglas-Peucker algorithm
- to the specified tolerance (higher tolerance => less points). If no
- tolerance provided, defaults to 0.
-
- By default, this function does not preserve topology - e.g. polygons can
- be split, collapse to lines or disappear holes can be created or
- disappear, and lines can cross. By specifying preserve_topology=True,
- the result will have the same dimension and number of components as the
- input. This is significantly slower.
- """
- if preserve_topology:
- return self._topology(geos_preservesimplify(self.ptr, tolerance))
- else:
- return self._topology(geos_simplify(self.ptr, tolerance))
-
- def sym_difference(self, other):
- """
- Returns a set combining the points in this Geometry not in other,
- and the points in other not in this Geometry.
- """
- return self._topology(geos_symdifference(self.ptr, other.ptr))
-
- def union(self, other):
- "Returns a Geometry representing all the points in this Geometry and other."
- return self._topology(geos_union(self.ptr, other.ptr))
-
- #### Other Routines ####
- @property
- def area(self):
- "Returns the area of the Geometry."
- return geos_area(self.ptr, byref(c_double()))
-
- def distance(self, other):
- """
- Returns the distance between the closest points on this Geometry
- and the other. Units will be in those of the coordinate system of
- the Geometry.
- """
- if not isinstance(other, GEOSGeometry):
- raise TypeError('distance() works only on other GEOS Geometries.')
- return geos_distance(self.ptr, other.ptr, byref(c_double()))
-
- @property
- def extent(self):
- """
- Returns the extent of this geometry as a 4-tuple, consisting of
- (xmin, ymin, xmax, ymax).
- """
- env = self.envelope
- if isinstance(env, Point):
- xmin, ymin = env.tuple
- xmax, ymax = xmin, ymin
- else:
- xmin, ymin = env[0][0]
- xmax, ymax = env[0][2]
- return (xmin, ymin, xmax, ymax)
-
- @property
- def length(self):
- """
- Returns the length of this Geometry (e.g., 0 for point, or the
- circumfrence of a Polygon).
- """
- return geos_length(self.ptr, byref(c_double()))
-
- def clone(self):
- "Clones this Geometry."
- return GEOSGeometry(geom_clone(self.ptr), srid=self.srid)
-
-# Class mapping dictionary
-from django.contrib.gis.geos.geometries import Point, Polygon, LineString, LinearRing
-from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon
-GEOS_CLASSES = {0 : Point,
- 1 : LineString,
- 2 : LinearRing,
- 3 : Polygon,
- 4 : MultiPoint,
- 5 : MultiLineString,
- 6 : MultiPolygon,
- 7 : GeometryCollection,
- }
+ # Default allowed pointer type.
+ ptr_type = c_void_p
+
+ # Pointer access property.
+ def _get_ptr(self):
+ # Raise an exception if the pointer isn't valid don't
+ # want to be passing NULL pointers to routines --
+ # that's very bad.
+ if self._ptr: return self._ptr
+ else: raise GEOSException('NULL GEOS %s pointer encountered.' % self.__class__.__name__)
+
+ def _set_ptr(self, ptr):
+ # Only allow the pointer to be set with pointers of the
+ # compatible type or None (NULL).
+ if isinstance(ptr, int):
+ self._ptr = self.ptr_type(ptr)
+ elif isinstance(ptr, (self.ptr_type, NoneType)):
+ self._ptr = ptr
+ else:
+ raise TypeError('Incompatible pointer type')
+
+ # Property for controlling access to the GEOS object pointers. Using
+ # this raises an exception when the pointer is NULL, thus preventing
+ # the C library from attempting to access an invalid memory location.
+ ptr = property(_get_ptr, _set_ptr)
View
121 django/contrib/gis/geos/collections.py
@@ -3,16 +3,17 @@
GeometryCollection, MultiPoint, MultiLineString, and MultiPolygon
"""
from ctypes import c_int, c_uint, byref
-from types import TupleType, ListType
-from django.contrib.gis.geos.base import GEOSGeometry
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
-from django.contrib.gis.geos.geometries import Point, LineString, LinearRing, Polygon
-from django.contrib.gis.geos.libgeos import get_pointer_arr, GEOM_PTR
-from django.contrib.gis.geos.prototypes import create_collection, destroy_geom, geom_clone, geos_typeid, get_cs, get_geomn
+from django.contrib.gis.geos.geometry import GEOSGeometry
+from django.contrib.gis.geos.libgeos import get_pointer_arr, GEOM_PTR, GEOS_PREPARE
+from django.contrib.gis.geos.linestring import LineString, LinearRing
+from django.contrib.gis.geos.point import Point
+from django.contrib.gis.geos.polygon import Polygon
+from django.contrib.gis.geos import prototypes as capi
class GeometryCollection(GEOSGeometry):
- _allowed = (Point, LineString, LinearRing, Polygon)
_typeid = 7
+ _minlength = 0
def __init__(self, *args, **kwargs):
"Initializes a Geometry Collection from a sequence of Geometry objects."
@@ -21,10 +22,10 @@ def __init__(self, *args, **kwargs):
if not args:
raise TypeError, 'Must provide at least one Geometry to initialize %s.' % self.__class__.__name__
- if len(args) == 1:
+ if len(args) == 1:
# If only one geometry provided or a list of geometries is provided
# in the first argument.
- if isinstance(args[0], (TupleType, ListType)):
+ if isinstance(args[0], (tuple, list)):
init_geoms = args[0]
else:
init_geoms = args
@@ -32,55 +33,55 @@ def __init__(self, *args, **kwargs):
init_geoms = args
# Ensuring that only the permitted geometries are allowed in this collection
- if False in [isinstance(geom, self._allowed) for geom in init_geoms]:
- raise TypeError('Invalid Geometry type encountered in the arguments.')
+ # this is moved to list mixin super class
+ self._check_allowed(init_geoms)
# Creating the geometry pointer array.
- ngeoms = len(init_geoms)
- geoms = get_pointer_arr(ngeoms)
- for i in xrange(ngeoms): geoms[i] = geom_clone(init_geoms[i].ptr)
- super(GeometryCollection, self).__init__(create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms)), **kwargs)
-
- def __getitem__(self, index):
- "Returns the Geometry from this Collection at the given index (0-based)."
- # Checking the index and returning the corresponding GEOS geometry.
- self._checkindex(index)
- return GEOSGeometry(geom_clone(get_geomn(self.ptr, index)), srid=self.srid)
-
- def __setitem__(self, index, geom):
- "Sets the Geometry at the specified index."
- self._checkindex(index)
- if not isinstance(geom, self._allowed):
- raise TypeError('Incompatible Geometry for collection.')
-
- ngeoms = len(self)
- geoms = get_pointer_arr(ngeoms)
- for i in xrange(ngeoms):
- if i == index:
- geoms[i] = geom_clone(geom.ptr)
- else:
- geoms[i] = geom_clone(get_geomn(self.ptr, i))
-
- # Creating a new collection, and destroying the contents of the previous poiner.
- prev_ptr = self.ptr
- srid = self.srid
- self._ptr = create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms))
- if srid: self.srid = srid
- destroy_geom(prev_ptr)
+ collection = self._create_collection(len(init_geoms), iter(init_geoms))
+ super(GeometryCollection, self).__init__(collection, **kwargs)
def __iter__(self):
"Iterates over each Geometry in the Collection."
for i in xrange(len(self)):
- yield self.__getitem__(i)
+ yield self[i]
def __len__(self):
"Returns the number of geometries in this Collection."
return self.num_geom
- def _checkindex(self, index):
- "Checks the given geometry index."
- if index < 0 or index >= self.num_geom:
- raise GEOSIndexError('invalid GEOS Geometry index: %s' % str(index))
+ ### Methods for compatibility with ListMixin ###
+ @classmethod
+ def _create_collection(cls, length, items):
+ # Creating the geometry pointer array.
+ geoms = get_pointer_arr(length)
+ for i, g in enumerate(items):
+ # this is a little sloppy, but makes life easier
+ # allow GEOSGeometry types (python wrappers) or pointer types
+ geoms[i] = capi.geom_clone(getattr(g, 'ptr', g))
+
+ return capi.create_collection(c_int(cls._typeid), byref(geoms), c_uint(length))
+
+ def _getitem_internal(self, index):
+ return capi.get_geomn(self.ptr, index)
+
+ def _getitem_external(self, index):
+ "Returns the Geometry from this Collection at the given index (0-based)."
+ # Checking the index and returning the corresponding GEOS geometry.
+ return GEOSGeometry(capi.geom_clone(self._getitem_internal(index)), srid=self.srid)
+
+ def _set_collection(self, length, items):
+ "Create a new collection, and destroy the contents of the previous pointer."
+ prev_ptr = self.ptr
+ srid = self.srid
+ self.ptr = self._create_collection(length, items)
+ if srid: self.srid = srid
+ capi.destroy_geom(prev_ptr)
+
+ # Because GeometryCollections need to be rebuilt upon the changing of a
+ # component geometry, these routines are set to their counterparts that
+ # rebuild the entire geometry.
+ _set_single = GEOSGeometry._set_single_rebuild
+ _assign_extended_slice = GEOSGeometry._assign_extended_slice_rebuild
@property
def kml(self):
@@ -94,12 +95,34 @@ def tuple(self):
coords = tuple
# MultiPoint, MultiLineString, and MultiPolygon class definitions.
-class MultiPoint(GeometryCollection):
+class MultiPoint(GeometryCollection):
_allowed = Point
_typeid = 4
-class MultiLineString(GeometryCollection):
+
+class MultiLineString(GeometryCollection):
_allowed = (LineString, LinearRing)
_typeid = 5
-class MultiPolygon(GeometryCollection):
+
+ @property
+ def merged(self):
+ """
+ Returns a LineString representing the line merge of this
+ MultiLineString.
+ """
+ return self._topology(capi.geos_linemerge(self.ptr))
+
+class MultiPolygon(GeometryCollection):
_allowed = Polygon
_typeid = 6
+
+ @property
+ def cascaded_union(self):
+ "Returns a cascaded union of this MultiPolygon."
+ if GEOS_PREPARE:
+ return GEOSGeometry(capi.geos_cascaded_union(self.ptr), self.srid)
+ else:
+ raise GEOSException('The cascaded union operation requires GEOS 3.1+.')
+
+# Setting the allowed types here since GeometryCollection is defined before
+# its subclasses.
+GeometryCollection._allowed = (Point, LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon)
View
34 django/contrib/gis/geos/coordseq.py
@@ -4,15 +4,16 @@
LineString, and LinearRing geometries.
"""
from ctypes import c_double, c_uint, byref
-from types import ListType, TupleType
+from django.contrib.gis.geos.base import GEOSBase, numpy
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
-from django.contrib.gis.geos.libgeos import CS_PTR, HAS_NUMPY
-from django.contrib.gis.geos.prototypes import cs_clone, cs_getdims, cs_getordinate, cs_getsize, cs_setordinate
-if HAS_NUMPY: from numpy import ndarray
+from django.contrib.gis.geos.libgeos import CS_PTR
+from django.contrib.gis.geos import prototypes as capi
-class GEOSCoordSeq(object):
+class GEOSCoordSeq(GEOSBase):
"The internal representation of a list of coordinates inside a Geometry."
+ ptr_type = CS_PTR
+
#### Python 'magic' routines ####
def __init__(self, ptr, z=False):
"Initializes from a GEOS pointer."
@@ -44,9 +45,9 @@ def __getitem__(self, index):
def __setitem__(self, index, value):
"Sets the coordinate sequence value at the given index."
# Checking the input value
- if isinstance(value, (ListType, TupleType)):
+ if isinstance(value, (list, tuple)):
pass
- elif HAS_NUMPY and isinstance(value, ndarray):
+ elif numpy and isinstance(value, numpy.ndarray):
pass
else:
raise TypeError('Must set coordinate with a sequence (list, tuple, or numpy array).')
@@ -76,27 +77,18 @@ def _checkdim(self, dim):
if dim < 0 or dim > 2:
raise GEOSException('invalid ordinate dimension "%d"' % dim)
- @property
- def ptr(self):
- """
- Property for controlling access to coordinate sequence pointer,
- preventing attempted access to a NULL memory location.
- """
- if self._ptr: return self._ptr
- else: raise GEOSException('NULL coordinate sequence pointer encountered.')
-
#### Ordinate getting and setting routines ####
def getOrdinate(self, dimension, index):
"Returns the value for the given dimension and index."
self._checkindex(index)
self._checkdim(dimension)
- return cs_getordinate(self.ptr, index, dimension, byref(c_double()))
+ return capi.cs_getordinate(self.ptr, index, dimension, byref(c_double()))
def setOrdinate(self, dimension, index, value):
"Sets the value for the given dimension and index."
self._checkindex(index)
self._checkdim(dimension)
- cs_setordinate(self.ptr, index, dimension, value)
+ capi.cs_setordinate(self.ptr, index, dimension, value)
def getX(self, index):
"Get the X value at the index."
@@ -126,12 +118,12 @@ def setZ(self, index, value):
@property
def size(self):
"Returns the size of this coordinate sequence."
- return cs_getsize(self.ptr, byref(c_uint()))
+ return capi.cs_getsize(self.ptr, byref(c_uint()))
@property
def dims(self):
"Returns the dimensions of this coordinate sequence."
- return cs_getdims(self.ptr, byref(c_uint()))
+ return capi.cs_getdims(self.ptr, byref(c_uint()))
@property
def hasz(self):
@@ -144,7 +136,7 @@ def hasz(self):
### Other Methods ###
def clone(self):
"Clones this coordinate sequence."
- return GEOSCoordSeq(cs_clone(self.ptr), self.hasz)
+ return GEOSCoordSeq(capi.cs_clone(self.ptr), self.hasz)
@property
def kml(self):
View
23 django/contrib/gis/geos/factory.py
@@ -0,0 +1,23 @@
+from django.contrib.gis.geos.geometry import GEOSGeometry, wkt_regex, hex_regex
+
+def fromfile(file_h):
+ """
+ Given a string file name, returns a GEOSGeometry. The file may contain WKB,
+ WKT, or HEX.
+ """
+ # If given a file name, get a real handle.
+ if isinstance(file_h, basestring):
+ file_h = open(file_h, 'rb')
+
+ # Reading in the file's contents,
+ buf = file_h.read()
+
+ # If we get WKB need to wrap in buffer(), so run through regexes.
+ if wkt_regex.match(buf) or hex_regex.match(buf):
+ return GEOSGeometry(buf)
+ else:
+ return GEOSGeometry(buffer(buf))
+
+def fromstr(string, **kwargs):
+ "Given a string value, returns a GEOSGeometry object."
+ return GEOSGeometry(string, **kwargs)
View
391 django/contrib/gis/geos/geometries.py
@@ -1,391 +0,0 @@
-"""
- This module houses the Point, LineString, LinearRing, and Polygon OGC
- geometry classes. All geometry classes in this module inherit from
- GEOSGeometry.
-"""
-from ctypes import c_uint, byref
-from django.contrib.gis.geos.base import GEOSGeometry
-from django.contrib.gis.geos.coordseq import GEOSCoordSeq
-from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
-from django.contrib.gis.geos.libgeos import get_pointer_arr, GEOM_PTR, HAS_NUMPY
-from django.contrib.gis.geos.prototypes import *
-if HAS_NUMPY: from numpy import ndarray, array
-
-class Point(GEOSGeometry):
-
- def __init__(self, x, y=None, z=None, srid=None):
- """
- The Point object may be initialized with either a tuple, or individual
- parameters.
-
- For Example:
- >>> p = Point((5, 23)) # 2D point, passed in as a tuple
- >>> p = Point(5, 23, 8) # 3D point, passed in with individual parameters
- """
-
- if isinstance(x, (tuple, list)):
- # Here a tuple or list was passed in under the `x` parameter.
- ndim = len(x)
- if ndim < 2 or ndim > 3:
- raise TypeError('Invalid sequence parameter: %s' % str(x))
- coords = x
- elif isinstance(x, (int, float, long)) and isinstance(y, (int, float, long)):
- # Here X, Y, and (optionally) Z were passed in individually, as parameters.
- if isinstance(z, (int, float, long)):
- ndim = 3
- coords = [x, y, z]
- else:
- ndim = 2
- coords = [x, y]
- else:
- raise TypeError('Invalid parameters given for Point initialization.')
-
- # Creating the coordinate sequence, and setting X, Y, [Z]
- cs = create_cs(c_uint(1), c_uint(ndim))
- cs_setx(cs, 0, coords[0])
- cs_sety(cs, 0, coords[1])
- if ndim == 3: cs_setz(cs, 0, coords[2])
-
- # Initializing using the address returned from the GEOS
- # createPoint factory.
- super(Point, self).__init__(create_point(cs), srid=srid)
-
- def __len__(self):
- "Returns the number of dimensions for this Point (either 0, 2 or 3)."
- if self.empty: return 0
- if self.hasz: return 3
- else: return 2
-
- def get_x(self):
- "Returns the X component of the Point."
- return self._cs.getOrdinate(0, 0)
-
- def set_x(self, value):
- "Sets the X component of the Point."
- self._cs.setOrdinate(0, 0, value)
-
- def get_y(self):
- "Returns the Y component of the Point."
- return self._cs.getOrdinate(1, 0)
-
- def set_y(self, value):
- "Sets the Y component of the Point."
- self._cs.setOrdinate(1, 0, value)
-
- def get_z(self):
- "Returns the Z component of the Point."
- if self.hasz:
- return self._cs.getOrdinate(2, 0)
- else:
- return None
-
- def set_z(self, value):
- "Sets the Z component of the Point."
- if self.hasz:
- self._cs.setOrdinate(2, 0, value)
- else:
- raise GEOSException('Cannot set Z on 2D Point.')
-
- # X, Y, Z properties
- x = property(get_x, set_x)
- y = property(get_y, set_y)
- z = property(get_z, set_z)
-
- ### Tuple setting and retrieval routines. ###
- def get_coords(self):
- "Returns a tuple of the point."
- return self._cs.tuple
-
- def set_coords(self, tup):
- "Sets the coordinates of the point with the given tuple."
- self._cs[0] = tup
-
- # The tuple and coords properties
- tuple = property(get_coords, set_coords)
- coords = tuple
-
-class LineString(GEOSGeometry):
-
- #### Python 'magic' routines ####
- def __init__(self, *args, **kwargs):
- """
- Initializes on the given sequence -- may take lists, tuples, NumPy arrays
- of X,Y pairs, or Point objects. If Point objects are used, ownership is
- _not_ transferred to the LineString object.
-
- Examples:
- ls = LineString((1, 1), (2, 2))
- ls = LineString([(1, 1), (2, 2)])
- ls = LineString(array([(1, 1), (2, 2)]))
- ls = LineString(Point(1, 1), Point(2, 2))
- """
- # If only one argument provided, set the coords array appropriately
- if len(args) == 1: coords = args[0]
- else: coords = args
-
- if isinstance(coords, (tuple, list)):
- # Getting the number of coords and the number of dimensions -- which
- # must stay the same, e.g., no LineString((1, 2), (1, 2, 3)).
- ncoords = len(coords)
- if coords: ndim = len(coords[0])
- else: raise TypeError('Cannot initialize on empty sequence.')
- self._checkdim(ndim)
- # Incrementing through each of the coordinates and verifying
- for i in xrange(1, ncoords):
- if not isinstance(coords[i], (tuple, list, Point)):
- raise TypeError('each coordinate should be a sequence (list or tuple)')
- if len(coords[i]) != ndim: raise TypeError('Dimension mismatch.')
- numpy_coords = False
- elif HAS_NUMPY and isinstance(coords, ndarray):
- shape = coords.shape # Using numpy's shape.
- if len(shape) != 2: raise TypeError('Too many dimensions.')
- self._checkdim(shape[1])
- ncoords = shape[0]
- ndim = shape[1]
- numpy_coords = True
- else:
- raise TypeError('Invalid initialization input for LineStrings.')
-
- # Creating a coordinate sequence object because it is easier to
- # set the points using GEOSCoordSeq.__setitem__().
- cs = GEOSCoordSeq(create_cs(ncoords, ndim), z=bool(ndim==3))
- for i in xrange(ncoords):
- if numpy_coords: cs[i] = coords[i,:]
- elif isinstance(coords[i], Point): cs[i] = coords[i].tuple
- else: cs[i] = coords[i]
-
- # Getting the correct initialization function
- if kwargs.get('ring', False):
- func = create_linearring
- else:
- func = create_linestring
-
- # If SRID was passed in with the keyword arguments
- srid = kwargs.get('srid', None)
-
- # Calling the base geometry initialization with the returned pointer
- # from the function.
- super(LineString, self).__init__(func(cs.ptr), srid=srid)
-
- def __getitem__(self, index):
- "Gets the point at the specified index."
- return self._cs[index]
-
- def __setitem__(self, index, value):
- "Sets the point at the specified index, e.g., line_str[0] = (1, 2)."
- self._cs[index] = value
-
- def __iter__(self):
- "Allows iteration over this LineString."
- for i in xrange(len(self)):
- yield self[i]
-
- def __len__(self):
- "Returns the number of points in this LineString."
- return len(self._cs)
-
- def _checkdim(self, dim):
- if dim not in (2, 3): raise TypeError('Dimension mismatch.')
-
- #### Sequence Properties ####
- @property
- def tuple(self):
- "Returns a tuple version of the geometry from the coordinate sequence."
- return self._cs.tuple
- coords = tuple
-
- def _listarr(self, func):
- """
- Internal routine that returns a sequence (list) corresponding with
- the given function. Will return a numpy array if possible.
- """
- lst = [func(i) for i in xrange(len(self))]
- if HAS_NUMPY: return array(lst) # ARRRR!
- else: return lst
-
- @property
- def array(self):
- "Returns a numpy array for the LineString."
- return self._listarr(self._cs.__getitem__)
-
- @property
- def x(self):
- "Returns a list or numpy array of the X variable."
- return self._listarr(self._cs.getX)
-
- @property
- def y(self):
- "Returns a list or numpy array of the Y variable."
- return self._listarr(self._cs.getY)
-
- @property
- def z(self):
- "Returns a list or numpy array of the Z variable."
- if not self.hasz: return None
- else: return self._listarr(self._cs.getZ)
-
-# LinearRings are LineStrings used within Polygons.
-class LinearRing(LineString):
- def __init__(self, *args, **kwargs):
- "Overriding the initialization function to set the ring keyword."
- kwargs['ring'] = True # Setting the ring keyword argument to True
- super(LinearRing, self).__init__(*args, **kwargs)
-
-class Polygon(GEOSGeometry):
-
- def __init__(self, *args, **kwargs):
- """
- Initializes on an exterior ring and a sequence of holes (both
- instances may be either LinearRing instances, or a tuple/list
- that may be constructed into a LinearRing).
-
- Examples of initialization, where shell, hole1, and hole2 are
- valid LinearRing geometries:
- >>> poly = Polygon(shell, hole1, hole2)
- >>> poly = Polygon(shell, (hole1, hole2))
-
- Example where a tuple parameters are used:
- >>> poly = Polygon(((0, 0), (0, 10), (10, 10), (0, 10), (0, 0)),
- ((4, 4), (4, 6), (6, 6), (6, 4), (4, 4)))
- """
- if not args:
- raise TypeError('Must provide at list one LinearRing instance to initialize Polygon.')
-
- # Getting the ext_ring and init_holes parameters from the argument list
- ext_ring = args[0]
- init_holes = args[1:]
- n_holes = len(init_holes)
-
- # If initialized as Polygon(shell, (LinearRing, LinearRing)) [for backward-compatibility]
- if n_holes == 1 and isinstance(init_holes[0], (tuple, list)) and \
- (len(init_holes[0]) == 0 or isinstance(init_holes[0][0], LinearRing)):
- init_holes = init_holes[0]
- n_holes = len(init_holes)
-
- # Ensuring the exterior ring and holes parameters are LinearRing objects
- # or may be instantiated into LinearRings.
- ext_ring = self._construct_ring(ext_ring, 'Exterior parameter must be a LinearRing or an object that can initialize a LinearRing.')
- holes_list = [] # Create new list, cause init_holes is a tuple.
- for i in xrange(n_holes):
- holes_list.append(self._construct_ring(init_holes[i], 'Holes parameter must be a sequence of LinearRings or objects that can initialize to LinearRings'))
-
- # Why another loop? Because if a TypeError is raised, cloned pointers will
- # be around that can't be cleaned up.
- holes = get_pointer_arr(n_holes)
- for i in xrange(n_holes): holes[i] = geom_clone(holes_list[i].ptr)
-
- # Getting the shell pointer address.
- shell = geom_clone(ext_ring.ptr)
-
- # Calling with the GEOS createPolygon factory.
- super(Polygon, self).__init__(create_polygon(shell, byref(holes), c_uint(n_holes)), **kwargs)
-
- def __getitem__(self, index):
- """
- Returns the ring at the specified index. The first index, 0, will
- always return the exterior ring. Indices > 0 will return the
- interior ring at the given index (e.g., poly[1] and poly[2] would
- return the first and second interior ring, respectively).
- """
- if index == 0:
- return self.exterior_ring
- else:
- # Getting the interior ring, have to subtract 1 from the index.
- return self.get_interior_ring(index-1)
-
- def __setitem__(self, index, ring):
- "Sets the ring at the specified index with the given ring."
- # Checking the index and ring parameters.
- self._checkindex(index)
- if not isinstance(ring, LinearRing):
- raise TypeError('must set Polygon index with a LinearRing object')
-
- # Getting the shell
- if index == 0:
- shell = geom_clone(ring.ptr)
- else:
- shell = geom_clone(get_extring(self.ptr))
-
- # Getting the interior rings (holes)
- nholes = len(self)-1
- if nholes > 0:
- holes = get_pointer_arr(nholes)
- for i in xrange(nholes):
- if i == (index-1):
- holes[i] = geom_clone(ring.ptr)
- else:
- holes[i] = geom_clone(get_intring(self.ptr, i))
- holes_param = byref(holes)
- else:
- holes_param = None
-
- # Getting the current pointer, replacing with the newly constructed
- # geometry, and destroying the old geometry.
- prev_ptr = self.ptr
- srid = self.srid
- self._ptr = create_polygon(shell, holes_param, c_uint(nholes))
- if srid: self.srid = srid
- destroy_geom(prev_ptr)
-
- def __iter__(self):
- "Iterates over each ring in the polygon."
- for i in xrange(len(self)):
- yield self[i]
-
- def __len__(self):
- "Returns the number of rings in this Polygon."
- return self.num_interior_rings + 1
-
- def _checkindex(self, index):
- "Internal routine for checking the given ring index."
- if index < 0 or index >= len(self):
- raise GEOSIndexError('invalid Polygon ring index: %s' % index)
-
- def _construct_ring(self, param, msg=''):
- "Helper routine for trying to construct a ring from the given parameter."
- if isinstance(param, LinearRing): return param
- try:
- ring = LinearRing(param)
- return ring
- except TypeError:
- raise TypeError(msg)
-
- def get_interior_ring(self, ring_i):
- """
- Gets the interior ring at the specified index, 0 is for the first
- interior ring, not the exterior ring.
- """
- self._checkindex(ring_i+1)
- return GEOSGeometry(geom_clone(get_intring(self.ptr, ring_i)), srid=self.srid)
-
- #### Polygon Properties ####
- @property
- def num_interior_rings(self):
- "Returns the number of interior rings."
- # Getting the number of rings
- return get_nrings(self.ptr)
-
- def get_ext_ring(self):
- "Gets the exterior ring of the Polygon."
- return GEOSGeometry(geom_clone(get_extring(self.ptr)), srid=self.srid)
-
- def set_ext_ring(self, ring):
- "Sets the exterior ring of the Polygon."
- self[0] = ring
-
- # properties for the exterior ring/shell
- exterior_ring = property(get_ext_ring, set_ext_ring)
- shell = exterior_ring
-
- @property
- def tuple(self):
- "Gets the tuple for each ring in this Polygon."
- return tuple([self[i].tuple for i in xrange(len(self))])
- coords = tuple
-
- @property
- def kml(self):
- "Returns the KML representation of this Polygon."
- inner_kml = ''.join(["<innerBoundaryIs>%s</innerBoundaryIs>" % self[i+1].kml
- for i in xrange(self.num_interior_rings)])
- return "<Polygon><outerBoundaryIs>%s</outerBoundaryIs>%s</Polygon>" % (self[0].kml, inner_kml)
View
622 django/contrib/gis/geos/geometry.py
@@ -0,0 +1,622 @@
+"""
+ This module contains the 'base' GEOSGeometry object -- all GEOS Geometries
+ inherit from this object.
+"""
+# Python, ctypes and types dependencies.
+import re
+from ctypes import addressof, byref, c_double, c_size_t
+
+# super-class for mutable list behavior
+from django.contrib.gis.geos.mutable_list import ListMixin
+
+# GEOS-related dependencies.
+from django.contrib.gis.geos.base import GEOSBase, gdal
+from django.contrib.gis.geos.coordseq import GEOSCoordSeq
+from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
+from django.contrib.gis.geos.libgeos import GEOM_PTR, GEOS_PREPARE
+from django.contrib.gis.geos.mutable_list import ListMixin
+
+# All other functions in this module come from the ctypes
+# prototypes module -- which handles all interaction with
+# the underlying GEOS library.
+from django.contrib.gis.geos import prototypes as capi
+from django.contrib.gis.geos import io
+
+# Regular expression for recognizing HEXEWKB and WKT. A prophylactic measure
+# 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'^(SRID=(?P<srid>\d+);)?(?P<wkt>(POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)[ACEGIMLONPSRUTY\d,\.\-\(\) ]+)$', re.I)
+
+class GEOSGeometry(GEOSBase, ListMixin):
+ "A class that, generally, encapsulates a GEOS geometry."
+
+ # Raise GEOSIndexError instead of plain IndexError
+ # (see ticket #4740 and GEOSIndexError docstring)
+ _IndexError = GEOSIndexError
+
+ ptr_type = GEOM_PTR
+
+ #### Python 'magic' routines ####
+ def __init__(self, geo_input, srid=None):
+ """
+ The base constructor for GEOS geometry objects, and may take the
+ following inputs:
+
+ * strings:
+ - WKT
+ - HEXEWKB (a PostGIS-specific canonical form)
+ - GeoJSON (requires GDAL)
+ * buffer:
+ - WKB
+
+ The `srid` keyword is used to specify the Source Reference Identifier
+ (SRID) number for this Geometry. If not set, the SRID will be None.
+ """
+ if isinstance(geo_input, basestring):
+ if isinstance(geo_input, unicode):
+ # Encoding to ASCII, WKT or HEXEWKB doesn't need any more.
+ geo_input = geo_input.encode('ascii')
+
+ wkt_m = wkt_regex.match(geo_input)
+ if wkt_m:
+ # Handling WKT input.
+ if wkt_m.group('srid'): srid = int(wkt_m.group('srid'))
+ g = io.wkt_r.read(wkt_m.group('wkt'))
+ elif hex_regex.match(geo_input):
+ # Handling HEXEWKB input.
+ g = io.wkb_r.read(geo_input)
+ elif gdal.GEOJSON and gdal.geometries.json_regex.match(geo_input):
+ # Handling GeoJSON input.
+ g = io.wkb_r.read(gdal.OGRGeometry(geo_input).wkb)
+ 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
+ elif isinstance(geo_input, buffer):
+ # When the input is a buffer (WKB).
+ g = io.wkb_r.read(geo_input)
+ elif isinstance(geo_input, GEOSGeometry):
+ g = capi.geom_clone(geo_input.ptr)
+ else:
+ # Invalid geometry type.
+ raise TypeError('Improper geometry input type: %s' % str(type(geo_input)))
+
+ if bool(g):
+ # Setting the pointer object with a valid pointer.
+ self.ptr = g
+ else:
+ raise GEOSException('Could not initialize GEOS Geometry with given input.')
+
+ # Post-initialization setup.
+ self._post_init(srid)
+
+ def _post_init(self, srid):
+ "Helper routine for performing post-initialization setup."
+ # Setting the SRID, if given.
+ if srid and isinstance(srid, int): self.srid = srid
+
+ # Setting the class type (e.g., Point, Polygon, etc.)
+ self.__class__ = GEOS_CLASSES[self.geom_typeid]
+
+ # Setting the coordinate sequence for the geometry (will be None on
+ # geometries that do not have coordinate sequences)
+ self._set_cs()
+
+ def __del__(self):
+ """
+ Destroys this Geometry; in other words, frees the memory used by the
+ GEOS C++ object.
+ """
+ if self._ptr: capi.destroy_geom(self._ptr)
+
+ def __copy__(self):
+ """
+ Returns a clone because the copy of a GEOSGeometry may contain an
+ invalid pointer location if the original is garbage collected.
+ """
+ return self.clone()
+
+ def __deepcopy__(self, memodict):
+ """
+ The `deepcopy` routine is used by the `Node` class of django.utils.tree;
+ thus, the protocol routine needs to be implemented to return correct
+ copies (clones) of these GEOS objects, which use C pointers.
+ """
+ return self.clone()
+
+ def __str__(self):
+ "WKT is used for the string representation."
+ return self.wkt
+
+ def __repr__(self):
+ "Short-hand representation because WKT may be very large."
+ return '<%s object at %s>' % (self.geom_type, hex(addressof(self.ptr)))
+
+ # Pickling support
+ def __getstate__(self):
+ # The pickled state is simply a tuple of the WKB (in string form)
+ # and the SRID.
+ return str(self.wkb), self.srid
+
+ def __setstate__(self, state):
+ # Instantiating from the tuple state that was pickled.
+ wkb, srid = state
+ ptr = capi.from_wkb(wkb, len(wkb))
+ if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.')
+ self.ptr = ptr
+ self._post_init(srid)
+
+ # Comparison operators
+ def __eq__(self, other):
+ """
+ Equivalence testing, a Geometry may be compared with another Geometry
+ or a WKT representation.
+ """
+ if isinstance(other, basestring):
+ return self.wkt == other
+ elif isinstance(other, GEOSGeometry):
+ return self.equals_exact(other)
+ else:
+ return False
+
+ def __ne__(self, other):
+ "The not equals operator."
+ return not (self == other)
+
+ ### Geometry set-like operations ###
+ # Thanks to Sean Gillies for inspiration:
+ # http://lists.gispython.org/pipermail/community/2007-July/001034.html
+ # g = g1 | g2
+ def __or__(self, other):
+ "Returns the union of this Geometry and the other."
+ return self.union(other)
+
+ # g = g1 & g2
+ def __and__(self, other):
+ "Returns the intersection of this Geometry and the other."
+ return self.intersection(other)
+
+ # g = g1 - g2
+ def __sub__(self, other):
+ "Return the difference this Geometry and the other."
+ return self.difference(other)
+
+ # g = g1 ^ g2
+ def __xor__(self, other):
+ "Return the symmetric difference of this Geometry and the other."
+ return self.sym_difference(other)
+
+ #### Coordinate Sequence Routines ####
+ @property
+ def has_cs(self):
+ "Returns True if this Geometry has a coordinate sequence, False if not."
+ # Only these geometries are allowed to have coordinate sequences.
+ if isinstance(self, (Point, LineString, LinearRing)):
+ return True
+ else:
+ return False
+
+ def _set_cs(self):
+ "Sets the coordinate sequence for this Geometry."
+ if self.has_cs:
+ self._cs = GEOSCoordSeq(capi.get_cs(self.ptr), self.hasz)
+ else:
+ self._cs = None
+
+ @property
+ def coord_seq(self):
+ "Returns a clone of the coordinate sequence for this Geometry."
+ if self.has_cs:
+ return self._cs.clone()
+
+ #### Geometry Info ####
+ @property
+ def geom_type(self):
+ "Returns a string representing the Geometry type, e.g. 'Polygon'"
+ return capi.geos_type(self.ptr)
+
+ @property
+ def geom_typeid(self):
+ "Returns an integer representing the Geometry type."
+ return capi.geos_typeid(self.ptr)
+
+ @property
+ def num_geom(self):
+ "Returns the number of geometries in the Geometry."
+ return capi.get_num_geoms(self.ptr)
+
+ @property
+ def num_coords(self):
+ "Returns the number of coordinates in the Geometry."
+ return capi.get_num_coords(self.ptr)
+
+ @property
+ def num_points(self):
+ "Returns the number points, or coordinates, in the Geometry."
+ return self.num_coords
+
+ @property
+ def dims(self):
+ "Returns the dimension of this Geometry (0=point, 1=line, 2=surface)."
+ return capi.get_dims(self.ptr)
+
+ def normalize(self):
+ "Converts this Geometry to normal form (or canonical form)."
+ return capi.geos_normalize(self.ptr)
+
+ #### Unary predicates ####
+ @property
+ def empty(self):
+ """
+ Returns a boolean indicating whether the set of points in this Geometry
+ are empty.
+ """
+ return capi.geos_isempty(self.ptr)
+
+ @property
+ def hasz(self):
+ "Returns whether the geometry has a 3D dimension."
+ return capi.geos_hasz(self.ptr)
+
+ @property
+ def ring(self):
+ "Returns whether or not the geometry is a ring."
+ return capi.geos_isring(self.ptr)
+
+ @property
+ def simple(self):
+ "Returns false if the Geometry not simple."
+ return capi.geos_issimple(self.ptr)
+
+ @property
+ def valid(self):
+ "This property tests the validity of this Geometry."
+ return capi.geos_isvalid(self.ptr)
+
+ #### Binary predicates. ####
+ def contains(self, other):
+ "Returns true if other.within(this) returns true."
+ return capi.geos_contains(self.ptr, other.ptr)
+
+ def crosses(self, other):
+ """
+ Returns true if the DE-9IM intersection matrix for the two Geometries
+ is T*T****** (for a point and a curve,a point and an area or a line and
+ an area) 0******** (for two curves).
+ """
+ return capi.geos_crosses(self.ptr, other.ptr)
+
+ def disjoint(self, other):
+ """
+ Returns true if the DE-9IM intersection matrix for the two Geometries
+ is FF*FF****.
+ """
+ return capi.geos_disjoint(self.ptr, other.ptr)
+
+ def equals(self, other):
+ """
+ Returns true if the DE-9IM intersection matrix for the two Geometries
+ is T*F**FFF*.
+ """
+ return capi.geos_equals(self.ptr, other.ptr)
+
+ def equals_exact(self, other, tolerance=0):
+ """
+ Returns true if the two Geometries are exactly equal, up to a
+ specified tolerance.
+ """
+ return capi.geos_equalsexact(self.ptr, other.ptr, float(tolerance))
+
+ def intersects(self, other):
+ "Returns true if disjoint returns false."
+ return capi.geos_intersects(self.ptr, other.ptr)
+
+ def overlaps(self, other):
+ """
+ Returns true if the DE-9IM intersection matrix for the two Geometries
+ is T*T***T** (for two points or two surfaces) 1*T***T** (for two curves).
+ """
+ return capi.geos_overlaps(self.ptr, other.ptr)
+
+ def relate_pattern(self, other, pattern):
+ """
+ Returns true if the elements in the DE-9IM intersection matrix for the
+ two Geometries match the elements in pattern.
+ """
+ if not isinstance(pattern, basestring) or len(pattern) > 9:
+ raise GEOSException('invalid intersection matrix pattern')
+ return capi.geos_relatepattern(self.ptr, other.ptr, pattern)
+
+ def touches(self, other):
+ """
+ Returns true if the DE-9IM intersection matrix for the two Geometries
+ is FT*******, F**T***** or F***T****.
+ """
+ return capi.geos_touches(self.ptr, other.ptr)
+
+ def within(self, other):
+ """
+ Returns true if the DE-9IM intersection matrix for the two Geometries
+ is T*F**F***.
+ """
+ return capi.geos_within(self.ptr, other.ptr)
+
+ #### SRID Routines ####
+ def get_srid(self):
+ "Gets the SRID for the geometry, returns None if no SRID is set."
+ s = capi.geos_get_srid(self.ptr)
+ if s == 0: return None
+ else: return s
+
+ def set_srid(self, srid):
+ "Sets the SRID for the geometry."
+ capi.geos_set_srid(self.ptr, 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."
+ return io.wkt_w.write(self.ptr)
+
+ @property
+ def hex(self):
+ """
+ Returns the HEX of the Geometry -- please note that the SRID is not
+ included in this representation, because the GEOS C library uses
+ -1 by default, even if the SRID is set.
+ """
+ # A possible faster, all-python, implementation:
+ # str(self.wkb).encode('hex')
+ return io.wkb_w.write_hex(self.ptr)
+
+ @property
+ def json(self):
+ """
+ Returns GeoJSON representation of this Geometry if GDAL 1.5+
+ is installed.
+ """
+ if gdal.GEOJSON:
+ return self.ogr.json
+ else:
+ raise GEOSException('GeoJSON output only supported on GDAL 1.5+.')
+ geojson = json
+
+ @property
+ def wkb(self):
+ "Returns the WKB of the Geometry as a buffer."
+ return io.wkb_w.write(self.ptr)
+
+ @property
+ def kml(self):
+ "Returns the KML representation of this Geometry."
+ gtype = self.geom_type
+ return '<%s>%s</%s>' % (gtype, self.coord_seq.kml, gtype)
+
+ @property
+ def prepared(self):
+ """
+ Returns a PreparedGeometry corresponding to this geometry -- it is
+ optimized for the contains, intersects, and covers operations.
+ """
+ if GEOS_PREPARE:
+ return PreparedGeometry(self)
+ else:
+ raise GEOSException('GEOS 3.1+ required for prepared geometry support.')
+
+ #### GDAL-specific output routines ####
+ @property
+ def ogr(self):
+ "Returns the OGR Geometry for this Geometry."
+ if gdal.HAS_GDAL:
+ if self.srid:
+ return gdal.OGRGeometry(self.wkb, self.srid)
+ else:
+ return gdal.OGRGeometry(self.wkb)
+ else:
+ raise GEOSException('GDAL required to convert to an OGRGeometry.')
+
+ @property
+ def srs(self):
+ "Returns the OSR SpatialReference for SRID of this Geometry."
+ if gdal.HAS_GDAL:
+ if self.srid:
+ return gdal.SpatialReference(self.srid)
+ else:
+ return None
+ else:
+ raise GEOSException('GDAL required to return a SpatialReference object.')
+
+ @property
+ def crs(self):
+ "Alias for `srs` property."
+ return self.srs
+
+ def transform(self, ct, clone=False):
+ """
+ Requires GDAL. Transforms the geometry according to the given
+ transformation object, which may be an integer SRID, and WKT or
+ PROJ.4 string. By default, the geometry is transformed in-place and
+ nothing is returned. However if the `clone` keyword is set, then this
+ geometry will not be modified and a transformed clone will be returned
+ instead.
+ """
+ srid = self.srid
+ if gdal.HAS_GDAL and srid:
+ # Creating an OGR Geometry, which is then transformed.
+ g = gdal.OGRGeometry(self.wkb, srid)
+ g.transform(ct)
+ # Getting a new GEOS pointer
+ ptr = io.wkb_r.read(g.wkb)
+ if clone:
+ # User wants a cloned transformed geometry returned.
+ return GEOSGeometry(ptr, srid=g.srid)
+ if ptr:
+ # Reassigning pointer, and performing post-initialization setup
+ # again due to the reassignment.
+ capi.destroy_geom(self.ptr)
+ self.ptr = ptr
+ self._post_init(g.srid)
+ else:
+ raise GEOSException('Transformed WKB was invalid.')
+
+ #### Topology Routines ####
+ def _topology(self, gptr):
+ "Helper routine to return Geometry from the given pointer."
+ return GEOSGeometry(gptr, srid=self.srid)
+
+ @property
+ def boundary(self):
+ "Returns the boundary as a newly allocated Geometry object."
+ return self._topology(capi.geos_boundary(self.ptr))
+
+ def buffer(self, width, quadsegs=8):
+ """
+ Returns a geometry that represents all points whose distance from this
+ Geometry is less than or equal to distance. Calculations are in the
+ Spatial Reference System of this Geometry. The optional third parameter sets
+ the number of segment used to approximate a quarter circle (defaults to 8).
+ (Text from PostGIS documentation at ch. 6.1.3)
+ """
+ return self._topology(capi.geos_buffer(self.ptr, width, quadsegs))
+
+ @property
+ def centroid(self):
+ """
+ The centroid is equal to the centroid of the set of component Geometries
+ of highest dimension (since the lower-dimension geometries contribute zero
+ "weight" to the centroid).
+ """
+ return self._topology(capi.geos_centroid(self.ptr))
+
+ @property
+ def convex_hull(self):
+ """
+ Returns the smallest convex Polygon that contains all the points
+ in the Geometry.
+ """
+ return self._topology(capi.geos_convexhull(self.ptr))
+
+ def difference(self, other):
+ """
+ Returns a Geometry representing the points making up this Geometry
+ that do not make up other.
+ """
+ return self._topology(capi.geos_difference(self.ptr, other.ptr))
+
+ @property
+ def envelope(self):
+ "Return the envelope for this geometry (a polygon)."
+ return self._topology(capi.geos_envelope(self.ptr))
+
+ def intersection(self, other):
+ "Returns a Geometry representing the points shared by this Geometry and other."
+ return self._topology(capi.geos_intersection(self.ptr, other.ptr))
+
+ @property
+ def point_on_surface(self):
+ "Computes an interior point of this Geometry."
+ return self._topology(capi.geos_pointonsurface(self.ptr))
+
+ def relate(self, other):
+ "Returns the DE-9IM intersection matrix for this Geometry and the other."
+ return capi.geos_relate(self.ptr, other.ptr)
+
+ def simplify(self, tolerance=0.0, preserve_topology=False):
+ """
+ Returns the Geometry, simplified using the Douglas-Peucker algorithm
+ to the specified tolerance (higher tolerance => less points). If no
+ tolerance provided, defaults to 0.
+
+ By default, this function does not preserve topology - e.g. polygons can
+ be split, collapse to lines or disappear holes can be created or
+ disappear, and lines can cross. By specifying preserve_topology=True,
+ the result will have the same dimension and number of components as the
+ input. This is significantly slower.
+ """
+ if preserve_topology:
+ return self._topology(capi.geos_preservesimplify(self.ptr, tolerance))
+ else:
+ return self._topology(capi.geos_simplify(self.ptr, tolerance))
+
+ def sym_difference(self, other):
+ """
+ Returns a set combining the points in this Geometry not in other,
+ and the points in other not in this Geometry.
+ """
+ return self._topology(capi.geos_symdifference(self.ptr, other.ptr))
+
+ def union(self, other):
+ "Returns a Geometry representing all the points in this Geometry and other."
+ return self._topology(capi.geos_union(self.ptr, other.ptr))
+
+ #### Other Routines ####
+ @property
+ def area(self):
+ "Returns the area of the Geometry."
+ return capi.geos_area(self.ptr, byref(c_double()))
+
+ def distance(self, other):
+ """
+ Returns the distance between the closest points on this Geometry
+ and the other. Units will be in those of the coordinate system of
+ the Geometry.
+ """
+ if not isinstance(other, GEOSGeometry):
+ raise TypeError('distance() works only on other GEOS Geometries.')
+ return capi.geos_distance(self.ptr, other.ptr, byref(c_double()))
+
+ @property
+ def extent(self):
+ """
+ Returns the extent of this geometry as a 4-tuple, consisting of
+ (xmin, ymin, xmax, ymax).
+ """
+ env = self.envelope
+ if isinstance(env, Point):
+ xmin, ymin = env.tuple
+ xmax, ymax = xmin, ymin
+ else:
+ xmin, ymin = env[0][0]
+ xmax, ymax = env[0][2]
+ return (xmin, ymin, xmax, ymax)
+
+ @property
+ def length(self):
+ """
+ Returns the length of this Geometry (e.g., 0 for point, or the
+ circumfrence of a Polygon).
+ """
+ return capi.geos_length(self.ptr, byref(c_double()))
+
+ def clone(self):
+ "Clones this Geometry."
+ return GEOSGeometry(capi.geom_clone(self.ptr), srid=self.srid)
+
+# Class mapping dictionary. Has to be at the end to avoid import
+# conflicts with GEOSGeometry.
+from django.contrib.gis.geos.linestring import LineString, LinearRing
+from django.contrib.gis.geos.point import Point
+from django.contrib.gis.geos.polygon import Polygon
+from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon
+GEOS_CLASSES = {0 : Point,
+ 1 : LineString,
+ 2 : LinearRing,
+ 3 : Polygon,
+ 4 : MultiPoint,
+ 5 : MultiLineString,
+ 6 : MultiPolygon,
+ 7 : GeometryCollection,
+ }
+
+# If supported, import the PreparedGeometry class.
+if GEOS_PREPARE:
+ from django.contrib.gis.geos.prepared import PreparedGeometry
View
109 django/contrib/gis/geos/io.py
@@ -0,0 +1,109 @@
+"""
+Module that holds classes for performing I/O operations on GEOS geometry
+objects. Specifically, this has Python implementations of WKB/WKT
+reader and writer classes.
+"""
+from ctypes import byref, c_size_t
+from django.contrib.gis.geos.base import GEOSBase
+from django.contrib.gis.geos.error import GEOSException
+from django.contrib.gis.geos.libgeos import GEOM_PTR
+from django.contrib.gis.geos.prototypes import io as capi
+
+class IOBase(GEOSBase):
+ "Base class for IO objects that that have `destroy` method."
+ def __init__(self):
+ # Getting the pointer with the constructor.
+ self.ptr = self.constructor()
+
+ def __del__(self):
+ # Cleaning up with the appropriate destructor.
+ if self._ptr: self.destructor(self._ptr)
+
+ def _get_geom_ptr(self, geom):
+ if hasattr(geom, 'ptr'): geom = geom.ptr
+ if not isinstance(geom, GEOM_PTR): raise TypeError
+ return geom
+
+### WKT Reading and Writing objects ###
+class WKTReader(IOBase):
+ constructor = capi.wkt_reader_create
+ destructor = capi.wkt_reader_destroy
+ ptr_type = capi.WKT_READ_PTR
+
+ def read(self, wkt, ptr=False):
+ if not isinstance(wkt, basestring): raise TypeError
+ return capi.wkt_reader_read(self.ptr, wkt)
+
+class WKTWriter(IOBase):
+ constructor = capi.wkt_writer_create
+ destructor = capi.wkt_writer_destroy
+ ptr_type = capi.WKT_WRITE_PTR
+
+ def write(self, geom):
+ return capi.wkt_writer_write(self.ptr, self._get_geom_ptr(geom))
+
+### WKB Reading and Writing objects ###
+class WKBReader(IOBase):
+ constructor = capi.wkb_reader_create
+ destructor = capi.wkb_reader_destroy
+ ptr_type = capi.WKB_READ_PTR
+
+ def read(self, wkb):
+ if isinstance(wkb, buffer):
+ wkb_s = str(wkb)
+ return capi.wkb_reader_read(self.ptr, wkb_s, len(wkb_s))
+ elif isinstance(wkb, basestring):
+ return capi.wkb_reader_read_hex(self.ptr, wkb, len(wkb))
+ else:
+ raise TypeError
+
+class WKBWriter(IOBase):
+ constructor = capi.wkb_writer_create
+ destructor = capi.wkb_writer_destroy
+ ptr_type = capi.WKB_READ_PTR
+
+ def write(self, geom):
+ return buffer(capi.wkb_writer_write(self.ptr, self._get_geom_ptr(geom), byref(c_size_t())))
+
+ def write_hex(self, geom):
+ return capi.wkb_writer_write_hex(self.ptr, self._get_geom_ptr(geom), byref(c_size_t()))
+
+ ### WKBWriter Properties ###
+
+ # Property for getting/setting the byteorder.
+ def _get_byteorder(self):
+ return capi.wkb_writer_get_byteorder(self.ptr)
+
+ def _set_byteorder(self, order):
+ if not order in (0, 1): raise ValueError('Byte order parameter must be 0 (Big Endian) or 1 (Little Endian).')
+ capi.wkb_writer_set_byteorder(self.ptr, order)
+
+ byteorder = property(_get_byteorder, _set_byteorder)
+
+ # Property for getting/setting the output dimension.
+ def _get_outdim(self):
+ return capi.wkb_writer_get_outdim(self.ptr)
+
+ def _set_outdim(self, new_dim):
+ if not new_dim in (2, 3): raise ValueError('WKB output dimension must be 2 or 3')
+ capi.wkb_writer_set_outdim(self.ptr, new_dim)
+
+ outdim = property(_get_outdim, _set_outdim)
+
+ # Property for getting/setting the include srid flag.
+ def _get_include_srid(self):
+ return bool(ord(capi.wkb_writer_get_include_srid(self.ptr)))
+
+ def _set_include_srid(self, include):
+ if bool(include): flag = chr(1)
+ else: flag = chr(0)
+ capi.wkb_writer_set_include_srid(self.ptr, flag)
+
+ srid = property(_get_include_srid, _set_include_srid)
+
+# Instances of the WKT and WKB reader/writer objects.
+wkt_r = WKTReader()
+wkt_w = WKTWriter()
+wkb_r = WKBReader()
+wkb_w = WKBWriter()
+
View
38 django/contrib/gis/geos/libgeos.py
@@ -11,13 +11,6 @@
from ctypes.util import find_library
from django.contrib.gis.geos.error import GEOSException
-# NumPy supported?
-try:
- from numpy import array, ndarray
- HAS_NUMPY = True
-except ImportError:
- HAS_NUMPY = False
-
# Custom library path set?
try:
from django.conf import settings
@@ -37,18 +30,18 @@
else:
raise ImportError('Unsupported OS "%s"' % os.name)
-# Using the ctypes `find_library` utility to find the the path to the GEOS
-# shared library. This is better than manually specifiying each library name
+# Using the ctypes `find_library` utility to find the the path to the GEOS
+# shared library. This is better than manually specifiying each library name
# and extension (e.g., libgeos_c.[so|so.1|dylib].).
-if lib_names:
+if lib_names:
for lib_name in lib_names:
lib_path = find_library(lib_name)
if not lib_path is None: break
# No GEOS library could be found.
-if lib_path is None:
+if lib_path is None:
raise ImportError('Could not find the GEOS library (tried "%s"). '
- 'Try setting GEOS_LIBRARY_PATH in your settings.' %
+ 'Try setting GEOS_LIBRARY_PATH in your settings.' %
'", "'.join(lib_names))
# Getting the GEOS C library. The C interface (CDLL) is used for
@@ -65,7 +58,7 @@ def notice_h(fmt, lst, output_h=sys.stdout):
try:
warn_msg = fmt % lst
except:
- warn_msg = fmt
+ warn_msg = fmt
output_h.write('GEOS_NOTICE: %s\n' % warn_msg)
notice_h = NOTICEFUNC(notice_h)
@@ -88,28 +81,30 @@ def error_h(fmt, lst, output_h=sys.stderr):
# Opaque GEOS geometry structures, used for GEOM_PTR and CS_PTR
class GEOSGeom_t(Structure): pass
+class GEOSPrepGeom_t(Structure): pass
class GEOSCoordSeq_t(Structure): pass
# Pointers to opaque GEOS geometry structures.
GEOM_PTR = POINTER(GEOSGeom_t)
+PREPGEOM_PTR = POINTER(GEOSPrepGeom_t)
CS_PTR = POINTER(GEOSCoordSeq_t)
-# Used specifically by the GEOSGeom_createPolygon and GEOSGeom_createCollection
+# Used specifically by the GEOSGeom_createPolygon and GEOSGeom_createCollection
# GEOS routines
def get_pointer_arr(n):
"Gets a ctypes pointer array (of length `n`) for GEOSGeom_t opaque pointer."
GeomArr = GEOM_PTR * n
return GeomArr()
-# Returns the string version of the GEOS library. Have to set the restype
+# Returns the string version of the GEOS library. Have to set the restype
# explicitly to c_char_p to ensure compatibility accross 32 and 64-bit platforms.
geos_version = lgeos.GEOSversion
-geos_version.argtypes = None
+geos_version.argtypes = None
geos_version.restype = c_char_p
# Regular expression should be able to parse version strings such as
# '3.0.0rc4-CAPI-1.3.3', or '3.0.0-CAPI-1.4.1'
-version_regex = re.compile(r'^(?P<version>\d+\.\d+\.\d+)(rc(?P<release_candidate>\d+))?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)$')
+version_regex = re.compile(r'^(?P<version>(?P<major>\d+)\.(?P<minor>\d+)\.\d+)(rc(?P<release_candidate>\d+))?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)$')
def geos_version_info():
"""
Returns a dictionary containing the various version metadata parsed from
@@ -120,7 +115,14 @@ def geos_version_info():
ver = geos_version()
m = version_regex.match(ver)
if not m: raise GEOSException('Could not parse version info string "%s"' % ver)
- return dict((key, m.group(key)) for key in ('version', 'release_candidate', 'capi_version'))
+ return dict((key, m.group(key)) for key in ('version', 'release_candidate', 'capi_version', 'major', 'minor'))
+
+# Version numbers and whether or not prepared geometry support is available.
+_verinfo = geos_version_info()
+GEOS_MAJOR_VERSION = int(_verinfo['major'])
+GEOS_MINOR_VERSION = int(_verinfo['minor'])
+del _verinfo
+GEOS_PREPARE = GEOS_MAJOR_VERSION > 3 or GEOS_MAJOR_VERSION == 3 and GEOS_MINOR_VERSION >= 1
# Calling the finishGEOS() upon exit of the interpreter.
atexit.register(lgeos.finishGEOS)
View
152 django/contrib/gis/geos/linestring.py
@@ -0,0 +1,152 @@
+from django.contrib.gis.geos.base import numpy
+from django.contrib.gis.geos.coordseq import GEOSCoordSeq
+from django.contrib.gis.geos.error import GEOSException
+from django.contrib.gis.geos.geometry import GEOSGeometry
+from django.contrib.gis.geos.point import Point
+from django.contrib.gis.geos import prototypes as capi
+
+class LineString(GEOSGeometry):
+ _init_func = capi.create_linestring
+ _minlength = 2
+
+ #### Python 'magic' routines ####
+ def __init__(self, *args, **kwargs):
+ """
+ Initializes on the given sequence -- may take lists, tuples, NumPy arrays
+ of X,Y pairs, or Point objects. If Point objects are used, ownership is
+ _not_ transferred to the LineString object.
+
+ Examples:
+ ls = LineString((1, 1), (2, 2))
+ ls = LineString([(1, 1), (2, 2)])
+ ls = LineString(array([(1, 1), (2, 2)]))
+ ls = LineString(Point(1, 1), Point(2, 2))
+ """
+ # If only one argument provided, set the coords array appropriately
+ if len(args) == 1: coords = args[0]
+ else: coords = args
+
+ if isinstance(coords, (tuple, list)):
+ # Getting the number of coords and the number of dimensions -- which
+ # must stay the same, e.g., no LineString((1, 2), (1, 2, 3)).
+ ncoords = len(coords)
+ if coords: ndim = len(coords[0])
+ else: raise TypeError('Cannot initialize on empty sequence.')
+ self._checkdim(ndim)
+ # Incrementing through each of the coordinates and verifying
+ for i in xrange(1, ncoords):
+ if not isinstance(coords[i], (tuple, list, Point)):
+ raise TypeError('each coordinate should be a sequence (list or tuple)')
+ if len(coords[i]) != ndim: raise TypeError('Dimension mismatch.')
+ numpy_coords = False
+ elif numpy and isinstance(coords, numpy.ndarray):
+ shape = coords.shape # Using numpy's shape.
+ if len(shape) != 2: raise TypeError('Too many dimensions.')
+ self._checkdim(shape[1])
+ ncoords = shape[0]
+ ndim = shape[1]