ClassInferenceTransformer#
- class ClassInferenceTransformer[source]#
Bases:
VectorTransformer
Infers missing class IDs from GeoJSON features.
- Rules:
If
class_id
is infeature['properties']
, use it.If
class_config
is set and"class_name"
or"label"
are infeature['properties']
and inclass_config
, use correspondingclass_id
.If
class_id_to_filter
is set and filter is true when applied to feature, use correspondingclass_id
.Otherwise, return the
default_class_id
.
- __init__(default_class_id: int | None, class_config: ClassConfig | None = None, class_id_to_filter: dict[int, list] | None = None, class_name_mapping: dict[str, str] | None = None)[source]#
Constructor.
- Parameters:
default_class_id (int | None) – The default
class_id
to use if class cannot be inferred using other mechanisms. If a feature has an inferredclass_id
of None, then it will be deleted. Defaults toNone
.class_config (ClassConfig | None) –
ClassConfig
to match the class names in the GeoJSON features to. Required if usingclass_name_mapping
. Defaults to None.class_id_to_filter (dict[int, list] | None) – Map from
class_id
to JSON filter used to infer missing class IDs. Each key should be a class ID, and its value should be a boolean expression which is run against the property field for each feature. This allows matching different features to different class IDs based on its properties. The expression schema is that described by https://docs.mapbox.com/mapbox-gl-js/style-spec/other/#other-filter. Defaults toNone
.class_name_mapping (dict[str, str] | None) –
old_name --> new_name
mapping for values in theclass_name
orlabel
property of the GeoJSON features. Thenew_name
must be a valid class name in theClassConfig
. This can also be used to merge multiple classes into one e.g.:dict(car="vehicle", truck="vehicle")
. Defaults toNone
.
Methods
__init__
(default_class_id[, class_config, ...])Constructor.
infer_feature_class_id
(feature, default_class_id)Infer the class ID for a GeoJSON feature.
transform
(geojson[, crs_transformer])Add class_id to feature properties and drop features with no class.
- __init__(default_class_id: int | None, class_config: ClassConfig | None = None, class_id_to_filter: dict[int, list] | None = None, class_name_mapping: dict[str, str] | None = None)[source]#
Constructor.
- Parameters:
default_class_id (int | None) – The default
class_id
to use if class cannot be inferred using other mechanisms. If a feature has an inferredclass_id
of None, then it will be deleted. Defaults toNone
.class_config (ClassConfig | None) –
ClassConfig
to match the class names in the GeoJSON features to. Required if usingclass_name_mapping
. Defaults to None.class_id_to_filter (dict[int, list] | None) – Map from
class_id
to JSON filter used to infer missing class IDs. Each key should be a class ID, and its value should be a boolean expression which is run against the property field for each feature. This allows matching different features to different class IDs based on its properties. The expression schema is that described by https://docs.mapbox.com/mapbox-gl-js/style-spec/other/#other-filter. Defaults toNone
.class_name_mapping (dict[str, str] | None) –
old_name --> new_name
mapping for values in theclass_name
orlabel
property of the GeoJSON features. Thenew_name
must be a valid class name in theClassConfig
. This can also be used to merge multiple classes into one e.g.:dict(car="vehicle", truck="vehicle")
. Defaults toNone
.
- static infer_feature_class_id(feature: dict, default_class_id: int | None, class_config: ClassConfig | None = None, class_id_to_filter: dict[int, list] | None = None, class_name_mapping: dict[str, str] | None = None) int | None [source]#
Infer the class ID for a GeoJSON feature.
- Rules:
If
class_id
is infeature['properties']
, use it.If
class_config
is set and"class_name"
or"label"
are infeature['properties']
and inclass_config
, use correspondingclass_id
.If
class_id_to_filter
is set and filter is true when applied to feature, use correspondingclass_id
.Otherwise, return the
default_class_id
.
- Parameters:
feature (dict) – GeoJSON feature.
default_class_id (int | None) – The default
class_id
to use if class cannot be inferred using other mechanisms. If a feature has an inferredclass_id
of None, then it will be deleted. Defaults toNone
.class_config (ClassConfig | None) –
ClassConfig
to match the class names in the GeoJSON features to. Required if usingclass_name_mapping
. Defaults to None.class_id_to_filter (dict[int, list] | None) – Map from
class_id
to JSON filter used to infer missing class IDs. Each key should be a class ID, and its value should be a boolean expression which is run against the property field for each feature. This allows matching different features to different class IDs based on its properties. The expression schema is that described by https://docs.mapbox.com/mapbox-gl-js/style-spec/other/#other-filter. Defaults toNone
.class_name_mapping (dict[str, str] | None) –
old_name --> new_name
mapping for values in theclass_name
orlabel
property of the GeoJSON features. Thenew_name
must be a valid class name in theClassConfig
. This can also be used to merge multiple classes into one e.g.:dict(car="vehicle", truck="vehicle")
. Defaults toNone
.
- Returns:
Inferred class ID.
- Return type:
int | None
- transform(geojson: dict, crs_transformer: CRSTransformer | None = None) dict [source]#
Add class_id to feature properties and drop features with no class.
For each feature in geojson, the class_id is inferred and is set into feature[‘properties’]. If the class_id is None (because none of the rules apply and the default_class_id is None), the feature is dropped.
- Parameters:
geojson (dict) –
crs_transformer (CRSTransformer | None) –
- Return type: