Skip to main content

Wrapper for the OneMap API

Project description

OneMap

This library provides a thin wrapper over the OneMapSG API (http://www.onemap.sg/api/help/)

You’ll need an access key from OneMap before you’ll be able to use the OneMap API

Examples

>>>
>>> import onemap
>>> OM = onemap.OneMap("onemap_key")
>>>
>>> res = OM.search_address("city hall")
>>> type(res)
<class 'core.ext.onemap.OneMapResult'>
>>>
>>> res
[{u'searchval': u'CITY HALL', u'category': u'Building', u'y': u'30360.5886', u'x': u'30051.7694'}, {u'searchval': u'CITY HALL (MONUMENTS)', u'category': u'Culture', u'y':     u'30339.5771', u'x': u'30047.6749'}, {u'searchval': u'CITY HALL (STREET AND PLACES)', u'category': u'Culture', u'y': u'30318.8100', u'x': u'30064.6100'}, {u'searchval': u'CITY     HALL AND THE PADANG (HERITAGE SITES)', u'category': u'Culture', u'y': u'30340.3699', u'x': u'30049.0300'}, {u'searchval': u'CITY HALL MRT STATION', u'category': u'Building',     u'y': u'30597.7817', u'x': u'30139.4704'}, {u'searchval': u'CITY HALL MRT STATION (EW13/NS25) (FRIENDLY BUILDINGS)', u'category': u'Community', u'y': u'30597.7817', u'x':     u'30139.4704'}, {u'searchval': u'CITY HALL MRT STATION EXIT A', u'category': u'Building', u'y': u'30611.1767', u'x': u'30182.8492'}, {u'searchval': u'CITY HALL MRT STATION EXIT     B', u'category': u'Building', u'y': u'30610.8982', u'x': u'30073.6413'}, {u'searchval': u'CITY HALL/ESPLANADE MRT STATION EXIT A', u'category': u'Building', u'y':     u'30415.3350', u'x': u'30369.4938'}, {u'searchval': u'CITY HALL/ESPLANADE MRT STATION EXIT B', u'category': u'Building', u'y': u'30457.5147', u'x': u'30385.8054'},     {u'searchval': u'CITY HALL/ESPLANADE MRT STATION EXIT C', u'category': u'Building', u'y': u'30412.5744', u'x': u'30434.3890'}, {u'searchval': u'CITY HALL/ESPLANADE MRT STATION     EXIT D', u'category': u'Building', u'y': u'30367.6908', u'x': u'30424.1965'}, {u'searchval': u'CITY HALL/ESPLANADE MRT STATION EXIT E', u'category': u'Building', u'y':     u'30479.6293', u'x': u'30394.6855'}]
>>>

Queries return a OneMapResult object, containing one or more OneMapResultItems

>>> geo = OM.geocode("%s,%s" % (res[0]['x'], res[0]['y']))
>>> type(geo)
<class 'core.ext.onemap.OneMapResult'>
>>>
>>> geo
[{u'buildingname': u'CITY HALL', 'y': 30338.9616, u'block': u'3', u'postalcode': u'178958', 'x': 30047.2715, u'road': u"SAINT ANDREW'S ROAD"}, {u'buildingname': u'THE ADELPHI',     'y': 30398.6047, u'block': u'1', u'postalcode': u'179803', 'x': 29987.9521, u'road': u'COLEMAN STREET'}, {u'buildingname': u'THE OLD SUPREME COURT BUILDING', 'y': 30253.3835,     u'block': u'1', u'postalcode': u'178957', 'x': 30025.5209, u'road': u"SAINT ANDREW'S ROAD"}, {u'buildingname': u'SUPREME COURT BUILDING', 'y': 30322.8247, u'block': u'1',     u'postalcode': u'178879', 'x': 29938.7931, u'road': u'SUPREME COURT LANE'}, {u'buildingname': u'FORMER SUPREME COURT BUILDING', 'y': 30250.6217, u'block': u'1', u'postalcode':     u'178957', 'x': 29985.2869, u'road': u"SAINT ANDREW'S ROAD"}]
>>>

Queries can return many results, so .filter(key=value) allows you to quickly filter by a particular value

>>> geo.filter(buildingname='CITY HALL')
[{u'buildingname': u'CITY HALL', 'y': 30338.9616, u'block': u'3', u'postalcode': u'178958', 'x': 30047.2715, u'road': u"SAINT ANDREW'S ROAD"}]
>>>
>>> geo.filter(block='1')
[{u'buildingname': u'THE ADELPHI', 'y': 30398.6047, u'block': u'1', u'postalcode': u'179803', 'x': 29987.9521, u'road': u'COLEMAN STREET'}, {u'buildingname': u'THE OLD SUPREME     COURT BUILDING', 'y': 30253.3835, u'block': u'1', u'postalcode': u'178957', 'x': 30025.5209, u'road': u"SAINT ANDREW'S ROAD"}, {u'buildingname': u'SUPREME COURT BUILDING', 'y':     30322.8247, u'block': u'1', u'postalcode': u'178879', 'x': 29938.7931, u'road': u'SUPREME COURT LANE'}, {u'buildingname': u'FORMER SUPREME COURT BUILDING', 'y': 30250.6217,     u'block': u'1', u'postalcode': u'178957', 'x': 29985.2869, u'road': u"SAINT ANDREW'S ROAD"}]
>>>
>>> type(geo[0])
<class 'core.ext.onemap.OneMapResultItem'>
>>>
>>> print geo[0]
{u'buildingname': u'CITY HALL', 'y': 30338.9616, u'block': u'3', u'postalcode': u'178958', 'x': 30047.2715, u'road': u"SAINT ANDREW'S ROAD"}
>>>

Feeling lucky? .resolve() cuts to the chase and returns you the first result it gets, or, raises an exception

>>>
>>> OM.resolve("the white house")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "core/ext/onemap/__init__.py", line 280, in resolve
    raise OneMapError(res.error)
core.ext.onemap.OneMapError: No result(s) found.
>>>
>>> res = OM.resolve("istana")
>>> type(res)
<type 'dict'>
>>> res
{u'buildingname': u'ISTANA', 'coordinates': [103.84309296124401, 1.3068716489030177], u'postalcode': u'238823', u'block': u'0', 'y': 32132.8209, 'x': 29087.8352, u'road':     u'ORCHARD ROAD'}

SVY21

The OneMap API returns coordinates in the SVY21 format (instead of the more commonly found WGS84 lat,lng format)

There is a OneMap API that can be called to convert between these two projection systems (that hasn’t been wrapped by this library yet).

However, if you’d like to be able to convert the points without hitting an external API, do check out this awesome opensource library

https://github.com/cgcai/SVY21

A copy of the Python version of the awesome SVY21 library has been included in this repo, but all credit goes to the team, naturally.

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

onemap-0.2.1.tar.gz (8.5 kB view details)

Uploaded Source

File details

Details for the file onemap-0.2.1.tar.gz.

File metadata

  • Download URL: onemap-0.2.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for onemap-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1d726dc479ef112d653a8c6f22f5e395197918f6d9dc06b6ea42cc4f8cf93845
MD5 6e69acca54f8aabc3bf7213d97025abe
BLAKE2b-256 3ac6cb21c63658a8029d56a00570a434522d3bcd8c3f3a3a37267cc8f3348c12

See more details on using hashes here.

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