validate_osm
osm.validate_osm
Validation of OSM pbf files.
pyosmium requires handler classes that inherit from their API. The methods of these handlers must be named node
, way
, relation
or area
. Exposing these methods would be of limited utility, therefore internal handler classes are used to collect and process the feature information.
A separate group of classes are then defined, inheriting from these internal handlers. These API classes are used to apply the logic of the handler classes to an osm.pbf file. The API classes also define methods associated with the user requirements, eg ‘find way Ids’, ‘find the coordinates for this list of node IDs’ or similar.
The API classes expose logic that enables users to: * Count all features in a pbf file by type (node, way, relation or area) * Return IDs of the above feature types * Extract tags for the above features * Find coordinates for node or way features * Plot the coordinates of a given list of node or way IDs
Classes
Name | Description |
---|---|
FindIds | Apply ID collation to an OSM file. |
FindLocations | Applies location collation to OSM file. |
FindTags | Applies tag collation to OSM file. |
PerformanceWarning | Operation may be slow. |
FindIds
osm.validate_osm.FindIds(self, osm_pth, id_collator=_IdHandler)
Apply ID collation to an OSM file.
Count or return available feature IDs in an OSM file.
Parameters
Name | Type | Description | Default |
---|---|---|---|
osm_pth |
Union[Path, str] | Path to osm file. | required |
id_collator |
_IdHandler | FindIds applies the logic from id_collator to a pbf file on init, storing the collated IDs in their appropriate attributes - __node_ids, __way_ids, __relations_ids and __area_ids. Defaults to _IdHandler. | _IdHandler |
Raises
Type | Description |
---|---|
TypeError : | osm_pth is not of type pathlib.Path or str. |
FileNotFoundError : | osm_pth file not found on disk. |
ValueError : | osm_pth does not have a .pbf extension. |
TypeError : | id_collator is not an instance of _IdHandler. |
Attributes
Name | Type | Description |
---|---|---|
counts | dict | Counts of feature IDs by feature type. |
id_dict | dict | IDs of all found features by feature type. |
__node_ids | list | Internal attribute contains list of all node IDs contained in pbf file. |
__way_ids | list | Internal attribute contains list of all way IDs contained in pbf file. |
__relations_ids | list | Internal attribute contains list of all relation IDs contained in pbf file. |
__area_ids | list | Internal attribute contains list of all area IDs contained in pbf file. |
Methods
Name | Description |
---|---|
count_features | Count numbers of each available feature type. |
get_feature_ids | Get a list of all available IDs in a pbf file for each feature type. |
count_features
osm.validate_osm.FindIds.count_features()
Count numbers of each available feature type.
Returns
Type | Description |
---|---|
dict | Counts of node, way, relation & area IDs in a pbf file. |
get_feature_ids
osm.validate_osm.FindIds.get_feature_ids()
Get a list of all available IDs in a pbf file for each feature type.
Returns
Type | Description |
---|---|
dict | Available IDs for nodes, ways, relations and areas. |
FindLocations
osm.validate_osm.FindLocations(self, osm_pth, loc_collator=_LocHandler)
Applies location collation to OSM file.
Parameters
Name | Type | Description | Default |
---|---|---|---|
osm_pth |
Union[Path, str] | Path to osm file. | required |
loc_collator |
_LocHandler | FindLocations applies the logic from loc_collator to a pbf file on init, storing the collated locations in __node_locs or __way_node_locs attributes. Defaults to _LocHandler. | _LocHandler |
Raises
Type | Description |
---|---|
TypeError : | osm_pth is not of type pathlib.Path or str. |
FileNotFoundError : | osm_pth file not found on disk. |
ValueError : | osm_pth does not have a .pbf extension. |
TypeError : | loc_collator is not an instance of _LocHandler. |
Attributes
Name | Type | Description |
---|---|---|
found_locs | dict | Found locations for specified feature IDs. |
__node_locs | dict | Locations of nodes. |
__way_node_locs | dict | Locations of nodes that belong to a way. |
_osm_pth | Union[Path, str] | Path to osm file on disk. Used for method plot_ids() when include_tags is True. |
Methods
Name | Description |
---|---|
check_locs_for_ids | Return coordinates for provided list of feature IDs. |
plot_ids | Plot coordinates for nodes or node members of a way. |
check_locs_for_ids
osm.validate_osm.FindLocations.check_locs_for_ids(ids, feature_type)
Return coordinates for provided list of feature IDs.
Parameters
Name | Type | Description | Default |
---|---|---|---|
ids |
list | A list of OSM feature IDs to check. IDs must be integer. | required |
feature_type |
str | The type of feature to which the IDS belong. Valid options are “node” or “way”. | required |
Returns
Type | Description |
---|---|
dict | ID : dict of tags, containing ID : location. |
Raises
Type | Description |
---|---|
NotImplementedError : | Relation location data could be extracted with a bit more munging. Please raise a feature request if you feel this is significant. |
plot_ids
osm.validate_osm.FindLocations.plot_ids(ids, feature_type, crs='epsg:4326', include_tags=False, tooltip_nm='custom_tooltip', tooltip_kwds={'labels': False}, tiles='CartoDB positron', style_kwds={'color': '#3f5277', 'fill': True, 'fillOpacity': 0.3, 'fillColor': '#3f5277', 'weight': 4})
Plot coordinates for nodes or node members of a way.
Provided with a list of node or way IDs, converts the coordinate data from dictionary to GeoDataFrame and uses the basic gdf.explore() method to visualise the features on a basemap.
Parameters
Name | Type | Description | Default |
---|---|---|---|
ids |
list | A list of Node or Way IDs. | required |
feature_type |
str | Whether the type of OSM feature to plot is node or way. | required |
crs |
Union[str, int] | The projection of the spatial features, by default “epsg:4326” | 'epsg:4326' |
include_tags |
bool | Should tag metadata be included in the map tooltips, by default False | False |
tooltip_nm |
str | Name to use for tooltip column in coord_gdf attribute, by default “custom_tooltip” | 'custom_tooltip' |
tooltip_kwds |
dict | Additional tooltip styling arguments to pass to gpd explore(), by default {“labels”: False} | {'labels': False} |
tiles |
Union[str, xyzservices.TileProvider] | Basemap provider tiles to use, by default “CartoDB positron” | 'CartoDB positron' |
style_kwds |
dict | Additional map styling arguments to pass to gpd explore(), by default { “color”: “#3f5277”, “fill”: True, “fillOpacity”: 0.3, “fillColor”: “#3f5277”, “weight”: 4, } | {'color': '#3f5277', 'fill': True, 'fillOpacity': 0.3, 'fillColor': '#3f5277', 'weight': 4} |
Returns
Type | Description |
---|---|
folium.Map | A plot of the coordinate data for each identifiable node. |
Raises
Type | Description |
---|---|
NotImplementedError : | Relation location data could be extracted with a bit more munging. Please raise a feature request if you feel this is significant. |
ValueError : | feature_type is not one of “node”, “way”, “relation” or “area”. |
TypeError : | ids is not of type list. feature_type is not of type str. |
FindTags
osm.validate_osm.FindTags(self, osm_pth, tag_collator=_TagHandler)
Applies tag collation to OSM file.
Parameters
Name | Type | Description | Default |
---|---|---|---|
osm_pth |
Union[Path, str] | Path to osm file. | required |
tag_collator |
_TagHandler | FindTags applies the logic from tag_collator to a pbf file on init, storing the collated tags in a found_tags attribute. Defaults to _TagHandler. |
_TagHandler |
Raises
Type | Description |
---|---|
TypeError : | osm_pth is not of type pathlib.Path or str. |
FileNotFoundError : | osm_pth file not found on disk. |
ValueError : | osm_pth does not have a .pbf extension. |
TypeError : | tag_collator is not an instance of _TagHandler. |
Attributes
Name | Type | Description |
---|---|---|
found_tags | dict | Found tags for specified feature IDs. |
__node_tags | dict | Tags found for OSM node features. |
__way_tags | dict | Tags found for OSM way features. |
__relation_tags | dict | Tags found for OSM relation features. |
__area_tags | dict | Tags found for OSM area features. |
__LARGE_FILE_THRESH | int | Size threshold in bytes on which to raise a PerformanceWarning when osm file on disk exceeds. |
Methods
Name | Description |
---|---|
check_tags_for_ids | Return tags for provided list of feature IDs. |
PerformanceWarning
osm.validate_osm.PerformanceWarning()
Operation may be slow.