みなさん、OpenStreetMap (OSM)のサイトで確認した地域のBoundaryの情報を手軽に取得したくなることはありませんか? 私はたまにあります。
OSMのRelation IDがわかっているときNominatim APIでBoundaryを取得する方法を調べたのでメモがてら残しておこうと思います。
OpenStreetMapのサイトでは検索BoxでOSMのオブジェクトを検索することができます。 OSMにおいて特定の地域の情報はRelationというオブジェクトとして管理されています。
例えば千代田区を探してみるとこのように千代田区のRelationを見つけることができます。
地図を見るとこのRelationのBoundaryが表示されていることが分かります。このBoundaryをデータとして手に入れたいのですが残念ながらこのWebサイトにはBoundaryのデータを取得する手段が用意されていません。
そこで今回はNominatimを使います。
NominatimはOSMのデータを検索したりできるツールです。 Nominatimは複数の組織がホストしていますが、一番手軽に利用できるのはOpenStreetMap Foundation (OSMF) がホストしているものだと思います。 ただしOSMF版は制限がきつめです。 データ分析の際に軽く使用する程度であれば問題はないですが、がっつりクエリを投げるような用途で使用する場合は有償版を契約したほうがよさそうです。 以降はOSMF版の利用を前提に話を進めますが、必要に応じて読み替えて下さい。
NominatimのAPI Referenceによると 次のようなクエリでOSMのデータの取得ができます。
https://nominatim.openstreetmap.org/details?osmtype=[N|W|R]&osmid=<value>&class=<value>
今回興味があるOSMのデータはRelationなのでosmtype
はR
、OSMで検索した結果によると千代田区のosmid
は1761742
です。
Responseはjson
が便利なのでformat=json
を追加します。
そして、resultにgeometryを含めるためにはpolygon_geonson
を1
にする必要があるそうなのでpolygon_geojson=1
も追加します。
ということで以下のクエリで千代田区のRelationの情報を取得します。
https://nominatim.openstreetmap.org/details?osmtype=R&osmid=1761742&format=json&polygon_geojson=1
ブラウザにこのURLを打ち込んでもいいですし、次のようにcurl
を使うのもありです。
$ curl "https://nominatim.openstreetmap.org/details?osmtype=R&osmid=1761742&format=json&polygon_geojson=1"
{"place_id":377733871,"parent_place_id":225058060,"osm_type":"R","osm_id":1761742,"category":"boundary","type":"administrative","admin_level":7,"localname":"千代田区","names":{"name": "千代田区", "name:en": "Chiyoda", "name:es": "Chiyoda", "name:it": "Chiyoda", "name:ja": "千代田区", "name:ko": "치요다", "name:ru": "Тиёда", "name:zh": "千代田區", "name:ja-Hira": "ちよだく", "name:ja-Latn": "Chiyoda-ku", "name:zh-Hans": "千代田区", "name:zh-Hant": "千代田區", "name:eu": "Chiyoda", "_place_name:ko": "지요다구", "name:lt": "Čijoda", "name:pl": "Chiyoda", "name:th": "เขตชิโยดะ", "name:uk": "Чійода"},"addresstags":{"city": "東京都"},"country_code":"jp","indexed_date":"2024-02-05T06:29:39.574311+00:00","importance":0.5587954165289415,"calculated_importance":0.5587954165289415,"extratags":{"place": "city", "wikidata": "Q214051", "land_area": "administrative", "wikipedia": "ja:千代田区", "population": "46397", "linked_place": "city"},"calculated_wikipedia":"ja:千代田区","rank_address":18,"rank_search":14,"isarea":true,"centroid":{"type": "Point","coordinates": [139.7532163, 35.6938097]},"geometry":{"type":"Polygon","coordinates":[[[139.7301347,35.6850171],[139.73036,35.6842285],[139.7306814,35.6835905],[139.7315753,35.6818161],[139.7317655,35.6814442],[139.7319908,35.6813135],[139.7321081,35.6811725],[139.7324283,35.6807877],[139.7327555,35.6804348],[139.7330506,35.6798727],[139.7332276,35.6793149],[139.7336728,35.6789837],[139.7345526,35.6787833],[139.7349657,35.678731],[139.7354699,35.6787397],[139.7361995,35.6787833],[139.7362549,35.6787873],[139.7365642,35.6788094],[139.7366908,35.6784515],[139.7367091,35.6783998],[139.7367527,35.6782974],[139.7370845,35.6775179],[139.7371893,35.6771542],[139.7372599,35.676958],[139.737302,35.6768526],[139.7373269,35.6767908],[139.7373518,35.676738],[139.7373927,35.6766601],[139.7374737,35.6765335],[139.7376121,35.676366],[139.7378179,35.6761211],[139.7379009,35.676031],[139.7380445,35.6758492],[139.7382454,35.6754832],[139.7385667,35.6746946],[139.738612,35.6745832],[139.7386597,35.6744691],[139.7388623,35.6739841],[139.7389558,35.6738278],[139.7390724,35.6736859],[139.7391178,35.6736304],[139.7392009,35.6735436],[139.739622,35.6734059],[139.7414054,35.6722868],[139.7415453,35.6722835],[139.7416615,35.6722807],[139.7426326,35.6716532],[139.7430611,35.6714153],[139.7430782,35.6714058],[139.7431375,35.6713729],[139.7432612,35.6713343],[139.7433302,35.6713028],[139.7448604,35.6710108],[139.7450298,35.6710063],[139.7450303,35.6709556],[139.7450047,35.6707758],[139.7449233,35.670484],[139.745529,35.6703601],[139.7458145,35.6703101],[139.7467469,35.6702049],[139.7476254,35.669611],[139.7481371,35.6700215],[139.7485428,35.6703452],[139.7486929,35.6704575],[139.7494764,35.6709329],[139.7496691,35.6708749],[139.749778,35.6708377],[139.7516443,35.6701986],[139.7517363,35.6701671],[139.751791,35.670136],[139.75205,35.6706072],[139.7521006,35.670625],[139.7521975,35.6706011],[139.7527084,35.670475],[139.7534943,35.6702438],[139.7539141,35.6701048],[139.7552481,35.6696365],[139.7553525,35.6696124],[139.7554563,35.6695573],[139.7555403,35.6695209],[139.7556352,35.6694912],[139.7569026,35.6690942],[139.7569775,35.6690707],[139.7570626,35.6690442],[139.757918,35.6687804],[139.7580261,35.668747],[139.758555,35.6686296],[139.7585894,35.6687497],[139.7587553,35.6691331],[139.7587954,35.6691493],[139.7590301,35.6695726],[139.7593304,35.6701014],[139.7594914,35.6704106],[139.7595544,35.6705317],[139.7601156,35.6715201],[139.760398,35.6720175],[139.7604246,35.6720681],[139.7605227,35.6721907],[139.760642,35.6723283],[139.7607989,35.6724438],[139.7609881,35.6725382],[139.7611866,35.6726058],[139.7615956,35.672706],[139.7623183,35.6728919],[139.7625945,35.6729503],[139.7630775,35.6730622],[139.7636341,35.6732322],[139.7638888,35.6733513],[139.7639845,35.6734227],[139.7640914,35.6735024],[139.7641987,35.673632],[139.7644454,35.674021],[139.7646723,35.6744155],[139.764723,35.6745036],[139.7650679,35.674873],[139.765384,35.6752242],[139.7660313,35.6759278],[139.7661032,35.676006],[139.7668594,35.6770826],[139.7669439,35.6770506],[139.7670967,35.677018],[139.76722,35.6771998],[139.7673368,35.677372],[139.7670007,35.6774366],[139.767422,35.6781072],[139.7678664,35.6788156],[139.7683366,35.6795019],[139.768556,35.6798572],[139.7688243,35.6802947],[139.7689181,35.6804451],[139.769004,35.6805829],[139.7692744,35.6810051],[139.7694548,35.6813018],[139.7695423,35.6814333],[139.7696162,35.681534],[139.7695905,35.6815477],[139.7697253,35.6817137],[139.7697898,35.6817914],[139.7702577,35.6825007],[139.7703382,35.6826707],[139.770703,35.6834107],[139.7707124,35.6834278],[139.7707083,35.6834651],[139.7706989,35.6835007],[139.7706869,35.6835239],[139.7706466,35.6835653],[139.7706118,35.683577],[139.7706529,35.6836897],[139.77069,35.6837938],[139.7708089,35.6837658],[139.7708478,35.68376],[139.7708813,35.6837654],[139.7709108,35.6837774],[139.7709377,35.6838047],[139.7711603,35.6843366],[139.7708934,35.684394],[139.7709768,35.6846493],[139.7710472,35.6848649],[139.7708266,35.6852419],[139.7707916,35.6853221],[139.7704431,35.6861201],[139.7703882,35.6862458],[139.7700522,35.6866915],[139.769635,35.6871302],[139.7687983,35.6880099],[139.7679367,35.6885931],[139.768818,35.688873],[139.7690747,35.6889545],[139.7697075,35.6891658],[139.770351,35.6893787],[139.7710217,35.6895941],[139.77108,35.6896151],[139.7711112,35.6896228],[139.7722657,35.6899937],[139.7723383,35.690017],[139.7723895,35.690038],[139.7724413,35.6900593],[139.7725122,35.6900828],[139.7737709,35.6904989],[139.7738131,35.6905129],[139.7738654,35.6905302],[139.7748069,35.690842],[139.7748782,35.6908656],[139.7749502,35.6908895],[139.7752056,35.6909742],[139.775289,35.6910039],[139.776611,35.691459],[139.7775234,35.691851],[139.77876,35.6923346],[139.7797821,35.6930844],[139.7799412,35.6931966],[139.7800706,35.693085],[139.7802409,35.6928966],[139.7802878,35.6928379],[139.7804334,35.6929487],[139.7811087,35.693462],[139.7811911,35.6935246],[139.7812645,35.6935776],[139.7821969,35.6942517],[139.7827612,35.6946624],[139.7828288,35.6949965],[139.7828444,35.6950805],[139.7827541,35.6950919],[139.7825891,35.6951125],[139.7823748,35.6951283],[139.7821724,35.6951432],[139.7815972,35.6951857],[139.7816089,35.6953131],[139.7816201,35.6953954],[139.7816255,35.6954352],[139.7816762,35.6958086],[139.7816843,35.6958683],[139.7817161,35.6961014],[139.7817365,35.6962514],[139.7817433,35.6962989],[139.7819891,35.6962657],[139.7826027,35.6961829],[139.7826909,35.6961729],[139.7826969,35.6962484],[139.7827041,35.6963361],[139.7827149,35.6964674],[139.7827349,35.6966147],[139.7827505,35.6967804],[139.7827736,35.6969804],[139.7828018,35.6972248],[139.7828254,35.6974247],[139.7828315,35.6974824],[139.7828445,35.6976245],[139.7827857,35.6976298],[139.7827678,35.6976314],[139.7823231,35.6976676],[139.7817814,35.6977117],[139.7812593,35.6977518],[139.7807692,35.6977894],[139.7802603,35.6978331],[139.78021,35.6978372],[139.7801629,35.6978412],[139.7802691,35.6985188],[139.7802932,35.6986494],[139.7803241,35.698819],[139.7803775,35.6991124],[139.780386,35.699159],[139.7804246,35.6993784],[139.7805051,35.6998322],[139.7805422,35.7000434],[139.7804661,35.7001119],[139.7769853,35.700421],[139.7757548,35.7005146],[139.7754731,35.7006028],[139.7755649,35.7015811],[139.7751015,35.7016191],[139.7745229,35.7016582],[139.773393,35.7017533],[139.7737318,35.7027925],[139.7730094,35.7028553],[139.7732677,35.7050056],[139.7720423,35.7051103],[139.771822,35.7051252],[139.7699594,35.7052513],[139.7699122,35.704627],[139.7698558,35.7037126],[139.7696837,35.7036886],[139.7696757,35.7036472],[139.7694229,35.7037002],[139.7693246,35.7032974],[139.7691098,35.70333],[139.7685003,35.7033864],[139.7680235,35.7033251],[139.7680189,35.7033247],[139.7680898,35.7028809],[139.7681012,35.7027945],[139.7681334,35.7027948],[139.7681468,35.7026246],[139.7679564,35.7026126],[139.7679483,35.7027422],[139.7675903,35.7027204],[139.7676023,35.702556],[139.7675661,35.7025527],[139.7674535,35.7025237],[139.7666583,35.702471],[139.7666611,35.7024887],[139.7666907,35.7026793],[139.7665618,35.7027011],[139.7662051,35.7018239],[139.7669808,35.7013274],[139.766972,35.7011304],[139.7669507,35.7010526],[139.7671477,35.7009366],[139.7673623,35.7011304],[139.767774,35.7009267],[139.7679269,35.7008302],[139.767888,35.7007971],[139.7680637,35.700632],[139.7681562,35.7006283],[139.7682643,35.7005457],[139.7686704,35.7005437],[139.7686197,35.6998329],[139.7684361,35.6998445],[139.7683229,35.6994885],[139.7675676,35.6998611],[139.7676198,35.699948],[139.7675299,35.6999792],[139.7674374,35.700038],[139.767276,35.7001309],[139.7670269,35.6996733],[139.7665418,35.6998221],[139.7664332,35.6995305],[139.7659405,35.6996559],[139.7649951,35.6999092],[139.7647483,35.6999753],[139.7634573,35.700263],[139.7631897,35.7003783],[139.7629786,35.7005214],[139.7627925,35.7007111],[139.7625722,35.7009804],[139.76237,35.7011671],[139.7621688,35.7013137],[139.7618951,35.7014421],[139.7616,35.7015236],[139.761296,35.7015647],[139.760871,35.7015921],[139.7602204,35.7015953],[139.7590495,35.7015907],[139.7578746,35.7015754],[139.7574423,35.701602],[139.7566652,35.7017695],[139.7553261,35.7020582],[139.7540624,35.7023306],[139.7533049,35.7024308],[139.7521709,35.7025308],[139.7518463,35.7025558],[139.7513889,35.7025662],[139.7508874,35.7026168],[139.7503205,35.7027133],[139.7491111,35.7029264],[139.7467978,35.702955],[139.7461998,35.7028584],[139.7451415,35.7025286],[139.7449656,35.7024514],[139.7449508,35.7024432],[139.7447438,35.7023283],[139.7444309,35.7024742],[139.7436148,35.7016875],[139.7440051,35.7014675],[139.7440352,35.7015018],[139.7443078,35.7013586],[139.7441385,35.7011369],[139.7439561,35.700856],[139.7434683,35.7001195],[139.7431598,35.7002586],[139.7430539,35.7000876],[139.743028,35.7001056],[139.7429704,35.7000359],[139.7430052,35.7000119],[139.7428406,35.6998382],[139.7418804,35.6987382],[139.7414932,35.698247],[139.7410596,35.697697],[139.7402675,35.6967416],[139.7401638,35.6966166],[139.7391957,35.6952325],[139.7390212,35.6949829],[139.7378195,35.6934537],[139.7375817,35.6932288],[139.7374279,35.6930834],[139.7357468,35.6919391],[139.7357143,35.6919781],[139.7356149,35.6919328],[139.7356188,35.6918848],[139.7352553,35.6916195],[139.7342351,35.691043],[139.7337211,35.6907525],[139.7323985,35.6900059],[139.7316172,35.689112],[139.7313095,35.6887599],[139.7309823,35.6879974],[139.7307463,35.6873351],[139.7304531,35.6862402],[139.7306403,35.6862013],[139.7308366,35.6861457],[139.7310671,35.6860326],[139.7309804,35.6858565],[139.7303824,35.6859677],[139.7301347,35.6850171]]]},"icon":"https://nominatim.openstreetmap.org/ui/mapicons/poi_boundary_administrative.p.20.png","linked_places":[{"localname":"千代田区","place_id":230489916,"osm_id":1042516435,"osm_type":"N","class":"place","type":"city","admin_level":15,"rank_address":16,"distance":0.0,"isaddress":true}]}
するとこの通り、Responseのgeometry
フィールドに千代田区のgeojsonが入っています。
試しにこのgeojsonをgeojson.ioで可視化してみるとこの通り千代田区のBoundaryが取れていることが分かります。
今回はOpenStreetMapのRelationのBoundaryをNominatimを使用して取得する方法を説明しました。 より複雑なクエリを行いたい場合はOverpass APIを使ったりしますが、今回くらいの内容だとNominatimのほうがシンプルで手軽に使えます。
また、今回は触れませんでしたがNominatimはGeocodingもできます。目的によってはこの機能も便利です。