Yahoo! 逆住所ジオコーダAPIを呼び出す

Yahoo!の逆住所ジオコーダAPIで、緯度経度情報から住所情報を取得する(逆住所ジオコーディング、reverse geocoding)AppleScriptです。

Yahoo!に開発者登録(無料)して、アプリケーションIDを取得し、リスト中のretAccessKey()ハンドラにアプリケーションIDを記入すると実行可能です。

番地表記までの詳細な住所情報を取得できますが、天気予報の情報を検索するために都道府県(prefecture)と区市(city)の情報のみ取り出しています。

AppleScript名:Yahoo! 逆住所ジオコーダAPIを呼び出す
– Created 2016-11-20 by Takaaki Naganoya
– 2016 Piyomaru Software
use AppleScript version “2.5″
use scripting additions
use framework “Foundation”
–http://piyocast.com/as/archives/4340

–http://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/reversegeocoder.html

set aLat to 35.74 as string
set aLon to 139.6 as string
set aResList to reverseGeoCoderByYahoo(aLat, aLon) of me
–>  {”東京都”, “練馬区”}

on reverseGeoCoderByYahoo(aLat, aLon)
  set reqURLStr to “http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder”
  
set aKey to retAccessKey() of me
  
set aRec to {lat:aLat, lon:aLon, output:“json”, appid:aKey, datum:“tky”} –日本測地系
  
set aURL to retURLwithParams(reqURLStr, aRec) of me
  
set aRes to callRestGETAPIAndParseResults(aURL) of me
  
  
set aRESCode to responseCode of aRes
  
if aRESCode is not equal to 200 then return false
  
set aRESHeader to responseHeader of aRes
  
  
set aJSONres to (json of aRes)
  
set anAddress to (aJSONres’s valueForKeyPath:“Feature.Property”)’s firstObject()
  
set anElement to anAddress’s valueForKeyPath:“AddressElement”
  
  
set aPrefecture to first item of (my filterRecListByLabel1(anElement, “Level == ’prefecture’”))
  
–>  {Kana:”とうきょうと”, Name:”東京都”, Level:”prefecture”, Code:”13″}
  
  
set aCity to first item of (my filterRecListByLabel1(anElement, “Level == ’city’”))
  
–>  {Kana:”ねりまく”, Name:”練馬区”, Level:”city”, Code:”13120″}
  
  
set aPref to |Name| of aPrefecture
  
set aCt to |Name| of aCity
  
return {aPref, aCt}
end reverseGeoCoderByYahoo

–GET methodのREST APIを呼ぶ
on callRestGETAPIAndParseResults(aURL)
  set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
  
  
aRequest’s setHTTPMethod:“GET”
  
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
  
aRequest’s setHTTPShouldHandleCookies:false
  
aRequest’s setTimeoutInterval:60
  
aRequest’s setValue:“application/json” forHTTPHeaderField:“Accept”
  
  
–CALL REST API
  
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
  
  
–Parse Results
  
set resList to aRes as list
  
  
set bRes to contents of (first item of resList)
  
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
  
  
set jsonString to current application’s NSString’s stringWithString:resStr
  
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
  
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
  
  
–Get Response Code & Header
  
set dRes to contents of second item of resList
  
if dRes is not equal to missing value then
    set resCode to (dRes’s statusCode()) as number
    
set resHeaders to (dRes’s allHeaderFields()) as record
  else
    set resCode to 0
    
set resHeaders to {}
  end if
  
  
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestGETAPIAndParseResults

on retURLwithParams(aBaseURL, aRec)
  set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec
  
  
set aKeyList to (aDic’s allKeys()) as list
  
set aValList to (aDic’s allValues()) as list
  
set aLen to length of aKeyList
  
  
set qList to {}
  
repeat with i from 1 to aLen
    set aName to contents of item i of aKeyList
    
set aVal to contents of item i of aValList
    
set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal)
  end repeat
  
  
set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL
  
aComp’s setQueryItems:qList
  
set aURL to (aComp’s |URL|()’s absoluteString()) as text
  
  
return aURL
end retURLwithParams

on retAccessKey()
  return “xxXxxxXxXXxxxXXXXXXXXXXXXXXxXXXxxxXXxXXxxXXxxxXXXxxXXXX-” –Yahoo! API Key
end retAccessKey

on urlencodeStr(aStr)
  set aString to current application’s NSString’s stringWithString:aStr
  
set aString to (aString’s stringByAddingPercentEncodingWithAllowedCharacters:(current application’s NSCharacterSet’s URLQueryAllowedCharacterSet())) as text
  
return aString
end urlencodeStr

–リストに入れたレコードを、指定の属性ラベルの値で抽出
on filterRecListByLabel1(aRecList as list, aPredicate as string)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to filteredArray as list
  
return bList
end filterRecListByLabel1

★Click Here to Open This Script 

Leave a Reply