-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeometry.py
1304 lines (1102 loc) · 38.8 KB
/
geometry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numbers
from copy import copy, deepcopy
from .exceptions import (
DependencyError,
WkbError,
SridError,
DimensionalityError,
CoordinateError,
GeojsonError,
CollectionError,
WktError
)
from .hex import HexReader, HexWriter, HexBytes
from .wkt import WktReader, WktWriter
class Geometry:
r"""A representation of a PostGIS geometry.
PostGIS geometries are either an OpenGIS Consortium Simple Features for SQL
specification type or a PostGIS extended type. The object's canonical form
is stored in WKB or EWKB format along with an SRID and flags indicating
whether the coordinates are 3DZ, 3DM or 4D.
``Geometry`` objects can be created in a number of ways. In all cases, a
subclass for the particular geometry type will be instantiated.
From an (E)WKB::
>>> Geometry(b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
<Point: 'geometry(Point)'>
From the hexadecimal string representation of an (E)WKB::
>>> Geometry("0101000080000000000000000000000000000000000000000000000000")
<Point: 'geometry(PointZ)'>
The response above indicates an instance of the ``Point`` class has been
created and that it represents a PostGIS ``geometry(PointZ)`` type.
From a GeoJSON::
>>> Geometry.from_geojson({'type': 'Point', 'coordinates': (0.0, 0.0)})
<Point: 'geometry(Point,4326)'>
From a Shapely object::
>>> from shapely import Point
>>> point = Point(0, 0)
>>> Geometry.from_shapely(point, 3857)
<Point: 'geometry(Point,3857)'>
From any object supporting ``__geo_interface__``::
>>> from shapefile import Reader
>>> feature = Reader("test/multipoint.shp").shape(0)
>>> Geometry.shape(feature)
<MultiPoint: 'geometry(MultiPoint)'>
A ``Geometry`` can be read as long as it is one of the following
types: ``Point``, ``LineString``, ``Polygon``, ``MultiPoint``, ``MultiLineString``,
``MultiPolygon`` or ``GeometryCollection``. The M dimension will be preserved.
"""
_WKBTYPE = 0x1FFFFFFF
_WKBZFLAG = 0x80000000
_WKBMFLAG = 0x40000000
_WKBSRIDFLAG = 0x20000000
__slots__ = ["_wkb", "_reader", "_srid", "_dimz", "_dimm"]
def __new__(cls, wkb, srid=None, dimz=False, dimm=False):
if cls == Geometry:
if not wkb:
raise WkbError("No EWKB provided")
wkb = HexBytes(wkb)
newcls, dimz, dimm, srid, reader = Geometry._from_wkb(wkb)
geom = super().__new__(newcls)
geom._wkb = wkb
geom._reader = reader
geom._srid = srid
geom._dimz = dimz
geom._dimm = dimm
else:
geom = super().__new__(cls)
geom._wkb = None
geom._reader = None
return geom
def __copy__(self):
cls = self.__class__
return cls(self.coordinates, self.srid, self.dimz, self.dimm)
def __deepcopy__(self, _):
return self.__copy__()
def __add__(self, other):
if self.srid != other.srid:
raise CollectionError(
"Can not add mixed SRID types")
if issubclass(type(other), _MultiGeometry):
return other + self
if type(self) is type(other):
if type(self) is Point:
cls = MultiPoint
elif type(self) is LineString:
cls = MultiLineString
elif type(self) is Polygon:
cls = MultiPolygon
else:
cls = GeometryCollection
return cls([self, other], srid=self.srid)
@staticmethod
def from_geojson(geojson, srid=4326):
"""
Create a Geometry from a GeoJSON. The SRID can be overridden from the
expected 4326.
"""
type_ = geojson["type"].lower()
if type_ == "geometrycollection":
geometries = []
for geometry in geojson["geometries"]:
geometries.append(Geometry.from_geojson(geometry, srid=None))
return GeometryCollection(geometries, srid)
if type_ == "point":
return Point(geojson["coordinates"], srid=srid)
if type_ == "linestring":
return LineString(geojson["coordinates"], srid=srid)
if type_ == "polygon":
return Polygon(geojson["coordinates"], srid=srid)
if type_ == "multipoint":
geometries = _MultiGeometry._multi_from_geojson(geojson, Point)
return MultiPoint(geometries, srid=srid)
if type_ == "multilinestring":
geometries = _MultiGeometry._multi_from_geojson(geojson, LineString)
return MultiLineString(geometries, srid=srid)
if type_ == "multipolygon":
geometries = _MultiGeometry._multi_from_geojson(geojson, Polygon)
return MultiPolygon(geometries, srid=srid)
raise GeojsonError(f"Invalid GeoJSON type: {type_}")
def _read_wkt_geom(reader):
type_ = reader.get_type()
dimz, dimm = reader.get_dims()
if type_ == "POINT":
cls = Point
elif type_ == "LINESTRING":
cls = LineString
elif type_ == "POLYGON":
cls = Polygon
elif type_ == "MULTIPOINT":
cls = MultiPoint
elif type_ == "MULTILINESTRING":
cls = MultiLineString
elif type_ == "MULTIPOLYGON":
cls = MultiPolygon
elif type_ == "GEOMETRYCOLLECTION":
cls = GeometryCollection
return cls._read_wkt(reader)
@staticmethod
def from_wkt(wkt, srid=None):
"""
Create a Geometry from a WKT or EWKT.
"""
reader = WktReader(wkt)
reader.get_srid()
geom = Geometry._read_wkt_geom(reader)
reader.close()
if srid:
geom.srid = srid
return geom
@staticmethod
def from_shapely(sgeom, srid=None):
"""
Create a Geometry from a Shapely geometry and the specified SRID.
The Shapely geometry will not be modified.
"""
try:
import shapely
if srid:
sgeom = shapely.set_srid(sgeom, srid)
wkb_hex = shapely.to_wkb(sgeom, include_srid=True, hex=True)
return Geometry(wkb_hex)
except ImportError:
raise DependencyError("Shapely")
@staticmethod
def shape(shape, srid=None):
"""
Create a Geometry using ``__geo_interface__`` and the specified SRID.
"""
return Geometry.from_geojson(shape.__geo_interface__, srid)
@property
def type(self):
"""
The geometry type.
"""
return self.__class__.__name__
@property
def srid(self):
"""
The geometry SRID.
"""
return self._srid
@srid.setter
def srid(self, value):
self._check_cache()
self._srid = value
@property
def coordinates(self):
"""
Get the geometry's coordinates.
"""
return self._coordinates()
@property
def geojson(self):
"""
Get the geometry as a GeoJSON dict. There is no check that the
GeoJSON is using an SRID of 4326.
"""
return self._to_geojson()
@property
def wkb(self):
"""
Get the geometry as an WKB.
"""
return self._to_wkb(use_srid=False, dimz=self.dimz, dimm=self.dimm)
@property
def wkt(self):
"""
Get the geometry as an WKT.
"""
return self._to_wkt(use_srid=False)
@property
def ewkb(self):
"""
Get the geometry as an EWKB.
"""
return self._to_wkb(use_srid=True, dimz=self.dimz, dimm=self.dimm)
@property
def ewkt(self):
"""
Get the geometry as an EWKT.
"""
return self._to_wkt(use_srid=True)
@property
def shapely(self):
"""
Get the geometry as a Shapely geometry. If the geometry has an SRID,
the Shapely object will be created with it set.
"""
return self._to_shapely()
@property
def bounds(self):
"""
Get the minimum and maximum extents of the geometry: (minx, miny, maxx,
maxy).
"""
return self._bounds()
@property
def postgis_type(self):
"""
Get the type of the geometry in PostGIS format, including additional
dimensions and SRID if they exist.
"""
dimz = "Z" if self.dimz else ""
dimm = "M" if self.dimm else ""
if self.srid:
return f"geometry({self.type}{dimz}{dimm},{self.srid})"
return f"geometry({self.type}{dimz}{dimm})"
@staticmethod
def _from_wkb(wkb):
try:
if wkb.startswith(b"\x00"):
reader = HexReader(wkb, ">") # big-endian reader
elif wkb.startswith(b"\x01"):
reader = HexReader(wkb, "<") # little-endian reader
else:
raise WkbError("First byte in WKB must be 0 or 1.")
except Exception as e:
raise WkbError("Error reading WKB") from e
return Geometry._get_wkb_type(reader) + (reader,)
def _check_cache(self):
if self._reader is not None:
self._load_geometry()
self._wkb = None
self._reader = None
def _to_wkb(self, use_srid, dimz, dimm):
if self._wkb is not None:
# use cached WKB if it is an EWKB and user requested EWKB
if use_srid and self.srid is not None:
return self._wkb
# use cached WKB if it is a WKB and user requested WKB
if not use_srid and self.srid is None:
return self._wkb
writer = HexWriter("<")
self._write_wkb_header(writer, use_srid, dimz, dimm)
self._write_wkb(writer, dimz, dimm)
return writer.data
def _to_wkt(self, use_srid):
writer = WktWriter(self, use_srid)
writer.add(
self._as_wkt(writer)
)
return writer.wkt
def _to_shapely(self):
try:
import shapely
import shapely.wkb
sgeom = shapely.wkb.loads(self.ewkb)
srid = shapely.get_srid(sgeom)
if srid == 0:
srid = None
if (srid or self.srid) and srid != self.srid:
raise SridError(f"SRID mismatch: {srid} {self.srid}")
return sgeom
except ImportError:
raise DependencyError("Shapely")
def _to_geojson(self):
coordinates = self._coordinates(dimm=False, tpl=False)
geojson = {"type": self.type, "coordinates": coordinates}
return geojson
def __repr__(self):
return f"<{self.type}: '{self.postgis_type}'>"
def __str__(self):
return self.ewkb.__str__()
@property
def __geo_interface__(self):
return self.geojson
def _set_dimensionality(self, geometries):
self._dimz = None
self._dimm = None
for geometry in geometries:
if self._dimz is None:
self._dimz = geometry.dimz
elif self._dimz != geometry.dimz:
raise DimensionalityError("Mixed dimensionality in MultiGeometry")
if self._dimm is None:
self._dimm = geometry.dimm
elif self._dimm != geometry.dimm:
raise DimensionalityError("Mixed dimensionality in MultiGeometry")
@staticmethod
def _get_wkb_type(reader):
lwgeomtype, dimz, dimm, srid = Geometry._read_wkb_header(reader)
if lwgeomtype == 1:
cls = Point
elif lwgeomtype == 2:
cls = LineString
elif lwgeomtype == 3:
cls = Polygon
elif lwgeomtype == 4:
cls = MultiPoint
elif lwgeomtype == 5:
cls = MultiLineString
elif lwgeomtype == 6:
cls = MultiPolygon
elif lwgeomtype == 7:
cls = GeometryCollection
else:
raise WkbError(f"Unsupported WKB type: {lwgeomtype}")
return cls, dimz, dimm, srid
@staticmethod
def _read_wkb_header(reader):
try:
reader.get_char()
header = reader.get_int()
lwgeomtype = header & Geometry._WKBTYPE
dimz = bool(header & Geometry._WKBZFLAG)
dimm = bool(header & Geometry._WKBMFLAG)
if header & Geometry._WKBSRIDFLAG:
srid = reader.get_int()
else:
srid = None
except TypeError as e:
raise WkbError() from e
return lwgeomtype, dimz, dimm, srid
def _write_wkb_header(self, writer, use_srid, dimz, dimm):
if not self.srid:
use_srid = False
writer.add_order()
header = (
self._LWGEOMTYPE
| (Geometry._WKBZFLAG if dimz else 0)
| (Geometry._WKBMFLAG if dimm else 0)
| (Geometry._WKBSRIDFLAG if use_srid else 0)
)
writer.add_int(header)
if use_srid:
writer.add_int(self.srid)
return writer
def _as_wkt(self, writer):
coords = self._to_wkt_coordinates(writer)
if coords is None:
return f"{writer.type(self)} EMPTY"
else:
coords = writer.wrap(coords)
return f"{writer.type(self)} {coords}"
class _MultiGeometry(Geometry):
__slots__ = ["_geometries"]
def __init__(self, multitype, geometries=None, srid=None):
if self._wkb:
self._geometries = None
else:
if not all(isinstance(geometry, multitype) for geometry in geometries):
raise CollectionError(
f"Found non-{multitype} when creating a Multi{multitype}"
)
self._geometries = geometries
self._srid = srid
self._set_multi_metadata()
def __copy__(self):
cls = self.__class__
geometries = copy(self._geometries)
return cls(geometries, self.srid)
def __deepcopy__(self, _):
cls = self.__class__
geometries = [deepcopy(geometry) for geometry in self._geometries]
return cls(geometries, self.srid)
def __getitem__(self, index):
return self._geometries[index]
def __setitem__(self, index, value):
if not issubclass(type(value), self.MULTITYPE):
raise CollectionError(
f"Can not add {type(value)} to a {type(self)}")
self._geometries[index] = value
def __len__(self):
return len(self._geometries)
def __add__(self, other):
if self.srid != other.srid:
raise CollectionError(
"Can not add mixed SRID types")
if type(other) is type(self):
new_geom = copy(self)
new_geom._geometries.extend(other._geometries)
elif type(other) is self.MULTITYPE:
new_geom = copy(self)
new_geom._geometries.append(other)
else:
new_geom = GeometryCollection(self._geometries + [other],
srid=self.srid)
return new_geom
def __iadd__(self, other):
if self.srid != other.srid:
raise CollectionError(
"Can not add mixed SRID types")
if type(self) is GeometryCollection:
if issubclass(type(other), _MultiGeometry):
self._geometries.extend(other)
else:
self._geometries.append(other)
elif type(self) is type(other):
self._geometries.extend(other.geometries)
elif type(other) is self.MULTITYPE:
self._geometries.append(other)
else:
raise CollectionError(
f"Can not add a {type(other)} to a {type(self)}")
return self
def pop(self, index=-1):
"""
Remove a geometry from the multigeometry.
"""
return self._geometries.pop(index)
@property
def geometries(self):
"""
List of all component geometries.
"""
self._check_cache()
return tuple(self._geometries)
@property
def dimz(self):
"""
Whether the geometry has a Z dimension.
:getter: ``True`` if the geometry has a Z dimension.
:setter: Add or remove the Z dimension from this and all geometries in the collection.
:rtype: bool
"""
return self._dimz
@dimz.setter
def dimz(self, value):
if self._dimz == value:
return
for geometry in self.geometries:
geometry.dimz = value
self._dimz = value
@property
def dimm(self):
"""
Whether the geometry has an M dimension.
:getter: ``True`` if the geometry has an M dimension.
:setter: Add or remove the M dimension from this and all geometries in the collection.
:rtype: bool
"""
return self._dimm
@dimm.setter
def dimm(self, value):
if self._dimm == value:
return
for geometry in self.geometries:
geometry.dimm = value
self._dimm = value
def _bounds(self):
bounds = [g.bounds for g in self.geometries]
minx = min(b[0] for b in bounds)
miny = min(b[1] for b in bounds)
maxx = max(b[2] for b in bounds)
maxy = max(b[3] for b in bounds)
return (minx, miny, maxx, maxy)
@staticmethod
def _multi_from_geojson(geojson, cls):
geometries = []
for coordinates in geojson["coordinates"]:
geometry = cls(coordinates, srid=None)
geometries.append(geometry)
return geometries
def _load_geometry(self):
self._geometries = self.__class__._read_wkb(
self._reader, self._dimz, self._dimm
)
def _set_multi_metadata(self):
self._dimz = None
self._dimm = None
for geometry in self.geometries:
if self._dimz is None:
self._dimz = geometry.dimz
elif self._dimz != geometry.dimz:
raise DimensionalityError("Mixed dimensionality in MultiGeometry")
if self._dimm is None:
self._dimm = geometry.dimm
elif self._dimm != geometry.dimm:
raise DimensionalityError("Mixed dimensionality in MultiGeometry")
if geometry._srid is not None:
if self._srid != geometry._srid:
raise SridError(
"Geometry can not be different from SRID in MultiGeometry"
)
geometry._srid = None
def _coordinates(self, dimz=True, dimm=True, tpl=True):
return [g._coordinates(dimz, dimm, tpl) for g in self.geometries]
def _load_geometry(self):
self._geometries = _MultiGeometry._read_wkb(
self._reader, self._dimz, self._dimm
)
@staticmethod
def _read_wkb(reader, dimz, dimm):
geometries = []
try:
for _ in range(reader.get_int()):
cls, dimz, dimm, srid = Geometry._get_wkb_type(reader)
coordinates = cls._read_wkb(reader, dimz, dimm)
geometry = cls(coordinates, srid=srid, dimz=dimz, dimm=dimm)
geometries.append(geometry)
except TypeError as e:
raise WkbError() from e
return geometries
@classmethod
def _read_wkt(cls, reader):
if reader.get_empty():
geoms = []
else:
reader.get_openpar()
geoms = [cls.MULTITYPE._read_wkt(reader)]
while reader.get_comma(req=False):
geom = cls.MULTITYPE._read_wkt(reader)
geoms.append(geom)
reader.get_closepar()
return cls(geoms, srid=reader.srid)
def _write_wkb(self, writer, dimz, dimm):
writer.add_int(len(self.geometries))
for geometry in self._geometries:
geometry._write_wkb_header(writer, False, dimz, dimm)
geometry._write_wkb(writer, dimz, dimm)
def _to_wkt_coordinates(self, writer):
if not self.geometries:
return None
return writer.join(
[writer.wrap(geom._to_wkt_coordinates(writer)) for geom in self.geometries]
)
class Point(Geometry):
"""
A representation of a PostGIS Point.
``Point`` objects can be created directly.
>>> Point((0, -52, 5), dimm=True, srid=4326)
<Point: 'geometry(PointM,4326)'>
The ``dimz`` and ``dimm`` parameters will indicate how to interpret the
coordinates that have been passed as the first argument. By default, the
third coordinate will be interpreted as representing the Z dimension.
"""
_LWGEOMTYPE = 1
__slots__ = ["_x", "_y", "_z", "_m"]
def __init__(self, coordinates=None, srid=None, dimz=False, dimm=False):
if self._wkb:
self._x = None
self._y = None
self._z = None
self._m = None
else:
for c in coordinates:
if c is None:
continue
if not isinstance(c, numbers.Number):
raise CoordinateError(
f"Coordinates must be numeric: {coordinates}", coordinates
)
self._srid = srid
self._x = coordinates[0]
self._y = coordinates[1]
num = len(coordinates)
if num > 4:
raise DimensionalityError(
f"Maximum dimensionality supported for coordinates is 4: {coordinates}"
)
if num == 2: # fill in Z and M if we are supposed to have them, else None
if dimz and dimm:
self._z = 0
self._m = 0
elif dimz:
self._z = 0
self._m = None
elif dimm:
self._z = None
self._m = 0
else:
self._z = None
self._m = None
elif num == 3: # use the 3rd coordinate for Z or M as directed or as Z
if dimz and dimm:
self._z = coordinates[2]
self._m = 0
elif dimm:
self._z = None
self._m = coordinates[2]
else:
self._z = coordinates[2]
self._m = None
else: # use both the 3rd and 4th coordinates, ensure not None
self._z = coordinates[2]
self._m = coordinates[3]
self._dimz = self._z is not None
self._dimm = self._m is not None
@property
def x(self):
"""
X coordinate.
"""
self._check_cache()
return self._x
@x.setter
def x(self, value):
self._check_cache()
self._x = value
@property
def y(self):
"""
M coordinate.
"""
self._check_cache()
return self._y
@y.setter
def y(self, value):
self._check_cache()
self._y = value
@property
def z(self):
"""
Z coordinate.
"""
if not self._dimz:
return None
self._check_cache()
return self._z
@z.setter
def z(self, value):
self._check_cache()
self._z = value
if value is None:
self._dimz = False
else:
self._dimz = True
@property
def m(self):
"""
M coordinate.
"""
if not self._dimm:
return None
self._check_cache()
return self._m
@m.setter
def m(self, value):
self._check_cache()
self._m = value
if value is None:
self._dimm = False
else:
self._dimm = True
@property
def dimz(self):
"""
Whether the geometry has a Z dimension.
:getter: ``True`` if the geometry has a Z dimension.
:setter: Add or remove the Z dimension.
:rtype: bool
"""
return self._dimz
@dimz.setter
def dimz(self, value):
if self._dimz == value:
return
self._check_cache()
if value and self._z is None:
self._z = 0
elif value is None:
self._z = None
self._dimz = value
@property
def dimm(self):
"""
Whether the geometry has an M dimension.
:getter: ``True`` if the geometry has an M dimension.
:setter: Add or remove the M dimension.
:rtype: bool
"""
return self._dimm
@dimm.setter
def dimm(self, value):
if self._dimm == value:
return
self._check_cache()
if value and self._m is None:
self._m = 0
elif value is None:
self._m = None
self._dimm = value
def _coordinates(self, dimz=True, dimm=True, tpl=True):
dimz = dimz & self.dimz
dimm = dimm & self.dimm
if dimz and dimm:
coordinates = (self.x, self.y, self.z, self.m)
elif dimz:
coordinates = (self.x, self.y, self.z)
elif dimm:
coordinates = (self.x, self.y, self.m)
else:
coordinates = (self.x, self.y)
if tpl:
return coordinates
return list(coordinates)
def _bounds(self):
return (self.x, self.y, self.x, self.y)
def _load_geometry(self):
self._x, self._y, self._z, self._m = Point._read_wkb(
self._reader, self._dimz, self._dimm
)
@staticmethod
def _read_wkb(reader, dimz, dimm):
try:
x = reader.get_double()
y = reader.get_double()
if dimz and dimm:
z = reader.get_double()
m = reader.get_double()
elif dimz:
z = reader.get_double()
m = None
elif dimm:
z = None
m = reader.get_double()
else:
z = None
m = None
except TypeError as e:
raise WkbError() from e
return x, y, z, m
def _write_wkb(self, writer, dimz, dimm):
writer.add_double(self.x)
writer.add_double(self.y)
if dimz and dimm:
writer.add_double(self.z)
writer.add_double(self.m)
elif dimz:
writer.add_double(self.z)
elif dimm:
writer.add_double(self.m)
@staticmethod
def _read_wkt_coordinates(reader):
if reader.get_empty():
raise WktError(reader, "Points with no coordinates are not supported in WKT.")
reader.get_openpar()
coords = reader.get_coordinates()
reader.get_closepar()
return coords
@staticmethod
def _read_wkt(reader):
coords = Point._read_wkt_coordinates(reader)
return Point(coords, dimz=reader.dimz, dimm=reader.dimm, srid=reader.srid)
def _to_wkt_coordinates(self, writer):
return writer.format(self.coordinates)
class LineString(Geometry):
"""
A representation of a PostGIS Line.
``LineString`` objects can be created directly.
>>> LineString([(0, 0, 0, 0), (1, 1, 0, 0), (2, 2, 0, 0)])
<LineString: 'geometry(LineStringZM)'>
The ``dimz`` and ``dimm`` parameters will indicate how to interpret the
coordinates that have been passed as the first argument. By default, the
third coordinate will be interpreted as representing the Z dimension.
"""
_LWGEOMTYPE = 2
__slots__ = ["_vertices"]
def __init__(self, vertices=None, srid=None, dimz=False, dimm=False):
if self._wkb:
self._vertices = None
else:
self._srid = srid
self._dimz = dimz
self._dimm = dimm
self._vertices = LineString._from_coordinates(
vertices, dimz=dimz, dimm=dimm
)
self._set_dimensionality(self._vertices)
@property
def vertices(self):
"""
List of vertices that comprise the line.
"""
self._check_cache()
return self._vertices
@property
def dimz(self):
"""
Whether the geometry has a Z dimension.
:getter: ``True`` if the geometry has a Z dimension.
:setter: Add or remove the Z dimension from this and all vertices in the line.
:rtype: bool
"""
return self._dimz
@dimz.setter
def dimz(self, value):
if self.dimz == value:
return
for vertex in self.vertices:
vertex.dimz = value
self._dimz = value
@property
def dimm(self):
"""
Whether the geometry has a M dimension.
:getter: ``True`` if the geometry has a M dimension.
:setter: Add or remove the M dimension from this and all vertices in the line.
:rtype: bool
"""
return self._dimm
@dimm.setter
def dimm(self, value):
if self.dimm == value:
return
for vertex in self.vertices:
vertex.dimm = value
self._dimm = value
def _coordinates(self, dimz=True, dimm=True, tpl=True):
return [v._coordinates(dimz, dimm, tpl) for v in self.vertices]
def _bounds(self):
x = [v.x for v in self.vertices]
y = [v.y for v in self.vertices]
return (min(x), min(y), max(x), max(y))
@staticmethod
def _from_coordinates(vertices, dimz, dimm):
return [Point(vertex, dimz=dimz, dimm=dimm) for vertex in vertices]
def _load_geometry(self):
vertices = LineString._read_wkb(self._reader, self._dimz, self._dimm)
self._vertices = LineString._from_coordinates(vertices, self._dimz, self._dimm)
@staticmethod
def _read_wkb(reader, dimz, dimm):
vertices = []
try:
for _ in range(reader.get_int()):
coordinates = Point._read_wkb(reader, dimz, dimm)
vertices.append(coordinates)
except TypeError as e:
raise WkbError() from e
return vertices
def _write_wkb(self, writer, dimz, dimm):