-
Notifications
You must be signed in to change notification settings - Fork 902
/
base.py
5421 lines (4556 loc) · 180 KB
/
base.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 warnings
from warnings import warn
import numpy as np
import pandas as pd
import shapely
from pandas import DataFrame, Series
from shapely.geometry import MultiPoint, box
from shapely.geometry.base import BaseGeometry
from . import _compat as compat
from .array import GeometryArray, GeometryDtype, points_from_xy
def is_geometry_type(data):
"""
Check if the data is of geometry dtype.
Does not include object array of shapely scalars.
"""
if isinstance(getattr(data, "dtype", None), GeometryDtype):
# GeometryArray, GeoSeries and Series[GeometryArray]
return True
else:
return False
def _delegate_binary_method(op, this, other, align, *args, **kwargs):
# type: (str, GeoSeries, GeoSeries) -> GeoSeries/Series
if align is None:
align = True
maybe_warn = True
else:
maybe_warn = False
this = this.geometry
if isinstance(other, GeoPandasBase):
if align and not this.index.equals(other.index):
if maybe_warn:
warn(
"The indices of the left and right GeoSeries' are not equal, and "
"therefore they will be aligned (reordering and/or introducing "
"missing values) before executing the operation. If this alignment "
"is the desired behaviour, you can silence this warning by passing "
"'align=True'. If you don't want alignment and protect yourself of "
"accidentally aligning, you can pass 'align=False'.",
stacklevel=4,
)
this, other = this.align(other.geometry)
else:
other = other.geometry
a_this = GeometryArray(this.values)
other = GeometryArray(other.values)
elif isinstance(other, BaseGeometry):
a_this = GeometryArray(this.values)
else:
raise TypeError(type(this), type(other))
data = getattr(a_this, op)(other, *args, **kwargs)
return data, this.index
def _binary_geo(op, this, other, align, *args, **kwargs):
# type: (str, GeoSeries, GeoSeries) -> GeoSeries
"""Binary operation on GeoSeries objects that returns a GeoSeries"""
from .geoseries import GeoSeries
geoms, index = _delegate_binary_method(op, this, other, align, *args, **kwargs)
return GeoSeries(geoms, index=index, crs=this.crs)
def _binary_op(op, this, other, align, *args, **kwargs):
# type: (str, GeoSeries, GeoSeries, args/kwargs) -> Series[bool/float]
"""Binary operation on GeoSeries objects that returns a Series"""
data, index = _delegate_binary_method(op, this, other, align, *args, **kwargs)
return Series(data, index=index)
def _delegate_property(op, this):
# type: (str, GeoSeries) -> GeoSeries/Series
a_this = GeometryArray(this.geometry.values)
data = getattr(a_this, op)
if isinstance(data, GeometryArray):
from .geoseries import GeoSeries
return GeoSeries(data, index=this.index, crs=this.crs)
else:
return Series(data, index=this.index)
def _delegate_geo_method(op, this, *args, **kwargs):
# type: (str, GeoSeries) -> GeoSeries
"""Unary operation that returns a GeoSeries"""
from .geoseries import GeoSeries
a_this = GeometryArray(this.geometry.values)
data = getattr(a_this, op)(*args, **kwargs)
return GeoSeries(data, index=this.index, crs=this.crs)
class GeoPandasBase(object):
@property
def area(self):
"""Returns a ``Series`` containing the area of each geometry in the
``GeoSeries`` expressed in the units of the CRS.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Polygon([(10, 0), (10, 5), (0, 0)]),
... Polygon([(0, 0), (2, 2), (2, 0)]),
... LineString([(0, 0), (1, 1), (0, 1)]),
... Point(0, 1)
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((10 0, 10 5, 0 0, 10 0))
2 POLYGON ((0 0, 2 2, 2 0, 0 0))
3 LINESTRING (0 0, 1 1, 0 1)
4 POINT (0 1)
dtype: geometry
>>> s.area
0 0.5
1 25.0
2 2.0
3 0.0
4 0.0
dtype: float64
See also
--------
GeoSeries.length : measure length
Notes
-----
Area may be invalid for a geographic CRS using degrees as units;
use :meth:`GeoSeries.to_crs` to project geometries to a planar
CRS before using this function.
Every operation in GeoPandas is planar, i.e. the potential third
dimension is not taken into account.
"""
return _delegate_property("area", self)
@property
def crs(self):
"""
The Coordinate Reference System (CRS) represented as a ``pyproj.CRS``
object.
Returns None if the CRS is not set, and to set the value it
:getter: Returns a ``pyproj.CRS`` or None. When setting, the value
can be anything accepted by
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
such as an authority string (eg "EPSG:4326") or a WKT string.
Examples
--------
>>> s.crs # doctest: +SKIP
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
See also
--------
GeoSeries.set_crs : assign CRS
GeoSeries.to_crs : re-project to another CRS
"""
return self.geometry.values.crs
@crs.setter
def crs(self, value):
"""Sets the value of the crs"""
self.geometry.values.crs = value
@property
def geom_type(self):
"""
Returns a ``Series`` of strings specifying the `Geometry Type` of each
object.
Examples
--------
>>> from shapely.geometry import Point, Polygon, LineString
>>> d = {'geometry': [Point(2, 1), Polygon([(0, 0), (1, 1), (1, 0)]),
... LineString([(0, 0), (1, 1)])]}
>>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
>>> gdf.geom_type
0 Point
1 Polygon
2 LineString
dtype: object
"""
return _delegate_property("geom_type", self)
@property
def type(self):
"""Return the geometry type of each geometry in the GeoSeries"""
return self.geom_type
@property
def length(self):
"""Returns a ``Series`` containing the length of each geometry
expressed in the units of the CRS.
In the case of a (Multi)Polygon it measures the length
of its exterior (i.e. perimeter).
Examples
--------
>>> from shapely.geometry import Polygon, LineString, MultiLineString, Point, \
GeometryCollection
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (0, 1)]),
... LineString([(10, 0), (10, 5), (0, 0)]),
... MultiLineString([((0, 0), (1, 0)), ((-1, 0), (1, 0))]),
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Point(0, 1),
... GeometryCollection([Point(1, 0), LineString([(10, 0), (10, 5), (0,\
0)])])
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 0 1)
1 LINESTRING (10 0, 10 5, 0 0)
2 MULTILINESTRING ((0 0, 1 0), (-1 0, 1 0))
3 POLYGON ((0 0, 1 1, 0 1, 0 0))
4 POINT (0 1)
5 GEOMETRYCOLLECTION (POINT (1 0), LINESTRING (1...
dtype: geometry
>>> s.length
0 2.414214
1 16.180340
2 3.000000
3 3.414214
4 0.000000
5 16.180340
dtype: float64
See also
--------
GeoSeries.area : measure area of a polygon
Notes
-----
Length may be invalid for a geographic CRS using degrees as units;
use :meth:`GeoSeries.to_crs` to project geometries to a planar
CRS before using this function.
Every operation in GeoPandas is planar, i.e. the potential third
dimension is not taken into account.
"""
return _delegate_property("length", self)
@property
def is_valid(self):
"""Returns a ``Series`` of ``dtype('bool')`` with value ``True`` for
geometries that are valid.
Examples
--------
An example with one invalid polygon (a bowtie geometry crossing itself)
and one missing geometry:
>>> from shapely.geometry import Polygon
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Polygon([(0,0), (1, 1), (1, 0), (0, 1)]), # bowtie geometry
... Polygon([(0, 0), (2, 2), (2, 0)]),
... None
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((0 0, 1 1, 1 0, 0 1, 0 0))
2 POLYGON ((0 0, 2 2, 2 0, 0 0))
3 None
dtype: geometry
>>> s.is_valid
0 True
1 False
2 True
3 False
dtype: bool
"""
return _delegate_property("is_valid", self)
@property
def is_empty(self):
"""
Returns a ``Series`` of ``dtype('bool')`` with value ``True`` for
empty geometries.
Examples
--------
An example of a GeoDataFrame with one empty point, one point and one missing
value:
>>> from shapely.geometry import Point
>>> d = {'geometry': [Point(), Point(2, 1), None]}
>>> gdf = geopandas.GeoDataFrame(d, crs="EPSG:4326")
>>> gdf
geometry
0 POINT EMPTY
1 POINT (2 1)
2 None
>>> gdf.is_empty
0 True
1 False
2 False
dtype: bool
See Also
--------
GeoSeries.isna : detect missing values
"""
return _delegate_property("is_empty", self)
def count_coordinates(self):
"""
Returns a ``Series`` containing the count of the number of coordinate pairs
in a geometry array.
Examples
--------
An example of a GeoDataFrame with two line strings, one point and one None
value:
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (1, -1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, -1)]),
... Point(0, 0),
... Polygon([(10, 10), (10, 20), (20, 20), (20, 10), (10, 10)]),
... None
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 1 -1, 0 1)
1 LINESTRING (0 0, 1 1, 1 -1)
2 POINT (0 0)
3 POLYGON ((10 10, 10 20, 20 20, 20 10, 10 10))
4 None
dtype: geometry
>>> s.count_coordinates()
0 4
1 3
2 1
3 5
4 0
dtype: int64
See also
--------
GeoSeries.get_coordinates : extract coordinates as a :class:`~pandas.DataFrame`
"""
return Series(self.geometry.values.count_coordinates(), index=self.index)
@property
def is_simple(self):
"""Returns a ``Series`` of ``dtype('bool')`` with value ``True`` for
geometries that do not cross themselves.
This is meaningful only for `LineStrings` and `LinearRings`.
Examples
--------
>>> from shapely.geometry import LineString
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (1, -1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, -1)]),
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 1 -1, 0 1)
1 LINESTRING (0 0, 1 1, 1 -1)
dtype: geometry
>>> s.is_simple
0 False
1 True
dtype: bool
"""
return _delegate_property("is_simple", self)
@property
def is_ring(self):
"""Returns a ``Series`` of ``dtype('bool')`` with value ``True`` for
features that are closed.
When constructing a LinearRing, the sequence of coordinates may be
explicitly closed by passing identical values in the first and last indices.
Otherwise, the sequence will be implicitly closed by copying the first tuple
to the last index.
Examples
--------
>>> from shapely.geometry import LineString, LinearRing
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (1, -1)]),
... LineString([(0, 0), (1, 1), (1, -1), (0, 0)]),
... LinearRing([(0, 0), (1, 1), (1, -1)]),
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 1 -1)
1 LINESTRING (0 0, 1 1, 1 -1, 0 0)
2 LINEARRING (0 0, 1 1, 1 -1, 0 0)
dtype: geometry
>>> s.is_ring
0 False
1 True
2 True
dtype: bool
"""
return _delegate_property("is_ring", self)
@property
def is_ccw(self):
"""Returns a ``Series`` of ``dtype('bool')`` with value ``True``
if a LineString or LinearRing is counterclockwise.
Note that there are no checks on whether lines are actually
closed and not self-intersecting, while this is a requirement
for ``is_ccw``. The recommended usage of this property for
LineStrings is ``GeoSeries.is_ccw & GeoSeries.is_simple`` and for
LinearRings ``GeoSeries.is_ccw & GeoSeries.is_valid``.
This property will return False for non-linear geometries and for
lines with fewer than 4 points (including the closing point).
Examples
--------
>>> from shapely.geometry import LineString, LinearRing, Point
>>> s = geopandas.GeoSeries(
... [
... LinearRing([(0, 0), (0, 1), (1, 1), (0, 0)]),
... LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (0, 1)]),
... Point(3, 3)
... ]
... )
>>> s
0 LINEARRING (0 0, 0 1, 1 1, 0 0)
1 LINEARRING (0 0, 1 1, 0 1, 0 0)
2 LINESTRING (0 0, 1 1, 0 1)
3 POINT (3 3)
dtype: geometry
>>> s.is_ccw
0 False
1 True
2 False
3 False
dtype: bool
"""
return _delegate_property("is_ccw", self)
@property
def is_closed(self):
"""Returns a ``Series`` of ``dtype('bool')`` with value ``True``
if a LineString's or LinearRing's first and last points are equal.
Returns False for any other geometry type.
Examples
--------
>>> from shapely.geometry import LineString, Point, Polygon
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (1, 1), (0, 1), (0, 0)]),
... LineString([(0, 0), (1, 1), (0, 1)]),
... Polygon([(0, 0), (0, 1), (1, 1), (0, 0)]),
... Point(3, 3)
... ]
... )
>>> s
0 LINESTRING (0 0, 1 1, 0 1, 0 0)
1 LINESTRING (0 0, 1 1, 0 1)
2 POLYGON ((0 0, 0 1, 1 1, 0 0))
3 POINT (3 3)
dtype: geometry
>>> s.is_closed
0 True
1 False
2 False
3 False
dtype: bool
"""
return _delegate_property("is_closed", self)
@property
def has_z(self):
"""Returns a ``Series`` of ``dtype('bool')`` with value ``True`` for
features that have a z-component.
Notes
-----
Every operation in GeoPandas is planar, i.e. the potential third
dimension is not taken into account.
Examples
--------
>>> from shapely.geometry import Point
>>> s = geopandas.GeoSeries(
... [
... Point(0, 1),
... Point(0, 1, 2),
... ]
... )
>>> s
0 POINT (0 1)
1 POINT Z (0 1 2)
dtype: geometry
>>> s.has_z
0 False
1 True
dtype: bool
"""
return _delegate_property("has_z", self)
def get_precision(self):
"""Returns a ``Series`` of the precision of each geometry.
If a precision has not been previously set, it will be 0, indicating regular
double precision coordinates are in use. Otherwise, it will return the precision
grid size that was set on a geometry.
Returns NaN for not-a-geometry values.
Examples
--------
>>> from shapely.geometry import Point
>>> s = geopandas.GeoSeries(
... [
... Point(0, 1),
... Point(0, 1, 2),
... Point(0, 1.5, 2),
... ]
... )
>>> s
0 POINT (0 1)
1 POINT Z (0 1 2)
2 POINT Z (0 1.5 2)
dtype: geometry
>>> s.get_precision()
0 0.0
1 0.0
2 0.0
dtype: float64
>>> s1 = s.set_precision(1)
>>> s1
0 POINT (0 1)
1 POINT Z (0 1 2)
2 POINT Z (0 2 2)
dtype: geometry
>>> s1.get_precision()
0 1.0
1 1.0
2 1.0
dtype: float64
See also
--------
GeoSeries.set_precision : set precision grid size
"""
return Series(self.geometry.values.get_precision(), index=self.index)
#
# Unary operations that return a GeoSeries
#
@property
def boundary(self):
"""Returns a ``GeoSeries`` of lower dimensional objects representing
each geometry's set-theoretic `boundary`.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.boundary
0 LINESTRING (0 0, 1 1, 0 1, 0 0)
1 MULTIPOINT ((0 0), (1 0))
2 GEOMETRYCOLLECTION EMPTY
dtype: geometry
See also
--------
GeoSeries.exterior : outer boundary (without interior rings)
"""
return _delegate_property("boundary", self)
@property
def centroid(self):
"""Returns a ``GeoSeries`` of points representing the centroid of each
geometry.
Note that centroid does not have to be on or within original geometry.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 POINT (0 0)
dtype: geometry
>>> s.centroid
0 POINT (0.33333 0.66667)
1 POINT (0.70711 0.5)
2 POINT (0 0)
dtype: geometry
See also
--------
GeoSeries.representative_point : point guaranteed to be within each geometry
"""
return _delegate_property("centroid", self)
def concave_hull(self, ratio=0.0, allow_holes=False):
"""Returns a ``GeoSeries`` of geometries representing the concave hull
of each geometry.
The concave hull of a geometry is the smallest concave `Polygon`
containing all the points in each geometry, unless the number of points
in the geometric object is less than three. For two points, the concave
hull collapses to a `LineString`; for 1, a `Point`.
The hull is constructed by removing border triangles of the Delaunay
Triangulation of the points as long as their "size" is larger than the
maximum edge length ratio and optionally allowing holes. The edge length factor
is a fraction of the length difference between the longest and shortest edges
in the Delaunay Triangulation of the input points. For further information
on the algorithm used, see
https://libgeos.org/doxygen/classgeos_1_1algorithm_1_1hull_1_1ConcaveHull.html
Parameters
----------
ratio : float, (optional, default 0.0)
Number in the range [0, 1]. Higher numbers will include fewer vertices
in the hull.
allow_holes : bool, (optional, default False)
If set to True, the concave hull may have holes.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1), (0, 1), (1, 0), (0.5, 0.5)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ],
... crs=3857
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1), (0 1), (1 0), (0.5 0...
3 MULTIPOINT ((0 0), (1 1))
4 POINT (0 0)
dtype: geometry
>>> s.concave_hull()
0 POLYGON ((0 1, 1 1, 0 0, 0 1))
1 POLYGON ((0 0, 1 1, 1 0, 0 0))
2 POLYGON ((0.5 0.5, 0 1, 1 1, 1 0, 0 0, 0.5 0.5))
3 LINESTRING (0 0, 1 1)
4 POINT (0 0)
dtype: geometry
See also
--------
GeoSeries.convex_hull : convex hull geometry
"""
return _delegate_geo_method(
"concave_hull", self, ratio=ratio, allow_holes=allow_holes
)
@property
def convex_hull(self):
"""Returns a ``GeoSeries`` of geometries representing the convex hull
of each geometry.
The convex hull of a geometry is the smallest convex `Polygon`
containing all the points in each geometry, unless the number of points
in the geometric object is less than three. For two points, the convex
hull collapses to a `LineString`; for 1, a `Point`.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1), (0, 1), (1, 0), (0.5, 0.5)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1), (0 1), (1 0), (0.5 0...
3 MULTIPOINT ((0 0), (1 1))
4 POINT (0 0)
dtype: geometry
>>> s.convex_hull
0 POLYGON ((0 0, 0 1, 1 1, 0 0))
1 POLYGON ((0 0, 1 1, 1 0, 0 0))
2 POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
3 LINESTRING (0 0, 1 1)
4 POINT (0 0)
dtype: geometry
See also
--------
GeoSeries.concave_hull : concave hull geometry
GeoSeries.envelope : bounding rectangle geometry
"""
return _delegate_property("convex_hull", self)
def delaunay_triangles(self, tolerance=0.0, only_edges=False):
"""Returns a ``GeoSeries`` consisting of objects representing
the computed Delaunay triangulation around the vertices of
an input geometry.
The output is a ``GeometryCollection`` containing polygons
(default) or linestrings (see only_edges).
Returns an empty GeometryCollection if an input geometry
contains less than 3 vertices.
Parameters
----------
tolerance : float | array-like, default 0.0
Snap input vertices together if their distance is less than this value.
only_edges : bool | array_like, (optional, default False)
If set to True, the triangulation will return a collection of
linestrings instead of polygons.
Examples
--------
>>> from shapely import LineString, MultiPoint, Polygon
>>> s = geopandas.GeoSeries(
... [
... MultiPoint([(5, 3), (6, 3), (10, 10)]),
... Polygon([(5, 3), (6, 3), (10, 10), (5, 3)]),
... LineString([(5, 3), (6, 3), (10, 10)]),
... ]
... )
>>> s
0 MULTIPOINT ((5 3), (6 3), (10 10))
1 POLYGON ((5 3, 6 3, 10 10, 5 3))
2 LINESTRING (5 3, 6 3, 10 10)
dtype: geometry
>>> s.delaunay_triangles()
0 GEOMETRYCOLLECTION (POLYGON ((10 10, 5 3, 6 3,...
1 GEOMETRYCOLLECTION (POLYGON ((10 10, 5 3, 6 3,...
2 GEOMETRYCOLLECTION (POLYGON ((10 10, 5 3, 6 3,...
dtype: geometry
>>> s.delaunay_triangles(only_edges=True)
0 MULTILINESTRING ((5 3, 10 10), (5 3, 6 3), (6 ...
1 MULTILINESTRING ((5 3, 10 10), (5 3, 6 3), (6 ...
2 MULTILINESTRING ((5 3, 10 10), (5 3, 6 3), (6 ...
dtype: geometry
"""
return _delegate_geo_method("delaunay_triangles", self, tolerance, only_edges)
@property
def envelope(self):
"""Returns a ``GeoSeries`` of geometries representing the envelope of
each geometry.
The envelope of a geometry is the bounding rectangle. That is, the
point or smallest rectangular polygon (with sides parallel to the
coordinate axes) that contains the geometry.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1))
3 POINT (0 0)
dtype: geometry
>>> s.envelope
0 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
1 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
2 POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))
3 POINT (0 0)
dtype: geometry
See also
--------
GeoSeries.convex_hull : convex hull geometry
"""
return _delegate_property("envelope", self)
def minimum_rotated_rectangle(self):
"""Returns a ``GeoSeries`` of the general minimum bounding rectangle
that contains the object.
Unlike envelope this rectangle is not constrained to be parallel
to the coordinate axes. If the convex hull of the object is a
degenerate (line or point) this degenerate is returned.
Examples
--------
>>> from shapely.geometry import Polygon, LineString, Point, MultiPoint
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... LineString([(0, 0), (1, 1), (1, 0)]),
... MultiPoint([(0, 0), (1, 1)]),
... Point(0, 0),
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 LINESTRING (0 0, 1 1, 1 0)
2 MULTIPOINT ((0 0), (1 1))
3 POINT (0 0)
dtype: geometry
>>> s.minimum_rotated_rectangle()
0 POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
1 POLYGON ((1 1, 1 0, 0 0, 0 1, 1 1))
2 LINESTRING (0 0, 1 1)
3 POINT (0 0)
dtype: geometry
See also
--------
GeoSeries.envelope : bounding rectangle
"""
return _delegate_geo_method("minimum_rotated_rectangle", self)
@property
def exterior(self):
"""Returns a ``GeoSeries`` of LinearRings representing the outer
boundary of each polygon in the GeoSeries.
Applies to GeoSeries containing only Polygons. Returns ``None``` for
other geometry types.
Examples
--------
>>> from shapely.geometry import Polygon, Point
>>> s = geopandas.GeoSeries(
... [
... Polygon([(0, 0), (1, 1), (0, 1)]),
... Polygon([(1, 0), (2, 1), (0, 0)]),
... Point(0, 1)
... ]
... )
>>> s
0 POLYGON ((0 0, 1 1, 0 1, 0 0))
1 POLYGON ((1 0, 2 1, 0 0, 1 0))
2 POINT (0 1)
dtype: geometry
>>> s.exterior
0 LINEARRING (0 0, 1 1, 0 1, 0 0)
1 LINEARRING (1 0, 2 1, 0 0, 1 0)
2 None
dtype: geometry
See also
--------
GeoSeries.boundary : complete set-theoretic boundary
GeoSeries.interiors : list of inner rings of each polygon
"""
# TODO: return empty geometry for non-polygons
return _delegate_property("exterior", self)
def extract_unique_points(self):
"""Returns a ``GeoSeries`` of MultiPoints representing all
distinct vertices of an input geometry.
Examples
--------
>>> from shapely import LineString, Polygon
>>> s = geopandas.GeoSeries(
... [
... LineString([(0, 0), (0, 0), (1, 1), (1, 1)]),
... Polygon([(0, 0), (0, 0), (1, 1), (1, 1)])
... ],
... )
>>> s
0 LINESTRING (0 0, 0 0, 1 1, 1 1)
1 POLYGON ((0 0, 0 0, 1 1, 1 1, 0 0))
dtype: geometry
>>> s.extract_unique_points()
0 MULTIPOINT ((0 0), (1 1))
1 MULTIPOINT ((0 0), (1 1))
dtype: geometry
See also
--------
GeoSeries.get_coordinates : extract coordinates as a :class:`~pandas.DataFrame`
"""
return _delegate_geo_method("extract_unique_points", self)
def offset_curve(self, distance, quad_segs=8, join_style="round", mitre_limit=5.0):
"""Returns a ``LineString`` or ``MultiLineString`` geometry at a
distance from the object on its right or its left side.
Parameters
----------
distance : float | array-like
Specifies the offset distance from the input geometry. Negative
for right side offset, positive for left side offset.
quad_segs : int (optional, default 8)
Specifies the number of linear segments in a quarter circle in the