Amazon Location Serviceでリバースジオコーダー

321 Views

October 14, 23

スライド概要

KoedoLUG 2023/10/14 発表資料

profile-image

Georepublic / OSGeo.JP / Japan Unix Society / OpenStreetMap Foundation Japan

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

Amazon Location Service でリバースジオコーダー Taro Matsuzawa@smellman KoedoLUG 2023.10.14

2.

自己紹介 ● ● ● ● ● ● ● 合同会社Georepublic Japan GIS Enginner 日本UNIXユーザー会 副会長 一般社団法人 OSGeo日本支部 理事 一般社団法人 OpenStreetMap Foundation Japan 理事 東京電機大学OB & CySec修了生 Breakcore クラスタ 参加予定イベント ○ ○ OSC Tokyo 10/21 FOSS4G Asia 2023 (Korea) 11/27〜12/3

3.

Amazon Location Serviceとは ● ● ● Amazon Web Service(AWS)が提供する地理情報関係のサービス AWSのアカウントがあればすぐに作成・利用可能 以下の機能がある ○ ○ ○ ○ ○ ○ ● マップ ジオコーダー Point of Interest (POI) ジオフェンシング 追跡 ルーティング 今回はジオコーダーをRuby on Railsで使ってみた

4.
[beta]
セットアップ - AWS CDK
new CfnResource(scope, 'AmazonLocationServiceResource', {
type: 'AWS::Location::PlaceIndex',
properties: {
DataSource: 'Esri',
DataSourceConfiguration: {
IntendedUse: 'SingleUse',
},
Description: Place Index',
IndexName: 'YourIndexName',
PricingPlan: 'RequestBasedUsage',
},
});

5.

Ruby on Railsで使う(1) bundle install $ vim Gemfile gem 'aws-sdk-locationservice' $ bundle install

6.
[beta]
Ruby on Railsで使う(2) awsの初期化
$ cat config/initializers/aws.rb
# frozen_string_literal: true
Aws.config.update({
region: 'ap-northeast-1',
credentials: Aws::Credentials.new(Rails.application.credentials.dig(:aws, :access_key_id),
Rails.application.credentials.dig(:aws, :secret_access_key))
})

7.

Ruby on Railsで使う(3) コントローラ ❯ cat app/controllers/reverse_geocoder_controller.rb class ReverseGeocoderController < ApplicationController def index render json: search_by_position(params[:longitude], params[:latitude]) end private def search_by_position(longitude, latitude) client = Aws::LocationService::Client.new client.search_place_index_for_position({ index_name: ‘YourIndexName’ position: [longitude, latitude] }) end end

8.

Ruby on Railsで使う(4) routes.rb $ cat config/routes.rb Rails.application.routes.draw do scope :api do resources :reverse_geocoder, only: %i[index] end end

9.

Ruby on Railsで使う(5) minitest - mock def setup @sample_response = { results: [{ place: { label: '東京都千代田区霞が関1-4-1' } }] } client_mock = mock('client') Aws::LocationService::Client.stubs(:new).returns(client_mock) client_mock.stubs(:search_place_index_for_position).returns(@sample_response) end

10.
[beta]
Ruby on Railsで使う(6) minitest - test
test 'should get index with longitude and latitude' do
get reverse_geocoder_index_url, params: {
longitude: 139.750431,
latitude: 35.670857
}
assert_response :success
assert_match '東京都千代田区霞が関1-4-1', @response.body
end

11.
[beta]
実行
$ curl "http://localhost:3000/api/reverse_geocoder?longitude=139.750431&latitude=35.670857"
{"results":[{"distance":2.5732278801626998,"place":{"address_number":"
1","categories":["AddressType"],"country":"JPN","geometry":{"point":[139.750452237088,35.67087
2384529]},"interpolated":false,"label":"東京都千代田区霞が関1 -4-1","neighborhood":"
1","postal_code":"1000013","region":"東京
都"}}],"summary":{"data_source":"Esri","max_results":50,"position":[139.750431,35.670857]}}

12.

まとめ ● ● Amazon Web Service便利 必要なgemが揃っているのでRuby on Railsから気軽に使える