# osm2geojson
![Test package](https://github.com/aspectumapp/osm2geojson/workflows/Test%20package/badge.svg)
Parse OSM and Overpass JSON with python.
**This library is under development!**
### Usage
Install this package with pip:
```sh
$ pip install osm2geojson
```
If you want to convert OSM xml or Overpass json/xml to Geojson you can import this lib and use one of 4 methods:
- `json2shapes(dict json_from_overpass)` - to convert Overpass json to \*Shape-objects
- `xml2shapes(str xml_from_osm)` - to convert OSM xml or Overpass xml to \*Shape-objects
- `json2geojson(dict json_from_overpass)` - to convert Overpass json to Geojson
- `xml2geojson(str xml_from_osm)` - to convert OSM xml or Overpass xml to Geojson
Additional parameters for all functions:
- `filter_used_refs` - (default: `True`) defines geometry filtration strategy (will return all geometry if set as `False`)
- `log_level` - (default: `'ERROR'`) controls logging level (will print all logs if set as `'INFO'`). More details [here](https://docs.python.org/3/library/logging.html#logging-levels)
- `area_keys` - (default: `None`) defines which keys and values of an area should be saved from the list of OSM tags, see `areaKeys.json` for the defaults
- `polygon_features` - (default: `None`) defines a whitelist/blacklist of features to be included in resulting polygons, see `polygon-features.json` for the defaults
- `raise_on_failure` - (default: `False`) controls whether to throw an exception when geometry generation fails
Other handy methods:
- `overpass_call(str query)` - runs query to overpass-api.de server (retries 5 times in case of error).
- `shape_to_feature(Shape shape, dict properties)` - Converts Shape-object to GeoJSON with passed properties.
**\*Shape-object - for convenience created simple dict to save Shapely object (geometry) and OSM-properties. Structure of this object:**
```py
shape_obj = {
'shape': Point | LineString | Polygon ...,
'properties': {
'type': 'relation' | 'node' ...,
'tags': { ... },
...
}
}
```
After installing via `pip`, the module may also be used as a `argparse`-based command-line script.
Either use the script name directly or call Python with the `-m` option and the package name:
```sh
osm2geojson --help
```
```sh
python3 -m osm2geojson --help
```
### Examples
Convert OSM-xml to Geojson:
```py
import codecs
import osm2geojson
with codecs.open('file.osm', 'r', encoding='utf-8') as data:
xml = data.read()
geojson = osm2geojson.xml2geojson(xml, filter_used_refs=False, log_level='INFO')
# >> { "type": "FeatureCollection", "features": [ ... ] }
```
Convert OSM-json to Shape-objects:
```py
import codecs
import osm2geojson
with codecs.open('file.json', 'r', encoding='utf-8') as data:
json = data.read()
shapes_with_props = osm2geojson.json2shapes(json)
# >> [ { "shape": <Shapely-object>, "properties": {...} }, ... ]
```
### Development
Clone project with submodules
```sh
$ git clone --recurse-submodules https://github.com/aspectumapp/osm2geojson.git
```
Setup package
```sh
$ python setup.py develop
```
Run tests
```sh
$ python -m unittest tests
```
Run single test
```sh
$ python tests/main.py TestOsm2GeoJsonMethods.test_barrier_wall
```
Update osm-polygon-features to last version (if you want last version)
```sh
$ ./update-osm-polygon-features.sh
```
### ToDo
* Add tests and examples for cli tool
* Add actions related to cli tool (more info https://github.com/aspectumapp/osm2geojson/pull/32#issuecomment-1073386381)
Raw data
{
"_id": null,
"home_page": "https://github.com/aspectumapp/osm2geojson",
"name": "osm2geojson",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "geometry gis osm parsing",
"author": "Parfeniuk Mykola",
"author_email": "mikola.parfenyuck@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/00/39/69d52d7c51d3575d42aac959949269f6aa11632c4436115829b75f4192a3/osm2geojson-0.2.5.tar.gz",
"platform": null,
"description": "# osm2geojson\n\n![Test package](https://github.com/aspectumapp/osm2geojson/workflows/Test%20package/badge.svg)\n\nParse OSM and Overpass JSON with python.\n**This library is under development!**\n\n### Usage\n\nInstall this package with pip:\n\n```sh\n$ pip install osm2geojson\n```\n\nIf you want to convert OSM xml or Overpass json/xml to Geojson you can import this lib and use one of 4 methods:\n\n- `json2shapes(dict json_from_overpass)` - to convert Overpass json to \\*Shape-objects\n- `xml2shapes(str xml_from_osm)` - to convert OSM xml or Overpass xml to \\*Shape-objects\n- `json2geojson(dict json_from_overpass)` - to convert Overpass json to Geojson\n- `xml2geojson(str xml_from_osm)` - to convert OSM xml or Overpass xml to Geojson\n\nAdditional parameters for all functions:\n\n- `filter_used_refs` - (default: `True`) defines geometry filtration strategy (will return all geometry if set as `False`)\n- `log_level` - (default: `'ERROR'`) controls logging level (will print all logs if set as `'INFO'`). More details [here](https://docs.python.org/3/library/logging.html#logging-levels)\n- `area_keys` - (default: `None`) defines which keys and values of an area should be saved from the list of OSM tags, see `areaKeys.json` for the defaults\n- `polygon_features` - (default: `None`) defines a whitelist/blacklist of features to be included in resulting polygons, see `polygon-features.json` for the defaults\n- `raise_on_failure` - (default: `False`) controls whether to throw an exception when geometry generation fails\n\nOther handy methods:\n\n- `overpass_call(str query)` - runs query to overpass-api.de server (retries 5 times in case of error).\n- `shape_to_feature(Shape shape, dict properties)` - Converts Shape-object to GeoJSON with passed properties.\n\n**\\*Shape-object - for convenience created simple dict to save Shapely object (geometry) and OSM-properties. Structure of this object:**\n\n```py\nshape_obj = {\n 'shape': Point | LineString | Polygon ...,\n 'properties': {\n 'type': 'relation' | 'node' ...,\n 'tags': { ... },\n ...\n }\n}\n```\n\nAfter installing via `pip`, the module may also be used as a `argparse`-based command-line script.\nEither use the script name directly or call Python with the `-m` option and the package name:\n\n```sh\nosm2geojson --help\n```\n\n```sh\npython3 -m osm2geojson --help\n```\n\n### Examples\n\nConvert OSM-xml to Geojson:\n\n```py\nimport codecs\nimport osm2geojson\n\nwith codecs.open('file.osm', 'r', encoding='utf-8') as data:\n xml = data.read()\n\ngeojson = osm2geojson.xml2geojson(xml, filter_used_refs=False, log_level='INFO')\n# >> { \"type\": \"FeatureCollection\", \"features\": [ ... ] }\n```\n\nConvert OSM-json to Shape-objects:\n\n```py\nimport codecs\nimport osm2geojson\n\nwith codecs.open('file.json', 'r', encoding='utf-8') as data:\n json = data.read()\n\nshapes_with_props = osm2geojson.json2shapes(json)\n# >> [ { \"shape\": <Shapely-object>, \"properties\": {...} }, ... ]\n```\n\n### Development\n\nClone project with submodules\n\n```sh\n$ git clone --recurse-submodules https://github.com/aspectumapp/osm2geojson.git\n```\n\nSetup package\n\n```sh\n$ python setup.py develop\n```\n\nRun tests\n\n```sh\n$ python -m unittest tests\n```\n\nRun single test\n\n```sh\n$ python tests/main.py TestOsm2GeoJsonMethods.test_barrier_wall\n```\n\nUpdate osm-polygon-features to last version (if you want last version)\n\n```sh\n$ ./update-osm-polygon-features.sh\n```\n\n### ToDo\n\n * Add tests and examples for cli tool\n * Add actions related to cli tool (more info https://github.com/aspectumapp/osm2geojson/pull/32#issuecomment-1073386381)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Parse OSM and Overpass JSON",
"version": "0.2.5",
"project_urls": {
"Homepage": "https://github.com/aspectumapp/osm2geojson"
},
"split_keywords": [
"geometry",
"gis",
"osm",
"parsing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "003969d52d7c51d3575d42aac959949269f6aa11632c4436115829b75f4192a3",
"md5": "e29a911d50d789f1450ce72177c1adc7",
"sha256": "f6348dd222e38246ac11789ef4410aa9a4b45bf4a6471c04fe011e6ce55aebc4"
},
"downloads": -1,
"filename": "osm2geojson-0.2.5.tar.gz",
"has_sig": false,
"md5_digest": "e29a911d50d789f1450ce72177c1adc7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 14200,
"upload_time": "2024-03-25T12:28:24",
"upload_time_iso_8601": "2024-03-25T12:28:24.637782Z",
"url": "https://files.pythonhosted.org/packages/00/39/69d52d7c51d3575d42aac959949269f6aa11632c4436115829b75f4192a3/osm2geojson-0.2.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-03-25 12:28:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "aspectumapp",
"github_project": "osm2geojson",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "shapely",
"specs": []
},
{
"name": "requests",
"specs": []
}
],
"lcname": "osm2geojson"
}