|
12 | 12 | from branca.element import Element |
13 | 13 |
|
14 | 14 | import folium |
15 | | -from folium import Map, Popup |
| 15 | +from folium import Map, Popup, GeoJson |
16 | 16 |
|
17 | 17 | import pytest |
18 | 18 |
|
@@ -213,3 +213,72 @@ def test_geojson_tooltip(): |
213 | 213 | warnings.simplefilter('always') |
214 | 214 | m._repr_html_() |
215 | 215 | assert issubclass(w[-1].category, UserWarning), 'GeoJsonTooltip GeometryCollection test failed.' |
| 216 | + |
| 217 | + |
| 218 | +def test_geojson_search_properties(): |
| 219 | + features = [{'properties': None} for _ in range(3)] |
| 220 | + assert GeoJson._search_properties(features, 'properties') is None |
| 221 | + features = [{'properties': {'hi': 'there'}}, |
| 222 | + {'properties': {'hi': 'what'}}] |
| 223 | + assert GeoJson._search_properties(features, 'properties') == 'feature.properties.hi' |
| 224 | + features = [{'properties': {'hi': {'more': 'some value'}}}, |
| 225 | + {'properties': {'hi': {'more': 'another value'}}}] |
| 226 | + assert (GeoJson._search_properties(features, 'properties') |
| 227 | + == 'feature.properties.hi.more') |
| 228 | + features = [{'properties': {'hi': 'there'}}, |
| 229 | + {'properties': {'hi': 'there'}}] |
| 230 | + assert GeoJson._search_properties(features, 'properties') is None |
| 231 | + features = [{'properties': {'hi': 'there'}}, |
| 232 | + {'properties': {'hi': None}}] |
| 233 | + assert GeoJson._search_properties(features, 'properties') is None |
| 234 | + features = [{'properties': {'hi': 'there'}}, |
| 235 | + {'properties': 42}] |
| 236 | + assert GeoJson._search_properties(features, 'properties') is None |
| 237 | + features = [{'properties': [42, 43]}, |
| 238 | + {'properties': [1, 2]}] |
| 239 | + assert GeoJson._search_properties(features, 'properties') is None |
| 240 | + |
| 241 | + |
| 242 | +def test_geojson_find_identifier(): |
| 243 | + |
| 244 | + def _create(properties): |
| 245 | + return {"type": "FeatureCollection", "features": [ |
| 246 | + {"type": "Feature", |
| 247 | + "properties": properties} |
| 248 | + ]} |
| 249 | + |
| 250 | + data_bare = _create(None) |
| 251 | + data_with_id = _create(None) |
| 252 | + data_with_id['features'][0]['id'] = 'this-is-an-id' |
| 253 | + data_with_unique_property = _create({ |
| 254 | + 'property-key': 'some-value', |
| 255 | + }) |
| 256 | + data_with_nested_properties = _create({ |
| 257 | + "summary": {"distance": 343.2}, |
| 258 | + "way_points": [3, 5], |
| 259 | + }) |
| 260 | + data_with_incompatible_properties = _create({ |
| 261 | + "summary": {"distances": [0, 6], "durations": None}, |
| 262 | + "way_points": [3, 5], |
| 263 | + }) |
| 264 | + |
| 265 | + geojson = GeoJson(data_with_id) |
| 266 | + assert geojson.find_identifier() == 'feature.id' |
| 267 | + geojson = GeoJson(data_bare) |
| 268 | + assert geojson.find_identifier() == 'feature.id' |
| 269 | + assert geojson.data['features'][0]['id'] == '0' # the id got added |
| 270 | + geojson = GeoJson(data_with_unique_property) |
| 271 | + assert geojson.find_identifier() == 'feature.properties.property-key' |
| 272 | + geojson = GeoJson(data_with_nested_properties) |
| 273 | + assert geojson.find_identifier() == 'feature.properties.summary.distance' |
| 274 | + geojson = GeoJson(data_with_incompatible_properties) |
| 275 | + assert geojson.find_identifier() == 'feature.id' |
| 276 | + assert geojson.data['features'][0]['id'] == '0' # the id got added |
| 277 | + |
| 278 | + data_loose_geometry = {"type": "LineString", "coordinates": [ |
| 279 | + [3.961389, 43.583333], [3.968056, 43.580833], [3.974722, 43.578333], |
| 280 | + [3.986389, 43.575278], [3.998333, 43.5725], [4.163333, 43.530556], |
| 281 | + ]} |
| 282 | + geojson = GeoJson(data_loose_geometry) |
| 283 | + geojson.convert_to_feature_collection() |
| 284 | + assert geojson.find_identifier() == 'feature.id' # id got added |
0 commit comments