#202401018 国土地理院データを緯度経度にする
#東京大学ジオコーディングサービス後にYahooAPIで補填する
library(httr)
tempDF1 <- read.csv("ADDR_1 (1).csv")
tempDF2 <- read.csv("ADDR_2 (1).csv")
tempDF3 <- read.csv("ADDR_3 (1).csv")
names(tempDF2) <- names(tempDF1)
names(tempDF3) <- names(tempDF1)
tempDF <- rbind(tempDF1,tempDF2)
tempDF <- rbind(tempDF,tempDF3)
tempDF$CODE <- as.character(tempDF$CODE)
targetData <- tempDF[tempDF$iLvl<5,]
tempDF <- tempDF[tempDF$iLvl>=5,]
targetData$CODE <- as.character(targetData$CODE)
#YahooジオコーディングAPIのエンドポイント
api_endpoint <- "https://map.yahooapis.jp/geocode/V1/geoCoder"
# YahooジオコーディングAPIのアプリケーションID(ご自身のIDに置き換えてください)
app_id <- "YahooAPIを記入"
geocordingDF <- data.frame()
for (i in 1:nrow(targetData)) {
address <- targetData[i,]$ADDR
code <- targetData[i,]$CODE
# APIリクエストのパラメーターを構築
query_params <- list(
appid = app_id,
output = "json", # 出力形式をJSONに指定
query = address
)
# APIリクエストを送信
response <- GET(api_endpoint, query = query_params)
# レスポンスの内容を取得
response_content <- content(response, "text", encoding = "UTF-8")
# JSONをデコード
result <- jsonlite::fromJSON(response_content)
if (!is.null( result$Feature)){
# 緯度経度の取得
latitude <- strsplit(result$Feature$Geometry$Coordinates[1],",")[[1]][2]
longitude <- strsplit(result$Feature$Geometry$Coordinates[1],",")[[1]][1]
geocordingDF <- rbind(geocordingDF,cbind(code,address,latitude,longitude))
}else{
nameDF <- data.frame(cbind(code,address,"",""))
names(nameDF) <- c("code","address","latitude","longitude")
geocordingDF <- rbind(geocordingDF,nameDF)
}
print(i)
}
write.csv(geocordingDF,"result_20240119_03.csv")
write.csv(tempDF[,c("CODE","ADDR","fY","fX")],"result_20240119_04.csv")
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme