PythonOsmApi
From OpenStreetMap Wiki
| Scripted imports and automated edits should only be carried out by those with experience and understanding of the way the OpenStreetMap community create maps, and only with careful planning and consultation. See Import/Guidelines and Automated Edits/Code of Conduct |
This documentation is about a python class to communicate with OpenStreetMap API (version 0.6) developped by User:EtienneChove. Others features may be included if required by someone.
Contents |
Hello World : node download
>>> import OsmApi >>> MyApi = OsmApi.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}
Constructor
The OmsApi constructor may get :
| Argument | Definition | Present since | Default |
|---|---|---|---|
| api | IP or alias of the API server | 0.2 | "www.openstreetmap.org" |
| username | API user (used only for write functions such as Update, Delete, Create...) | 0.2 | None |
| password | API password (used only for write functions such as Update, Delete, Create...) | 0.2 | None |
| passwordfile | A file where the password can be found (file format should be user:password). If no username is given, but a password file is, username will be the first username in the password file. | 0.2.1 | None |
| appid | The name of the application built on top of the library. The appid together with the library version and the default identifier, will be used to set the created_by tag in the created change sets. When appid is provided, created_by's value will be <appid> (PythonOsmApi/<version>), otherwise it will be PythonOsmApi/<version>. | 0.2.2 | "" |
| changesetauto | If True, changesets are created and closed automatically. Tags are taken from changesetautotags. It uses changeset/#/upload. The last changeset upload will be trown by .flush() method or by garbage collector. | 0.2.9 | False |
| changesetautotags | Cf changesetauto. | 0.2.9 | {} |
| changesetautosize | Cf changesetauto. | 0.2.9 | 500 |
| changesetautomulti | Cf changesetauto. | 0.2.14 | 1 |
| debug | If True, OsmApi will print some debug (such as urls) messages on stderr. | 0.2.15 | False |
>>> import OsmApi >>> MyApi = OsmApi.OsmApi(api="api06.dev.openstreetmap.org", username = "you", password = "***") >>> MyApi = OsmApi.OsmApi(username = "you", passwordfile = "/etc/mypasswords") >>> MyApi = OsmApi.OsmApi(passwordfile = "/etc/mypasswords") # username will be first line username
Note : username and password are not required for read-only methods (e.g. NodeGet, WayGet, NodeHistory).
Methods
An OmsApi object has these methods:
| Method | Arguments | Result | Comment |
|---|---|---|---|
| Capabilities | Returns API capabilities. eg:{u'changesets': {u'maximum_elements': 50000.0}, u'waynodes': {u'maximum': 2000.0}, u'area': {u'maximum': 0.25}, u'version': {u'minimum': 0.6, u'maximum': 0.6}, u'timeout': {u'seconds': 300.0}, u'tracepoints': {u'per_page': 5000.0}} | ||
| NodeGet |
|
NodeData as dict(id, lat, lon, tag, changeset, version, user, uid, timestamp) | |
| NodeCreate |
|
NodeData as dict(lat, lon, tag) | Need changeset |
| NodeUpdate |
|
NodeData as dict(id, lat, lon, tag, changeset, version) | Need changeset |
| NodeDelete |
|
NodeData as dict(id, lat, lon, tag, changeset, version) | Need changeset |
| NodeHistory |
|
dict(NodeVersion: NodeData like in NodeGet) | |
| NodeWays |
|
list(WayData containing this node) | |
| NodeRelations |
|
list(RelationData containing this node) | |
| NodesGet |
|
dict(NodeId: NodeData) | |
| WayGet |
|
NodeData as dict(nd, tag, changeset, version, user, uid, timestamp) | |
| WayCreate |
|
WayData as dict(nd, tag, changeset, version) | Need changeset |
| WayUpdate |
|
WayData as dict(nd, tag, changeset, version) | Need changeset |
| WayDelete |
|
WayData as dict(nd, tag, changeset, version) | Need changeset |
| WayHistory |
|
dict(WayVersion: WayData like in WayGet) | |
| WayRelations |
|
list(RelationData containing this way) | |
| WayFull |
|
way", data: NodeData|WayData)) | |
| WaysGet |
|
dict(WayId: WayData) | |
| RelationGet |
|
RelationData as dict(member, tag, changeset, version, user, uid, timestamp) | |
| RelationCreate |
|
RelationData as dict(member, tag, changeset, version) | Need changeset |
| RelationUpdate |
|
RelationData as dict(member, tag, changeset, version) | Need changeset |
| RelationDelete |
|
RelationData as dict(member, tag, changeset, version) | Need changeset |
| RelationHistory |
|
dict(WayVersion: RelationData like in RelationGet) | |
| RelationRelations |
|
list(RelationData containing this relation as member) | |
| RelationFull |
|
way|relation", data: NodeData|WayData|RelationData)) | |
| RelationFullRecur |
|
way|relation", data: NodeData|WayData|RelationData)) | Will download data recursively. RelationRecur only gets two levels members. Usefull for relation containing relations. |
| RelationsGet |
|
dict(RelationId: RelationData) | |
| ChangesetGet |
|
ChangesetData (without changes) | |
| ChangesetDownload |
|
list of dict {type: node/way/relation, action: create/delete/modify, data: {...} } | |
| ChangesetUpload |
|
Updated list with new ids | |
| ChangesetCreate |
|
ChangesetId (useless) | |
| ChangesetUpdate |
|
||
| ChangesetClose | ChangesetId (useless) | ||
| ChangesetsGet |
All args are optional.
|
dict(ChangesetId:ChangesetInfo) | |
| Map |
|
MapData inside givent bounding box | |
| ParseOsm |
|
Data formated with python objects | |
| ParseOsc |
|
Data formated with python objects |
Note :
- Dict keys are unicode
Data format :
- changeset is integer
- version is integer
- tag is a dictionary (keys and values should be unicode)
- user is unicode
- uid is integer
- timestamp is unicode
- node lat and lon are floats
- way nd is list of integers
- relation member is a list of dictionary like {u"role":u"", u"ref":123, u"type":u"node"}
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 100)
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
>>> import OsmApi >>> MyApi = OsmApi.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()
Download
You can download it at :