| 1 |
""" |
| 2 |
This module houses the Geometry Collection objects: |
| 3 |
GeometryCollection, MultiPoint, MultiLineString, and MultiPolygon |
| 4 |
""" |
| 5 |
|
| 6 |
from django.contrib.gis.geos import prototypes as capi |
| 7 |
from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin |
| 8 |
from django.contrib.gis.geos.libgeos import GEOM_PTR |
| 9 |
from django.contrib.gis.geos.linestring import LinearRing, LineString |
| 10 |
from django.contrib.gis.geos.point import Point |
| 11 |
from django.contrib.gis.geos.polygon import Polygon |
| 12 |
|
| 13 |
|
| 14 |
class GeometryCollection(GEOSGeometry): |
| 15 |
_typeid = 7 |
| 16 |
|
| 17 |
def __init__(self, *args, **kwargs): |
| 18 |
"Initialize a Geometry Collection from a sequence of Geometry objects." |
| 19 |
# Checking the arguments |
| 20 |
if len(args) == 1: |
| 21 |
# If only one geometry provided or a list of geometries is provided |
| 22 |
# in the first argument. |
| 23 |
if isinstance(args[0], (tuple, list)): |
| 24 |
init_geoms = args[0] |
| 25 |
else: |
| 26 |
init_geoms = args |
| 27 |
else: |
| 28 |
init_geoms = args |
| 29 |
|
| 30 |
# Ensuring that only the permitted geometries are allowed in this collection |
| 31 |
# this is moved to list mixin super class |
| 32 |
self._check_allowed(init_geoms) |
| 33 |
|
| 34 |
# Creating the geometry pointer array. |
| 35 |
collection = self._create_collection(len(init_geoms), init_geoms) |
| 36 |
super().__init__(collection, **kwargs) |
| 37 |
|
| 38 |
def __iter__(self): |
| 39 |
"Iterate over each Geometry in the Collection." |
| 40 |
for i in range(len(self)): |
| 41 |
yield self[i] |
| 42 |
|
| 43 |
def __len__(self): |
| 44 |
"Return the number of geometries in this Collection." |
| 45 |
return self.num_geom |
| 46 |
|
| 47 |
# ### Methods for compatibility with ListMixin ### |
| 48 |
def _create_collection(self, length, items): |
| 49 |
# Creating the geometry pointer array. |
| 50 |
geoms = (GEOM_PTR * length)( |
| 51 |
*[ |
| 52 |
# this is a little sloppy, but makes life easier |
| 53 |
# allow GEOSGeometry types (python wrappers) or pointer types |
| 54 |
capi.geom_clone(getattr(g, "ptr", g)) |
| 55 |
for g in items |
| 56 |
] |
| 57 |
) |
| 58 |
return capi.create_collection(self._typeid, geoms, length) |
| 59 |
|
| 60 |
def _get_single_internal(self, index): |
| 61 |
return capi.get_geomn(self.ptr, index) |
| 62 |
|
| 63 |
def _get_single_external(self, index): |
| 64 |
"Return the Geometry from this Collection at the given index (0-based)." |
| 65 |
# Checking the index and returning the corresponding GEOS geometry. |
| 66 |
return GEOSGeometry( |
| 67 |
capi.geom_clone(self._get_single_internal(index)), srid=self.srid |
| 68 |
) |
| 69 |
|
| 70 |
def _set_list(self, length, items): |
| 71 |
"Create a new collection, and destroy the contents of the previous pointer." |
| 72 |
prev_ptr = self.ptr |
| 73 |
srid = self.srid |
| 74 |
self.ptr = self._create_collection(length, items) |
| 75 |
if srid: |
| 76 |
self.srid = srid |
| 77 |
capi.destroy_geom(prev_ptr) |
| 78 |
|
| 79 |
_set_single = GEOSGeometry._set_single_rebuild |
| 80 |
_assign_extended_slice = GEOSGeometry._assign_extended_slice_rebuild |
| 81 |
|
| 82 |
@property |
| 83 |
def kml(self): |
| 84 |
"Return the KML for this Geometry Collection." |
| 85 |
return "<MultiGeometry>%s</MultiGeometry>" % "".join(g.kml for g in self) |
| 86 |
|
| 87 |
@property |
| 88 |
def tuple(self): |
| 89 |
"Return a tuple of all the coordinates in this Geometry Collection" |
| 90 |
return tuple(g.tuple for g in self) |
| 91 |
|
| 92 |
coords = tuple |
| 93 |
|
| 94 |
|
| 95 |
# MultiPoint, MultiLineString, and MultiPolygon class definitions. |
| 96 |
class MultiPoint(GeometryCollection): |
| 97 |
_allowed = Point |
| 98 |
_typeid = 4 |
| 99 |
|
| 100 |
|
| 101 |
class MultiLineString(LinearGeometryMixin, GeometryCollection): |
| 102 |
_allowed = (LineString, LinearRing) |
| 103 |
_typeid = 5 |
| 104 |
|
| 105 |
|
| 106 |
class MultiPolygon(GeometryCollection): |
| 107 |
_allowed = Polygon |
| 108 |
_typeid = 6 |
| 109 |
|
| 110 |
|
| 111 |
# Setting the allowed types here since GeometryCollection is defined before |
| 112 |
# its subclasses. |
| 113 |
GeometryCollection._allowed = ( |
| 114 |
Point, |
| 115 |
LineString, |
| 116 |
LinearRing, |
| 117 |
Polygon, |
| 118 |
MultiPoint, |
| 119 |
MultiLineString, |
| 120 |
MultiPolygon, |
| 121 |
) |