/
test_geometry.py
285 lines (226 loc) · 11.7 KB
/
test_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
from __future__ import absolute_import
import numpy as np
import osgeo
import pytest
try:
import cPickle as pickle
except ImportError:
import pickle
from datacube.utils import geometry
def test_pickleable():
poly = geometry.polygon([(10, 20), (20, 20), (20, 10), (10, 20)], crs=geometry.CRS('EPSG:4326'))
pickled = pickle.dumps(poly, pickle.HIGHEST_PROTOCOL)
unpickled = pickle.loads(pickled)
assert poly == unpickled
def test_geobox_simple():
from affine import Affine
t = geometry.GeoBox(4000, 4000,
Affine(0.00025, 0.0, 151.0, 0.0, -0.00025, -29.0),
geometry.CRS('EPSG:4326'))
expect_lon = np.asarray([151.000125, 151.000375, 151.000625, 151.000875, 151.001125,
151.001375, 151.001625, 151.001875, 151.002125, 151.002375])
expect_lat = np.asarray([-29.000125, -29.000375, -29.000625, -29.000875, -29.001125,
-29.001375, -29.001625, -29.001875, -29.002125, -29.002375])
expect_resolution = np.asarray([-0.00025, 0.00025])
assert (np.abs(np.r_[t.resolution] - expect_resolution) < 1e-6).all()
assert t.coordinates['latitude'].values.shape == (4000,)
assert t.coordinates['longitude'].values.shape == (4000,)
assert (np.abs(t.coords['latitude'].values[:10] - expect_lat) < 1e-6).all()
assert (np.abs(t.coords['longitude'].values[:10] - expect_lon) < 1e-6).all()
def test_props():
box1 = geometry.box(10, 10, 30, 30, crs=geometry.CRS('EPSG:4326'))
assert box1
assert box1.is_valid
assert not box1.is_empty
assert box1.area == 400.0
assert box1.boundary.length == 80.0
assert box1.centroid == geometry.point(20, 20, geometry.CRS('EPSG:4326'))
triangle = geometry.polygon([(10, 20), (20, 20), (20, 10), (10, 20)], crs=geometry.CRS('EPSG:4326'))
assert triangle.envelope == geometry.BoundingBox(10, 10, 20, 20)
outer = next(iter(box1))
assert outer.length == 80.0
box1copy = geometry.box(10, 10, 30, 30, crs=geometry.CRS('EPSG:4326'))
assert box1 == box1copy
assert box1.convex_hull == box1copy # NOTE: this might fail because of point order
box2 = geometry.box(20, 10, 40, 30, crs=geometry.CRS('EPSG:4326'))
assert box1 != box2
def test_tests():
box1 = geometry.box(10, 10, 30, 30, crs=geometry.CRS('EPSG:4326'))
box2 = geometry.box(20, 10, 40, 30, crs=geometry.CRS('EPSG:4326'))
box3 = geometry.box(30, 10, 50, 30, crs=geometry.CRS('EPSG:4326'))
box4 = geometry.box(40, 10, 60, 30, crs=geometry.CRS('EPSG:4326'))
minibox = geometry.box(15, 15, 25, 25, crs=geometry.CRS('EPSG:4326'))
assert not box1.touches(box2)
assert box1.touches(box3)
assert not box1.touches(box4)
assert box1.intersects(box2)
assert box1.intersects(box3)
assert not box1.intersects(box4)
assert not box1.crosses(box2)
assert not box1.crosses(box3)
assert not box1.crosses(box4)
assert not box1.disjoint(box2)
assert not box1.disjoint(box3)
assert box1.disjoint(box4)
assert box1.contains(minibox)
assert not box1.contains(box2)
assert not box1.contains(box3)
assert not box1.contains(box4)
assert minibox.within(box1)
assert not box1.within(box2)
assert not box1.within(box3)
assert not box1.within(box4)
def test_ops():
box1 = geometry.box(10, 10, 30, 30, crs=geometry.CRS('EPSG:4326'))
box2 = geometry.box(20, 10, 40, 30, crs=geometry.CRS('EPSG:4326'))
box4 = geometry.box(40, 10, 60, 30, crs=geometry.CRS('EPSG:4326'))
union1 = box1.union(box2)
assert union1.area == 600.0
inter1 = box1.intersection(box2)
assert bool(inter1)
assert inter1.area == 200.0
inter2 = box1.intersection(box4)
assert not bool(inter2)
assert inter2.is_empty
# assert not inter2.is_valid TODO: what's going on here?
diff1 = box1.difference(box2)
assert diff1.area == 200.0
symdiff1 = box1.symmetric_difference(box2)
assert symdiff1.area == 400.0
def test_unary_union():
box1 = geometry.box(10, 10, 30, 30, crs=geometry.CRS('EPSG:4326'))
box2 = geometry.box(20, 10, 40, 30, crs=geometry.CRS('EPSG:4326'))
box3 = geometry.box(30, 10, 50, 30, crs=geometry.CRS('EPSG:4326'))
box4 = geometry.box(40, 10, 60, 30, crs=geometry.CRS('EPSG:4326'))
union0 = geometry.unary_union([box1])
assert union0 == box1
union1 = geometry.unary_union([box1, box4])
assert union1.type == 'MultiPolygon'
assert union1.area == 2.0*box1.area
union2 = geometry.unary_union([box1, box2])
assert union2.type == 'Polygon'
assert union2.area == 1.5*box1.area
union3 = geometry.unary_union([box1, box2, box3, box4])
assert union3.type == 'Polygon'
assert union3.area == 2.5*box1.area
union4 = geometry.unary_union([union1, box2, box3])
assert union4.type == 'Polygon'
assert union4.area == 2.5*box1.area
def test_unary_intersection():
box1 = geometry.box(10, 10, 30, 30, crs=geometry.CRS('EPSG:4326'))
box2 = geometry.box(15, 10, 35, 30, crs=geometry.CRS('EPSG:4326'))
box3 = geometry.box(20, 10, 40, 30, crs=geometry.CRS('EPSG:4326'))
box4 = geometry.box(25, 10, 45, 30, crs=geometry.CRS('EPSG:4326'))
box5 = geometry.box(30, 10, 50, 30, crs=geometry.CRS('EPSG:4326'))
box6 = geometry.box(35, 10, 55, 30, crs=geometry.CRS('EPSG:4326'))
inter1 = geometry.unary_intersection([box1])
assert bool(inter1)
assert inter1 == box1
inter2 = geometry.unary_intersection([box1, box2])
assert bool(inter2)
assert inter2.area == 300.0
inter3 = geometry.unary_intersection([box1, box2, box3])
assert bool(inter3)
assert inter3.area == 200.0
inter4 = geometry.unary_intersection([box1, box2, box3, box4])
assert bool(inter4)
assert inter4.area == 100.0
inter5 = geometry.unary_intersection([box1, box2, box3, box4, box5])
assert bool(inter5)
assert inter5.type == 'LineString'
assert inter5.length == 20.0
inter6 = geometry.unary_intersection([box1, box2, box3, box4, box5, box6])
assert not bool(inter6)
assert inter6.is_empty
def test_crs_equality():
a = geometry.CRS("""PROJCS["unnamed",GEOGCS["Unknown datum based upon the custom spheroid",
DATUM["Not specified (based on custom spheroid)",SPHEROID["Custom spheroid",6371007.181,0]],
PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Sinusoidal"],
PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],
PARAMETER["false_northing",0],UNIT["Meter",1]]""")
b = geometry.CRS("""PROJCS["unnamed",GEOGCS["unnamed ellipse",DATUM["unknown",SPHEROID["unnamed",6371007.181,0]],
PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Sinusoidal"],
PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],
PARAMETER["false_northing",0],UNIT["Meter",1]]""")
c = geometry.CRS('+a=6371007.181 +b=6371007.181 +units=m +y_0=0 +proj=sinu +lon_0=0 +no_defs +x_0=0')
assert a == b
assert a == c
assert b == c
assert a != geometry.CRS('EPSG:4326')
a = geometry.CRS("""GEOGCS["GEOCENTRIC DATUM of AUSTRALIA",DATUM["GDA94",SPHEROID["GRS80",6378137,298.257222101]],
PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]""")
b = geometry.CRS("""GEOGCS["GRS 1980(IUGG, 1980)",DATUM["unknown",SPHEROID["GRS80",6378137,298.257222101]],
PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]""")
c = geometry.CRS('+proj=longlat +no_defs +ellps=GRS80')
assert a == b
assert a == c
assert b == c
def test_geobox():
points_list = [
[(148.2697, -35.20111), (149.31254, -35.20111), (149.31254, -36.331431), (148.2697, -36.331431)],
[(148.2697, 35.20111), (149.31254, 35.20111), (149.31254, 36.331431), (148.2697, 36.331431)],
[(-148.2697, 35.20111), (-149.31254, 35.20111), (-149.31254, 36.331431), (-148.2697, 36.331431)],
[(-148.2697, -35.20111), (-149.31254, -35.20111), (-149.31254, -36.331431), (-148.2697, -36.331431),
(148.2697, -35.20111)],
]
for points in points_list:
polygon = geometry.polygon(points, crs=geometry.CRS('EPSG:3577'))
resolution = (-25, 25)
geobox = geometry.GeoBox.from_geopolygon(polygon, resolution)
assert abs(resolution[0]) > abs(geobox.extent.boundingbox.left - polygon.boundingbox.left)
assert abs(resolution[0]) > abs(geobox.extent.boundingbox.right - polygon.boundingbox.right)
assert abs(resolution[1]) > abs(geobox.extent.boundingbox.top - polygon.boundingbox.top)
assert abs(resolution[1]) > abs(geobox.extent.boundingbox.bottom - polygon.boundingbox.bottom)
@pytest.mark.skipif(tuple(int(i) for i in osgeo.__version__.split('.')) < (2, 2),
reason='Fails under GDAL 2.1')
def test_wrap_dateline():
sinus_crs = geometry.CRS("""PROJCS["unnamed",
GEOGCS["Unknown datum based upon the custom spheroid",
DATUM["Not specified (based on custom spheroid)", SPHEROID["Custom spheroid",6371007.181,0]],
PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],
PROJECTION["Sinusoidal"],
PARAMETER["longitude_of_center",0],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["Meter",1]]""")
albers_crs = geometry.CRS('EPSG:3577')
geog_crs = geometry.CRS('EPSG:4326')
wrap = geometry.polygon([(12231455.716333, -5559752.598333),
(12231455.716333, -4447802.078667),
(13343406.236, -4447802.078667),
(13343406.236, -5559752.598333),
(12231455.716333, -5559752.598333)], crs=sinus_crs)
wrapped = wrap.to_crs(geog_crs)
assert wrapped.type == 'Polygon'
wrapped = wrap.to_crs(geog_crs, wrapdateline=True)
# assert wrapped.type == 'MultiPolygon' TODO: these cases are quite hard to implement.
# hopefully GDAL's CutGeometryOnDateLineAndAddToMulti will be available through py API at some point
wrap = geometry.polygon([(13343406.236, -5559752.598333),
(13343406.236, -4447802.078667),
(14455356.755667, -4447802.078667),
(14455356.755667, -5559752.598333),
(13343406.236, -5559752.598333)], crs=sinus_crs)
wrapped = wrap.to_crs(geog_crs)
assert wrapped.type == 'Polygon'
wrapped = wrap.to_crs(geog_crs, wrapdateline=True)
# assert wrapped.type == 'MultiPolygon' TODO: same as above
wrap = geometry.polygon([(14455356.755667, -5559752.598333),
(14455356.755667, -4447802.078667),
(15567307.275333, -4447802.078667),
(15567307.275333, -5559752.598333),
(14455356.755667, -5559752.598333)], crs=sinus_crs)
wrapped = wrap.to_crs(geog_crs)
assert wrapped.type == 'Polygon'
wrapped = wrap.to_crs(geog_crs, wrapdateline=True)
# assert wrapped.type == 'MultiPolygon' TODO: same as above
wrap = geometry.polygon([(3658653.1976781483, -4995675.379595791),
(4025493.916030875, -3947239.249752495),
(4912789.243100313, -4297237.125269571),
(4465089.861944263, -5313778.16975072),
(3658653.1976781483, -4995675.379595791)], crs=albers_crs)
wrapped = wrap.to_crs(geog_crs)
assert wrapped.type == 'Polygon'
assert wrapped.intersects(geometry.line([(0, -90), (0, 90)], crs=geog_crs))
wrapped = wrap.to_crs(geog_crs, wrapdateline=True)
assert wrapped.type == 'MultiPolygon'
assert not wrapped.intersects(geometry.line([(0, -90), (0, 90)], crs=geog_crs))