Last active
August 12, 2020 14:22
-
-
Save mutolisp/0f7f597510e5de65b67391f605d20485 to your computer and use it in GitHub Desktop.
Using geocoding API to translate school address to coordinates
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
library(data.table) | |
library(jsonlite) | |
library(readxl) | |
library(curl) | |
# Get your API from GCP | |
API_KEY <- '&key=$YOUR_API_KEY' | |
URL <- 'https://maps.googleapis.com/maps/api/geocode/json?address=' | |
# Data source: | |
# # Elementary high school: https://data.gov.tw/dataset/6087 | |
# # Junior high school: https://data.gov.tw/dataset/6088 | |
# # elementary school | |
types <- rep('character', 7) | |
elem <- data.table::fread('../../elem_school_2019.csv', colClasses = types) | |
junior <- data.table::fread('../../junior_school_2019.csv', colClasses = types) | |
htypes <- rep('character', 8) | |
high <- data.table::fread('../../senior_school_2019.csv', colClasses = htypes) | |
high[, `備註` := NULL] | |
# new data sources | |
selectedCode <- data.table::fread('../../finalList.csv', | |
colClasses = 'character') | |
je <- rbind(elem, junior, high) | |
# remove county codes and zipcodes in address | |
je[, county := gsub('[0-9]|\\[|\\]', '', `縣市名稱`)] | |
je[, add := gsub('^\\[[0-9]{1,}\\]', '', `地址`)] | |
s1_list <- je[ `代碼` %in% selectedCode$V1 ] | |
for ( i in 1:dim(s1_list)[1] ){ | |
print(i) | |
res <- fromJSON(paste(URL, s1_list[i, add], API_KEY, sep='')) | |
res <- res$results | |
lat <- res$geometry$location$lat[1] | |
lng <- res$geometry$location$lng[1] | |
s1_list[i, place_id := res$place_id] | |
s1_list[i, latitude := lat ] | |
s1_list[i, longitude := lng ] | |
} | |
# extract coordinates from school list | |
listCoor <- s1_list[, .(longitude, latitude)] | |
colnames(listCoor) <- c('x', 'y') | |
listCoor_kmeans <- kmeans(listCoor, 10) | |
s1_list[, group := listCoor_kmeans$cluster ] | |
data.table::fwrite(s1_list, '../../s1_school_list.csv') | |
# get schools' polygons | |
s1_list[, fullnames := paste(gsub('市$|縣$', '', county), `學校名稱`, sep = '')] | |
s1_list[, fullnames] | |
# Get school polygons from openstreetmaps | |
osmSearchURL <- 'https://nominatim.openstreetmap.org/search?q=' | |
osmParameters <- '&format=geojson&polygon_geojson=1' | |
for ( s in 1:dim(s1_list)[1]) { | |
print(paste(s, s1_list[s, fullnames])) | |
querySchool <- paste(osmSearchURL, s1_list[s, fullnames], | |
osmParameters, sep='') | |
curl_download(querySchool, paste('../../schoolPolygons/', | |
s1_list[s, fullnames], | |
'.geojson', sep=''), mode = 'w') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment