LoginSignup
0
0

Amazon Location Serviceでジオコーディングとリバースジオコーディングをやってみた

Last updated at Posted at 2024-05-21

今回は、Amazon Location Serviceを使ってジオコーディングとリバースジオコーディングに挑戦してみました。
備忘録も兼ねて、手順をまとめてみたので、皆さんの参考になれば嬉しいです。

1. Amazon Location Serviceの設定

まずは、AWS CloudFormationを使ってAmazon Location Serviceの設定をします。以下のYAMLファイルを使って、PlaceIndexとAPIKeyを作成します。

AWSTemplateFormatVersion: "2010-09-09"
Description: Create AmazonLocationService PlaceIndex&APIKey

Resources:
  ########################
  ### AmazonLocationService PlaceIndex
  ########################
  PlaceIndex:
    Type: AWS::Location::PlaceIndex
    Properties:
      DataSource: Esri
      IndexName: !Sub test-als-placeindex
      Tags:
        - Key: Name
          Value: !Sub test-als-placeindex

  ########################
  ### AmazonLocationService APIKey
  ########################
  APIKey:
    Type: AWS::Location::APIKey
    Properties:
      KeyName: !Sub test-als-apikey
      NoExpiry: true
      Restrictions:
        AllowActions:
          - geo:GetPlace
          - geo:SearchPlaceIndexForPosition
          - geo:SearchPlaceIndexForSuggestions
          - geo:SearchPlaceIndexForText
        AllowResources:
          - !Sub 'arn:aws:geo:${AWS::Region}:${AWS::AccountId}:place-index/${PlaceIndex}'
      Tags:
        - Key: Name
          Value: !Sub test-als-apikey

このYAMLファイルをAWS Management ConsoleのCloudFormationのスタック作成時にアップロードして実行すると、Amazon Location ServiceのPlaceIndexとAPIKeyが作成されます。

2. AWS CLIの設定

次に、AWS CLIを使ってジオコーディングとリバースジオコーディングを実行するために、AWS CLIの設定を行います。

aws configure

設定時の入力例:

AWS Access Key ID [********************]:
AWS Secret Access Key [********************]:
Default region name [ap-northeast-1]:
Default output format [None]:

3. ジオコーディングをやってみた

住所を座標に変換するジオコーディングを試してみました。例えば、東京スカイツリーの住所を使ってみます。

aws location search-place-index-for-text --index-name "プレースインデックス名" --text "〒131-0045東京都墨田区押上1丁目1-2, 東京スカイツリ-タウン・ソラマチ イ-ストヤ-ド4F"

実行すると、以下のような結果が得られます。

{
    "Results": [
        {
            "Place": {
                "AddressNumber": "2",
                "Categories": [
                    "AddressType"
                ],
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        139.809450274245,
                        35.710038964502
                    ]
                },
                "Interpolated": false,
                "Label": "東京都墨田区押上1-1-2",
                "Neighborhood": "1",
                "PostalCode": "1310045",
                "Region": "東京都",
                "SubMunicipality": "押上"
            },
            "Relevance": 0.9447
        }
    ],
    "Summary": {
        "DataSource": "Esri",
        "MaxResults": 50,
        "ResultBBox": [
            139.809450274245,
            35.710038964502,
            139.809450274245,
            35.710038964502
        ],
        "Text": "〒131-0045東京都墨田区押上1丁目1-2, 東京スカイツリ-タウン・ソラマチ イ-ストヤ-ド4F"
    }
}

リバースジオコーディングもやってみた

次に、座標を住所に変換するリバースジオコーディングを試してみます。

aws location search-place-index-for-position --index-name "プレースインデックス名" --position 139.809450274245 35.710038964502

実行すると、以下のような結果が得られます。

{
    "Results": [
        {
            "Distance": 0,
            "Place": {
                "AddressNumber": "2",
                "Categories": [
                    "AddressType"
                ],
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        139.809450274245,
                        35.710038964502
                    ]
                },
                "Interpolated": false,
                "Label": "東京都墨田区押上1-1-2",
                "Neighborhood": "1",
                "PostalCode": "1310045",
                "Region": "東京都",
                "SubMunicipality": "押上"
            }
        }
    ],
    "Summary": {
        "DataSource": "Esri",
        "MaxResults": 50,
        "Position": [
            139.809450274245,
            35.710038964502
        ]
    }
}

まとめ

今回、CLoudFormationでYAMLファイルを使ってAmazon Location Serviceのジオコーディングとリバースジオコーディングに挑戦してみたものを備忘録として残しました。
IaCでのサービス管理は人為的なミスも少なく、YAMLとして残しておけばサービス再作成も変更も楽なものです(YAMLを間違えていたら元も子もありませんが。。。)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0