forked from ilyankou/geocoder-for-google-sheets
-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
geocoder-census-geographies.gs
111 lines (92 loc) · 2.65 KB
/
geocoder-census-geographies.gs
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
var ui = SpreadsheetApp.getUi();
var addressColumn = 1;
var latColumn = 2;
var lngColumn = 3;
var foundAddressColumn = 4;
var qualityColumn = 5;
var sourceColumn = 6;
var geoIdColumn = 7;
var tractColumn = 8;
/**
* Use US Census geocoder
*/
function geocode() {
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getActiveRange();
if (cells.getNumColumns() != 8) {
ui.alert(
'Warning',
'You must select 8 columns: Location, Latitude, Longitude, Found, Quality, Source, GeoID, Tract',
ui.ButtonSet.OK
);
return;
}
var nAll = 0;
var nFailure = 0;
var quality;
var printComplete = true;
for (addressRow = 1; addressRow <= cells.getNumRows(); addressRow++) {
var address = cells.getCell(addressRow, addressColumn).getValue();
if (!address) {continue}
nAll++;
nFailure += withUSCensus(cells, addressRow, address);
}
if (printComplete) {
ui.alert('Completed!', 'Geocoded: ' + (nAll - nFailure)
+ '\nFailed: ' + nFailure, ui.ButtonSet.OK);
}
}
/**
* Geocoding with US Census Geocoder https://geocoding.geo.census.gov/geocoder/
*/
function withUSCensus(cells, row, address) {
var url = 'https://geocoding.geo.census.gov/'
+ 'geocoder/geographies/onelineaddress?address='
+ encodeURIComponent(address)
+ '&vintage=Current_Current'
+ '&benchmark=Public_AR_Current'
+ '&format=json';
var response = JSON.parse(UrlFetchApp.fetch(url));
var matches = (response.result.addressMatches.length > 0) ? 'Match' : 'No Match';
if (matches !== 'Match') {
insertDataIntoSheet(cells, row, [
[foundAddressColumn, ''],
[latColumn, ''],
[lngColumn, ''],
[qualityColumn, 'No Match'],
[sourceColumn, '']
]);
return 1;
}
var z = response.result.addressMatches[0];
var quality;
if (address.toLowerCase().replace(/[,\']/g, '') ==
z.matchedAddress.toLowerCase().replace(/[,\']/g, '')) {
quality = 'Exact';
} else {
quality = 'Match';
}
insertDataIntoSheet(cells, row, [
[foundAddressColumn, z.matchedAddress],
[latColumn, z.coordinates.y],
[lngColumn, z.coordinates.x],
[qualityColumn, quality],
[sourceColumn, 'US Census 2010'],
[geoIdColumn, z.geographies['2010 Census Blocks'][0].GEOID],
[tractColumn, z.geographies['Census Tracts'][0].BASENAME]
]);
return 0;
}
/**
* Sets cells from a 'row' to values in data
*/
function insertDataIntoSheet(cells, row, data) {
for (d in data) {
cells.getCell(row, data[d][0]).setValue(data[d][1]);
}
}
function onOpen() {
ui.createMenu('Geocoder')
.addItem('US Census 2010 Geographies', 'geocode')
.addToUi();
}