Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
逆ジオコーディング結果を地図にプロット
library(RPostgreSQL)
library(leaflet)
# データベースに接続
con <- dbConnect(PostgreSQL(), host="host_name", port=port_number, dbname="database_name", user="user_name", password="password")
# クエリ文字列を定義
query <- paste("SELECT pref, city, town, block,",
"ST_Y(ST_Transform(the_geom, 4326)) AS lat,",
"ST_X(ST_Transform(the_geom, 4326)) AS lon",
"FROM loc_ref_info",
"WHERE representative = 1",
"ORDER BY ST_SETSRID(ST_POINT(139.745433, 35.658581), 4326)",
"<-> the_geom",
"LIMIT 10")
# クエリの結果を取得
df <- dbGetQuery(con, query)
# データベースから切断
dbDisconnect(con)
# Leafletにプロットするマーカーを定義
markerIcons <- iconList(blue = makeIcon("http://maps.google.com/mapfiles/ms/icons/blue.png"),
red = makeIcon("http://maps.google.com/mapfiles/ms/icons/red.png"))
# クエリの結果(青)と東京タワー(赤)を地図にプロット
leaflet(df) %>%
addProviderTiles("OpenStreetMap.DE") %>%
addMarkers(~lon, ~lat, popup = ~paste0(pref, city, town, block), icon = ~markerIcons["blue"]) %>%
addMarkers(139.745433, 35.658581, popup = "東京タワー", icon = ~markerIcons["red"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment