|
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,93 @@ 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_find_identifier(): |
| 219 | + |
| 220 | + def _create(*properties): |
| 221 | + return {"type": "FeatureCollection", "features": [ |
| 222 | + {"type": "Feature", "properties": item} |
| 223 | + for item in properties]} |
| 224 | + |
| 225 | + def _assert_id_got_added(data): |
| 226 | + _geojson = GeoJson(data) |
| 227 | + assert _geojson.find_identifier() == 'feature.id' |
| 228 | + assert _geojson.data['features'][0]['id'] == '0' |
| 229 | + |
| 230 | + data_with_id = _create(None, None) |
| 231 | + data_with_id['features'][0]['id'] = 'this-is-an-id' |
| 232 | + data_with_id['features'][1]['id'] = 'this-is-another-id' |
| 233 | + geojson = GeoJson(data_with_id) |
| 234 | + assert geojson.find_identifier() == 'feature.id' |
| 235 | + assert geojson.data['features'][0]['id'] == 'this-is-an-id' |
| 236 | + |
| 237 | + data_with_unique_properties = _create( |
| 238 | + {'property-key': 'some-value'}, |
| 239 | + {'property-key': 'another-value'}, |
| 240 | + ) |
| 241 | + geojson = GeoJson(data_with_unique_properties) |
| 242 | + assert geojson.find_identifier() == 'feature.properties.property-key' |
| 243 | + |
| 244 | + data_with_unique_properties = _create( |
| 245 | + {'property-key': 42}, |
| 246 | + {'property-key': 43}, |
| 247 | + {'property-key': 'or a string'}, |
| 248 | + ) |
| 249 | + geojson = GeoJson(data_with_unique_properties) |
| 250 | + assert geojson.find_identifier() == 'feature.properties.property-key' |
| 251 | + |
| 252 | + # The test cases below have no id field or unique property, |
| 253 | + # so an id will be added to the data. |
| 254 | + |
| 255 | + data_with_identical_ids = _create(None, None) |
| 256 | + data_with_identical_ids['features'][0]['id'] = 'identical-ids' |
| 257 | + data_with_identical_ids['features'][1]['id'] = 'identical-ids' |
| 258 | + _assert_id_got_added(data_with_identical_ids) |
| 259 | + |
| 260 | + data_with_some_missing_ids = _create(None, None) |
| 261 | + data_with_some_missing_ids['features'][0]['id'] = 'this-is-an-id' |
| 262 | + # the second feature doesn't have an id |
| 263 | + _assert_id_got_added(data_with_some_missing_ids) |
| 264 | + |
| 265 | + data_with_identical_properties = _create( |
| 266 | + {'property-key': 'identical-value'}, |
| 267 | + {'property-key': 'identical-value'}, |
| 268 | + ) |
| 269 | + _assert_id_got_added(data_with_identical_properties) |
| 270 | + |
| 271 | + data_bare = _create(None) |
| 272 | + _assert_id_got_added(data_bare) |
| 273 | + |
| 274 | + data_empty_dict = _create({}) |
| 275 | + _assert_id_got_added(data_empty_dict) |
| 276 | + |
| 277 | + data_without_properties = _create(None) |
| 278 | + del data_without_properties['features'][0]['properties'] |
| 279 | + _assert_id_got_added(data_without_properties) |
| 280 | + |
| 281 | + data_some_without_properties = _create({'key': 'value'}, 'will be deleted') |
| 282 | + # the first feature has properties, but the second doesn't |
| 283 | + del data_some_without_properties['features'][1]['properties'] |
| 284 | + _assert_id_got_added(data_some_without_properties) |
| 285 | + |
| 286 | + data_with_nested_properties = _create({ |
| 287 | + "summary": {"distance": 343.2}, |
| 288 | + "way_points": [3, 5], |
| 289 | + }) |
| 290 | + _assert_id_got_added(data_with_nested_properties) |
| 291 | + |
| 292 | + data_with_incompatible_properties = _create({ |
| 293 | + "summary": {"distances": [0, 6], "durations": None}, |
| 294 | + "way_points": [3, 5], |
| 295 | + }) |
| 296 | + _assert_id_got_added(data_with_incompatible_properties) |
| 297 | + |
| 298 | + data_loose_geometry = {"type": "LineString", "coordinates": [ |
| 299 | + [3.961389, 43.583333], [3.968056, 43.580833], [3.974722, 43.578333], |
| 300 | + [3.986389, 43.575278], [3.998333, 43.5725], [4.163333, 43.530556], |
| 301 | + ]} |
| 302 | + geojson = GeoJson(data_loose_geometry) |
| 303 | + geojson.convert_to_feature_collection() |
| 304 | + assert geojson.find_identifier() == 'feature.id' |
| 305 | + assert geojson.data['features'][0]['id'] == '0' |
0 commit comments