Created
March 4, 2019 08:02
-
-
Save Ray901/b06fd0ce03f6e341f15533e2f50ffa60 to your computer and use it in GitHub Desktop.
USE TGOS API CONVERT ADDRESS TO LAT, LON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) | |
options(digits = 9) | |
library(httr) | |
library(xml2) | |
library(dplyr) | |
library(magrittr) | |
TGOS_ADDR <- function(setADDR) { | |
r <- GET("http://addr.tgos.nat.gov.tw/addrws/v30/QueryAddr.asmx/QueryAddr", | |
query = list( | |
oAPPId = "", | |
oAPIKey = "", | |
oAddress = setADDR, | |
oSRS = "EPSG:4326", | |
oFuzzyType = 2, | |
oResultDataType = "JSON", | |
oFuzzyBuffer = 0, | |
oIsOnlyFullMatch = FALSE, | |
oIsLockCounty = FALSE, | |
oIsLockTown = FALSE, | |
oIsLockVillage = FALSE, | |
oIsLockRoadSection = FALSE, | |
oIsLockLane = FALSE, | |
oIsLockAlley = FALSE, | |
oIsLockArea = FALSE, | |
oIsSameNumber_SubNumber = TRUE, | |
oCanIgnoreVillage = TRUE, | |
oCanIgnoreNeighborhood = TRUE, | |
oReturnMaxCount = 0 | |
), | |
add_headers(Host= "addr.tgos.nat.gov.tw") | |
) | |
if (!is.na(iconv(rawToChar(r$content),"UTF8","BIG5"))) { | |
result <- iconv(rawToChar(r$content),"UTF8","BIG5") %>% | |
read_xml() %>% | |
as_list() %>% | |
unlist() %>% | |
jsonlite::fromJSON() %>% | |
extract2('AddressList') | |
if (length(result)>0) { | |
return(result[1,]) | |
} else { | |
return(NULL) | |
} | |
} else { | |
return(NULL) | |
} | |
} | |
TGOS_ADDR('台北市信義區市府路1號') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment