Skip to main content

Python wrapper for the OSM API

Project description

osmapi

Build osmapi Version License Coverage Code style: black pre-commit

Python wrapper for the OSM API (requires Python >= 3.9).

NOTE: Since version 5.0 of this library, all method names are in snake_case, the CamelCase versions are deprecated and will be removed in version 6.0.

Installation

Install osmapi from PyPi by using pip:

pip install osmapi

Documentation

The documentation is generated using pdoc and can be viewed online.

The build the documentation locally, you can use

make docs

This project uses GitHub Pages to publish its documentation. To update the online documentation, you need to re-generate the documentation with the above command and update the main branch of this repository.

Examples

To test this library, please create an account on the development server of OpenStreetMap (https://api06.dev.openstreetmap.org).

Check the examples directory to find more example code.

Read from OpenStreetMap

>>> import osmapi
>>> api = osmapi.OsmApi()
>>> print(api.node_get(123))
{'changeset': 532907, 'uid': 14298,
'timestamp': '2007-09-29T09:19:17Z',
'lon': 10.790009299999999, 'visible': True,
'version': 1, 'user': 'Mede',
'lat': 59.9503044, 'tag': {}, 'id': 123}

Write to OpenStreetMap

>>> import osmapi
>>> api = osmapi.OsmApi(api="https://api06.dev.openstreetmap.org", username = "metaodi", password = "*******")
>>> api.changeset_create({"comment": "My first test"})
>>> print(api.node_create({"lon":1, "lat":1, "tag": {}}))
{'changeset': 532907, 'lon': 1, 'version': 1, 'lat': 1, 'tag': {}, 'id': 164684}
>>> api.changeset_close()

OAuth authentication

Username/Password authentication will be deprecated in July 2024 (see official OWG announcemnt for details). In order to use this library in the future, you'll need to use OAuth 2.0.

To use OAuth 2.0, you must register an application with an OpenStreetMap account, either on the development server or on the production server. Once this registration is done, you'll get a client_id and a client_secret that you can use to authenticate users.

auth = OpenStreetMapDevAuth(

Example code using cli-oauth2 on the development server, replace OpenStreetMapDevAuth with OpenStreetMapAuth to use the production server:

import osmapi
from oauthcli import OpenStreetMapDevAuth

client_id = "<client_id>"
client_secret = "<client_secret>"

auth = OpenStreetMapDevAuth(
    client_id, client_secret, ['read_prefs', 'write_map']
).auth_code()

api = osmapi.OsmApi(
    api="https://api06.dev.openstreetmap.org",
    session=auth.session
)

with api.changeset({"comment": "My first test"}) as changeset_id:
    print(f"Part of Changeset {changeset_id}")
    node1 = api.node_create({"lon": 1, "lat": 1, "tag": {}})
    print(node1)

An alternative way using the requests-oauthlib library can be found in the examples.

User agent / credit for application

To credit the application that supplies changes to OSM, an appid can be provided. This is a string identifying the application. If this is omitted "osmapi" is used.

api = osmapi.OsmApi(
    api="https://api06.dev.openstreetmap.org",
    appid="MyOSM Script"
)

If then changesets are made using this osmapi instance, they get a tag created_by with the following content: MyOSM Script (osmapi/<version>)

Example changeset of Kort using osmapi

Note about imports / automated edits

Scripted imports and automated edits should only be carried out by those with experience and understanding of the way the OpenStreetMap community creates maps, and only with careful planning and consultation with the local community.

See the Import/Guidelines and Automated Edits/Code of Conduct for more information.

Development

If you want to help with the development of osmapi, you should clone this repository and install the requirements:

make deps

Better yet use the provided setup.sh script to create a virtual env and install this package in it.

You can lint the source code using this command:

make lint

And if you want to reformat the files (using the black code style) simply run:

make format

To run the tests use the following command:

make test

Release

To create a new release, follow these steps (please respect Semantic Versioning):

  1. Adapt the version number in osmapi/__init__.py
  2. Update the CHANGELOG with the version
  3. Re-build the documentation (make docs)
  4. Create a pull request to merge develop into main (make sure the tests pass!)
  5. Create a new release/tag on GitHub (on the main branch)
  6. The publication on PyPI happens via GitHub Actions on every tagged commit

Attribution

This project was orginally developed by Etienne Chové. This repository is a copy of the original code from SVN (http://svn.openstreetmap.org/applications/utils/python_lib/OsmApi/OsmApi.py), with the goal to enable easy contribution via GitHub and release of this package via PyPI.

See also the OSM wiki: http://wiki.openstreetmap.org/wiki/Osmapi

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osmapi-5.0.0.tar.gz (47.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osmapi-5.0.0-py3-none-any.whl (54.8 kB view details)

Uploaded Python 3

File details

Details for the file osmapi-5.0.0.tar.gz.

File metadata

  • Download URL: osmapi-5.0.0.tar.gz
  • Upload date:
  • Size: 47.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for osmapi-5.0.0.tar.gz
Algorithm Hash digest
SHA256 244b0fb432f7810a38b38a13eacc6d53df5441a1ea02a5e952b0fb1c44f2a420
MD5 63337b73ad46d3301d5e42561dd8f7cd
BLAKE2b-256 e672a085a221fa51fd2686e27228a954161d2ee2828c1e5c41f42f8c334d3509

See more details on using hashes here.

Provenance

The following attestation bundles were made for osmapi-5.0.0.tar.gz:

Publisher: publish_python.yml on metaodi/osmapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file osmapi-5.0.0-py3-none-any.whl.

File metadata

  • Download URL: osmapi-5.0.0-py3-none-any.whl
  • Upload date:
  • Size: 54.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for osmapi-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5fb46e8f896e02e457811ca0b20dae70937409887c7e891acd1b7b6fb0fc22c5
MD5 bfc984ca68e8308f63bf96b368d91107
BLAKE2b-256 a92fb5f9d150226b49974e3b5ceae2460080ccae1e03f3e0876c09d62740fce8

See more details on using hashes here.

Provenance

The following attestation bundles were made for osmapi-5.0.0-py3-none-any.whl:

Publisher: publish_python.yml on metaodi/osmapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page