-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocode.R
215 lines (170 loc) · 9.68 KB
/
geocode.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#geocoding
getjsonHeader <-function() {
'{"ValidateAddressesRequest": { "AddressToValidateList": { "AddressToValidate": ['
# '{ValidateAddressesRequest: { AddressToValidateList: { AddressToValidate: ['
}
getjsonFooter <-function() {
# add 2 extra } at beginning to close the last item in enumeration due to replacement of
# AddressBlockLines":"' by '"AddressBlockLines":{"UnstructuredAddressLine": {"*body
'}}] }, "CallerIdentification": { "CallerName": "klimaatzaak" } } }'
}
buildGoogleApiGeocodeJsonUrlEncode <- function(dt){
jsonInput <- copy(dt[,.(id, address,street_nb, address2,zip, locality, country)])
jsonInput[is.na(jsonInput)] <- '' #replace NAs by blanks for building address
jsonOutput <- jsonInput[, .( Address = paste(c(address,street_nb, address2, ',' ,zip, locality, country), collapse = '+')
,components = paste(c( paste(c('postal_code' , zip ), collapse = ':')
,paste(c('locality' , locality), collapse = ':')
,paste(c('country' , country ), collapse = ':')
)
, collapse = '|')
,postal_code = zip
)
,by = id]
jsonOutput[, Address := str_replace_all(Address, fixed(' '), '+')]
jsonOutput[, components := str_replace_all(components, fixed(' '), '+')]
jsonOutput[, postal_code := str_replace_all(postal_code, fixed(' '), '+')]
}
getSingleGoogleGeocodingRest <- function(address, components = NULL, postal_code = NULL){
#if zip code in Brussels area, need FR. else set according to region.
#https://en.wikipedia.org/wiki/List_of_postal_codes_in_Belgium
if(postal_code < 1500)
{ language_tag <- '&language=fr'}
else {
if(postal_code < 4000)
{ language_tag <- '&language=nl'}
else {
if(postal_code < 8000)
{ language_tag <- '&language=fr'}
else
{ language_tag <- '&language=nl'}
}
}
#build URL: add address, then optionally language, then components then key
urlFromParts <- paste( "https://maps.googleapis.com/maps/api/geocode/json?address", paste(address , language_tag , sep='' ),sep='=')
urlFromParts <- paste(urlFromParts , paste('components', components , sep='='),sep='&')
urlFromParts <- paste(urlFromParts , paste('key' , parameters$google_api_key, sep='='),sep='&')
#print(paste('debug: ',urlFromParts, sep= ''))
result <- GET(urlFromParts)
stop_for_status(result)
dfBuffer <- content(result,"parsed")
if(length(dfBuffer$results) == 0) {
#retry without components
#to handle cases where country= BE is wrong because it s an address abroad
urlFromParts <- paste( "https://maps.googleapis.com/maps/api/geocode/json?address", paste(address , language_tag , sep='' ),sep='=')
urlFromParts <- paste(urlFromParts , paste('key' , parameters$google_api_key, sep='='),sep='&')
#print(paste('debug: ',urlFromParts, sep= ''))
result <- GET(urlFromParts)
stop_for_status(result)
dfBuffer <- content(result,"parsed")
}
#still no luck
if(length(dfBuffer$results) == 0) {
paste( NA
,NA
,NA
,NA
,dfBuffer$status
,sep = '|')
}
else {
paste(dfBuffer$results[[1]]$formatted_address
,dfBuffer$results[[1]]$geometry$location$lat
,dfBuffer$results[[1]]$geometry$location$lng
,dfBuffer$results[[1]]$geometry$location_type
,dfBuffer$status
,sep = '|')
}
}
# input: data.table of id s and addresses (mapIdtoAddress)
# output:same structure id s and cleansed addresses (mapIdtoAddress)
geocodeAddress <- function(my_mapIdtoAddress, get_new = TRUE){
#internal persistence
#connect to DB table or file to retrieve addresses
existingGeocodedAddress <- readCsvFromDirectory('geocodedAddress', parameters$path_geocoded_address)
existingGeocodedAddress[, X:=NULL]
#check uniqueAddress against data store of validated addresses
#only submit missing addresses for geocoding
uniqueAddress <- unique(my_mapIdtoAddress[, .(Address, components, postal_code)])
newUniqueAddress <- base::merge(uniqueAddress, existingGeocodedAddress ,all.x = TRUE )
newUniqueAddress <- newUniqueAddress[is.na(address_validated), .(Address, components, postal_code)]
print('addresses to geolocate using webservice: ')
print(nrow(newUniqueAddress))
#initialize
geocodedAddress <- existingGeocodedAddress
if(get_new == TRUE) {
#submit addresses that were not matched
for(i in 1:floor(nrow(newUniqueAddress)/ parameters$fetch_size)){
geocodedAddressBuffer = NULL
for(j in 1:parameters$fetch_size){
geocodedAddressBuffer <- rbind (geocodedAddressBuffer, newUniqueAddress[(((i-1)*parameters$fetch_size)+j)
, .(Address, lapply(Address
, getSingleGoogleGeocodingRest
, components = components
, postal_code = postal_code))])
}
# print('debug API call finished ')
geocodedAddressBuffer[, c("address_validated", "latitude", "longitude", "location_type", "status") := tstrsplit(V2, "|", fixed=TRUE)]
geocodedAddressBuffer[, V2:= NULL]
write.csv2(geocodedAddressBuffer
, file = paste(paste('..',parameters$batch_name, sep = '/'), parameters$path_geocoded_address, sep = '/') %>%
paste(i, sep = "/") %>%
paste("geocodedAddress.csv" ,sep='_')
, row.names = TRUE
, quote = FALSE)
# print('debug write csv finished ')
print('debug iteration ')
print(i)
print('valid records ')
print(nrow(geocodedAddressBuffer[status=='OK' & location_type != 'APPROXIMATE']))
geocodedAddress <- rbindlist(list(geocodedAddress, geocodedAddressBuffer[status=='OK' & location_type != 'APPROXIMATE',]), idcol = FALSE, fill =FALSE , use.names = TRUE)
#internal persistence
#store incrementally into fst as we loop, since process crashes frequently
writeCsvIntoDirectory(unique(geocodedAddress[,.(Address,address_validated, latitude, longitude, location_type, status)]), 'geocodedAddress', parameters$path_geocoded_address)
}
}
#merge with id
validated_data <- base::merge(my_mapIdtoAddress, geocodedAddress[,.(Address, address_validated)] )
validated_data[, Address:=NULL]
writeCsvIntoDirectory(validated_data, 'mapIdtoAddressValidated', parameters$path_geocoded_address)
#return mapIdToAddress id, validatedAddress
validated_data
}
#high level geocoding entry point, procedure refactored from main
cleanupAddresses <- function(cleanData){
#validate addresses and store in a separate permanent store
mapIdtoAddress <- buildGoogleApiGeocodeJsonUrlEncode(cleanData)
writeCsvIntoDirectory(mapIdtoAddress, 'mapIdtoAddress', parameters$path_geocoded_address)
mapIdtoAddressValidated <- geocodeAddress(mapIdtoAddress , get_new = parameters$get_new_geocoding)
#no need to geolocate the id's above 100000, which are the plaintiffs already added to trial.
missing_geoloc <- base::merge(cleanData[id < 100000,], mapIdtoAddressValidated, all.x = TRUE)[is.na(address_validated) & reason != 'U' & reason != 'D',]
writeCsvIntoDirectory(missing_geoloc, 'missing_geoloc', parameters$path_dataQualityCheck)
geocodedData <- base::merge(cleanData, mapIdtoAddressValidated, all.x = TRUE)
#build validated address from components for reason = 'U'se existing
geocodedData[reason == 'U' & is.na(address_validated) , address_validated := paste(address,street_nb, ifelse(is.na(address2), '', address2 ), ',' ,zip, locality, country) ]
writeCsvIntoDirectory(geocodedData, 'geocoded_data', parameters$path_forupload)
#writeFstIntoDirectory(geocodedData, 'geocoded_data', parameters$path_forupload)
geocodedData
}
#ad hoc task start
#run from within "geocodeAddress" context
#filter geocoded addresses that are not in Brussels
#initial statistics
# valid_zip
# geocoded FALSE TRUE
# FALSE 956 14507
# TRUE 106 38782
#uniqueAddress[as.integer(postal_code) < parameters$zip_codes_brussels_region_lowerbound
# | as.integer(postal_code) > parameters$zip_codes_brussels_region_upperbound,]
#43462 unique address records not in brussels at the moment of cleanup
# geocodeAddressNoBrussels <- base::merge(existingGeocodedAddress, uniqueAddress[as.integer(postal_code) < parameters$zip_codes_brussels_region_lowerbound
# |as.integer(postal_code) > parameters$zip_codes_brussels_region_upperbound
# ,.(Address, postal_code)]
# , by=c('Address')
# ,all.x = TRUE )
# geocodeAddressNoBrussels <- geocodeAddressNoBrussels[!is.na(postal_code),]
# geocodeAddressNoBrussels[,postal_code:=NULL]
# valid_zip
# geocoded FALSE TRUE
# FALSE 2 32
# TRUE 7370 31599
#ad hoc task end