지오코딩(GeoCoding)

지오코딩을 통해서 주소나 위치정보를 지도에 표시해보자.

저자
소속
소스코드

1 패키지

코드
# Install R packages if not installed -------------------------------------

if (!require(tidyverse)) install.packages("tidyverse")
if (!require(rvest)) install.packages("rvest")
if (!require(usethis)) install.packages("usethis")
if (!require(tidygeocoder)) install.packages("tidygeocoder")
if (!require(leaflet)) install.packages("leaflet")


# Attach R packages -------------------------------------------------------

library(tidyverse)
library(rvest)
library(usethis)
library(tidygeocoder)
library(leaflet)

2 데이터

코드
library(readxl)
library(tidyverse)

mst_raw <- read_excel("data/인천서구강소연구개발특구 기업현황 데이터 요청사항_230618.xlsx", sheet = "환경산업연구단지 입주기업", skip = 1)

mst_tbl <- mst_raw %>% 
  janitor::clean_names(ascii = FALSE) %>% 
  select(!starts_with("x")) 

mst_tbl
#> # A tibble: 129 × 19
#>     연번 회사명        대표자명 전화번호 분야  이메일주소 사업자번호 주소  매출 
#>    <dbl> <chr>         <chr>    <chr>    <chr> <chr>      <chr>      <chr> <chr>
#>  1     1 주식회사 네…  전진오   070-422… 생활… neo@neost… 166-88-00… 인천… 5-9억
#>  2     2 프로덕트테크  박재민   <NA>     대기  <NA>       <NA>       인천… 5억 …
#>  3     3 (주)스피너스  조철호   02-2138… 폐기… ryam_cho@… 327-88-02… 인천… 5-9억
#>  4     4 한국도시재생… 이정학   032-467… 폐기… <NA>       339-87-00… 인천… 5-9억
#>  5     5 에코드릴 주…  박건준   02-808-… 대기  <NA>       116-88-01… 인천… 5-9억
#>  6     6 에코프로에이… 김종섭   043-210… 대기  ecopro@re… 283-87-02… 인천… 1,00…
#>  7     7 (주)어라운드… 최준영   070-485… 생활… aroundblu… 575-81-02… 인천… 5억 …
#>  8     8 주식회사 코…  이주혁   070-864… 생활… contact@c… 188-81-02… 인천… 10-4…
#>  9     9 주식회사 쉘…  최수빈   <NA>     생활… <NA>       443-87-02… 인천… 5억 …
#> 10    10 (주)에너지로… 이완구   032-573… 생활… e-road@e-… 120-88-03… 인천… 100-…
#> # ℹ 119 more rows
#> # ℹ 10 more variables: 녹색기술_녹색생활 <chr>, 기술1 <chr>, 기술2 <chr>,
#> #   기술3 <chr>, 개발예정품목 <chr>, 홈페이지 <chr>, 주요사업 <chr>,
#> #   주요취급품 <chr>, 주요매출처 <chr>, 비고 <chr>

3 지오코딩

코드
tidygeocoder::api_info_reference %>% 
  gt::gt() %>% 
  gtExtras::gt_theme_538() %>% 
  gt::cols_align(align = "center")
method method_display_name site_url api_documentation_url api_usage_policy_url
osm Nominatim https://nominatim.org https://nominatim.org/release-docs/develop/api/Search/ https://operations.osmfoundation.org/policies/nominatim/
census US Census https://geocoding.geo.census.gov/ https://www.census.gov/programs-surveys/geography/technical-documentation/complete-technical-documentation/census-geocoder.html https://www.census.gov/programs-surveys/geography/technical-documentation/complete-technical-documentation/census-geocoder.html
arcgis ArcGIS https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm https://developers.arcgis.com/rest/geocode/api-reference/geocoding-free-vs-paid.htm
geocodio Geocodio https://www.geocod.io/ https://www.geocod.io/docs/ https://www.geocod.io/pricing/
iq Location IQ https://locationiq.com/ https://locationiq.com/docs https://locationiq.com/pricing
google Google https://developers.google.com/maps/documentation/geocoding/overview https://developers.google.com/maps/documentation/geocoding/overview https://developers.google.com/maps/documentation/geocoding/usage-and-billing
opencage OpenCage https://opencagedata.com https://opencagedata.com/api https://opencagedata.com/pricing
mapbox Mapbox https://docs.mapbox.com/api/search/ https://docs.mapbox.com/api/search/geocoding/ https://www.mapbox.com/pricing/
here HERE https://developer.here.com/products/geocoding-and-search https://developer.here.com/documentation/geocoding-search-api/dev_guide/index.html https://developer.here.com/pricing
tomtom TomTom https://developer.tomtom.com/search-api/search-api-documentation/geocoding https://developer.tomtom.com/search-api/search-api-documentation-geocoding/geocode https://developer.tomtom.com/store/maps-api
mapquest MapQuest https://developer.mapquest.com/documentation/geocoding-api/ https://developer.mapquest.com/documentation/geocoding-api/ https://developer.mapquest.com/plans
bing Bing https://docs.microsoft.com/en-us/bingmaps/rest-services/locations/ https://docs.microsoft.com/en-us/bingmaps/rest-services/locations/ https://docs.microsoft.com/en-us/bingmaps/spatial-data-services/geocode-and-data-source-limits
geoapify Geoapify https://www.geoapify.com/geocoding-api https://apidocs.geoapify.com/docs/geocoding/api/ https://www.geoapify.com/term-and-conditions
코드
mst_tbl_geocodes <- mst_tbl %>%
  geocode(address = 주소,  method = "arcgis")

mst_tbl_geocodes_sample <- mst_tbl_geocodes %>% 
  select(회사명, 주소, lat, long) %>% 
  group_by(lat) %>% 
  slice_sample(n=1)

mst_tbl_geocodes_sample %>% 
  write_rds("data/incheon.rds")

4 시각화

코드

mst_tbl_geocodes_sample <- 
  read_rds("data/incheon.rds")

mst_tbl_geocodes_sample %>%
  mutate(popup = paste0("<b>", 회사명, "</b>", "<br>",
                        주소, "<br>")) %>%
  leaflet() %>% 
  addTiles() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addMarkers(~long, ~lat, 
             popup  = ~popup) %>%
  addLayersControl(overlayGroups = ~회사명,
                   options = layersControlOptions(collapsed = FALSE))