都道府県リストから隣接都道府県を含む該当のコードを抽出する

膨大な位置情報の距離を計算するような場合に、対象の都道府県と隣接する都道府県に限定して距離計算を行うことで計算量を削減するためのAppleScriptです。

対象は日本国内の都道府県。海外の住所情報でも同様に都道府県にコードを振って隣接情報をデータ化すれば同様の処理は可能です。

AppleScript名:都道府県リストから隣接都道府県を含む該当のコードを抽出する
— Created 2015-12-06 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html

property prefList : {{prefCode:1, prefName:"北海道", neighbors:{}}, {prefCode:2, prefName:"青森県", neighbors:{3, 5}}, {prefCode:3, prefName:"岩手県", neighbors:{2, 4, 5}}, {prefCode:4, prefName:"宮城県", neighbors:{3, 5, 6, 7}}, {prefCode:5, prefName:"秋田県", neighbors:{2, 3, 4, 6}}, {prefCode:6, prefName:"山形県", neighbors:{3, 4, 5, 7, 15}}, {prefCode:7, prefName:"福島県", neighbors:{4, 6, 8, 9, 10, 15}}, {prefCode:8, prefName:"茨城県", neighbors:{7, 9, 10, 11, 12}}, {prefCode:9, prefName:"栃木県", neighbors:{7, 8, 10, 11, 12}}, {prefCode:10, prefName:"群馬県", neighbors:{7, 9, 11, 15, 20}}, {prefCode:11, prefName:"埼玉県", neighbors:{8, 9, 10, 12, 13, 19, 20}}, {prefCode:12, prefName:"千葉県", neighbors:{8, 11, 13}}, {prefCode:13, prefName:"東京都", neighbors:{11, 12, 19, 14}}, {prefCode:14, prefName:"神奈川県", neighbors:{13, 19, 22}}, {prefCode:15, prefName:"新潟県", neighbors:{6, 7, 10, 16, 20}}, {prefCode:16, prefName:"富山県", neighbors:{15, 17, 20, 21}}, {prefCode:17, prefName:"石川県", neighbors:{16, 18, 21}}, {prefCode:18, prefName:"福井県", neighbors:{17, 21, 25, 26}}, {prefCode:19, prefName:"山梨県", neighbors:{11, 13, 14, 20, 22}}, {prefCode:20, prefName:"長野県", neighbors:{10, 11, 15, 16, 19, 21, 22, 23}}, {prefCode:21, prefName:"岐阜県", neighbors:{16, 17, 18, 20, 23, 24, 25}}, {prefCode:22, prefName:"静岡県", neighbors:{14, 19, 20, 23}}, {prefCode:23, prefName:"愛知県", neighbors:{20, 21, 22, 24}}, {prefCode:24, prefName:"三重県", neighbors:{21, 23, 25, 26, 29}}, {prefCode:25, prefName:"滋賀県", neighbors:{18, 21, 24, 26}}, {prefCode:26, prefName:"京都府", neighbors:{18, 24, 25, 27, 28, 29}}, {prefCode:27, prefName:"大阪府", neighbors:{26, 29, 28, 30}}, {prefCode:28, prefName:"兵庫県", neighbors:{26, 27, 31, 33}}, {prefCode:29, prefName:"奈良県", neighbors:{24, 25, 26, 27, 30}}, {prefCode:30, prefName:"和歌山県", neighbors:{24, 27, 29}}, {prefCode:31, prefName:"鳥取県", neighbors:{28, 33, 32, 34}}, {prefCode:32, prefName:"島根県", neighbors:{31, 34, 35}}, {prefCode:33, prefName:"岡山県", neighbors:{28, 31, 34}}, {prefCode:34, prefName:"広島県", neighbors:{33, 31, 32, 35}}, {prefCode:35, prefName:"山口県", neighbors:{32, 34}}, {prefCode:36, prefName:"徳島県", neighbors:{37, 39}}, {prefCode:37, prefName:"香川県", neighbors:{36, 38, 39}}, {prefCode:38, prefName:"愛媛県", neighbors:{37, 39}}, {prefCode:39, prefName:"高知県", neighbors:{36, 37, 38}}, {prefCode:40, prefName:"福岡県", neighbors:{44, 43, 41}}, {prefCode:41, prefName:"佐賀県", neighbors:{40, 42}}, {prefCode:42, prefName:"長崎県", neighbors:{41}}, {prefCode:43, prefName:"熊本県", neighbors:{40, 42, 44, 45, 46}}, {prefCode:44, prefName:"大分県", neighbors:{40, 43, 45}}, {prefCode:45, prefName:"宮崎県", neighbors:{43, 44, 46}}, {prefCode:46, prefName:"鹿児島県", neighbors:{43, 45}}, {prefCode:47, prefName:"沖縄県", neighbors:{}}}

set aPref to 13 –Tokyo
set aRes to my filterRecListByLabel2(prefList, "prefCode == [c]%@", {aPref})
set targList to neighbors of aRes & aPref
–>  {​​​​​11, ​​​​​12, ​​​​​19, ​​​​​14, ​​​​​13​​​}–Saitama, Chiba, yamanashi, Kanagawa, Tokyo

–リストに入れたレコードを、指定の属性ラベルの値で抽出(predicateとパラメータを分離)し、1つのアイテムだけを返す
on filterRecListByLabel2(aRecList as list, aPredicate as string, aParam)
  set aArray to current application’s NSArray’s arrayWithArray:aRecList
  
set aPredicate to current application’s NSPredicate’s predicateWithFormat:aPredicate argumentArray:aParam
  
set filteredArray to aArray’s filteredArrayUsingPredicate:aPredicate
  
set bList to ASify from filteredArray as list
  
set cList to first item of bList
  
return cList
end filterRecListByLabel2

★Click Here to Open This Script 

Please follow and like us:

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です