OSMPythonTools


NameOSMPythonTools JSON
Version 0.3.5 PyPI version JSON
download
home_pagehttps://github.com/mocnik-science/osm-python-tools
SummaryA library to access OpenStreetMap related services
upload_time2022-06-10 11:23:29
maintainer
docs_urlNone
authorFranz-Benjamin Mocnik
requires_python
licenseGPL-3
keywords openstreetmap osm service overpass nominatim
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # OSMPythonTools

The python package `OSMPythonTools` provides easy access to [OpenStreetMap (OSM)](http://www.openstreetmap.org) related services, among them an [Overpass endpoint](https://wiki.openstreetmap.org/wiki/Overpass_API), [Nominatim](http://nominatim.openstreetmap.org), and the [OSM API](https://wiki.openstreetmap.org/wiki/API).

## Installation

To install `OSMPythonTools`, you will need `python3` and `pip` ([How to install pip](https://pip.pypa.io/en/stable/installing/)). Then execute:
```bash
pip install OSMPythonTools
```
On some operating systems, `pip` for `python3` is named `pip3`:
```bash
pip3 install OSMPythonTools
```

## Example 1

*Which object does the way with the ID `5887599` represent?*

We can use the OSM API to answer this question:
```python
from OSMPythonTools.api import Api
api = Api()
way = api.query('way/5887599')
```
The resulting object contains information about the way, which can easily be accessed:
```python
way.tag('building')
# 'castle'
way.tag('architect')
# 'Johann Lucas von Hildebrandt'
way.tag('website')
# 'http://www.belvedere.at'
```

## Example 2

*What is the English name of the church called ‘Stephansdom’, what address does it have, and which of which denomination is the church?*

We use the Overpass API to query the corresponding data:
```python
from OSMPythonTools.overpass import Overpass
overpass = Overpass()
result = overpass.query('way["name"="Stephansdom"]; out body;')
```
This time, the result is a number of objects, which can be accessed by `result.elements()`. We just pick the first one: 
```python
stephansdom = result.elements()[0]
```
Information about the church can now easily be accessed:
```python
stephansdom.tag('name:en')
# "Saint Stephen's Cathedral"
'%s %s, %s %s' % (stephansdom.tag('addr:street'), stephansdom.tag('addr:housenumber'), stephansdom.tag('addr:postcode'), stephansdom.tag('addr:city'))
# 'Stephansplatz 3, 1010 Wien'
stephansdom.tag('building')
# 'cathedral'
stephansdom.tag('denomination')
# 'catholic'
```

## Example 3

*How many trees are in the OSM data of Vienna? And how many trees have there been in 2013?*

This time, we have to first resolve the name ‘Vienna’ to an area ID:
```python
from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
areaId = nominatim.query('Vienna, Austria').areaId()
```
This area ID can now be used to build the corresponding query:
```python
from OSMPythonTools.overpass import overpassQueryBuilder, Overpass
overpass = Overpass()
query = overpassQueryBuilder(area=areaId, elementType='node', selector='"natural"="tree"', out='count')
result = overpass.query(query)
result.countElements()
# 137830
```
There are 134520 trees in the current OSM data of Vienna. How many have there been in 2013?
```python
result = overpass.query(query, date='2013-01-01T00:00:00Z', timeout=60)
result.countElements()
# 127689
```

## Example 4

*Where are waterbodies located in Vienna?*

Again, we have to resolve the name ‘Vienna’ before running the query:
```python
from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
areaId = nominatim.query('Vienna, Austria').areaId()
```
The query can be built like in the examples before.  This time, however, the argument `includeGeometry=True` is provided to the `overpassQueryBuilder` in order to let him generate a query that downloads the geometry data.
```python
from OSMPythonTools.overpass import overpassQueryBuilder, Overpass
overpass = Overpass()
query = overpassQueryBuilder(area=areaId, elementType=['way', 'relation'], selector='"natural"="water"', includeGeometry=True)
result = overpass.query(query)
```
Next, we can exemplarily choose one random waterbody (the first one of the download ones) and compute its geometry like follows:
```python
firstElement = result.elements()[0]
firstElement.geometry()
# {"coordinates": [[[16.498671, 48.27628], [16.4991, 48.276345], ... ]], "type": "Polygon"}
```
Observe that the resulting geometry is provided in the [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) format.

## Example 5

*How did the number of trees in Berlin, Paris, and Vienna change over time?*

Before we can answer the question, we have to import some modules:
```python
from collections import OrderedDict
from OSMPythonTools.data import Data, dictRangeYears, ALL
from OSMPythonTools.overpass import overpassQueryBuilder, Overpass
```
The question has two ‘dimensions’: the dimension of time, and the dimension of different cities:
```python
dimensions = OrderedDict([
    ('year', dictRangeYears(2013, 2017.5, 1)),
    ('city', OrderedDict({
        'berlin': 'Berlin, Germany',
        'paris': 'Paris, France',
        'vienna': 'Vienna, Austria',
    })),
])
```
We have to define how we fetch the data. We again use Nominatim and the Overpass API to query the data (it can take some time to perform this query the first time!):
```python
overpass = Overpass()
def fetch(year, city):
    areaId = nominatim.query(city).areaId()
    query = overpassQueryBuilder(area=areaId, elementType='node', selector='"natural"="tree"', out='count')
    return overpass.query(query, date=year, timeout=60).countElements()
data = Data(fetch, dimensions)
```
We can now easily generate a plot from the result:
```python
data.plot(city=ALL, filename='example4.png')
```

![data.plot(city=ALL, filename='example4.png')](https://github.com/mocnik-science/osm-python-tools/blob/master/examples/example4.png)

Alternatively, we can generate a table from the result
```python
data.select(city=ALL).getCSV()
# year,berlin,paris,vienna
# 2013.0,10180,1936,127689
# 2014.0,17971,26905,128905
# 2015.0,28277,90599,130278
# 2016.0,86769,103172,132293
# 2017.0,108432,103246,134616
```

More examples can be found inside the documentation of the modules.

## Usage

The following modules are available (please click on their names to access further documentation):

* [OSMPythonTools.**Api**](docs/api.md) - Access to the official OSM API
* [OSMPythonTools.**Data**](docs/data.md) - Collecting, mining, and drawing data from OSM; to be used in combination with the other modules
* [OSMPythonTools.**Element**](docs/element.md) - Elements are returned by other services, like the OSM API or the Overpass API
* [OSMPythonTools.**Nominatim**](docs/nominatim.md) - Access to Nominatim, a reverse geocoder
* [OSMPythonTools.**Overpass**](docs/overpass.md) - Access to the Overpass API

Please refer to the [general remarks](docs/general-remarks.md) page if you have further questions related to `OSMPythonTools` in general or functionality that the several modules have in common.

**Observe the [breaking changes as included in the version history](version-history.md).**

## Logging

This library is a little bit more verbose than other Python libraries. The good reason behind is that the OpenStreetMap, the Nominatim, and the Overpass servers experience a heavy load already and their resources should be used carefully. In order to make you, the user of this library, aware of when `OSMPythonTools` accesses these servers, corresponding information is logged by default. In case you want to suppress these messages, you have to insert the following lines *after* the import of `OSMPythonTools`:
```python
import logging
logging.getLogger('OSMPythonTools').setLevel(logging.ERROR)
```
Please note that suppressing the messages means that you have to ensure on your own that you do not overuse the provided services and that you stick to their fair policy guidelines.

## Tests

You can test the package by installing the corresponding dependencies
```bash
pip install OSMPythonTools [test]
# or: pip3 install OSMPythonTools [test]
```
and then running
```bash
pytest --verbose
```
Please note that the tests might run very long (several minutes) because the overpass server will most likely defer the downloads.

## Author

This application is written and maintained by Franz-Benjamin Mocnik, <mail@mocnik-science.net>.

(c) by Franz-Benjamin Mocnik, 2017-2022.

The code is licensed under the [GPL-3](https://github.com/mocnik-science/osm-python-tools/blob/master/LICENSE).



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mocnik-science/osm-python-tools",
    "name": "OSMPythonTools",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "OpenStreetMap,OSM,service,overpass,nominatim",
    "author": "Franz-Benjamin Mocnik",
    "author_email": "mail@mocnik-science.net",
    "download_url": "https://files.pythonhosted.org/packages/b8/c0/d7fac2c6992a07bedec01de2ed3b1678b28fb9c374961a124c7d6bb73772/OSMPythonTools-0.3.5.tar.gz",
    "platform": null,
    "description": "# OSMPythonTools\n\nThe python package `OSMPythonTools` provides easy access to [OpenStreetMap (OSM)](http://www.openstreetmap.org) related services, among them an [Overpass endpoint](https://wiki.openstreetmap.org/wiki/Overpass_API), [Nominatim](http://nominatim.openstreetmap.org), and the [OSM API](https://wiki.openstreetmap.org/wiki/API).\n\n## Installation\n\nTo install `OSMPythonTools`, you will need `python3` and `pip` ([How to install pip](https://pip.pypa.io/en/stable/installing/)). Then execute:\n```bash\npip install OSMPythonTools\n```\nOn some operating systems, `pip` for `python3` is named `pip3`:\n```bash\npip3 install OSMPythonTools\n```\n\n## Example 1\n\n*Which object does the way with the ID `5887599` represent?*\n\nWe can use the OSM API to answer this question:\n```python\nfrom OSMPythonTools.api import Api\napi = Api()\nway = api.query('way/5887599')\n```\nThe resulting object contains information about the way, which can easily be accessed:\n```python\nway.tag('building')\n# 'castle'\nway.tag('architect')\n# 'Johann Lucas von Hildebrandt'\nway.tag('website')\n# 'http://www.belvedere.at'\n```\n\n## Example 2\n\n*What is the English name of the church called \u2018Stephansdom\u2019, what address does it have, and which of which denomination is the church?*\n\nWe use the Overpass API to query the corresponding data:\n```python\nfrom OSMPythonTools.overpass import Overpass\noverpass = Overpass()\nresult = overpass.query('way[\"name\"=\"Stephansdom\"]; out body;')\n```\nThis time, the result is a number of objects, which can be accessed by `result.elements()`. We just pick the first one: \n```python\nstephansdom = result.elements()[0]\n```\nInformation about the church can now easily be accessed:\n```python\nstephansdom.tag('name:en')\n# \"Saint Stephen's Cathedral\"\n'%s %s, %s %s' % (stephansdom.tag('addr:street'), stephansdom.tag('addr:housenumber'), stephansdom.tag('addr:postcode'), stephansdom.tag('addr:city'))\n# 'Stephansplatz 3, 1010 Wien'\nstephansdom.tag('building')\n# 'cathedral'\nstephansdom.tag('denomination')\n# 'catholic'\n```\n\n## Example 3\n\n*How many trees are in the OSM data of Vienna? And how many trees have there been in 2013?*\n\nThis time, we have to first resolve the name \u2018Vienna\u2019 to an area ID:\n```python\nfrom OSMPythonTools.nominatim import Nominatim\nnominatim = Nominatim()\nareaId = nominatim.query('Vienna, Austria').areaId()\n```\nThis area ID can now be used to build the corresponding query:\n```python\nfrom OSMPythonTools.overpass import overpassQueryBuilder, Overpass\noverpass = Overpass()\nquery = overpassQueryBuilder(area=areaId, elementType='node', selector='\"natural\"=\"tree\"', out='count')\nresult = overpass.query(query)\nresult.countElements()\n# 137830\n```\nThere are 134520 trees in the current OSM data of Vienna. How many have there been in 2013?\n```python\nresult = overpass.query(query, date='2013-01-01T00:00:00Z', timeout=60)\nresult.countElements()\n# 127689\n```\n\n## Example 4\n\n*Where are waterbodies located in Vienna?*\n\nAgain, we have to resolve the name \u2018Vienna\u2019 before running the query:\n```python\nfrom OSMPythonTools.nominatim import Nominatim\nnominatim = Nominatim()\nareaId = nominatim.query('Vienna, Austria').areaId()\n```\nThe query can be built like in the examples before.  This time, however, the argument `includeGeometry=True` is provided to the `overpassQueryBuilder` in order to let him generate a query that downloads the geometry data.\n```python\nfrom OSMPythonTools.overpass import overpassQueryBuilder, Overpass\noverpass = Overpass()\nquery = overpassQueryBuilder(area=areaId, elementType=['way', 'relation'], selector='\"natural\"=\"water\"', includeGeometry=True)\nresult = overpass.query(query)\n```\nNext, we can exemplarily choose one random waterbody (the first one of the download ones) and compute its geometry like follows:\n```python\nfirstElement = result.elements()[0]\nfirstElement.geometry()\n# {\"coordinates\": [[[16.498671, 48.27628], [16.4991, 48.276345], ... ]], \"type\": \"Polygon\"}\n```\nObserve that the resulting geometry is provided in the [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) format.\n\n## Example 5\n\n*How did the number of trees in Berlin, Paris, and Vienna change over time?*\n\nBefore we can answer the question, we have to import some modules:\n```python\nfrom collections import OrderedDict\nfrom OSMPythonTools.data import Data, dictRangeYears, ALL\nfrom OSMPythonTools.overpass import overpassQueryBuilder, Overpass\n```\nThe question has two \u2018dimensions\u2019: the dimension of time, and the dimension of different cities:\n```python\ndimensions = OrderedDict([\n    ('year', dictRangeYears(2013, 2017.5, 1)),\n    ('city', OrderedDict({\n        'berlin': 'Berlin, Germany',\n        'paris': 'Paris, France',\n        'vienna': 'Vienna, Austria',\n    })),\n])\n```\nWe have to define how we fetch the data. We again use Nominatim and the Overpass API to query the data (it can take some time to perform this query the first time!):\n```python\noverpass = Overpass()\ndef fetch(year, city):\n    areaId = nominatim.query(city).areaId()\n    query = overpassQueryBuilder(area=areaId, elementType='node', selector='\"natural\"=\"tree\"', out='count')\n    return overpass.query(query, date=year, timeout=60).countElements()\ndata = Data(fetch, dimensions)\n```\nWe can now easily generate a plot from the result:\n```python\ndata.plot(city=ALL, filename='example4.png')\n```\n\n![data.plot(city=ALL, filename='example4.png')](https://github.com/mocnik-science/osm-python-tools/blob/master/examples/example4.png)\n\nAlternatively, we can generate a table from the result\n```python\ndata.select(city=ALL).getCSV()\n# year,berlin,paris,vienna\n# 2013.0,10180,1936,127689\n# 2014.0,17971,26905,128905\n# 2015.0,28277,90599,130278\n# 2016.0,86769,103172,132293\n# 2017.0,108432,103246,134616\n```\n\nMore examples can be found inside the documentation of the modules.\n\n## Usage\n\nThe following modules are available (please click on their names to access further documentation):\n\n* [OSMPythonTools.**Api**](docs/api.md) - Access to the official OSM API\n* [OSMPythonTools.**Data**](docs/data.md) - Collecting, mining, and drawing data from OSM; to be used in combination with the other modules\n* [OSMPythonTools.**Element**](docs/element.md) - Elements are returned by other services, like the OSM API or the Overpass API\n* [OSMPythonTools.**Nominatim**](docs/nominatim.md) - Access to Nominatim, a reverse geocoder\n* [OSMPythonTools.**Overpass**](docs/overpass.md) - Access to the Overpass API\n\nPlease refer to the [general remarks](docs/general-remarks.md) page if you have further questions related to `OSMPythonTools` in general or functionality that the several modules have in common.\n\n**Observe the [breaking changes as included in the version history](version-history.md).**\n\n## Logging\n\nThis library is a little bit more verbose than other Python libraries. The good reason behind is that the OpenStreetMap, the Nominatim, and the Overpass servers experience a heavy load already and their resources should be used carefully. In order to make you, the user of this library, aware of when `OSMPythonTools` accesses these servers, corresponding information is logged by default. In case you want to suppress these messages, you have to insert the following lines *after* the import of `OSMPythonTools`:\n```python\nimport logging\nlogging.getLogger('OSMPythonTools').setLevel(logging.ERROR)\n```\nPlease note that suppressing the messages means that you have to ensure on your own that you do not overuse the provided services and that you stick to their fair policy guidelines.\n\n## Tests\n\nYou can test the package by installing the corresponding dependencies\n```bash\npip install OSMPythonTools [test]\n# or: pip3 install OSMPythonTools [test]\n```\nand then running\n```bash\npytest --verbose\n```\nPlease note that the tests might run very long (several minutes) because the overpass server will most likely defer the downloads.\n\n## Author\n\nThis application is written and maintained by Franz-Benjamin Mocnik, <mail@mocnik-science.net>.\n\n(c) by Franz-Benjamin Mocnik, 2017-2022.\n\nThe code is licensed under the [GPL-3](https://github.com/mocnik-science/osm-python-tools/blob/master/LICENSE).\n\n\n",
    "bugtrack_url": null,
    "license": "GPL-3",
    "summary": "A library to access OpenStreetMap related services",
    "version": "0.3.5",
    "project_urls": {
        "Homepage": "https://github.com/mocnik-science/osm-python-tools"
    },
    "split_keywords": [
        "openstreetmap",
        "osm",
        "service",
        "overpass",
        "nominatim"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b8c0d7fac2c6992a07bedec01de2ed3b1678b28fb9c374961a124c7d6bb73772",
                "md5": "f697f3b149a390006f7834fb7288e055",
                "sha256": "13ff721f760fdad5dd78b4d1461d286b78bba96ee151a7301ee8c11a0c258be9"
            },
            "downloads": -1,
            "filename": "OSMPythonTools-0.3.5.tar.gz",
            "has_sig": false,
            "md5_digest": "f697f3b149a390006f7834fb7288e055",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 28654,
            "upload_time": "2022-06-10T11:23:29",
            "upload_time_iso_8601": "2022-06-10T11:23:29.371370Z",
            "url": "https://files.pythonhosted.org/packages/b8/c0/d7fac2c6992a07bedec01de2ed3b1678b28fb9c374961a124c7d6bb73772/OSMPythonTools-0.3.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-06-10 11:23:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mocnik-science",
    "github_project": "osm-python-tools",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "osmpythontools"
}
        
Elapsed time: 0.08374s