Skip to content

Instantly share code, notes, and snippets.

@Keareys
Created January 25, 2018 19:55
Show Gist options
  • Save Keareys/7d05367f4e3bf6630d6eff6a578d7fe6 to your computer and use it in GitHub Desktop.
Save Keareys/7d05367f4e3bf6630d6eff6a578d7fe6 to your computer and use it in GitHub Desktop.
Gmaps API Location Search for City Names in California
# Example Script for GMAPS API Search
library(anonymizer)
library(readr)
library(dplyr)
library(stringr)
library(RPostgreSQL)
library(data.table)
library(tidyr)
library(chron)
library(lettercase)
library(stringi)
#library(googleway) #Won't use this one as the resuts are parsed out as a list that must be processed to return key values
library(placement)
#Check City List for real city names using Google Geocoder
key <- 'Use Your Key 1'
gmaps_geocode_key <- 'Use Your Key 2'
'%ni%' <- Negate('%in%')
ClipperAccounts <- read_csv("Documents/Data/Clipper/MTC_RegUsers.csv")
#Fix String Case for selected fields
ClipperAccounts$CITY <- stri_trans_general(ClipperAccounts$CITY, id="Title")
ClipperAccounts$FIRST_NAME <- stri_trans_general(ClipperAccounts$FIRST_NAME, id="Title")
ClipperAccounts$LAST_NAME <- stri_trans_general(ClipperAccounts$LAST_NAME, id="Title")
#ClipperAccounts$STATE <- stri_trans_general(ClipperAccounts$STATE, id="Title")
ClipperAccounts$STATUS <- stri_trans_general(ClipperAccounts$STATUS, id="Title")
ClipperAccounts$CARD_TYPE <- stri_trans_general(ClipperAccounts$CARD_TYPE, id="Title")
#Filter Records for Subset that match minimum criteria for matching
# Need to evaluate record counts for this table for all key fields listed below
# Show a count of unique values in key fields
# This would be useful in a table or som eother form of visual to explain which records we will be dropping from the set.
ClipperAccounts %>%
select(CONTACT_ID,FIRST_NAME,LAST_NAME,CITY,STATE,CARD_SERIAL_NUMBER,CARD_TYPE, STATUS) %>%
filter(!is.na(CONTACT_ID) &
!is.na(LAST_NAME) &
!is.na(CITY) &
!is.na(STATE) &
!is.na(CARD_SERIAL_NUMBER) &
!is.na(CARD_TYPE) &
!is.na(STATUS) & STATE == "CA" &
STATUS %in% c("Blocked","Active","blocked") &
CARD_TYPE %ni% c("Failed","Initialized", "Adult - Obsolete")
) -> ClipperGood
#Turn City List into Data Frame
CityList <- data.frame(CityName=unique(ClipperGood$CITY))
# Append California to all City Name Values as these locations should all only be California Records
CityList$Location <- paste0(CityList$CityName, ", California")
CityList %>% top_n(3407) -> CityList
CityList <- as.character(CityList$Location)
# This Method uses the placement library
#https://www.r-bloggers.com/placement-an-r-package-to-access-the-google-maps-api/
MatchedCities <- geocode_url(CityList, auth="standard_api", privkey=key,
clean=TRUE, add_date='today', verbose=TRUE)
#Merge return cities locations from google to main dataframe based upon Cityname value
#This will be used to decide whether city values are accurate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment