Download Google map my data. Save it in your working directory. See this for how to extract map data: https://webapps.stackexchange.com/questions/16177/is-there-a-way-to-export-my-starred-locations-from-google-maps
setwd("C:/Users/YoonJoung Choi/Dropbox/0 Project/Map")
dir()
## [1] "Added dishes, products, activities.json"
## [2] "Labeled places.json"
## [3] "MapTopia.html"
## [4] "MapTopia.rmd"
## [5] "Reviews.json"
## [6] "rsconnect"
## [7] "Saved Places.json"
Inspect data, gather only relevant information, and put them into a dataframe.
suppressPackageStartupMessages(library(jsonlite))
myjson<-fromJSON("Saved places.json")
jsondta<-as.data.frame(myjson)
dim(jsondta)
names(jsondta)
head(jsondta)
geometry<-jsondta[ , 2]
#str(geometry)
properties<-jsondta[ , 3]
#str(properties)
location<-properties[ , 2]
#str(location)
head(location)
geo<-location[ , 6]
#str(geo)
head(geo)
location<-location[ , 1:5]
url<-as.data.frame(properties[ , 1])
colnames(url)[1] <- "url"
title<-as.data.frame(properties[ , 4])
colnames(title)[1] <- "title"
str(location)
str(title)
str(url)
str(geo)
#confirm there is no nested dataframes
suppressPackageStartupMessages(library(dplyr))
dtaset<-bind_cols(location, url, title, geo)
Clean data, including selecting a specific area based on latitute and longitude. The area selection can be done with country name, in theory, but it is often missing for names of city/town, etc (see below).
#check what you have
colnames(dtaset)<-tolower(names(dtaset))
head(dtaset)
latitude longitude address
1 -0.954457 -90.964147 <NA>
2 <NA> <NA> Puerto Baquerizo Moreno, Ecuador
3 <NA> <NA> Carlos Mora, Puerto Baquerizo Moreno, Ecuador
4 -0.743292 -90.315689 <NA>
5 <NA> <NA> Ecuador
6 -1.053483 -77.548439 <NA>
business name country code
1 <NA> <NA>
2 Airport Isla San Cristobal EC
3 Hostal Gosén EC
4 <NA> <NA>
5 GAIA Amazon Lodge EC
6 <NA> <NA>
url
1 http://maps.google.com/?q=Puerto+Villamil,+Ecuador&ftid=0x9aabeb96e0ec8dbb:0x64746d53fc14dc0f
2 http://maps.google.com/?cid=17614056007148402264
3 http://maps.google.com/?cid=3726830005299073107
4 http://maps.google.com/?q=Puerto+Ayora,+Ecuador&ftid=0x9aaa5d43fb94bf6b:0x9841fa352c866135
5 http://maps.google.com/?cid=7746604468468777656
6 http://maps.google.com/?q=Ahuano,+Ecuador&ftid=0x91d6c83d6d0de0bb:0x48bdf34f4f103803
title latitude1 longitude1
1 Puerto Villamil, Ecuador <NA> <NA>
2 Airport Isla San Cristobal -0.9086996 -89.6162348
3 Hostal Gosén -0.9003580 -89.6087425
4 Puerto Ayora, Ecuador <NA> <NA>
5 GAIA Amazon Lodge -1.0308977 -77.5269610
6 Ahuano, Ecuador <NA> <NA>
#clean
suppressPackageStartupMessages(library(Hmisc))
dta<- dtaset %>%
rename(country = 'country code') %>%
rename(business = 'business name') %>%
mutate(
latitude = ifelse(is.na(latitude)==TRUE, latitude1, latitude) ,
longitude = ifelse(is.na(longitude)==TRUE, longitude1, longitude) ,
latitude = as.numeric(as.character(latitude)),
longitude = as.numeric(as.character(longitude))
) %>%
select( -latitude1, -longitude1) %>%
filter( latitude<10 & longitude<0 ) %>%
filter( country=="PE" | country=="EC" | is.na(country)==TRUE)
#Now, check cleaned data.
head(dta)
latitude longitude address
1 -0.9544570 -90.96415 <NA>
2 -0.9086996 -89.61623 Puerto Baquerizo Moreno, Ecuador
3 -0.9003580 -89.60874 Carlos Mora, Puerto Baquerizo Moreno, Ecuador
4 -0.7432920 -90.31569 <NA>
5 -1.0308977 -77.52696 Ecuador
6 -1.0534830 -77.54844 <NA>
business country
1 <NA> <NA>
2 Airport Isla San Cristobal EC
3 Hostal Gosén EC
4 <NA> <NA>
5 GAIA Amazon Lodge EC
6 <NA> <NA>
url
1 http://maps.google.com/?q=Puerto+Villamil,+Ecuador&ftid=0x9aabeb96e0ec8dbb:0x64746d53fc14dc0f
2 http://maps.google.com/?cid=17614056007148402264
3 http://maps.google.com/?cid=3726830005299073107
4 http://maps.google.com/?q=Puerto+Ayora,+Ecuador&ftid=0x9aaa5d43fb94bf6b:0x9841fa352c866135
5 http://maps.google.com/?cid=7746604468468777656
6 http://maps.google.com/?q=Ahuano,+Ecuador&ftid=0x91d6c83d6d0de0bb:0x48bdf34f4f103803
title
1 Puerto Villamil, Ecuador
2 Airport Isla San Cristobal
3 Hostal Gosén
4 Puerto Ayora, Ecuador
5 GAIA Amazon Lodge
6 Ahuano, Ecuador
table(dta$country)
EC PE
5 17
#check data without country. They also do not have business. These are all names of city/town. These can be dropped.
check<- dta %>% filter( is.na(country)==TRUE)
check
Using leaflet! Popup content can be customized. Refine the map as needed!
google<- "http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga"
df <- data.frame(latitude = dta$latitude,
longitude = dta$longitude,
popup = dta$title)
suppressPackageStartupMessages(library(leaflet))
df %>%
leaflet() %>%
addTiles(urlTemplate = google) %>%
addMarkers(popup=df$popup)