Find a unique identifier for each feature, create it if needed. According to the GeoJSON specs a feature: - MAY have an 'id' field with a string or numerical value. - MUST have a 'properties' field. The content can be any json object or even null.
(self)
| 817 | ) |
| 818 | |
| 819 | def find_identifier(self) -> str: |
| 820 | """Find a unique identifier for each feature, create it if needed. |
| 821 | |
| 822 | According to the GeoJSON specs a feature: |
| 823 | - MAY have an 'id' field with a string or numerical value. |
| 824 | - MUST have a 'properties' field. The content can be any json object |
| 825 | or even null. |
| 826 | |
| 827 | """ |
| 828 | feats = self.data["features"] |
| 829 | # Each feature has an 'id' field with a unique value. |
| 830 | unique_ids = {feat.get("id", None) for feat in feats} |
| 831 | if None not in unique_ids and len(unique_ids) == len(feats): |
| 832 | return "feature.id" |
| 833 | # Each feature has a unique string or int property. |
| 834 | if all(isinstance(feat.get("properties", None), dict) for feat in feats): |
| 835 | for key in feats[0]["properties"]: |
| 836 | unique_values = { |
| 837 | feat["properties"].get(key, None) |
| 838 | for feat in feats |
| 839 | if isinstance(feat["properties"].get(key, None), (str, int)) |
| 840 | } |
| 841 | if len(unique_values) == len(feats): |
| 842 | return f"feature.properties.{key}" |
| 843 | # We add an 'id' field with a unique value to the data. |
| 844 | if self.embed: |
| 845 | for i, feature in enumerate(feats): |
| 846 | feature["id"] = str(i) |
| 847 | return "feature.id" |
| 848 | raise ValueError( |
| 849 | "There is no unique identifier for each feature and because " |
| 850 | "`embed=False` it cannot be added. Consider adding an `id` " |
| 851 | "field to your geojson data or set `embed=True`. " |
| 852 | ) |
| 853 | |
| 854 | def _get_self_bounds(self) -> List[List[Optional[float]]]: |
| 855 | """ |
no outgoing calls