-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathtest_geopandas.py
More file actions
234 lines (204 loc) · 9.93 KB
/
Copy pathtest_geopandas.py
File metadata and controls
234 lines (204 loc) · 9.93 KB
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
"""
Test for the GeoPandasInterface
"""
import numpy as np
import pandas as pd
from shapely import geometry as sgeom
try:
import geopandas
from geopandas.array import GeometryDtype
except ImportError:
geopandas = None
import pytest
from holoviews import render
from holoviews.core.data import Dataset
from holoviews.core.data.interface import DataError
from holoviews.element import Path, Points, Polygons
from holoviews.testing import assert_data_equal, assert_element_equal
from holoviews.tests.core.data.test_multiinterface import GeomTests
from geoviews.data import GeoPandasInterface
from .test_multigeometry import GeomInterfaceTest
class RoundTripTests:
datatype = None
interface = None
__test__ = False
def test_point_roundtrip(self):
points = Points([{'x': 0, 'y': 1, 'z': 0},
{'x': 1, 'y': 0, 'z': 1}], ['x', 'y'],
'z', datatype=[self.datatype])
assert isinstance(points.data.geometry.dtype, GeometryDtype)
roundtrip = points.clone(datatype=['multitabular'])
assert roundtrip.interface.datatype == 'multitabular'
expected = Points([{'x': 0, 'y': 1, 'z': 0},
{'x': 1, 'y': 0, 'z': 1}], ['x', 'y'],
'z', datatype=['multitabular'])
assert_element_equal(roundtrip, expected)
def test_multi_point_roundtrip(self):
xs = [1, 2, 3, 2]
ys = [2, 0, 7, 4]
points = Points([{'x': xs, 'y': ys, 'z': 0},
{'x': xs[::-1], 'y': ys[::-1], 'z': 1}],
['x', 'y'], 'z', datatype=[self.datatype])
assert isinstance(points.data.geometry.dtype, GeometryDtype)
roundtrip = points.clone(datatype=['multitabular'])
assert roundtrip.interface.datatype == 'multitabular'
expected = Points([{'x': xs, 'y': ys, 'z': 0},
{'x': xs[::-1], 'y': ys[::-1], 'z': 1}],
['x', 'y'], 'z', datatype=['multitabular'])
assert_element_equal(roundtrip, expected)
def test_line_roundtrip(self):
xs = [1, 2, 3]
ys = [2, 0, 7]
path = Path([{'x': xs, 'y': ys, 'z': 1},
{'x': xs[::-1], 'y': ys[::-1], 'z': 2}],
['x', 'y'], 'z', datatype=[self.datatype])
assert isinstance(path.data.geometry.dtype, GeometryDtype)
roundtrip = path.clone(datatype=['multitabular'])
assert roundtrip.interface.datatype == 'multitabular'
expected = Path([{'x': xs, 'y': ys, 'z': 1},
{'x': xs[::-1], 'y': ys[::-1], 'z': 2}],
['x', 'y'], 'z', datatype=['multitabular'])
assert_element_equal(roundtrip, expected)
def test_multi_line_roundtrip(self):
xs = [1, 2, 3, np.nan, 6, 7, 3]
ys = [2, 0, 7, np.nan, 7, 5, 2]
path = Path([{'x': xs, 'y': ys, 'z': 0},
{'x': xs[::-1], 'y': ys[::-1], 'z': 1}],
['x', 'y'], 'z', datatype=[self.datatype])
assert isinstance(path.data.geometry.dtype, GeometryDtype)
roundtrip = path.clone(datatype=['multitabular'])
assert roundtrip.interface.datatype == 'multitabular'
expected = Path([{'x': xs, 'y': ys, 'z': 0},
{'x': xs[::-1], 'y': ys[::-1], 'z': 1}],
['x', 'y'], 'z', datatype=['multitabular'])
assert_element_equal(roundtrip, expected)
def test_polygon_roundtrip(self):
xs = [1, 2, 3]
ys = [2, 0, 7]
poly = Polygons([{'x': xs, 'y': ys, 'z': 0},
{'x': xs[::-1], 'y': ys[::-1], 'z': 1}],
['x', 'y'], 'z', datatype=[self.datatype])
assert isinstance(poly.data.geometry.dtype, GeometryDtype)
roundtrip = poly.clone(datatype=['multitabular'])
assert roundtrip.interface.datatype == 'multitabular'
expected = Polygons([{'x': xs+[1], 'y': ys+[2], 'z': 0},
{'x': xs[::-1]+[3], 'y': ys[::-1]+[7], 'z': 1}],
['x', 'y'], 'z', datatype=['multitabular'])
assert_element_equal(roundtrip, expected)
def test_multi_polygon_roundtrip(self):
xs = [1, 2, 3, np.nan, 6, 7, 3]
ys = [2, 0, 7, np.nan, 7, 5, 2]
holes = [
[[(1.5, 2), (2, 3), (1.6, 1.6)], [(2.1, 4.5), (2.5, 5), (2.3, 3.5)]],
[]
]
poly = Polygons([{'x': xs, 'y': ys, 'holes': holes, 'z': 1},
{'x': xs[::-1], 'y': ys[::-1], 'z': 2}],
['x', 'y'], 'z', datatype=[self.datatype])
assert isinstance(poly.data.geometry.dtype, GeometryDtype)
roundtrip = poly.clone(datatype=['multitabular'])
assert roundtrip.interface.datatype == 'multitabular'
expected = Polygons([{'x': [1, 2, 3, 1, np.nan, 6, 7, 3, 6],
'y': [2, 0, 7, 2, np.nan, 7, 5, 2, 7], 'holes': holes, 'z': 1},
{'x': [3, 7, 6, 3, np.nan, 3, 2, 1, 3],
'y': [2, 5, 7, 2, np.nan, 7, 0, 2, 7], 'z': 2}],
['x', 'y'], 'z', datatype=['multitabular'])
assert_element_equal(roundtrip, expected)
class GeoPandasInterfaceTest(GeomInterfaceTest, GeomTests, RoundTripTests):
"""
Test of the GeoPandasInterface.
"""
datatype = 'geodataframe'
interface = GeoPandasInterface
__test__ = True
def setup_method(self):
if geopandas is None:
pytest.skip('GeoPandasInterface requires geopandas, skipping tests')
super().setup_method()
def test_df_dataset(self):
if not pd:
pytest.skip('Pandas not available')
dfs = [pd.DataFrame(np.column_stack([np.arange(i, i+2), np.arange(i, i+2)]), columns=['x', 'y'])
for i in range(2)]
mds = Path(dfs, kdims=['x', 'y'], datatype=[self.datatype])
assert mds.interface is self.interface
for i, ds in enumerate(mds.split(datatype='dataframe')):
ds['x'] = ds.x.astype(int)
ds['y'] = ds.y.astype(int)
np.testing.assert_array_equal(ds, dfs[i])
def test_multi_geom_point_coord_values(self):
geoms = [{'geometry': sgeom.Point([(0, 1)])},
{'geometry': sgeom.Point([(3, 5)])}]
mds = Dataset(geoms, kdims=['x', 'y'], datatype=[self.datatype])
np.testing.assert_array_equal(mds.dimension_values('x'), np.array([0, 3]))
np.testing.assert_array_equal(mds.dimension_values('y'), np.array([1, 5]))
def test_multi_geom_point_length(self):
geoms = [{'geometry': sgeom.Point([(0, 0)])},
{'geometry': sgeom.Point([(3, 3)])}]
mds = Dataset(geoms, kdims=['x', 'y'], datatype=[self.datatype])
assert len(mds) == 2
def test_array_points_iloc_index_rows_index_cols(self):
arrays = [np.array([(1+i, i), (2+i, i), (3+i, i)]) for i in range(2)]
mds = Dataset(arrays, kdims=['x', 'y'], datatype=[self.datatype])
assert mds.interface is self.interface
with pytest.raises(DataError):
mds.iloc[3, 0]
def test_polygon_dtype(self):
poly = Polygons([{'x': [1, 2, 3], 'y': [2, 0, 7]}], datatype=[self.datatype])
assert poly.interface is self.interface
assert poly.interface.dtype(poly, 'x') == 'float64'
def test_geometry_column_not_named_geometry(self):
# The geodataframe has its geometry column not named 'geometry'
gdf = geopandas.GeoDataFrame(
{
'v': [1, 2],
'not geometry': [sgeom.Point(0, 1), sgeom.Point(1, 2)],
},
geometry='not geometry',
)
ds = Dataset(gdf, kdims=['Longitude', 'Latitude'], datatype=[self.datatype])
assert_data_equal(ds.dimension_values('Longitude'), np.array([0, 1]))
assert_data_equal(ds.dimension_values('Latitude'), np.array([1, 2]))
def test_geometry_column_not_named_geometry_and_additional_geometry_column(self):
gdf = geopandas.GeoDataFrame(
{
'v': [1, 2],
'not geometry': [sgeom.Point(0, 1), sgeom.Point(1, 2)],
},
geometry='not geometry',
)
# The geodataframe contains a column called 'geometry' that doesn't contain geometry data.
gdf = gdf.rename(columns={'v': 'geometry'})
ds = Dataset(gdf, kdims=['Longitude', 'Latitude'], datatype=[self.datatype])
assert_data_equal(ds.dimension_values('Longitude'), np.array([0, 1]))
assert_data_equal(ds.dimension_values('Latitude'), np.array([1, 2]))
def test_geopandas_dataframe_with_different_dtype_column(self):
# Fix for https://github.com/holoviz/geoviews/issues/721
df = pd.DataFrame(
{
"x": [1, 2, 3, 4, 5],
"y": [1, 2, 3, 4, 5],
"value": [5, '4', 3, 2, 1],
}
)
gdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.x, df.y))
render(Points(gdf))
def test_geopandas_vdim_with_custom_label_resolved_by_name(self):
# Fix for https://github.com/holoviz/geoviews/issues/840
# A dim() expression carries a Dimension whose label is the column
# name, which must still resolve against a vdim with a custom label.
from holoviews import dim
gdf = geopandas.GeoDataFrame(
{"status": ["safe", "endangered", "safe"]},
geometry=geopandas.points_from_xy([0, 1, 2], [0, 1, 2]),
)
points = Points(gdf, vdims=[("status", "Conservation Status")])
probe = dim("status").dimension
assert self.interface.isscalar(points, probe, per_geom=True)
assert_data_equal(
self.interface.values(points, probe),
np.array(["safe", "endangered", "safe"], dtype=object),
)
assert self.interface.range(points, probe) == ("endangered", "safe")
color = dim("status").categorize({"safe": "green", "endangered": "red"})
render(points.opts(color=color))