filtering of OSM pbf-files
Project description
esy-osmfilter
esy-osmfilter
is a Python library to read and filter OpenStreetMap data files in the Protocol
Buffers (PBF) format and export them to a dictionary and/or JSON file.
Usage
In the usage
>>> import configparser, contextlib
>>> import os, sys
>>> from esy.osmfilter import osm_colors as CC
>>> from esy.osmfilter.osm_filter import run_filter as run_filter
>>> from esy.osm.pbf import Node, Way, Relation
The prefilter
will return the Data-dictionary.
With the prefilter, we accept all way-items that have man_made as key and pipeline as value in their taglist.
>>> prefilter = {Node: {}, Way: {"man_made":["pipeline",],}, Relation: {}}
Additionally, all corresponent nodes that are refered in these way-items will be stored as Nodes.
You can also set "man_made":True to accept items independently of a key value.
In this example, we have only found two pipelines "Wäschgräbli" and "Wäschgräble" and their correspondent Nodes.
The Element dictionary will be constructed from the Data dictionary by the usage of the
whitefilter and the blackfilter.
The blackfilter
is a list of tuples.
It ignores items if one tuple is present as tag.
The whitefilter
is a list of list of tuples.
It accepts entries if a list of tuples is present in the taglist of an OSM item.
By the way, in case of conflict, the blackfilter wins.
In this example, we blackfilter the prefiltered items for pipeline substation.
>>> blackfilter = [("pipeline","substation"),]
We further only accept the drain pipelines that have really funny name "Wäschgräbli".
>>> whitefilter =[(("waterway","drain"),("name","Wäschgräbli")), ]
>>> PBF_inputfile = os.path.join(os.getcwd(),'tests/input/liechtenstein-latest.osm.pbf')
>>> JSON_outputfile = os.path.join(os.getcwd(),'tests/output/LI/liechtenstein-latest.json')
Filtered elements are given a name:
>>> elementname='funny-waterway-pipelines'
>>> [Data,Elements]=run_filter(elementname,PBF_inputfile, JSON_outputfile,
... prefilter,whitefilter, blackfilter, NewPreFilterData = True,
... CreateElements = True, LoadElements = True,verbose=True)
>>> len(Data['Node'])
13
>>> len(Data['Relation'])
0
>>> len(Data['Way'])
2
Only one way left after the blackfilter and whitefiler
>>> len(Elements['funny-waterway-pipelines']['Node'])
0
>>> len(Elements['funny-waterway-pipelines']['Relation'])
0
>>> len(Elements['funny-waterway-pipelines']['Way'])
1
One can access the referenced nodes from the Data dictionary.
>>> elementlist=list(Elements['funny-waterway-pipelines']['Way'].keys())
>>> first_element=elementlist.pop()
>>> first_item_refs=list(Elements['funny-waterway-pipelines']['Way'][first_element]['refs'])
>>> first_ref_id=str(first_item_refs[0])
>>> first_ref_item=Data['Node'][first_ref_id]
>>> first_ref_item['lonlat']
[9.502233600000002, 47.153427400000005]
This pipeline has 3 node references. The first referenced node of the first element was selected via its id-string from the Data-dictionary: Data['Node'][id-string].
for more details, jump to the documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for esy_osmfilter-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0c075d3e25a8be02c49c01f3020e372ce7909bf42890f3b639d493e28ebee13 |
|
MD5 | dddb76a1b59a82338155b0beedce4f4b |
|
BLAKE2b-256 | b9a8633383fda5a13d5b7200b1d4101c390636fc042973fa954e2856b64bb861 |