|
jbronn@1
|
1 |
""" |
|
jbronn@1
|
2 |
This module contains the 'base' GEOSGeometry object -- all GEOS Geometries |
|
jbronn@1
|
3 |
inherit from this object. |
|
jbronn@1
|
4 |
""" |
|
jbronn@1
|
5 |
# Python, ctypes and types dependencies. |
|
jbronn@1
|
6 |
import re |
|
jbronn@1
|
7 |
from ctypes import addressof, byref, c_double, c_size_t |
|
jbronn@1
|
8 |
|
|
jbronn@1
|
9 |
# GEOS-related dependencies. |
|
jbronn@138
|
10 |
from django.contrib.gis.geos.base import GEOSBase, ListMixin, gdal |
|
jbronn@1
|
11 |
from django.contrib.gis.geos.coordseq import GEOSCoordSeq |
|
jbronn@1
|
12 |
from django.contrib.gis.geos.error import GEOSException |
|
jbronn@123
|
13 |
from django.contrib.gis.geos.libgeos import GEOM_PTR, GEOS_PREPARE |
|
jbronn@1
|
14 |
|
|
jbronn@1
|
15 |
# All other functions in this module come from the ctypes |
|
jbronn@1
|
16 |
# prototypes module -- which handles all interaction with |
|
jbronn@1
|
17 |
# the underlying GEOS library. |
|
jbronn@123
|
18 |
from django.contrib.gis.geos import prototypes as capi |
|
jbronn@1
|
19 |
from django.contrib.gis.geos import io |
|
jbronn@1
|
20 |
|
|
jbronn@1
|
21 |
# Regular expression for recognizing HEXEWKB and WKT. A prophylactic measure |
|
jbronn@1
|
22 |
# to prevent potentially malicious input from reaching the underlying C |
|
jbronn@1
|
23 |
# library. Not a substitute for good web security programming practices. |
|
jbronn@1
|
24 |
hex_regex = re.compile(r'^[0-9A-F]+$', re.I) |
|
jbronn@1
|
25 |
wkt_regex = re.compile(r'^(SRID=(?P<srid>\d+);)?(?P<wkt>(POINT|LINESTRING|LINEARRING|POLYGON|MULTIPOINT|MULTILINESTRING|MULTIPOLYGON|GEOMETRYCOLLECTION)[ACEGIMLONPSRUTY\d,\.\-\(\) ]+)$', re.I) |
|
jbronn@1
|
26 |
|
|
jbronn@138
|
27 |
class GEOSGeometry(GEOSBase, ListMixin): |
|
jbronn@1
|
28 |
"A class that, generally, encapsulates a GEOS geometry." |
|
jbronn@1
|
29 |
|
|
jbronn@1
|
30 |
ptr_type = GEOM_PTR |
|
jbronn@1
|
31 |
|
|
jbronn@1
|
32 |
#### Python 'magic' routines #### |
|
jbronn@1
|
33 |
def __init__(self, geo_input, srid=None): |
|
jbronn@1
|
34 |
""" |
|
jbronn@1
|
35 |
The base constructor for GEOS geometry objects, and may take the |
|
jbronn@1
|
36 |
following inputs: |
|
jbronn@1
|
37 |
|
|
jbronn@125
|
38 |
* strings: |
|
jbronn@125
|
39 |
- WKT |
|
jbronn@125
|
40 |
- HEXEWKB (a PostGIS-specific canonical form) |
|
jbronn@125
|
41 |
- GeoJSON (requires GDAL) |
|
jbronn@125
|
42 |
* buffer: |
|
jbronn@125
|
43 |
- WKB |
|
jbronn@1
|
44 |
|
|
jbronn@1
|
45 |
The `srid` keyword is used to specify the Source Reference Identifier |
|
jbronn@1
|
46 |
(SRID) number for this Geometry. If not set, the SRID will be None. |
|
jbronn@1
|
47 |
""" |
|
jbronn@1
|
48 |
if isinstance(geo_input, basestring): |
|
jbronn@1
|
49 |
if isinstance(geo_input, unicode): |
|
jbronn@1
|
50 |
# Encoding to ASCII, WKT or HEXEWKB doesn't need any more. |
|
jbronn@1
|
51 |
geo_input = geo_input.encode('ascii') |
|
jbronn@1
|
52 |
|
|
jbronn@1
|
53 |
wkt_m = wkt_regex.match(geo_input) |
|
jbronn@1
|
54 |
if wkt_m: |
|
jbronn@1
|
55 |
# Handling WKT input. |
|
jbronn@1
|
56 |
if wkt_m.group('srid'): srid = int(wkt_m.group('srid')) |
|
jbronn@1
|
57 |
g = io.wkt_r.read(wkt_m.group('wkt')) |
|
jbronn@1
|
58 |
elif hex_regex.match(geo_input): |
|
jbronn@1
|
59 |
# Handling HEXEWKB input. |
|
jbronn@1
|
60 |
g = io.wkb_r.read(geo_input) |
|
jbronn@1
|
61 |
elif gdal.GEOJSON and gdal.geometries.json_regex.match(geo_input): |
|
jbronn@1
|
62 |
# Handling GeoJSON input. |
|
jbronn@1
|
63 |
g = io.wkb_r.read(gdal.OGRGeometry(geo_input).wkb) |
|
jbronn@1
|
64 |
else: |
|
jbronn@1
|
65 |
raise ValueError('String or unicode input unrecognized as WKT EWKT, and HEXEWKB.') |
|
jbronn@1
|
66 |
elif isinstance(geo_input, GEOM_PTR): |
|
jbronn@1
|
67 |
# When the input is a pointer to a geomtry (GEOM_PTR). |
|
jbronn@1
|
68 |
g = geo_input |
|
jbronn@1
|
69 |
elif isinstance(geo_input, buffer): |
|
jbronn@1
|
70 |
# When the input is a buffer (WKB). |
|
jbronn@1
|
71 |
g = io.wkb_r.read(geo_input) |
|
jbronn@123
|
72 |
elif isinstance(geo_input, GEOSGeometry): |
|
jbronn@123
|
73 |
g = capi.geom_clone(geo_input.ptr) |
|
jbronn@1
|
74 |
else: |
|
jbronn@1
|
75 |
# Invalid geometry type. |
|
jbronn@1
|
76 |
raise TypeError('Improper geometry input type: %s' % str(type(geo_input))) |
|
jbronn@1
|
77 |
|
|
jbronn@1
|
78 |
if bool(g): |
|
jbronn@1
|
79 |
# Setting the pointer object with a valid pointer. |
|
jbronn@123
|
80 |
self.ptr = g |
|
jbronn@1
|
81 |
else: |
|
jbronn@1
|
82 |
raise GEOSException('Could not initialize GEOS Geometry with given input.') |
|
jbronn@1
|
83 |
|
|
jbronn@1
|
84 |
# Post-initialization setup. |
|
jbronn@1
|
85 |
self._post_init(srid) |
|
jbronn@1
|
86 |
|
|
jbronn@1
|
87 |
def _post_init(self, srid): |
|
jbronn@1
|
88 |
"Helper routine for performing post-initialization setup." |
|
jbronn@1
|
89 |
# Setting the SRID, if given. |
|
jbronn@1
|
90 |
if srid and isinstance(srid, int): self.srid = srid |
|
jbronn@1
|
91 |
|
|
jbronn@1
|
92 |
# Setting the class type (e.g., Point, Polygon, etc.) |
|
jbronn@1
|
93 |
self.__class__ = GEOS_CLASSES[self.geom_typeid] |
|
jbronn@1
|
94 |
|
|
jbronn@1
|
95 |
# Setting the coordinate sequence for the geometry (will be None on |
|
jbronn@1
|
96 |
# geometries that do not have coordinate sequences) |
|
jbronn@1
|
97 |
self._set_cs() |
|
jbronn@1
|
98 |
|
|
jbronn@1
|
99 |
def __del__(self): |
|
jbronn@1
|
100 |
""" |
|
jbronn@1
|
101 |
Destroys this Geometry; in other words, frees the memory used by the |
|
jbronn@1
|
102 |
GEOS C++ object. |
|
jbronn@1
|
103 |
""" |
|
jbronn@123
|
104 |
if self._ptr: capi.destroy_geom(self._ptr) |
|
jbronn@1
|
105 |
|
|
jbronn@1
|
106 |
def __copy__(self): |
|
jbronn@1
|
107 |
""" |
|
jbronn@1
|
108 |
Returns a clone because the copy of a GEOSGeometry may contain an |
|
jbronn@1
|
109 |
invalid pointer location if the original is garbage collected. |
|
jbronn@1
|
110 |
""" |
|
jbronn@1
|
111 |
return self.clone() |
|
jbronn@1
|
112 |
|
|
jbronn@1
|
113 |
def __deepcopy__(self, memodict): |
|
jbronn@1
|
114 |
""" |
|
jbronn@1
|
115 |
The `deepcopy` routine is used by the `Node` class of django.utils.tree; |
|
jbronn@1
|
116 |
thus, the protocol routine needs to be implemented to return correct |
|
jbronn@1
|
117 |
copies (clones) of these GEOS objects, which use C pointers. |
|
jbronn@1
|
118 |
""" |
|
jbronn@1
|
119 |
return self.clone() |
|
jbronn@1
|
120 |
|
|
jbronn@1
|
121 |
def __str__(self): |
|
jbronn@1
|
122 |
"WKT is used for the string representation." |
|
jbronn@1
|
123 |
return self.wkt |
|
jbronn@1
|
124 |
|
|
jbronn@1
|
125 |
def __repr__(self): |
|
jbronn@1
|
126 |
"Short-hand representation because WKT may be very large." |
|
jbronn@1
|
127 |
return '<%s object at %s>' % (self.geom_type, hex(addressof(self.ptr))) |
|
jbronn@1
|
128 |
|
|
jbronn@1
|
129 |
# Pickling support |
|
jbronn@1
|
130 |
def __getstate__(self): |
|
jbronn@1
|
131 |
# The pickled state is simply a tuple of the WKB (in string form) |
|
jbronn@1
|
132 |
# and the SRID. |
|
jbronn@1
|
133 |
return str(self.wkb), self.srid |
|
jbronn@1
|
134 |
|
|
jbronn@1
|
135 |
def __setstate__(self, state): |
|
jbronn@1
|
136 |
# Instantiating from the tuple state that was pickled. |
|
jbronn@1
|
137 |
wkb, srid = state |
|
jbronn@123
|
138 |
ptr = capi.from_wkb(wkb, len(wkb)) |
|
jbronn@1
|
139 |
if not ptr: raise GEOSException('Invalid Geometry loaded from pickled state.') |
|
jbronn@123
|
140 |
self.ptr = ptr |
|
jbronn@1
|
141 |
self._post_init(srid) |
|
jbronn@1
|
142 |
|
|
jbronn@1
|
143 |
# Comparison operators |
|
jbronn@1
|
144 |
def __eq__(self, other): |
|
jbronn@1
|
145 |
""" |
|
jbronn@1
|
146 |
Equivalence testing, a Geometry may be compared with another Geometry |
|
jbronn@1
|
147 |
or a WKT representation. |
|
jbronn@1
|
148 |
""" |
|
jbronn@1
|
149 |
if isinstance(other, basestring): |
|
jbronn@1
|
150 |
return self.wkt == other |
|
jbronn@1
|
151 |
elif isinstance(other, GEOSGeometry): |
|
jbronn@1
|
152 |
return self.equals_exact(other) |
|
jbronn@1
|
153 |
else: |
|
jbronn@1
|
154 |
return False |
|
jbronn@1
|
155 |
|
|
jbronn@1
|
156 |
def __ne__(self, other): |
|
jbronn@1
|
157 |
"The not equals operator." |
|
jbronn@1
|
158 |
return not (self == other) |
|
jbronn@1
|
159 |
|
|
jbronn@1
|
160 |
### Geometry set-like operations ### |
|
jbronn@1
|
161 |
# Thanks to Sean Gillies for inspiration: |
|
jbronn@1
|
162 |
# http://lists.gispython.org/pipermail/community/2007-July/001034.html |
|
jbronn@1
|
163 |
# g = g1 | g2 |
|
jbronn@1
|
164 |
def __or__(self, other): |
|
jbronn@1
|
165 |
"Returns the union of this Geometry and the other." |
|
jbronn@1
|
166 |
return self.union(other) |
|
jbronn@1
|
167 |
|
|
jbronn@1
|
168 |
# g = g1 & g2 |
|
jbronn@1
|
169 |
def __and__(self, other): |
|
jbronn@1
|
170 |
"Returns the intersection of this Geometry and the other." |
|
jbronn@1
|
171 |
return self.intersection(other) |
|
jbronn@1
|
172 |
|
|
jbronn@1
|
173 |
# g = g1 - g2 |
|
jbronn@1
|
174 |
def __sub__(self, other): |
|
jbronn@1
|
175 |
"Return the difference this Geometry and the other." |
|
jbronn@1
|
176 |
return self.difference(other) |
|
jbronn@1
|
177 |
|
|
jbronn@1
|
178 |
# g = g1 ^ g2 |
|
jbronn@1
|
179 |
def __xor__(self, other): |
|
jbronn@1
|
180 |
"Return the symmetric difference of this Geometry and the other." |
|
jbronn@1
|
181 |
return self.sym_difference(other) |
|
jbronn@1
|
182 |
|
|
jbronn@1
|
183 |
#### Coordinate Sequence Routines #### |
|
jbronn@1
|
184 |
@property |
|
jbronn@1
|
185 |
def has_cs(self): |
|
jbronn@1
|
186 |
"Returns True if this Geometry has a coordinate sequence, False if not." |
|
jbronn@1
|
187 |
# Only these geometries are allowed to have coordinate sequences. |
|
jbronn@1
|
188 |
if isinstance(self, (Point, LineString, LinearRing)): |
|
jbronn@1
|
189 |
return True |
|
jbronn@1
|
190 |
else: |
|
jbronn@1
|
191 |
return False |
|
jbronn@1
|
192 |
|
|
jbronn@1
|
193 |
def _set_cs(self): |
|
jbronn@1
|
194 |
"Sets the coordinate sequence for this Geometry." |
|
jbronn@1
|
195 |
if self.has_cs: |
|
jbronn@123
|
196 |
self._cs = GEOSCoordSeq(capi.get_cs(self.ptr), self.hasz) |
|
jbronn@1
|
197 |
else: |
|
jbronn@1
|
198 |
self._cs = None |
|
jbronn@1
|
199 |
|
|
jbronn@1
|
200 |
@property |
|
jbronn@1
|
201 |
def coord_seq(self): |
|
jbronn@1
|
202 |
"Returns a clone of the coordinate sequence for this Geometry." |
|
jbronn@1
|
203 |
if self.has_cs: |
|
jbronn@1
|
204 |
return self._cs.clone() |
|
jbronn@1
|
205 |
|
|
jbronn@1
|
206 |
#### Geometry Info #### |
|
jbronn@1
|
207 |
@property |
|
jbronn@1
|
208 |
def geom_type(self): |
|
jbronn@1
|
209 |
"Returns a string representing the Geometry type, e.g. 'Polygon'" |
|
jbronn@123
|
210 |
return capi.geos_type(self.ptr) |
|
jbronn@1
|
211 |
|
|
jbronn@1
|
212 |
@property |
|
jbronn@1
|
213 |
def geom_typeid(self): |
|
jbronn@1
|
214 |
"Returns an integer representing the Geometry type." |
|
jbronn@123
|
215 |
return capi.geos_typeid(self.ptr) |
|
jbronn@1
|
216 |
|
|
jbronn@1
|
217 |
@property |
|
jbronn@1
|
218 |
def num_geom(self): |
|
jbronn@1
|
219 |
"Returns the number of geometries in the Geometry." |
|
jbronn@123
|
220 |
return capi.get_num_geoms(self.ptr) |
|
jbronn@1
|
221 |
|
|
jbronn@1
|
222 |
@property |
|
jbronn@1
|
223 |
def num_coords(self): |
|
jbronn@1
|
224 |
"Returns the number of coordinates in the Geometry." |
|
jbronn@123
|
225 |
return capi.get_num_coords(self.ptr) |
|
jbronn@1
|
226 |
|
|
jbronn@1
|
227 |
@property |
|
jbronn@1
|
228 |
def num_points(self): |
|
jbronn@1
|
229 |
"Returns the number points, or coordinates, in the Geometry." |
|
jbronn@1
|
230 |
return self.num_coords |
|
jbronn@1
|
231 |
|
|
jbronn@1
|
232 |
@property |
|
jbronn@1
|
233 |
def dims(self): |
|
jbronn@1
|
234 |
"Returns the dimension of this Geometry (0=point, 1=line, 2=surface)." |
|
jbronn@123
|
235 |
return capi.get_dims(self.ptr) |
|
jbronn@1
|
236 |
|
|
jbronn@1
|
237 |
def normalize(self): |
|
jbronn@1
|
238 |
"Converts this Geometry to normal form (or canonical form)." |
|
jbronn@123
|
239 |
return capi.geos_normalize(self.ptr) |
|
jbronn@1
|
240 |
|
|
jbronn@1
|
241 |
#### Unary predicates #### |
|
jbronn@1
|
242 |
@property |
|
jbronn@1
|
243 |
def empty(self): |
|
jbronn@1
|
244 |
""" |
|
jbronn@1
|
245 |
Returns a boolean indicating whether the set of points in this Geometry |
|
jbronn@1
|
246 |
are empty. |
|
jbronn@1
|
247 |
""" |
|
jbronn@123
|
248 |
return capi.geos_isempty(self.ptr) |
|
jbronn@1
|
249 |
|
|
jbronn@1
|
250 |
@property |
|
jbronn@1
|
251 |
def hasz(self): |
|
jbronn@1
|
252 |
"Returns whether the geometry has a 3D dimension." |
|
jbronn@123
|
253 |
return capi.geos_hasz(self.ptr) |
|
jbronn@1
|
254 |
|
|
jbronn@1
|
255 |
@property |
|
jbronn@1
|
256 |
def ring(self): |
|
jbronn@1
|
257 |
"Returns whether or not the geometry is a ring." |
|
jbronn@123
|
258 |
return capi.geos_isring(self.ptr) |
|
jbronn@1
|
259 |
|
|
jbronn@1
|
260 |
@property |
|
jbronn@1
|
261 |
def simple(self): |
|
jbronn@1
|
262 |
"Returns false if the Geometry not simple." |
|
jbronn@123
|
263 |
return capi.geos_issimple(self.ptr) |
|
jbronn@1
|
264 |
|
|
jbronn@1
|
265 |
@property |
|
jbronn@1
|
266 |
def valid(self): |
|
jbronn@1
|
267 |
"This property tests the validity of this Geometry." |
|
jbronn@123
|
268 |
return capi.geos_isvalid(self.ptr) |
|
jbronn@1
|
269 |
|
|
jbronn@1
|
270 |
#### Binary predicates. #### |
|
jbronn@1
|
271 |
def contains(self, other): |
|
jbronn@1
|
272 |
"Returns true if other.within(this) returns true." |
|
jbronn@123
|
273 |
return capi.geos_contains(self.ptr, other.ptr) |
|
jbronn@1
|
274 |
|
|
jbronn@1
|
275 |
def crosses(self, other): |
|
jbronn@1
|
276 |
""" |
|
jbronn@1
|
277 |
Returns true if the DE-9IM intersection matrix for the two Geometries |
|
jbronn@1
|
278 |
is T*T****** (for a point and a curve,a point and an area or a line and |
|
jbronn@1
|
279 |
an area) 0******** (for two curves). |
|
jbronn@1
|
280 |
""" |
|
jbronn@123
|
281 |
return capi.geos_crosses(self.ptr, other.ptr) |
|
jbronn@1
|
282 |
|
|
jbronn@1
|
283 |
def disjoint(self, other): |
|
jbronn@1
|
284 |
""" |
|
jbronn@1
|
285 |
Returns true if the DE-9IM intersection matrix for the two Geometries |
|
jbronn@1
|
286 |
is FF*FF****. |
|
jbronn@1
|
287 |
""" |
|
jbronn@123
|
288 |
return capi.geos_disjoint(self.ptr, other.ptr) |
|
jbronn@1
|
289 |
|
|
jbronn@1
|
290 |
def equals(self, other): |
|
jbronn@1
|
291 |
""" |
|
jbronn@1
|
292 |
Returns true if the DE-9IM intersection matrix for the two Geometries |
|
jbronn@1
|
293 |
is T*F**FFF*. |
|
jbronn@1
|
294 |
""" |
|
jbronn@123
|
295 |
return capi.geos_equals(self.ptr, other.ptr) |
|
jbronn@1
|
296 |
|
|
jbronn@1
|
297 |
def equals_exact(self, other, tolerance=0): |
|
jbronn@1
|
298 |
""" |
|
jbronn@1
|
299 |
Returns true if the two Geometries are exactly equal, up to a |
|
jbronn@1
|
300 |
specified tolerance. |
|
jbronn@1
|
301 |
""" |
|
jbronn@123
|
302 |
return capi.geos_equalsexact(self.ptr, other.ptr, float(tolerance)) |
|
jbronn@1
|
303 |
|
|
jbronn@1
|
304 |
def intersects(self, other): |
|
jbronn@1
|
305 |
"Returns true if disjoint returns false." |
|
jbronn@123
|
306 |
return capi.geos_intersects(self.ptr, other.ptr) |
|
jbronn@1
|
307 |
|
|
jbronn@1
|
308 |
def overlaps(self, other): |
|
jbronn@1
|
309 |
""" |
|
jbronn@1
|
310 |
Returns true if the DE-9IM intersection matrix for the two Geometries |
|
jbronn@1
|
311 |
is T*T***T** (for two points or two surfaces) 1*T***T** (for two curves). |
|
jbronn@1
|
312 |
""" |
|
jbronn@123
|
313 |
return capi.geos_overlaps(self.ptr, other.ptr) |
|
jbronn@1
|
314 |
|
|
jbronn@1
|
315 |
def relate_pattern(self, other, pattern): |
|
jbronn@1
|
316 |
""" |
|
jbronn@1
|
317 |
Returns true if the elements in the DE-9IM intersection matrix for the |
|
jbronn@1
|
318 |
two Geometries match the elements in pattern. |
|
jbronn@1
|
319 |
""" |
|
jbronn@123
|
320 |
if not isinstance(pattern, basestring) or len(pattern) > 9: |
|
jbronn@1
|
321 |
raise GEOSException('invalid intersection matrix pattern') |
|
jbronn@123
|
322 |
return capi.geos_relatepattern(self.ptr, other.ptr, pattern) |
|
jbronn@1
|
323 |
|
|
jbronn@1
|
324 |
def touches(self, other): |
|
jbronn@1
|
325 |
""" |
|
jbronn@1
|
326 |
Returns true if the DE-9IM intersection matrix for the two Geometries |
|
jbronn@1
|
327 |
is FT*******, F**T***** or F***T****. |
|
jbronn@1
|
328 |
""" |
|
jbronn@123
|
329 |
return capi.geos_touches(self.ptr, other.ptr) |
|
jbronn@1
|
330 |
|
|
jbronn@1
|
331 |
def within(self, other): |
|
jbronn@1
|
332 |
""" |
|
jbronn@1
|
333 |
Returns true if the DE-9IM intersection matrix for the two Geometries |
|
jbronn@1
|
334 |
is T*F**F***. |
|
jbronn@1
|
335 |
""" |
|
jbronn@123
|
336 |
return capi.geos_within(self.ptr, other.ptr) |
|
jbronn@1
|
337 |
|
|
jbronn@1
|
338 |
#### SRID Routines #### |
|
jbronn@1
|
339 |
def get_srid(self): |
|
jbronn@1
|
340 |
"Gets the SRID for the geometry, returns None if no SRID is set." |
|
jbronn@123
|
341 |
s = capi.geos_get_srid(self.ptr) |
|
jbronn@1
|
342 |
if s == 0: return None |
|
jbronn@1
|
343 |
else: return s |
|
jbronn@1
|
344 |
|
|
jbronn@1
|
345 |
def set_srid(self, srid): |
|
jbronn@1
|
346 |
"Sets the SRID for the geometry." |
|
jbronn@123
|
347 |
capi.geos_set_srid(self.ptr, srid) |
|
jbronn@1
|
348 |
srid = property(get_srid, set_srid) |
|
jbronn@1
|
349 |
|
|
jbronn@1
|
350 |
#### Output Routines #### |
|
jbronn@1
|
351 |
@property |
|
jbronn@1
|
352 |
def ewkt(self): |
|
jbronn@1
|
353 |
"Returns the EWKT (WKT + SRID) of the Geometry." |
|
jbronn@1
|
354 |
if self.get_srid(): return 'SRID=%s;%s' % (self.srid, self.wkt) |
|
jbronn@1
|
355 |
else: return self.wkt |
|
jbronn@1
|
356 |
|
|
jbronn@1
|
357 |
@property |
|
jbronn@1
|
358 |
def wkt(self): |
|
jbronn@1
|
359 |
"Returns the WKT (Well-Known Text) of the Geometry." |
|
jbronn@1
|
360 |
return io.wkt_w.write(self.ptr) |
|
jbronn@1
|
361 |
|
|
jbronn@1
|
362 |
@property |
|
jbronn@1
|
363 |
def hex(self): |
|
jbronn@1
|
364 |
""" |
|
jbronn@1
|
365 |
Returns the HEX of the Geometry -- please note that the SRID is not |
|
jbronn@1
|
366 |
included in this representation, because the GEOS C library uses |
|
jbronn@1
|
367 |
-1 by default, even if the SRID is set. |
|
jbronn@1
|
368 |
""" |
|
jbronn@1
|
369 |
# A possible faster, all-python, implementation: |
|
jbronn@1
|
370 |
# str(self.wkb).encode('hex') |
|
jbronn@1
|
371 |
return io.wkb_w.write_hex(self.ptr) |
|
jbronn@1
|
372 |
|
|
jbronn@1
|
373 |
@property |
|
jbronn@1
|
374 |
def json(self): |
|
jbronn@1
|
375 |
""" |
|
jbronn@1
|
376 |
Returns GeoJSON representation of this Geometry if GDAL 1.5+ |
|
jbronn@1
|
377 |
is installed. |
|
jbronn@1
|
378 |
""" |
|
jbronn@1
|
379 |
if gdal.GEOJSON: return self.ogr.json |
|
jbronn@1
|
380 |
geojson = json |
|
jbronn@1
|
381 |
|
|
jbronn@1
|
382 |
@property |
|
jbronn@1
|
383 |
def wkb(self): |
|
jbronn@1
|
384 |
"Returns the WKB of the Geometry as a buffer." |
|
jbronn@1
|
385 |
return io.wkb_w.write(self.ptr) |
|
jbronn@1
|
386 |
|
|
jbronn@1
|
387 |
@property |
|
jbronn@1
|
388 |
def kml(self): |
|
jbronn@1
|
389 |
"Returns the KML representation of this Geometry." |
|
jbronn@1
|
390 |
gtype = self.geom_type |
|
jbronn@1
|
391 |
return '<%s>%s</%s>' % (gtype, self.coord_seq.kml, gtype) |
|
jbronn@1
|
392 |
|
|
jbronn@125
|
393 |
@property |
|
jbronn@125
|
394 |
def prepared(self): |
|
jbronn@125
|
395 |
""" |
|
jbronn@125
|
396 |
Returns a PreparedGeometry corresponding to this geometry -- it is |
|
jbronn@125
|
397 |
optimized for the contains, intersects, and covers operations. |
|
jbronn@125
|
398 |
""" |
|
jbronn@125
|
399 |
if GEOS_PREPARE: |
|
jbronn@125
|
400 |
return PreparedGeometry(self) |
|
jbronn@125
|
401 |
else: |
|
jbronn@125
|
402 |
raise GEOSException('GEOS 3.1+ required for prepared geometry support.') |
|
jbronn@125
|
403 |
|
|
jbronn@1
|
404 |
#### GDAL-specific output routines #### |
|
jbronn@1
|
405 |
@property |
|
jbronn@1
|
406 |
def ogr(self): |
|
jbronn@1
|
407 |
"Returns the OGR Geometry for this Geometry." |
|
jbronn@1
|
408 |
if gdal.HAS_GDAL: |
|
jbronn@1
|
409 |
if self.srid: |
|
jbronn@1
|
410 |
return gdal.OGRGeometry(self.wkb, self.srid) |
|
jbronn@1
|
411 |
else: |
|
jbronn@1
|
412 |
return gdal.OGRGeometry(self.wkb) |
|
jbronn@1
|
413 |
else: |
|
jbronn@1
|
414 |
return None |
|
jbronn@1
|
415 |
|
|
jbronn@1
|
416 |
@property |
|
jbronn@1
|
417 |
def srs(self): |
|
jbronn@1
|
418 |
"Returns the OSR SpatialReference for SRID of this Geometry." |
|
jbronn@1
|
419 |
if gdal.HAS_GDAL and self.srid: |
|
jbronn@1
|
420 |
return gdal.SpatialReference(self.srid) |
|
jbronn@1
|
421 |
else: |
|
jbronn@1
|
422 |
return None |
|
jbronn@1
|
423 |
|
|
jbronn@1
|
424 |
@property |
|
jbronn@1
|
425 |
def crs(self): |
|
jbronn@1
|
426 |
"Alias for `srs` property." |
|
jbronn@1
|
427 |
return self.srs |
|
jbronn@1
|
428 |
|
|
jbronn@1
|
429 |
def transform(self, ct, clone=False): |
|
jbronn@1
|
430 |
""" |
|
jbronn@1
|
431 |
Requires GDAL. Transforms the geometry according to the given |
|
jbronn@1
|
432 |
transformation object, which may be an integer SRID, and WKT or |
|
jbronn@1
|
433 |
PROJ.4 string. By default, the geometry is transformed in-place and |
|
jbronn@1
|
434 |
nothing is returned. However if the `clone` keyword is set, then this |
|
jbronn@1
|
435 |
geometry will not be modified and a transformed clone will be returned |
|
jbronn@1
|
436 |
instead. |
|
jbronn@1
|
437 |
""" |
|
jbronn@1
|
438 |
srid = self.srid |
|
jbronn@1
|
439 |
if gdal.HAS_GDAL and srid: |
|
jbronn@123
|
440 |
# Creating an OGR Geometry, which is then transformed. |
|
jbronn@1
|
441 |
g = gdal.OGRGeometry(self.wkb, srid) |
|
jbronn@1
|
442 |
g.transform(ct) |
|
jbronn@123
|
443 |
# Getting a new GEOS pointer |
|
jbronn@1
|
444 |
ptr = io.wkb_r.read(g.wkb) |
|
jbronn@1
|
445 |
if clone: |
|
jbronn@1
|
446 |
# User wants a cloned transformed geometry returned. |
|
jbronn@1
|
447 |
return GEOSGeometry(ptr, srid=g.srid) |
|
jbronn@1
|
448 |
if ptr: |
|
jbronn@1
|
449 |
# Reassigning pointer, and performing post-initialization setup |
|
jbronn@1
|
450 |
# again due to the reassignment. |
|
jbronn@123
|
451 |
capi.destroy_geom(self.ptr) |
|
jbronn@123
|
452 |
self.ptr = ptr |
|
jbronn@1
|
453 |
self._post_init(g.srid) |
|
jbronn@1
|
454 |
else: |
|
jbronn@1
|
455 |
raise GEOSException('Transformed WKB was invalid.') |
|
jbronn@1
|
456 |
|
|
jbronn@1
|
457 |
#### Topology Routines #### |
|
jbronn@1
|
458 |
def _topology(self, gptr): |
|
jbronn@1
|
459 |
"Helper routine to return Geometry from the given pointer." |
|
jbronn@1
|
460 |
return GEOSGeometry(gptr, srid=self.srid) |
|
jbronn@1
|
461 |
|
|
jbronn@1
|
462 |
@property |
|
jbronn@1
|
463 |
def boundary(self): |
|
jbronn@1
|
464 |
"Returns the boundary as a newly allocated Geometry object." |
|
jbronn@123
|
465 |
return self._topology(capi.geos_boundary(self.ptr)) |
|
jbronn@1
|
466 |
|
|
jbronn@1
|
467 |
def buffer(self, width, quadsegs=8): |
|
jbronn@1
|
468 |
""" |
|
jbronn@1
|
469 |
Returns a geometry that represents all points whose distance from this |
|
jbronn@1
|
470 |
Geometry is less than or equal to distance. Calculations are in the |
|
jbronn@1
|
471 |
Spatial Reference System of this Geometry. The optional third parameter sets |
|
jbronn@1
|
472 |
the number of segment used to approximate a quarter circle (defaults to 8). |
|
jbronn@1
|
473 |
(Text from PostGIS documentation at ch. 6.1.3) |
|
jbronn@1
|
474 |
""" |
|
jbronn@123
|
475 |
return self._topology(capi.geos_buffer(self.ptr, width, quadsegs)) |
|
jbronn@1
|
476 |
|
|
jbronn@1
|
477 |
@property |
|
jbronn@1
|
478 |
def centroid(self): |
|
jbronn@1
|
479 |
""" |
|
jbronn@1
|
480 |
The centroid is equal to the centroid of the set of component Geometries |
|
jbronn@1
|
481 |
of highest dimension (since the lower-dimension geometries contribute zero |
|
jbronn@1
|
482 |
"weight" to the centroid). |
|
jbronn@1
|
483 |
""" |
|
jbronn@123
|
484 |
return self._topology(capi.geos_centroid(self.ptr)) |
|
jbronn@1
|
485 |
|
|
jbronn@1
|
486 |
@property |
|
jbronn@1
|
487 |
def convex_hull(self): |
|
jbronn@1
|
488 |
""" |
|
jbronn@1
|
489 |
Returns the smallest convex Polygon that contains all the points |
|
jbronn@1
|
490 |
in the Geometry. |
|
jbronn@1
|
491 |
""" |
|
jbronn@123
|
492 |
return self._topology(capi.geos_convexhull(self.ptr)) |
|
jbronn@1
|
493 |
|
|
jbronn@1
|
494 |
def difference(self, other): |
|
jbronn@1
|
495 |
""" |
|
jbronn@1
|
496 |
Returns a Geometry representing the points making up this Geometry |
|
jbronn@1
|
497 |
that do not make up other. |
|
jbronn@1
|
498 |
""" |
|
jbronn@123
|
499 |
return self._topology(capi.geos_difference(self.ptr, other.ptr)) |
|
jbronn@1
|
500 |
|
|
jbronn@1
|
501 |
@property |
|
jbronn@1
|
502 |
def envelope(self): |
|
jbronn@1
|
503 |
"Return the envelope for this geometry (a polygon)." |
|
jbronn@123
|
504 |
return self._topology(capi.geos_envelope(self.ptr)) |
|
jbronn@1
|
505 |
|
|
jbronn@1
|
506 |
def intersection(self, other): |
|
jbronn@1
|
507 |
"Returns a Geometry representing the points shared by this Geometry and other." |
|
jbronn@123
|
508 |
return self._topology(capi.geos_intersection(self.ptr, other.ptr)) |
|
jbronn@1
|
509 |
|
|
jbronn@1
|
510 |
@property |
|
jbronn@1
|
511 |
def point_on_surface(self): |
|
jbronn@1
|
512 |
"Computes an interior point of this Geometry." |
|
jbronn@123
|
513 |
return self._topology(capi.geos_pointonsurface(self.ptr)) |
|
jbronn@1
|
514 |
|
|
jbronn@1
|
515 |
def relate(self, other): |
|
jbronn@1
|
516 |
"Returns the DE-9IM intersection matrix for this Geometry and the other." |
|
jbronn@123
|
517 |
return capi.geos_relate(self.ptr, other.ptr) |
|
jbronn@1
|
518 |
|
|
jbronn@1
|
519 |
def simplify(self, tolerance=0.0, preserve_topology=False): |
|
jbronn@1
|
520 |
""" |
|
jbronn@1
|
521 |
Returns the Geometry, simplified using the Douglas-Peucker algorithm |
|
jbronn@1
|
522 |
to the specified tolerance (higher tolerance => less points). If no |
|
jbronn@1
|
523 |
tolerance provided, defaults to 0. |
|
jbronn@1
|
524 |
|
|
jbronn@1
|
525 |
By default, this function does not preserve topology - e.g. polygons can |
|
jbronn@1
|
526 |
be split, collapse to lines or disappear holes can be created or |
|
jbronn@1
|
527 |
disappear, and lines can cross. By specifying preserve_topology=True, |
|
jbronn@1
|
528 |
the result will have the same dimension and number of components as the |
|
jbronn@1
|
529 |
input. This is significantly slower. |
|
jbronn@1
|
530 |
""" |
|
jbronn@1
|
531 |
if preserve_topology: |
|
jbronn@123
|
532 |
return self._topology(capi.geos_preservesimplify(self.ptr, tolerance)) |
|
jbronn@1
|
533 |
else: |
|
jbronn@123
|
534 |
return self._topology(capi.geos_simplify(self.ptr, tolerance)) |
|
jbronn@1
|
535 |
|
|
jbronn@1
|
536 |
def sym_difference(self, other): |
|
jbronn@1
|
537 |
""" |
|
jbronn@1
|
538 |
Returns a set combining the points in this Geometry not in other, |
|
jbronn@1
|
539 |
and the points in other not in this Geometry. |
|
jbronn@1
|
540 |
""" |
|
jbronn@123
|
541 |
return self._topology(capi.geos_symdifference(self.ptr, other.ptr)) |
|
jbronn@1
|
542 |
|
|
jbronn@1
|
543 |
def union(self, other): |
|
jbronn@1
|
544 |
"Returns a Geometry representing all the points in this Geometry and other." |
|
jbronn@123
|
545 |
return self._topology(capi.geos_union(self.ptr, other.ptr)) |
|
jbronn@1
|
546 |
|
|
jbronn@1
|
547 |
#### Other Routines #### |
|
jbronn@1
|
548 |
@property |
|
jbronn@1
|
549 |
def area(self): |
|
jbronn@1
|
550 |
"Returns the area of the Geometry." |
|
jbronn@123
|
551 |
return capi.geos_area(self.ptr, byref(c_double())) |
|
jbronn@1
|
552 |
|
|
jbronn@1
|
553 |
def distance(self, other): |
|
jbronn@1
|
554 |
""" |
|
jbronn@1
|
555 |
Returns the distance between the closest points on this Geometry |
|
jbronn@1
|
556 |
and the other. Units will be in those of the coordinate system of |
|
jbronn@1
|
557 |
the Geometry. |
|
jbronn@1
|
558 |
""" |
|
jbronn@1
|
559 |
if not isinstance(other, GEOSGeometry): |
|
jbronn@1
|
560 |
raise TypeError('distance() works only on other GEOS Geometries.') |
|
jbronn@123
|
561 |
return capi.geos_distance(self.ptr, other.ptr, byref(c_double())) |
|
jbronn@1
|
562 |
|
|
jbronn@1
|
563 |
@property |
|
jbronn@1
|
564 |
def extent(self): |
|
jbronn@1
|
565 |
""" |
|
jbronn@1
|
566 |
Returns the extent of this geometry as a 4-tuple, consisting of |
|
jbronn@1
|
567 |
(xmin, ymin, xmax, ymax). |
|
jbronn@1
|
568 |
""" |
|
jbronn@1
|
569 |
env = self.envelope |
|
jbronn@1
|
570 |
if isinstance(env, Point): |
|
jbronn@1
|
571 |
xmin, ymin = env.tuple |
|
jbronn@1
|
572 |
xmax, ymax = xmin, ymin |
|
jbronn@1
|
573 |
else: |
|
jbronn@1
|
574 |
xmin, ymin = env[0][0] |
|
jbronn@1
|
575 |
xmax, ymax = env[0][2] |
|
jbronn@1
|
576 |
return (xmin, ymin, xmax, ymax) |
|
jbronn@1
|
577 |
|
|
jbronn@1
|
578 |
@property |
|
jbronn@1
|
579 |
def length(self): |
|
jbronn@1
|
580 |
""" |
|
jbronn@1
|
581 |
Returns the length of this Geometry (e.g., 0 for point, or the |
|
jbronn@1
|
582 |
circumfrence of a Polygon). |
|
jbronn@1
|
583 |
""" |
|
jbronn@123
|
584 |
return capi.geos_length(self.ptr, byref(c_double())) |
|
jbronn@1
|
585 |
|
|
jbronn@1
|
586 |
def clone(self): |
|
jbronn@1
|
587 |
"Clones this Geometry." |
|
jbronn@123
|
588 |
return GEOSGeometry(capi.geom_clone(self.ptr), srid=self.srid) |
|
jbronn@1
|
589 |
|
|
jbronn@123
|
590 |
# Class mapping dictionary. Has to be at the end to avoid import |
|
jbronn@123
|
591 |
# conflicts with GEOSGeometry. |
|
jbronn@1
|
592 |
from django.contrib.gis.geos.linestring import LineString, LinearRing |
|
jbronn@1
|
593 |
from django.contrib.gis.geos.point import Point |
|
jbronn@1
|
594 |
from django.contrib.gis.geos.polygon import Polygon |
|
jbronn@1
|
595 |
from django.contrib.gis.geos.collections import GeometryCollection, MultiPoint, MultiLineString, MultiPolygon |
|
jbronn@1
|
596 |
GEOS_CLASSES = {0 : Point, |
|
jbronn@1
|
597 |
1 : LineString, |
|
jbronn@1
|
598 |
2 : LinearRing, |
|
jbronn@1
|
599 |
3 : Polygon, |
|
jbronn@1
|
600 |
4 : MultiPoint, |
|
jbronn@1
|
601 |
5 : MultiLineString, |
|
jbronn@1
|
602 |
6 : MultiPolygon, |
|
jbronn@1
|
603 |
7 : GeometryCollection, |
|
jbronn@1
|
604 |
} |
|
jbronn@125
|
605 |
|
|
jbronn@125
|
606 |
# If supported, import the PreparedGeometry class. |
|
jbronn@125
|
607 |
if GEOS_PREPARE: |
|
jbronn@125
|
608 |
from django.contrib.gis.geos.prepared import PreparedGeometry |