PythonOsmApi

From OpenStreetMap Wiki
Jump to: navigation, search


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
  • NodeId
  • optional NodeVersion
NodeData as dict(id, lat, lon, tag, changeset, version, user, uid, timestamp)
NodeCreate
  • NodeData as dict(lat, lon, tag)
NodeData as dict(lat, lon, tag) Need changeset
NodeUpdate
  • NodeData as dict(id, lat, lon, tag, changeset, version)
NodeData as dict(id, lat, lon, tag, changeset, version) Need changeset
NodeDelete
  • NodeData as dict(id, lat, lon, tag, changeset, version)
NodeData as dict(id, lat, lon, tag, changeset, version) Need changeset
NodeHistory
  • NodeId
dict(NodeVersion: NodeData like in NodeGet)
NodeWays
  • NodeId
list(WayData containing this node)
NodeRelations
  • NodeId
list(RelationData containing this node)
NodesGet
  • NodeIdList
dict(NodeId: NodeData)
WayGet
  • WayId
  • optional WayVersion
NodeData as dict(nd, tag, changeset, version, user, uid, timestamp)
WayCreate
  • WayData as dict(nd, tag)
WayData as dict(nd, tag, changeset, version) Need changeset
WayUpdate
  • WayData as dict(nd, tag, changeset, version)
WayData as dict(nd, tag, changeset, version) Need changeset
WayDelete
  • WayData as dict(nd, tag, changeset, version)
WayData as dict(nd, tag, changeset, version) Need changeset
WayHistory
  • WayId
dict(WayVersion: WayData like in WayGet)
WayRelations
  • WayId
list(RelationData containing this way)
WayFull
  • WayId
way", data: NodeData|WayData))
WaysGet
  • WayIdList
dict(WayId: WayData)
RelationGet
  • RelationId
  • optional RelationVersion
RelationData as dict(member, tag, changeset, version, user, uid, timestamp)
RelationCreate
  • RelationData as dict(member, tag)
RelationData as dict(member, tag, changeset, version) Need changeset
RelationUpdate
  • RelationData as dict(member, tag, changeset, version)
RelationData as dict(member, tag, changeset, version) Need changeset
RelationDelete
  • RelationData as dict(member, tag, changeset, version)
RelationData as dict(member, tag, changeset, version) Need changeset
RelationHistory
  • RelationId
dict(WayVersion: RelationData like in RelationGet)
RelationRelations
  • RelationId
list(RelationData containing this relation as member)
RelationFull
  • RelationId
way|relation", data: NodeData|WayData|RelationData))
RelationFullRecur
  • RelationId
way|relation", data: NodeData|WayData|RelationData)) Will download data recursively. RelationRecur only gets two levels members. Usefull for relation containing relations.
RelationsGet
  • RelationIdList
dict(RelationId: RelationData)
ChangesetGet
  • ChangesetId
ChangesetData (without changes)
ChangesetDownload
  • ChangesetId
list of dict {type: node/way/relation, action: create/delete/modify, data: {...} }
ChangesetUpload
  • list of dict {type: node/way/relation, action: create/delete/modify, data: {...} }
Updated list with new ids
ChangesetCreate
  • tag as dict
ChangesetId (useless)
ChangesetUpdate
  • tag as dict
ChangesetClose ChangesetId (useless)
ChangesetsGet

All args are optional.

  • min_lon=None
  • min_lat=None
  • max_lon=None
  • max_lat=None
  • userid=None
  • username=None
  • closed_after=None
  • created_before=None
  • only_open=False
  • only_closed=False
dict(ChangesetId:ChangesetInfo)
Map
  • min_lon
  • min_lat
  • max_lon
  • max_lat
MapData inside givent bounding box
ParseOsm
  • data (as string)
Data formated with python objects
ParseOsc
  • data (as string)
Data formated with python objects

Note :

Data format :

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

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 :

Personal tools
Namespaces
Variants
Actions
site
Toolbox