web/lib/django/contrib/gis/geos/collections.py
author ymh <ymh.work@gmail.com>
Wed, 02 Jun 2010 18:57:35 +0200
changeset 38 77b6da96e6f1
permissions -rw-r--r--
update django
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
"""
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
 This module houses the Geometry Collection objects:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 GeometryCollection, MultiPoint, MultiLineString, and MultiPolygon
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
"""
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from ctypes import c_int, c_uint, byref
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from django.contrib.gis.geos.error import GEOSException, GEOSIndexError
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.contrib.gis.geos.geometry import GEOSGeometry
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.contrib.gis.geos.libgeos import get_pointer_arr, GEOM_PTR, GEOS_PREPARE
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
from django.contrib.gis.geos.linestring import LineString, LinearRing
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.contrib.gis.geos.point import Point
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from django.contrib.gis.geos.polygon import Polygon
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from django.contrib.gis.geos import prototypes as capi
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
class GeometryCollection(GEOSGeometry):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    _typeid = 7
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    def __init__(self, *args, **kwargs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        "Initializes a Geometry Collection from a sequence of Geometry objects."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        # Checking the arguments
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        if not args:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
            raise TypeError('Must provide at least one Geometry to initialize %s.' % self.__class__.__name__)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        if len(args) == 1:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
            # If only one geometry provided or a list of geometries is provided
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
            #  in the first argument.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
            if isinstance(args[0], (tuple, list)):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
                init_geoms = args[0]
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            else:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
                init_geoms = args
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        else:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            init_geoms = args
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        # Ensuring that only the permitted geometries are allowed in this collection
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        # this is moved to list mixin super class
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        self._check_allowed(init_geoms)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        # Creating the geometry pointer array.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        collection = self._create_collection(len(init_geoms), iter(init_geoms))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        super(GeometryCollection, self).__init__(collection, **kwargs)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    def __iter__(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        "Iterates over each Geometry in the Collection."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        for i in xrange(len(self)):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            yield self[i]
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    def __len__(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        "Returns the number of geometries in this Collection."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        return self.num_geom
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    ### Methods for compatibility with ListMixin ###
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    def _create_collection(self, length, items):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        # Creating the geometry pointer array.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        geoms = get_pointer_arr(length)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
        for i, g in enumerate(items):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            # this is a little sloppy, but makes life easier
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            # allow GEOSGeometry types (python wrappers) or pointer types
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
            geoms[i] = capi.geom_clone(getattr(g, 'ptr', g))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        return capi.create_collection(c_int(self._typeid), byref(geoms), c_uint(length))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    def _get_single_internal(self, index):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        return capi.get_geomn(self.ptr, index)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    def _get_single_external(self, index):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        "Returns the Geometry from this Collection at the given index (0-based)."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        # Checking the index and returning the corresponding GEOS geometry.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        return GEOSGeometry(capi.geom_clone(self._get_single_internal(index)), srid=self.srid)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    def _set_list(self, length, items):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        "Create a new collection, and destroy the contents of the previous pointer."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        prev_ptr = self.ptr
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        srid = self.srid
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        self.ptr = self._create_collection(length, items)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        if srid: self.srid = srid
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        capi.destroy_geom(prev_ptr)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
    _set_single = GEOSGeometry._set_single_rebuild
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    _assign_extended_slice = GEOSGeometry._assign_extended_slice_rebuild
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    @property
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
    def kml(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        "Returns the KML for this Geometry Collection."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        return '<MultiGeometry>%s</MultiGeometry>' % ''.join([g.kml for g in self])
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    @property
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    def tuple(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        "Returns a tuple of all the coordinates in this Geometry Collection"
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        return tuple([g.tuple for g in self])
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
    coords = tuple
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
# MultiPoint, MultiLineString, and MultiPolygon class definitions.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
class MultiPoint(GeometryCollection):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    _allowed = Point
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
    _typeid = 4
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
class MultiLineString(GeometryCollection):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
    _allowed = (LineString, LinearRing)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    _typeid = 5
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
    @property
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
    def merged(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        """ 
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        Returns a LineString representing the line merge of this 
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        MultiLineString.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        """ 
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
        return self._topology(capi.geos_linemerge(self.ptr))         
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
class MultiPolygon(GeometryCollection):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
    _allowed = Polygon
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    _typeid = 6
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
    @property
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
    def cascaded_union(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        "Returns a cascaded union of this MultiPolygon."
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        if GEOS_PREPARE:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            return GEOSGeometry(capi.geos_cascaded_union(self.ptr), self.srid)
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        else:
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
            raise GEOSException('The cascaded union operation requires GEOS 3.1+.')
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
# Setting the allowed types here since GeometryCollection is defined before
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
# its subclasses.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
GeometryCollection._allowed = (Point, LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon)