ClassInferenceTransformer#

class ClassInferenceTransformer[source]#

Bases: VectorTransformer

Infers missing class IDs from GeoJSON features.

Rules:
  1. If class_id is in feature['properties'], use it.

  2. If class_config is set and "class_name" or "label" are in feature['properties'] and in class_config, use corresponding class_id.

  3. If class_id_to_filter is set and filter is true when applied to feature, use corresponding class_id.

  4. 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 inferred class_id of None, then it will be deleted. Defaults to None.

  • class_config (ClassConfig | None) – ClassConfig to match the class names in the GeoJSON features to. Required if using class_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 to None.

  • class_name_mapping (dict[str, str] | None) – old_name --> new_name mapping for values in the class_name or label property of the GeoJSON features. The new_name must be a valid class name in the ClassConfig. This can also be used to merge multiple classes into one e.g.: dict(car="vehicle", truck="vehicle"). Defaults to None.

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 inferred class_id of None, then it will be deleted. Defaults to None.

  • class_config (ClassConfig | None) – ClassConfig to match the class names in the GeoJSON features to. Required if using class_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 to None.

  • class_name_mapping (dict[str, str] | None) – old_name --> new_name mapping for values in the class_name or label property of the GeoJSON features. The new_name must be a valid class name in the ClassConfig. This can also be used to merge multiple classes into one e.g.: dict(car="vehicle", truck="vehicle"). Defaults to None.

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:
  1. If class_id is in feature['properties'], use it.

  2. If class_config is set and "class_name" or "label" are in feature['properties'] and in class_config, use corresponding class_id.

  3. If class_id_to_filter is set and filter is true when applied to feature, use corresponding class_id.

  4. 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 inferred class_id of None, then it will be deleted. Defaults to None.

  • class_config (ClassConfig | None) – ClassConfig to match the class names in the GeoJSON features to. Required if using class_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 to None.

  • class_name_mapping (dict[str, str] | None) – old_name --> new_name mapping for values in the class_name or label property of the GeoJSON features. The new_name must be a valid class name in the ClassConfig. This can also be used to merge multiple classes into one e.g.: dict(car="vehicle", truck="vehicle"). Defaults to None.

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:
Return type:

dict