SimpleGeo with Python
今日 SimpleGeo という位置情報APIを提供するクラウドサービスにふれた。中で使っているDBは
We call it GiselleDB. Based on Cassandra and other NoSQL
らしい。
SimpleGeoにはLayerという位置情報データを貯めておくストレージがあって、1つだけなら無料で作って動かすことができる。この無料プランの場合、APIの呼び出しは月100万回まで。 有料プランは$399〜$9999まであってAmazonS3にバックアップができる。
SimpleGeo – Plans & Pricing
面白そうなのが Layer MarketPlace で、いくつか無償のLayerが提供されている。天気や震源地のLayerや米国政府が提供している保養地のLayerとかもある。これらを自分のLayerと組み合わせたりして、マッシュアップアプリが作れる。
以下アカウントをつくってPythonで使うAPIのチュートリアルをやってみた。
How do I get started using Python?
必要なモジュールのインストール
$ pip insatll httplib2 $ pip install oauth2 $ git clone http://github.com/simplegeo/python-simplegeo.git $ cd python-simplegeo $ python setup.py install
OAuth TokenはAccount Settingsで得られる
>>> import simplegeo >>> from simplegeo import Record >>> oauth_token = 'xxxxxxxxxxxxxxxxxxxxxxx' >>> oauth_secret = 'xxxxxxxxxxxxxxxxxxxxxxx' >>> client = simplegeo.Client(oauth_token, oauth_secret)
Layer MarketPlaceにあったgeonames
(com.simplegeo.global.geonames)に近所の座標を与えて半径0.5kmを検索
>>> dic = client.get_nearby('com.simplegeo.global.geonames', \
>>> 35.7031, 139.5797, limit=1, radius=0.5)
>>> simplejson.dumps(dic, indent=True)
{
"next_cursor": "XaUpV0Qdvf8oSm160myzhy2zce2pshtRJ2aPOqf0aVhy2udPYjp0TxrgzwtH_dtwsu3OA3gLWWfv32HtTYKJ2aS4",
"type": "FeatureCollection",
"features": [
{
"distance": 421.1493384988064,
"created": 1269776951,
"geometry": {
"type": "Point",
"coordinates": [
139.58269999999999,
35.700200000000002
]
},
"properties": {
"admin2": "",
"layer": "com.simplegeo.global.geonames",
"elevation": "",
"name": "Kichijoji Tokyu Inn",
"gtopo30": "54",
"feature_class": "S",
"admin3": "",
"cc2": "",
"admin1": "",
"feature_code": "HTL",
"last_modified": "2007-04-13",
"admin4": "",
"country_code": "JP",
"asciiname": "Kichijoji Tokyu Inn",
"alternatenames": "",
"timezone": "Asia/Tokyo",
"type": "place",
"population": "0"
},
"layerLink": {
"href": "http://api.simplegeo.com/0.1/layer/com.simplegeo.global.geonames.json"
},
"type": "Feature",
"id": "6471863",
"selfLink": {
"href": "http://api.simplegeo.com/0.1/records/com.simplegeo.global.geonames/6471863.json"
}
}
]
}
リバースジオコーディングは米国のみしか使えない。
>>> address = client.get_nearby_address(35.7031, 139.5797) >>> print address simplegeo.APIError: Cannot query outside the US. (#404)
自分のLayerに座標を入れる
>>> r = Record('com.yamakk.test0', '4', 35.7031, 139.5795)
>>> client.add_record(r)
時間や好きなデータ構造を入れる
>>> r = Record('com.yamakk.test0', '5', 35.705045, 139.577916, 'place',
>>> 379868400, foo = 'bar', title='tokyu BLD', prop={'hello':'world'})
>>> client.add_record(r)
こんな感じで見れる。
Posted: July 6th, 2010 | Author: yamakk | Filed under: 技術 | Tags: api, cassandra, geo, gis, nosql, python, simplegeo | No Comments »

Leave a Reply