0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

YahooジオコーディングAPIを叩く

Posted at
#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")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?