-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathtest_polygons.py
392 lines (326 loc) · 15 KB
/
test_polygons.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
import pytest
import pandas as pd
import numpy as np
from numpy import nan
import xarray as xr
import datashader as ds
from datashader.tests.test_pandas import assert_eq_ndarray, assert_eq_xr
import dask.dataframe as dd
try:
# Import to register extension arrays
import spatialpandas # noqa (register EAs)
from spatialpandas import GeoDataFrame
from spatialpandas.geometry import MultiPolygonArray
except ImportError:
spatialpandas = None
GeoDataFrame = None
MultiPolygonArray = None
try:
from geodatasets import get_path
import geopandas
except ImportError:
get_path = None
geopandas = None
try:
import dask_geopandas
except ImportError:
dask_geopandas = None
def dask_GeoDataFrame(*args, **kwargs):
return dd.from_pandas(GeoDataFrame(*args, **kwargs), npartitions=3)
DataFrames = [GeoDataFrame, dask_GeoDataFrame]
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
@pytest.mark.parametrize('DataFrame', DataFrames)
def test_multipolygon_manual_range(DataFrame):
df = DataFrame({
'polygons': pd.Series([[
[
[0, 0, 2, 0, 2, 2, 1, 3, 0, 0],
[1, 0.25, 1, 2, 1.75, .25, 0.25, 0.25]
], [
[2.5, 1, 4, 1, 4, 2, 2.5, 2, 2.5, 1]
],
]], dtype='MultiPolygon[float64]'),
'v': [1]
})
cvs = ds.Canvas(plot_width=16, plot_height=16)
agg = cvs.polygons(df, geometry='polygons', agg=ds.count())
axis = ds.core.LinearAxis()
lincoords_x = axis.compute_index(
axis.compute_scale_and_translate((0., 4.), 16), 16)
lincoords_y = axis.compute_index(
axis.compute_scale_and_translate((0., 3.), 16), 16)
sol = np.array([
[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
], dtype='i4')
out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
assert_eq_xr(agg, out)
assert_eq_ndarray(agg.x_range, (0, 4), close=True)
assert_eq_ndarray(agg.y_range, (0, 3), close=True)
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
@pytest.mark.parametrize('DataFrame', DataFrames)
def test_multiple_polygons_auto_range(DataFrame):
df = DataFrame({
'polygons': pd.Series([[
[
[0, 0, 2, 0, 2, 2, 1, 3, 0, 0],
[1, 0.25, 1, 2, 1.75, .25, 0.25, 0.25]
], [
[2.5, 1, 4, 1, 4, 2, 2.5, 2, 2.5, 1]
],
]], dtype='MultiPolygon[float64]'),
'v': [1]
})
cvs = ds.Canvas(plot_width=16, plot_height=16,
x_range=[-1, 3.5], y_range=[0.1, 2])
agg = cvs.polygons(df, geometry='polygons', agg=ds.count())
axis = ds.core.LinearAxis()
lincoords_x = axis.compute_index(
axis.compute_scale_and_translate((-1, 3.5), 16), 16)
lincoords_y = axis.compute_index(
axis.compute_scale_and_translate((0.1, 2), 16), 16)
sol = np.array([
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1]
], dtype='i4')
out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
assert_eq_xr(agg, out)
assert_eq_ndarray(agg.x_range, (-1, 3.5), close=True)
assert_eq_ndarray(agg.y_range, (0.1, 2), close=True)
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
@pytest.mark.parametrize('DataFrame', DataFrames)
def test_no_overlap(DataFrame):
df = DataFrame({
'polygons': pd.Series([
[
[1, 1, 2, 2, 1, 3, 0, 2, 1, 1],
[0.5, 1.5, 0.5, 2.5, 1.5, 2.5, 1.5, 1.5, 0.5, 1.5]
], [
[0.5, 1.5, 1.5, 1.5, 1.5, 2.5, 0.5, 2.5, 0.5, 1.5]
], [
[0, 1, 2, 1, 2, 3, 0, 3, 0, 1, 1, 1, 0, 2, 1, 3, 2, 2, 1, 1]
]
], dtype='Polygon[float64]'),
})
cvs = ds.Canvas(plot_width=16, plot_height=16)
agg = cvs.polygons(df, geometry='polygons', agg=ds.count())
axis = ds.core.LinearAxis()
lincoords_x = axis.compute_index(
axis.compute_scale_and_translate((0, 2), 16), 16)
lincoords_y = axis.compute_index(
axis.compute_scale_and_translate((1, 3), 16), 16)
sol = np.array([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
], dtype='i4')
out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
assert_eq_xr(agg, out)
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
@pytest.mark.parametrize('DataFrame', DataFrames)
def test_no_overlap_agg(DataFrame):
df = DataFrame({
'polygons': pd.Series([
[[1, 1, 2, 2, 1, 3, 0, 2, 1, 1],
[0.5, 1.5, 0.5, 2.5, 1.5, 2.5, 1.5, 1.5, 0.5, 1.5]],
[[0.5, 1.5, 1.5, 1.5, 1.5, 2.5, 0.5, 2.5, 0.5, 1.5]],
[[0, 1, 2, 1, 2, 3, 0, 3, 0, 1, 1, 1, 0, 2, 1, 3, 2, 2, 1, 1]]
], dtype='Polygon[float64]'),
'v': range(3)
})
cvs = ds.Canvas(plot_width=16, plot_height=16)
agg = cvs.polygons(df, geometry='polygons', agg=ds.sum('v'))
axis = ds.core.LinearAxis()
lincoords_x = axis.compute_index(
axis.compute_scale_and_translate((0, 2), 16), 16)
lincoords_y = axis.compute_index(
axis.compute_scale_and_translate((1, 3), 16), 16)
sol = np.array([
[2., 2., 2., 2., 2., 2., 2., 2., 0., 2., 2., 2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2., 2., 2., 0., 0., 0., 2., 2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 0., 0., 2., 2., 2., 2.],
[2., 2., 2., 2., 1., 1., 1., 1., 1., 1., 1., 1., 0., 2., 2., 2.],
[2., 2., 2., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 2., 2.],
[2., 2., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 2.],
[2., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0.],
[2., 0., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0.],
[2., 2., 0., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 2.],
[2., 2., 2., 0., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 2., 2.],
[2., 2., 2., 2., 1., 1., 1., 1., 1., 1., 1., 1., 0., 2., 2., 2.],
[2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 0., 0., 2., 2., 2., 2.],
[2., 2., 2., 2., 2., 2., 0., 0., 0., 0., 0., 2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2., 2., 2., 0., 0., 0., 2., 2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2., 2., 2., 2., 0., 2., 2., 2., 2., 2., 2., 2.]
])
out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
assert_eq_xr(agg, out)
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
@pytest.mark.parametrize('DataFrame', DataFrames)
@pytest.mark.parametrize('scale', [4, 100])
def test_multipolygon_subpixel_vertical(DataFrame, scale):
df = GeoDataFrame({
'geometry': MultiPolygonArray([[
[[0, 0, 1, 0, 1, 1, 0, 1, 0, 0]],
[[2, 0, 3, 0, 3, 1, 2, 1, 2, 0]],
]])
})
cvs = ds.Canvas(
plot_height=8, plot_width=8,
x_range=(0, 4),
y_range=(-2 * scale, 2 * scale)
)
agg = cvs.polygons(df, 'geometry', agg=ds.count())
sol = np.array([
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
], dtype=np.int32)
axis = ds.core.LinearAxis()
lincoords_x = axis.compute_index(
axis.compute_scale_and_translate((0, 4), 8), 8)
lincoords_y = axis.compute_index(
axis.compute_scale_and_translate((-2 * scale, 2 * scale), 8), 8)
out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
assert_eq_xr(agg, out)
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
@pytest.mark.parametrize('DataFrame', DataFrames)
@pytest.mark.parametrize('scale', [4, 100])
def test_multipolygon_subpixel_horizontal(DataFrame, scale):
df = GeoDataFrame({
'geometry': MultiPolygonArray([[
[[0, 0, 1, 0, 1, 1, 0, 1, 0, 0]],
[[0, 2, 1, 2, 1, 3, 0, 3, 0, 2]],
]])
})
cvs = ds.Canvas(
plot_height=8, plot_width=8,
x_range=(-2 * scale, 2 * scale),
y_range=(0, 4)
)
agg = cvs.polygons(df, 'geometry', agg=ds.count())
sol = np.array([
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
], dtype=np.int32)
axis = ds.core.LinearAxis()
lincoords_x = axis.compute_index(
axis.compute_scale_and_translate((-2 * scale, 2 * scale), 8), 8)
lincoords_y = axis.compute_index(
axis.compute_scale_and_translate((0, 4), 8), 8)
out = xr.DataArray(sol, coords=[lincoords_y, lincoords_x], dims=['y', 'x'])
assert_eq_xr(agg, out)
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
def test_spatial_index_not_dropped():
# Issue 1121
df = GeoDataFrame({
'some_geom': MultiPolygonArray([
[[[0, 0, 1, 0, 1, 1, 0, 1, 0, 0]]],
[[[0, 2, 1, 2, 1, 3, 0, 3, 0, 2]]],
]),
'other': [23, 45], # This column is not used and will be dropped.
})
assert df.some_geom.array._sindex is None
sindex = df.some_geom.array.sindex
assert sindex is not None
glyph = ds.glyphs.polygon.PolygonGeom('some_geom')
agg = ds.count()
df2, _ = ds.core._bypixel_sanitise(df, glyph, agg)
assert df2.columns == ['some_geom']
assert df2.some_geom.array._sindex == df.some_geom.array._sindex
natural_earth_sol = np.array([
[nan, 7, 7, 7, 7, 7, 0, 2, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, nan],
[nan, nan, nan, nan, 5, nan, 6, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
[nan, nan, nan, nan, nan, nan, 9, nan, nan, nan, nan, nan, nan, 10, nan, nan, nan, nan, 11, 12],
[nan, nan, nan, nan, nan, nan, 95, nan, nan, nan, nan, 112, nan, nan, nan, nan, 21, 21, 21, 13],
[ 17, nan, nan, nan, nan, nan, 95, 95, nan, nan, nan, 112, 20, nan, nan, nan, 31, 32, 34, 22],
[nan, nan, nan, nan, nan, nan, 95, nan, nan, 112, 112, 112, 112, nan, 44, 41, 50, 43, 37, nan],
[nan, 60, nan, nan, 95, 65, 54, nan, nan, 112, 112, 112, 112, nan, 112, 112, 63, nan, nan, nan],
[nan, nan, nan, 95, 95, 95, 74, nan, nan, nan, 72, 68, 112, 112, 112, 112, 112, 71, 73, nan],
[ 87, 82, 78, 95, 95, 88, 95, nan, nan, 80, 83, 112, 112, 112, 112, 112, 112, 112, nan, nan],
[ 94, nan, nan, 116, 118, 125, 125, 126, 126, nan, nan, 121, 122, 109, nan, 123, nan, 101, 106, 93],
])
@pytest.mark.skipif(not geopandas, reason="geopandas not installed")
def test_natural_earth_geopandas():
df = geopandas.read_file(get_path("naturalearth.land"))
df["col"] = np.arange(len(df))
canvas = ds.Canvas(plot_height=10, plot_width=20)
agg = canvas.polygons(source=df, geometry="geometry", agg=ds.max("col"))
assert_eq_ndarray(agg.data, natural_earth_sol)
@pytest.mark.skipif(not geopandas, reason="geopandas not installed")
@pytest.mark.skipif(not dask_geopandas, reason="dask_geopandas not installed")
@pytest.mark.parametrize('npartitions', [1, 2, 5])
def test_natural_earth_dask_geopandas(npartitions):
df = geopandas.read_file(get_path("naturalearth.land"))
df["col"] = np.arange(len(df))
df = dd.from_pandas(df, npartitions=npartitions)
assert df.npartitions == npartitions
df.calculate_spatial_partitions()
canvas = ds.Canvas(plot_height=10, plot_width=20)
agg = canvas.polygons(source=df, geometry="geometry", agg=ds.max("col"))
assert_eq_ndarray(agg.data, natural_earth_sol)
@pytest.mark.skipif(not geopandas, reason="geopandas not installed")
@pytest.mark.skipif(not spatialpandas, reason="spatialpandas not installed")
@pytest.mark.parametrize('npartitions', [0, 1, 2, 5])
def test_natural_earth_spatialpandas(npartitions):
df = geopandas.read_file(get_path("naturalearth.land"))
df["col"] = np.arange(len(df))
df = spatialpandas.GeoDataFrame(df)
if npartitions > 0:
df = dd.from_pandas(df, npartitions=npartitions)
assert df.npartitions == npartitions
canvas = ds.Canvas(plot_height=10, plot_width=20)
agg = canvas.polygons(source=df, geometry="geometry", agg=ds.max("col"))
assert_eq_ndarray(agg.data, natural_earth_sol)