Amazon Location ServiceでフリーテキストによるPlaceの検索をしてみた(AWS CLI)

2022.10.02

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

Amazon Location Serviceは、地図(Map、マップ)や位置情報(Place、プレイス)などの地理情報リソースにAPIやSDKでアクセスして、アプリケーションに統合して利用することができるAWSサービスです。以前に下記エントリで概要を紹介させて頂きました。

Amazon Location Serviceを活用できる案件にはまだ巡り会えていませんが、私的に是非とも活用してみたいAWSサービスの1つです。

今回は、アプリケーションに統合したい際によくありそうなユースケースとして、AWS CLIでフリーテキストによるPlaceの検索を試してみました。

やってみた

Place Indexの確認(作成)

Amazon Location ServiceでPlaceの検索やGeocoding、Reverse Geocodingを行いたい場合は、Place Indexを使う必要があります。

既存のインデックスの一覧はlist-place-indexesコマンドで確認できます。

$ aws location list-place-indexes
{
    "Entries": [
        {
            "CreateTime": "2021-05-23T14:14:24.398000+00:00",
            "DataSource": "Here",
            "Description": "",
            "IndexName": "MyIndex",
            "PricingPlan": "RequestBasedUsage",
            "UpdateTime": "2021-05-23T14:14:24.398000+00:00"
        },
        {
            "CreateTime": "2021-05-26T14:06:05.649000+00:00",
            "DataSource": "Esri",
            "Description": "Created by Amazon Location Service explore",
            "IndexName": "explore.place",
            "PricingPlan": "RequestBasedUsage",
            "UpdateTime": "2021-05-26T14:06:05.649000+00:00"
        }
    ]
}

まだ1つも作成されていないと思っていましたが、以前に検証した際にコンソールから作成したものが残っていたようです。

Amazon Location Serviceでは、IndexのData SourceとしてHereEsriのいずれかが使用できます。いずれも外部のMap Providerで、有料のため利用に応じて課金が行われます。

Indexが未作成の場合は、コンソールまたはcreate-place-indexコマンドで作成します。

Placeを検索する

先程確認したHereの方のIndex(MyIndex)を使用してPlaceを検索してみます。コマンドはsearch-place-index-for-textを使用します。

東京スカイツリーで検索すると結果を1つだけ取得できました。緯度経度や住所など様々な情報が合わせて取得できており、これらを使用して地図上にマーカーを配置したり、ユーザーに情報を提示したりすることになります。

$ aws location search-place-index-for-text \
  --index-name MyIndex \
  --text "東京スカイツリー"

{
    "Results": [
        {
            "Place": {
                "AddressNumber": "2",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        139.81075,
                        35.71001
                    ]
                },
                "Interpolated": false,
                "Label": "〒131-0045 東京都墨田区押上1丁目1-2 東京スカイツリー",
                "Municipality": "墨田区",
                "PostalCode": "131-0045",
                "Region": "東京都",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        }
    ],
    "Summary": {
        "DataSource": "Here",
        "MaxResults": 50,
        "ResultBBox": [
            139.81075,
            35.71001,
            139.81075,
            35.71001
        ],
        "Text": "東京スカイツリー"
    }
}

複数の結果がヒットする場合もあります。

$ aws location search-place-index-for-text \
  --index-name MyIndex \
  --text "丸ビル"    
{
    "Results": [
        {
            "Place": {
                "AddressNumber": "4",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        131.606,
                        33.24238
                    ]
                },
                "Interpolated": false,
                "Label": "〒870-0033 大分県大分市千代町2丁目1-4 丸ビル",
                "Municipality": "大分市",
                "PostalCode": "870-0033",
                "Region": "大分県",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        },
        {
            "Place": {
                "AddressNumber": "37",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        143.35373,
                        44.35268
                    ]
                },
                "Interpolated": false,
                "Label": "〒094-0005 北海道紋別市幸町4丁目2-37 丸ビル",
                "Municipality": "紋別市",
                "PostalCode": "094-0005",
                "Region": "北海道",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        },
        {
            "Place": {
                "AddressNumber": "6",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        136.44766,
                        36.40721
                    ]
                },
                "Interpolated": false,
                "Label": "〒923-0904 石川県小松市小馬出町27-6 丸ビル",
                "Municipality": "小松市",
                "PostalCode": "923-0904",
                "Region": "石川県",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        },
        {
            "Place": {
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        137.73007,
                        34.70398
                    ]
                },
                "Interpolated": false,
                "Label": "〒430-0934 静岡県浜松市中区千歳町56 丸ビル",
                "Municipality": "浜松市",
                "Neighborhood": "中区",
                "PostalCode": "430-0934",
                "Region": "静岡県",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        },
        {
            "Place": {
                "AddressNumber": "2",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        139.77015,
                        35.94779
                    ]
                },
                "Interpolated": false,
                "Label": "〒344-0022 埼玉県春日部市大畑304-2 丸ビル",
                "Municipality": "春日部市",
                "PostalCode": "344-0022",
                "Region": "埼玉県",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        },
        {
            "Place": {
                "AddressNumber": "3",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        139.58204,
                        35.70377
                    ]
                },
                "Interpolated": false,
                "Label": "〒180-0004 東京都武蔵野市吉祥寺本町1丁目25-3 丸ビル",
                "Municipality": "武蔵野市",
                "PostalCode": "180-0004",
                "Region": "東京都",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        }
    ],
    "Summary": {
        "DataSource": "Here",
        "MaxResults": 50,
        "ResultBBox": [
            131.606,
            33.24238,
            143.35373,
            44.35268
        ],
        "Text": "丸ビル"
    }
}

この場合は結果の全てのPlaceのポイントの範囲がSummaryのResultBBoxで確認できるため、地図上へ各Placeのマーカーを配置したい場合に便利です。

またfilter-b-boxオプションを使用すれば、BBoxを検索条件にしてフィルターすることもできます。表示中の地図の範囲内のみ検索したい場合などに便利なオプションですね。

$ aws location search-place-index-for-text \
  --index-name MyIndex \
  --text "丸ビル" \
  --filter-b-box '[131.606, 33.24238, 139.0, 36.0]'
{
    "Results": [
        {
            "Place": {
                "AddressNumber": "15",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        135.18693,
                        34.69217
                    ]
                },
                "Interpolated": false,
                "Label": "〒650-0011 兵庫県神戸市中央区下山手通4丁目7-15 北丸ビル",
                "Municipality": "神戸市",
                "Neighborhood": "中央区",
                "PostalCode": "650-0011",
                "Region": "兵庫県",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        },
        {
            "Place": {
                "AddressNumber": "8",
                "Country": "JPN",
                "Geometry": {
                    "Point": [
                        135.20368,
                        34.70399
                    ]
                },
                "Interpolated": false,
                "Label": "〒651-0067 兵庫県神戸市中央区神若通2丁目3-8 内丸ビル",
                "Municipality": "神戸市",
                "Neighborhood": "中央区",
                "PostalCode": "651-0067",
                "Region": "兵庫県",
                "TimeZone": {
                    "Name": "Asia/Tokyo",
                    "Offset": 32400
                }
            }
        }
    ],
    "Summary": {
        "DataSource": "Here",
        "FilterBBox": [
            131.606,
            33.24238,
            139,
            36
        ],
        "MaxResults": 50,
        "ResultBBox": [
            135.18693,
            34.69217,
            135.20368,
            34.70399
        ],
        "Text": "丸ビル"
    }
}

以上