-
Notifications
You must be signed in to change notification settings - Fork 901
/
test_geom_methods.py
2136 lines (1834 loc) · 77 KB
/
test_geom_methods.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 string
import warnings
import numpy as np
import pytest
import shapely
from numpy.testing import assert_array_equal
from pandas import DataFrame, Index, MultiIndex, Series, concat
from pandas.testing import assert_frame_equal, assert_index_equal, assert_series_equal
from shapely import wkt
from shapely.geometry import (
LinearRing,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
box,
)
from shapely.geometry.collection import GeometryCollection
from shapely.ops import unary_union
from geopandas import GeoDataFrame, GeoSeries
from geopandas.base import GeoPandasBase
from geopandas.testing import assert_geodataframe_equal
from geopandas.tests.util import assert_geoseries_equal, geom_almost_equals, geom_equals
from geopandas._compat import HAS_PYPROJ
def assert_array_dtype_equal(a, b, *args, **kwargs):
a = np.asanyarray(a)
b = np.asanyarray(b)
assert a.dtype == b.dtype
assert_array_equal(a, b, *args, **kwargs)
class TestGeomMethods:
def setup_method(self):
self.t1 = Polygon([(0, 0), (1, 0), (1, 1)])
self.t2 = Polygon([(0, 0), (1, 1), (0, 1)])
self.t3 = Polygon([(2, 0), (3, 0), (3, 1)])
self.tz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3)])
self.tz1 = Polygon([(2, 2, 2), (1, 1, 1), (3, 3, 3)])
self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
self.sqz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)])
self.t4 = Polygon([(0, 0), (3, 0), (3, 3), (0, 2)])
self.t5 = Polygon([(2, 0), (3, 0), (3, 3), (2, 3)])
self.t6 = Polygon([(2, 0), (2, 0), (3, 0), (3, 0)])
self.inner_sq = Polygon(
[(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]
)
self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary])
self.p0 = Point(5, 5)
self.p3d = Point(5, 5, 5)
self.g0 = GeoSeries(
[
self.t1,
self.t2,
self.sq,
self.inner_sq,
self.nested_squares,
self.p0,
None,
]
)
self.g1 = GeoSeries([self.t1, self.sq])
self.g2 = GeoSeries([self.sq, self.t1])
self.g3 = GeoSeries([self.t1, self.t2])
self.gz = GeoSeries([self.tz, self.sqz, self.tz1])
self.g3.crs = "epsg:4326"
self.g4 = GeoSeries([self.t2, self.t1])
self.g4.crs = "epsg:4326"
self.g_3d = GeoSeries([self.p0, self.p3d])
self.na = GeoSeries([self.t1, self.t2, Polygon()])
self.na_none = GeoSeries([self.t1, None])
self.a1 = self.g1.copy()
self.a1.index = ["A", "B"]
self.a2 = self.g2.copy()
self.a2.index = ["B", "C"]
self.esb = Point(-73.9847, 40.7484, 30.3244)
self.sol = Point(-74.0446, 40.6893, 31.2344)
self.landmarks = GeoSeries([self.esb, self.sol], crs="epsg:4326")
self.pt2d = Point(-73.9847, 40.7484)
self.landmarks_mixed = GeoSeries([self.esb, self.sol, self.pt2d], crs=4326)
self.pt_empty = wkt.loads("POINT EMPTY")
self.landmarks_mixed_empty = GeoSeries(
[self.esb, self.sol, self.pt2d, self.pt_empty], crs=4326
)
self.l1 = LineString([(0, 0), (0, 1), (1, 1)])
self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)])
self.g5 = GeoSeries([self.l1, self.l2])
self.g6 = GeoSeries([self.p0, self.t3])
self.g7 = GeoSeries([self.sq, self.t4])
self.g8 = GeoSeries([self.t1, self.t5])
self.empty = GeoSeries([])
self.all_none = GeoSeries([None, None])
self.all_geometry_collection_empty = GeoSeries(
[GeometryCollection([]), GeometryCollection([])]
)
self.empty_poly = Polygon()
self.g9 = GeoSeries(self.g0, index=range(1, 8))
self.g10 = GeoSeries([self.t1, self.t4])
# Crossed lines
self.l3 = LineString([(0, 0), (1, 1)])
self.l4 = LineString([(0, 1), (1, 0)])
self.crossed_lines = GeoSeries([self.l3, self.l4])
# Placeholder for testing, will just drop in different geometries
# when needed
self.gdf1 = GeoDataFrame(
{"geometry": self.g1, "col0": [1.0, 2.0], "col1": ["geo", "pandas"]}
)
self.gdf2 = GeoDataFrame(
{"geometry": self.g1, "col3": [4, 5], "col4": ["rand", "string"]}
)
self.gdf3 = GeoDataFrame(
{"geometry": self.g3, "col3": [4, 5], "col4": ["rand", "string"]}
)
self.gdfz = GeoDataFrame(
{"geometry": self.gz, "col3": [4, 5, 6], "col4": ["rand", "string", "geo"]}
)
self.g11 = GeoSeries(
[
self.p0,
self.p3d,
self.pt_empty,
self.t1,
self.tz,
self.empty_poly,
self.l1,
]
)
# expected coordinates from g11
self.expected_2d = np.array(
[
[5.0, 5.0],
[5.0, 5.0],
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 0.0],
[1.0, 1.0],
[2.0, 2.0],
[3.0, 3.0],
[1.0, 1.0],
[0.0, 0.0],
[0.0, 1.0],
[1.0, 1.0],
]
)
self.expected_3d = np.array(
[
[5.0, 5.0, np.nan],
[5.0, 5.0, 5.0],
[0.0, 0.0, np.nan],
[1.0, 0.0, np.nan],
[1.0, 1.0, np.nan],
[0.0, 0.0, np.nan],
[1.0, 1.0, 1.0],
[2.0, 2.0, 2.0],
[3.0, 3.0, 3.0],
[1.0, 1.0, 1.0],
[0.0, 0.0, np.nan],
[0.0, 1.0, np.nan],
[1.0, 1.0, np.nan],
]
)
self.squares = GeoSeries([self.sq for _ in range(3)])
self.l5 = LineString([(100, 0), (0, 0), (0, 100)])
self.l6 = LineString([(5, 5), (5, 100), (100, 5)])
self.g12 = GeoSeries([self.l5])
self.g13 = GeoSeries([self.l6])
self.l5 = LineString([(100, 0), (0, 0), (0, 100)])
self.l6 = LineString([(5, 5), (5, 100), (100, 5)])
self.g12 = GeoSeries([self.l5])
self.g13 = GeoSeries([self.l6])
self.g14 = GeoSeries(
[
MultiLineString([[(0, 2), (0, 10)], [(0, 10), (5, 10)]]),
MultiLineString([[(0, 2), (0, 10)], [(0, 11), (5, 10)]]),
MultiLineString(),
MultiLineString([[(0, 0), (1, 0)], [(0, 0), (3, 0)]]),
Point(0, 0),
],
crs=4326,
index=range(2, 7),
)
def _test_unary_real(self, op, expected, a):
"""Tests for 'area', 'length', 'is_valid', etc."""
fcmp = assert_series_equal
self._test_unary(op, expected, a, fcmp)
def _test_unary_topological(self, op, expected, a, method=False):
if isinstance(expected, GeoPandasBase):
fcmp = assert_geoseries_equal
else:
def fcmp(a, b):
assert a.equals(b)
self._test_unary(op, expected, a, fcmp, method=method)
def _test_binary_topological(self, op, expected, a, b, *args, **kwargs):
"""Tests for 'intersection', 'union', 'symmetric_difference', etc."""
if isinstance(expected, GeoPandasBase):
fcmp = assert_geoseries_equal
else:
def fcmp(a, b):
assert geom_equals(a, b)
if isinstance(b, GeoPandasBase):
right_df = True
else:
right_df = False
self._binary_op_test(op, expected, a, b, fcmp, True, right_df, *args, **kwargs)
def _test_binary_real(self, op, expected, a, b, *args, **kwargs):
fcmp = assert_series_equal
self._binary_op_test(op, expected, a, b, fcmp, True, False, *args, **kwargs)
def _binary_op_test(
self, op, expected, left, right, fcmp, left_df, right_df, *args, **kwargs
):
"""
This is a helper to call a function on GeoSeries and GeoDataFrame
arguments. For example, 'intersection' is a member of both GeoSeries
and GeoDataFrame and can take either GeoSeries or GeoDataFrame inputs.
This function has the ability to test all four combinations of input
types.
Parameters
----------
expected : str
The operation to be tested. e.g., 'intersection'
left: GeoSeries
right: GeoSeries
fcmp: function
Called with the result of the operation and expected. It should
assert if the result is incorrect
left_df: bool
If the left input should also be called with a GeoDataFrame
right_df: bool
Indicates whether the right input should be called with a
GeoDataFrame
"""
def _make_gdf(s):
n = len(s)
col1 = string.ascii_lowercase[:n]
col2 = range(n)
return GeoDataFrame(
{"geometry": s.values, "col1": col1, "col2": col2},
index=s.index,
crs=s.crs,
)
# Test GeoSeries.op(GeoSeries)
result = getattr(left, op)(right, *args, **kwargs)
fcmp(result, expected)
if left_df:
# Test GeoDataFrame.op(GeoSeries)
gdf_left = _make_gdf(left)
result = getattr(gdf_left, op)(right, *args, **kwargs)
fcmp(result, expected)
if right_df:
# Test GeoSeries.op(GeoDataFrame)
gdf_right = _make_gdf(right)
result = getattr(left, op)(gdf_right, *args, **kwargs)
fcmp(result, expected)
if left_df:
# Test GeoDataFrame.op(GeoDataFrame)
result = getattr(gdf_left, op)(gdf_right, *args, **kwargs)
fcmp(result, expected)
def _test_unary(self, op, expected, a, fcmp, method=False):
# GeoSeries, (GeoSeries or geometry)
if method:
result = getattr(a, op)()
else:
result = getattr(a, op)
fcmp(result, expected)
# GeoDataFrame, (GeoSeries or geometry)
gdf = self.gdf1.set_geometry(a)
if method:
result = getattr(gdf, op)()
else:
result = getattr(gdf, op)
fcmp(result, expected)
# TODO re-enable for all operations once we use pyproj > 2
# def test_crs_warning(self):
# # operations on geometries should warn for different CRS
# no_crs_g3 = self.g3.copy()
# no_crs_g3.crs = None
# with pytest.warns(UserWarning):
# self._test_binary_topological('intersection', self.g3,
# self.g3, no_crs_g3)
def test_alignment_warning(self):
with pytest.warns(
UserWarning,
match="The indices of the left and right GeoSeries' are not equal",
):
self.g0.intersection(self.g9, align=None)
with warnings.catch_warnings(record=True) as record:
self.g0.intersection(self.g9, align=True)
self.g0.intersection(self.g9, align=False)
assert len(record) == 0
def test_intersection(self):
self._test_binary_topological("intersection", self.t1, self.g1, self.g2)
self._test_binary_topological(
"intersection", self.all_none, self.g1, self.empty, align=True
)
assert len(self.g0.intersection(self.g9, align=True) == 8)
assert len(self.g0.intersection(self.g9, align=False) == 7)
def test_clip_by_rect(self):
self._test_binary_topological(
"clip_by_rect", self.g1, self.g10, *self.sq.bounds
)
# self.g1 and self.t3.bounds do not intersect
self._test_binary_topological(
"clip_by_rect", self.all_geometry_collection_empty, self.g1, *self.t3.bounds
)
def test_union_series(self):
self._test_binary_topological("union", self.sq, self.g1, self.g2)
assert len(self.g0.union(self.g9, align=True) == 8)
assert len(self.g0.union(self.g9, align=False) == 7)
def test_union_polygon(self):
self._test_binary_topological("union", self.sq, self.g1, self.t2)
def test_symmetric_difference_series(self):
self._test_binary_topological("symmetric_difference", self.sq, self.g3, self.g4)
assert len(self.g0.symmetric_difference(self.g9, align=True) == 8)
assert len(self.g0.symmetric_difference(self.g9, align=False) == 7)
def test_symmetric_difference_poly(self):
expected = GeoSeries([GeometryCollection(), self.sq], crs=self.g3.crs)
self._test_binary_topological(
"symmetric_difference", expected, self.g3, self.t1
)
def test_difference_series(self):
expected = GeoSeries([GeometryCollection(), self.t2])
self._test_binary_topological("difference", expected, self.g1, self.g2)
assert len(self.g0.difference(self.g9, align=True) == 8)
assert len(self.g0.difference(self.g9, align=False) == 7)
def test_difference_poly(self):
expected = GeoSeries([self.t1, self.t1])
self._test_binary_topological("difference", expected, self.g1, self.t2)
def test_shortest_line(self):
expected = GeoSeries([LineString([(1, 1), (5, 5)]), None])
assert_array_dtype_equal(expected, self.na_none.shortest_line(self.p0))
expected = GeoSeries(
[
LineString([(5, 5), (1, 1)]),
LineString([(2, 0), (2, 0)]),
]
)
assert_array_dtype_equal(expected, self.g6.shortest_line(self.g7))
expected = GeoSeries(
[LineString([(0.5, 0.5), (0.5, 0.5)]), LineString([(0.5, 0.5), (0.5, 0.5)])]
)
crossed_lines_inv = self.crossed_lines[::-1]
assert_array_dtype_equal(
expected, self.crossed_lines.shortest_line(crossed_lines_inv, align=False)
)
def test_snap(self):
expected = GeoSeries([Polygon([(0, 0.5), (1, 0), (1, 1), (0, 0.5)]), None])
assert_array_dtype_equal(
expected, self.na_none.snap(Point(0, 0.5), tolerance=1)
)
expected = GeoSeries(
[
Point((5, 5)),
Polygon([(0, 2), (0, 0), (3, 0), (3, 3), (0, 2)]),
]
)
assert_array_dtype_equal(expected, self.g6.snap(self.g7, tolerance=3))
def test_geo_op_empty_result(self):
l1 = LineString([(0, 0), (1, 1)])
l2 = LineString([(2, 2), (3, 3)])
expected = GeoSeries([GeometryCollection()])
# binary geo resulting in empty geometry
result = GeoSeries([l1]).intersection(l2)
assert_geoseries_equal(result, expected)
# binary geo empty result with right GeoSeries
result = GeoSeries([l1]).intersection(GeoSeries([l2]))
assert_geoseries_equal(result, expected)
# unary geo resulting in empty geometry
result = GeoSeries([GeometryCollection()]).convex_hull
assert_geoseries_equal(result, expected)
def test_boundary(self):
l1 = LineString([(0, 0), (1, 0), (1, 1), (0, 0)])
l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
expected = GeoSeries([l1, l2], index=self.g1.index, crs=self.g1.crs)
self._test_unary_topological("boundary", expected, self.g1)
def test_area(self):
expected = Series(np.array([0.5, 1.0]), index=self.g1.index)
self._test_unary_real("area", expected, self.g1)
expected = Series(np.array([0.5, np.nan]), index=self.na_none.index)
self._test_unary_real("area", expected, self.na_none)
@pytest.mark.skipif(not HAS_PYPROJ, reason="pyproj not available")
def test_area_crs_warn(self):
with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"):
self.g4.area
def test_bounds(self):
# Set columns to get the order right
expected = DataFrame(
{
"minx": [0.0, 0.0],
"miny": [0.0, 0.0],
"maxx": [1.0, 1.0],
"maxy": [1.0, 1.0],
},
index=self.g1.index,
columns=["minx", "miny", "maxx", "maxy"],
)
result = self.g1.bounds
assert_frame_equal(expected, result)
gdf = self.gdf1.set_geometry(self.g1)
result = gdf.bounds
assert_frame_equal(expected, result)
def test_bounds_empty(self):
# test bounds of empty GeoSeries
# https://github.com/geopandas/geopandas/issues/1195
s = GeoSeries([])
result = s.bounds
expected = DataFrame(
columns=["minx", "miny", "maxx", "maxy"], index=s.index, dtype="float64"
)
assert_frame_equal(result, expected)
def test_union_all(self):
p1 = self.t1
p2 = Polygon([(2, 0), (3, 0), (3, 1)])
expected = unary_union([p1, p2])
g = GeoSeries([p1, p2])
self._test_unary_topological("union_all", expected, g, method=True)
g2 = GeoSeries([p1, None])
self._test_unary_topological("union_all", p1, g2, method=True)
g3 = GeoSeries([None, None])
assert g3.union_all().equals(shapely.GeometryCollection())
assert g.union_all(method="coverage").equals(expected)
def test_unary_union_deprecated(self):
p1 = self.t1
p2 = Polygon([(2, 0), (3, 0), (3, 1)])
g = GeoSeries([p1, p2])
with pytest.warns(
FutureWarning, match="The 'unary_union' attribute is deprecated"
):
result = g.unary_union
assert result == g.union_all()
def test_intersection_all(self):
expected = Polygon([(1, 1), (1, 1.5), (1.5, 1.5), (1.5, 1), (1, 1)])
g = GeoSeries([box(0, 0, 2, 2), box(1, 1, 3, 3), box(0, 0, 1.5, 1.5)])
assert g.intersection_all().equals(expected)
g2 = GeoSeries([box(0, 0, 2, 2), None])
assert g2.intersection_all().equals(g2[0])
g3 = GeoSeries([None, None])
assert g3.intersection_all().equals(shapely.GeometryCollection())
def test_contains(self):
expected = [True, False, True, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.contains(self.t1))
expected = [False, True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.contains(self.g9, align=True))
expected = [False, False, True, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.contains(self.g9, align=False))
def test_contains_properly(self):
expected = [False, False, True, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.contains_properly(Point(0.25, 0.25)))
expected = [False, False, False, False, False, True, False, False]
assert_array_dtype_equal(
expected, self.g0.contains_properly(self.g9, align=True)
)
expected = [False, False, True, False, False, False, False]
assert_array_dtype_equal(
expected, self.g0.contains_properly(self.g9, align=False)
)
@pytest.mark.skipif(shapely.geos_version < (3, 10, 0), reason="requires GEOS>=3.10")
def test_dwithin(self):
expected = [True, True, True, False, True, True, False]
assert_array_dtype_equal(expected, self.g0.dwithin(self.p0, 6))
expected = [False, True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.dwithin(self.g9, 1, align=True))
expected = [True, True, True, True, False, False, False]
assert_array_dtype_equal(expected, self.g0.dwithin(self.g9, 1, align=False))
def test_length(self):
expected = Series(np.array([2 + np.sqrt(2), 4]), index=self.g1.index)
self._test_unary_real("length", expected, self.g1)
expected = Series(np.array([2 + np.sqrt(2), np.nan]), index=self.na_none.index)
self._test_unary_real("length", expected, self.na_none)
@pytest.mark.skipif(not HAS_PYPROJ, reason="pyproj not available")
def test_length_crs_warn(self):
with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"):
self.g4.length
def test_count_coordinates(self):
expected = Series(np.array([4, 5]), index=self.g1.index)
assert_series_equal(self.g1.count_coordinates(), expected, check_dtype=False)
expected = Series(np.array([4, 0]), index=self.na_none.index)
assert_series_equal(
self.na_none.count_coordinates(), expected, check_dtype=False
)
def test_count_geometries(self):
expected = Series(np.array([4, 2, 1, 1, 0]))
s = GeoSeries(
[
MultiPoint([(0, 0), (1, 1), (1, -1), (0, 1)]),
MultiLineString([((0, 0), (1, 1)), ((-1, 0), (1, 0))]),
LineString([(0, 0), (1, 1), (1, -1)]),
Point(0, 0),
None,
]
)
assert_series_equal(s.count_geometries(), expected, check_dtype=False)
def test_count_interior_rings(self):
expected = Series(np.array([1, 2, 0, 0]))
s = GeoSeries(
[
Polygon(
[(0, 0), (0, 5), (5, 5), (5, 0)],
[[(1, 1), (1, 4), (4, 4), (4, 1)]],
),
Polygon(
[(0, 0), (0, 5), (5, 5), (5, 0)],
[
[(1, 1), (1, 2), (2, 2), (2, 1)],
[(3, 2), (3, 3), (4, 3), (4, 2)],
],
),
Point(0, 1),
None,
]
)
assert_series_equal(s.count_interior_rings(), expected, check_dtype=False)
def test_crosses(self):
expected = [False, False, False, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.crosses(self.t1))
expected = [False, True]
assert_array_dtype_equal(expected, self.crossed_lines.crosses(self.l3))
expected = [False] * 8
assert_array_dtype_equal(expected, self.g0.crosses(self.g9, align=True))
expected = [False] * 7
assert_array_dtype_equal(expected, self.g0.crosses(self.g9, align=False))
def test_disjoint(self):
expected = [False, False, False, False, False, True, False]
assert_array_dtype_equal(expected, self.g0.disjoint(self.t1))
expected = [False] * 8
assert_array_dtype_equal(expected, self.g0.disjoint(self.g9, align=True))
expected = [False, False, False, False, True, False, False]
assert_array_dtype_equal(expected, self.g0.disjoint(self.g9, align=False))
def test_relate(self):
expected = Series(
[
"212101212",
"212101212",
"212FF1FF2",
"2FFF1FFF2",
"FF2F112F2",
"FF0FFF212",
None,
],
index=self.g0.index,
)
assert_array_dtype_equal(expected, self.g0.relate(self.inner_sq))
expected = Series(["FF0FFF212", None], index=self.g6.index)
assert_array_dtype_equal(expected, self.g6.relate(self.na_none))
expected = Series(
[
None,
"2FFF1FFF2",
"2FFF1FFF2",
"2FFF1FFF2",
"2FFF1FFF2",
"0FFFFFFF2",
None,
None,
],
index=range(8),
)
assert_array_dtype_equal(expected, self.g0.relate(self.g9, align=True))
expected = Series(
[
"FF2F11212",
"2FF11F212",
"212FF1FF2",
"FF2F1F212",
"FF2FF10F2",
None,
None,
],
index=self.g0.index,
)
assert_array_dtype_equal(expected, self.g0.relate(self.g9, align=False))
def test_relate_pattern(self):
expected = Series([True] * 4 + [False] * 3, index=self.g0.index, dtype=bool)
assert_array_dtype_equal(
expected, self.g0.relate_pattern(self.inner_sq, "2********")
)
expected = Series([True, False], index=self.g6.index, dtype=bool)
assert_array_dtype_equal(
expected, self.g6.relate_pattern(self.na_none, "FF0******")
)
expected = Series(
[False] + [True] * 5 + [False, False], index=range(8), dtype=bool
)
with pytest.warns(UserWarning, match="The indices of the left and right"):
assert_array_dtype_equal(
expected, self.g0.relate_pattern(self.g9, "T********", align=None)
)
expected = Series(
[False] + [True] * 2 + [False] * 4, index=self.g0.index, dtype=bool
)
assert_array_dtype_equal(
expected, self.g0.relate_pattern(self.g9, "T********", align=False)
)
def test_distance(self):
expected = Series(
np.array([np.sqrt((5 - 1) ** 2 + (5 - 1) ** 2), np.nan]), self.na_none.index
)
assert_array_dtype_equal(expected, self.na_none.distance(self.p0))
expected = Series(np.array([np.sqrt(4**2 + 4**2), np.nan]), self.g6.index)
assert_array_dtype_equal(expected, self.g6.distance(self.na_none))
expected = Series(np.array([np.nan, 0, 0, 0, 0, 0, np.nan, np.nan]), range(8))
assert_array_dtype_equal(expected, self.g0.distance(self.g9, align=True))
val = self.g0.iloc[4].distance(self.g9.iloc[4])
expected = Series(np.array([0, 0, 0, 0, val, np.nan, np.nan]), self.g0.index)
assert_array_dtype_equal(expected, self.g0.distance(self.g9, align=False))
@pytest.mark.skipif(not HAS_PYPROJ, reason="pyproj not available")
def test_distance_crs_warning(self):
with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"):
self.g4.distance(self.p0)
def test_hausdorff_distance(self):
# closest point is (0, 0) in self.p1
expected = Series(np.array([np.sqrt(5**2 + 5**2), np.nan]), self.na_none.index)
assert_array_dtype_equal(expected, self.na_none.hausdorff_distance(self.p0))
expected = Series(np.array([np.sqrt(5**2 + 5**2), np.nan]), self.na_none.index)
assert_array_dtype_equal(expected, self.na_none.hausdorff_distance(self.p0))
expected = Series(np.array([np.nan, 0, 0, 0, 0, 0, np.nan, np.nan]), range(8))
assert_array_dtype_equal(
expected, self.g0.hausdorff_distance(self.g9, align=True)
)
val_1 = self.g0.iloc[0].hausdorff_distance(self.g9.iloc[0])
val_2 = self.g0.iloc[2].hausdorff_distance(self.g9.iloc[2])
val_3 = self.g0.iloc[4].hausdorff_distance(self.g9.iloc[4])
expected = Series(
np.array([val_1, val_1, val_2, val_2, val_3, np.nan, np.nan]), self.g0.index
)
assert_array_dtype_equal(
expected, self.g0.hausdorff_distance(self.g9, align=False)
)
expected = Series(np.array([52.5]), self.g12.index)
assert_array_dtype_equal(
expected, self.g12.hausdorff_distance(self.g13, densify=0.25)
)
@pytest.mark.skipif(
shapely.geos_version < (3, 10, 0), reason="buggy with GEOS<3.10"
)
def test_frechet_distance(self):
# closest point is (0, 0) in self.p1
expected = Series(np.array([np.sqrt(5**2 + 5**2), np.nan]), self.na_none.index)
assert_array_dtype_equal(expected, self.na_none.frechet_distance(self.p0))
expected = Series(np.array([np.nan, 0, 0, 0, 0, 0, np.nan, np.nan]), range(8))
assert_array_dtype_equal(
expected, self.g0.frechet_distance(self.g9, align=True)
)
# expected returns
val_1 = 1.0
val_2 = np.sqrt(2) / 4
val_3 = np.sqrt(2) / 2
val_4 = (np.sqrt(2) / 2) * 10
expected = Series(
np.array([val_1, val_1, val_2, val_3, val_4, np.nan, np.nan]), self.g0.index
)
assert_array_dtype_equal(
expected, self.g0.frechet_distance(self.g9, align=False)
)
expected = Series(np.array([np.sqrt(100**2 + (100 - 5) ** 2)]), self.g12.index)
assert_array_dtype_equal(
expected, self.g12.frechet_distance(self.g13, densify=0.25)
)
def test_intersects(self):
expected = [True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.intersects(self.t1))
expected = [True, False]
assert_array_dtype_equal(expected, self.na_none.intersects(self.t2))
expected = np.array([], dtype=bool)
assert_array_dtype_equal(expected, self.empty.intersects(self.t1))
expected = np.array([], dtype=bool)
assert_array_dtype_equal(expected, self.empty.intersects(self.empty_poly))
expected = [False] * 7
assert_array_dtype_equal(expected, self.g0.intersects(self.empty_poly))
expected = [False, True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.intersects(self.g9, align=True))
expected = [True, True, True, True, False, False, False]
assert_array_dtype_equal(expected, self.g0.intersects(self.g9, align=False))
def test_overlaps(self):
expected = [True, True, False, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.overlaps(self.inner_sq))
expected = [False, False]
assert_array_dtype_equal(expected, self.g4.overlaps(self.t1))
expected = [False] * 8
assert_array_dtype_equal(expected, self.g0.overlaps(self.g9, align=True))
expected = [False] * 7
assert_array_dtype_equal(expected, self.g0.overlaps(self.g9, align=False))
def test_touches(self):
expected = [False, True, False, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.touches(self.t1))
expected = [False] * 8
assert_array_dtype_equal(expected, self.g0.touches(self.g9, align=True))
expected = [True, False, False, True, False, False, False]
assert_array_dtype_equal(expected, self.g0.touches(self.g9, align=False))
def test_within(self):
expected = [True, False, False, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.within(self.t1))
expected = [True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.within(self.sq))
expected = [False, True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.within(self.g9, align=True))
expected = [False, True, False, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.within(self.g9, align=False))
def test_covers_itself(self):
# Each polygon in a Series covers itself
res = self.g1.covers(self.g1)
exp = Series([True, True])
assert_series_equal(res, exp)
def test_covers(self):
res = self.g7.covers(self.g8)
exp = Series([True, False])
assert_series_equal(res, exp)
expected = [False, True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.covers(self.g9, align=True))
expected = [False, False, True, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.covers(self.g9, align=False))
def test_covers_inverse(self):
res = self.g8.covers(self.g7)
exp = Series([False, False])
assert_series_equal(res, exp)
def test_covered_by(self):
res = self.g1.covered_by(self.g1)
exp = Series([True, True])
assert_series_equal(res, exp)
expected = [False, True, True, True, True, True, False, False]
assert_array_dtype_equal(expected, self.g0.covered_by(self.g9, align=True))
expected = [False, True, False, False, False, False, False]
assert_array_dtype_equal(expected, self.g0.covered_by(self.g9, align=False))
def test_is_valid(self):
expected = Series(np.array([True] * len(self.g1)), self.g1.index)
self._test_unary_real("is_valid", expected, self.g1)
def test_is_empty(self):
expected = Series(np.array([False] * len(self.g1)), self.g1.index)
self._test_unary_real("is_empty", expected, self.g1)
def test_is_ring(self):
expected = Series(np.array([False] * len(self.g1)), self.g1.index)
self._test_unary_real("is_ring", expected, self.g1)
expected = Series(np.array([True] * len(self.g1)), self.g1.index)
self._test_unary_real("is_ring", expected, self.g1.exterior)
def test_is_simple(self):
expected = Series(np.array([True] * len(self.g1)), self.g1.index)
self._test_unary_real("is_simple", expected, self.g1)
def test_is_ccw(self):
expected = Series(np.array([False] * len(self.g1)), self.g1.index)
self._test_unary_real("is_ccw", expected, self.g1)
def test_is_closed(self):
expected = Series(np.array([False, False]), self.g5.index)
self._test_unary_real("is_closed", expected, self.g5)
def test_has_z(self):
expected = Series([False, True], self.g_3d.index)
self._test_unary_real("has_z", expected, self.g_3d)
def test_xyz_points(self):
expected_x = [-73.9847, -74.0446]
expected_y = [40.7484, 40.6893]
expected_z = [30.3244, 31.2344]
assert_array_dtype_equal(expected_x, self.landmarks.geometry.x)
assert_array_dtype_equal(expected_y, self.landmarks.geometry.y)
assert_array_dtype_equal(expected_z, self.landmarks.geometry.z)
# mixed dimensions
expected_z = [30.3244, 31.2344, np.nan]
assert_array_dtype_equal(expected_z, self.landmarks_mixed.geometry.z)
def test_xyz_points_empty(self):
expected_x = [-73.9847, -74.0446, -73.9847, np.nan]
expected_y = [40.7484, 40.6893, 40.7484, np.nan]
expected_z = [30.3244, 31.2344, np.nan, np.nan]
assert_array_dtype_equal(expected_x, self.landmarks_mixed_empty.geometry.x)
assert_array_dtype_equal(expected_y, self.landmarks_mixed_empty.geometry.y)
assert_array_dtype_equal(expected_z, self.landmarks_mixed_empty.geometry.z)
def test_xyz_polygons(self):
# accessing x attribute in polygon geoseries should raise an error
with pytest.raises(ValueError):
_ = self.gdf1.geometry.x
# and same for accessing y attribute in polygon geoseries
with pytest.raises(ValueError):
_ = self.gdf1.geometry.y
# and same for accessing z attribute in polygon geoseries
with pytest.raises(ValueError):
_ = self.gdfz.geometry.z
def test_centroid(self):
polygon = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)])
point = Point(0, 0)
polygons = GeoSeries([polygon for i in range(3)])
points = GeoSeries([point for i in range(3)])
assert_geoseries_equal(polygons.centroid, points)
@pytest.mark.skipif(not HAS_PYPROJ, reason="pyproj not available")
def test_centroid_crs_warn(self):
with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"):
self.g4.centroid
def test_normalize(self):
polygon = Polygon([(0, 0), (1, 1), (0, 1)])
linestring = LineString([(0, 0), (1, 1), (1, 0)])
point = Point(0, 0)
series = GeoSeries([polygon, linestring, point])
polygon2 = Polygon([(0, 0), (0, 1), (1, 1)])
expected = GeoSeries([polygon2, linestring, point])
assert_geoseries_equal(series.normalize(), expected)
def test_make_valid(self):
polygon1 = Polygon([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)])
polygon2 = Polygon([(0, 2), (0, 1), (2, 0), (0, 0), (0, 2)])
linestring = LineString([(0, 0), (1, 1), (1, 0)])
series = GeoSeries([polygon1, polygon2, linestring])
out_polygon1 = MultiPolygon(
[
Polygon([(1, 1), (0, 0), (0, 2), (1, 1)]),
Polygon([(2, 0), (1, 1), (2, 2), (2, 0)]),
]
)
out_polygon2 = GeometryCollection(
[Polygon([(2, 0), (0, 0), (0, 1), (2, 0)]), LineString([(0, 2), (0, 1)])]
)
expected = GeoSeries([out_polygon1, out_polygon2, linestring])
assert not series.is_valid.all()
result = series.make_valid()
assert_geoseries_equal(result, expected)
assert result.is_valid.all()
def test_reverse(self):
expected = GeoSeries(
[
LineString([(0, 0), (0, 1), (1, 1)]),
LineString([(0, 0), (1, 0), (1, 1), (0, 1)]),
]
)
assert_geoseries_equal(expected, self.g5.reverse())
@pytest.mark.skipif(shapely.geos_version < (3, 10, 0), reason="requires GEOS>=3.10")
def test_segmentize_linestrings(self):
expected_g1 = GeoSeries(
[
Polygon(
(
(
(0, 0),
(0.5, 0),
(1, 0),
(1, 0.5),
(1, 1),
(0.6666666666666666, 0.6666666666666666),
(0.3333333333333333, 0.3333333333333333),
(0, 0),
)
)
),
Polygon(
(
(