Osmapi

From OpenStreetMap Wiki
(Redirected from PythonOsmApi)
Jump to: navigation, search

This documentation is about a python class to communicate with OpenStreetMap API v0.6 developped by User:EtienneChove (currently maintained by User:Metaodi).

Please submit an issue if you require other features.

Hello World : node download

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

Documentation

The documentation is available online: http://osmapi.metaodi.ch.

Examples

Changeset Automated Example

>>> api = OsmApi(api="api06.dev.openstreetmap.org", username="EtienneChove", passwordfile="/home/etienne/osm/passwords", changesetauto=True, changesetautotags={"comment":u"changeset auto"})
>>> for i in range(100):
>>>   node = {"id":-i, "lat":i, "lon":i, "tag":{}}
>>>   api.NodeCreate(node)
>>> api.flush() # to send last updates
  • changesetautosize : size of each upload (default 500)
  • changesetautomulti : number of upload between changeset close/create (default 1)

Example:

>>> api = OsmApi(api="api06.dev.openstreetmap.org", username="EtienneChove", passwordfile="/home/etienne/osm/passwords", changesetauto=True, changesetautotags={"comment":u"changeset auto"}, changesetautosize=1000, changesetautomulti=10)

This will upload every 1000 updates, and will close/create changeset every 50 upload (so every 50.000 elementary update).

Full Example

>>> from osmapi import OsmApi
>>> MyApi = OsmApi(username = u"EtienneChove", password = u"*******")
>>> MyApi.ChangesetCreate({u"comment": u"My first test"})
>>> print MyApi.NodeCreate({u"lon":1, u"lat":1, u"tag": {}})
{u'changeset': 532907, u'lon': 1, u'version': 1, u'lat': 1, u'tag': {}, u'id': 164684}
>>> MyApi.ChangesetClose()

Full Example with a password file

  • Create an API handle with endpoint and credentials
  • Use API handle to create a changeset
  • Make changes; flush changes;
  • Close changeset
  • Verify if data is available on the map (visually or by using NodeGet or ChangeSetGet)
In [1]: from osmapi import OsmApi
In [2]: api_handle = OsmApi(api="api06.dev.openstreetmap.org", passwordfile="/tmp/osm.password")
In [3]: api_handle.ChangesetCreate({u'comment': u'Test Node Creation'})
''
Out[3]: 86112
In [4]: data = { u"lat": 28.491903, u"lon": 77.0938, u"tag": { u"amenity": u"atm", u"name": u"Central Bank ATM",  u"operator": u"Central Bank of India", u"opening_hours": u"24/7",  u"fee": u"some", u"cash-in": u"no", u"drive-through": u"no", u"currency:INR": u"yes", u"language:en": u"yes", u"language:hi": u"yes" } }
In [5]: api_handle.NodeCreate(data)
''
Out[5]:
{u'changeset': 86112,
 u'id': 4303625527,
 u'lat': 28.491903,
 u'lon': 77.0938,
 u'tag': {u'amenity': u'atm',
  u'cash-in': u'no',
  u'currency:INR': u'yes',
  u'drive-through': u'no',
  u'fee': u'some',
  u'language:en': u'yes',
  u'language:hi': u'yes',
  u'name': u'Central Bank ATM',
  u'opening_hours': u'24/7',
  u'operator': u'Central Bank of India'},
 u'version': 1}
In [6]: api_handle.ChangesetClose()
''
Out[6]: 86112

Deleting nodes

  • Create a changeset
  • Delete nodes; flush
  • Close changeset
In [20]: api_handle.ChangesetCreate()
''
Out[20]: 86121
In [21]: byebye = api_handle.NodeGet(4303625527)
''

In [22]: byebye
Out[22]:
{u'changeset': 86112,
 u'id': 4303625527,
 u'lat': 28.491903,
 u'lon': 77.0938,
 u'tag': {u'amenity': u'atm',
  u'cash-in': u'no',
  u'currency:INR': u'yes',
  u'drive-through': u'no',
  u'fee': u'some',
  u'language:en': u'yes',
  u'language:hi': u'yes',
  u'name': u'Central Bank ATM',
  u'opening_hours': u'24/7',
  u'operator': u'Central Bank of India'},
 u'timestamp': datetime.datetime(2016, 6, 10, 7, 25, 51),
 u'uid': 3589,
 u'user': u'marauder',
 u'version': 1,
 u'visible': True}
In [23]: api_handle.NodeDelete(byebye)
''
Out[23]:
{u'changeset': 86121,
 u'id': 4303625527,
 u'lat': 28.491903,
 u'lon': 77.0938,
 u'tag': {u'amenity': u'atm',
  u'cash-in': u'no',
  u'currency:INR': u'yes',
  u'drive-through': u'no',
  u'fee': u'some',
  u'language:en': u'yes',
  u'language:hi': u'yes',
  u'name': u'Central Bank ATM',
  u'opening_hours': u'24/7',
  u'operator': u'Central Bank of India'},
 u'uid': 3589,
 u'user': u'marauder',
 u'version': 2,
 u'visible': False}

In [24]: api_handle.ChangesetClose()
''
Out[24]: 86121

Install and contribution

The code from the SVN-Server of OpenStreetMap has been moved to GitHub by User:Metaodi:

There you can contribute to the project of you like (e.g. by creating issues or pull requests). The osmapi package is also available via Python Package Index (PyPI) and can therefore be integrated in Python projects pretty easily.


Installation with pip

pip install osmapi