-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (158 loc) · 5.87 KB
/
index.js
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
#!/usr/bin/env node
const csv = require('csvtojson')
const wktParse = require('wellknown');
const turfBooleanPointInPolygon = require('@turf/boolean-point-in-polygon').default;
const turfIntersect = require('@turf/intersect').default;
const yargs = require('yargs');
const version = require('./package.json').version;
//command line help
const argv = yargs
.usage('Link two CSV files by geographic coordinates (WKT format)')
.example('$0 -c example/assets.csv -k geo -p example/neighborhood-associations-geo.csv -q the_geom -f OrgName')
.example('')
.example('$0 --coordinatesfile example/assets.csv --pointfile-wkt-field geo --polyfile example/neighborhood-associations-geo.csv --polyfile-wkt-field the_geom --fields OrgName')
.alias('p', 'polyfile').describe('p', 'CSV file with WKT Polygons and GEOID in the properties')
.alias('q', 'polyfile-wkt-field').describe('q', 'Fieldname for the poly file WKT field (default: geometry)')
.alias('c', 'coordinatesfile').describe('c', 'CSV with coordinates/points')
.alias('k', 'pointfile-wkt-field').describe('k', 'Fieldname for the point file WKT field (default: geometry)')
.array('f').alias('f', 'fields').describe('f', 'Fields to match, separated by a space (default: GEOID)')
.boolean('r').alias('r', 'reverse').describe('r', 'Copy the data from the point data to the polygon data instead')
.wrap(yargs.terminalWidth())
.demandOption(['f','p','q','c','k'])
.version(version).alias('version','v')
.help('h').alias('h', 'help').showHelpOnFail(true)
.argv;
if (require.main == module) {
//command line
if (argv.p && argv.c) {
csv().fromFile(argv.p).then((polyData) => {
var featureCollection = {
"type": "FeatureCollection",
"features": []
}
polyData.forEach(function(f) {
var geom = wktParse(f[argv.q] || f.geometry);
var props = f;
delete props.geometry;
if (geom) {
featureCollection.features.push({
"type": "Feature",
"geometry": geom,
"properties": props
})
}
});
csv().fromFile(argv.c).then((pointData) => {
var pointFeatureCollection = {
"type": "FeatureCollection",
"features": []
}
pointData.forEach(function(f) {
var geom = wktParse(f[argv.k] || f.geometry);
var props = f;
delete props.geometry;
if (geom) {
pointFeatureCollection.features.push({
"type": "Feature",
"geometry": geom,
"properties": props
})
}
})
match({
polyfile: featureCollection,
coordinatesfile: pointFeatureCollection,
fields: argv.f || ['GEOID'],
reverse: argv.r,
sync: true
}, function(err, newFile) {
if (err && err.length) console.error(err);
else {
let rows = [];
let data = newFile.features;
data.forEach(function(row) {
rows.push(row.properties)
})
const items = rows;
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(items[0])
let csv = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
csv.unshift(header.join(','))
csv = csv.join('\r\n')
console.log(csv);
}
});
})
})
}
}
function match(options, callback) {
var fields = options.fields;
var polyGeoJSON = options.polyfile
var pointGeoJSON = options.coordinatesfile;
var err = [];
var points = [...new Set(pointGeoJSON.features)];
var polies = [...new Set(polyGeoJSON.features)];
points.forEach(function(point) {
if (typeof fields === "string") {
fields = [fields];
}
fields.forEach(function(fname) {
if (!point.properties[fname]) {
point.properties[fname] = null;
}
})
polies.forEach(function(poly) {
let isIn = false;
if (point.geometry.type == "Point") {
isIn = turfBooleanPointInPolygon(point, poly);
} else {
isIn = turfIntersect(point,poly);
}
if (isIn) {
if (typeof fields === "string") {
fields = [fields];
}
fields.forEach(function(fname) {
if (options.reverse) {
if (poly.properties[fname] && typeof poly.properties[fname] === "string") {
poly.properties[fname] = poly.properties[fname].split(',')
} else if (poly.properties[fname]) {
poly.properties[fname].push(point.properties[fname]);
poly.properties[fname] = [...new Set(poly.properties[fname])]
} else {
poly.properties[fname] = [point.properties[fname]];
}
} else {
if (point.properties[fname] && typeof point.properties[fname] === "string") {
point.properties[fname] = point.properties[fname].split(',')
} else if (point.properties[fname]) {
point.properties[fname].push(poly.properties[fname]);
point.properties[fname] = [...new Set(point.properties[fname])]
} else {
point.properties[fname] = [poly.properties[fname]];
}
}
});
fields.forEach(function(fname) {
if (options.reverse) {
if (poly.properties[fname] && poly.properties[fname].length) {
poly.properties[fname] = poly.properties[fname].join(',')
}
} else {
if (point.properties[fname] && point.properties[fname].length) {
point.properties[fname] = point.properties[fname].join(',')
}
}
})
}
})
})
if (typeof callback === "function") {
if (options.reverse) {
callback(err, polyGeoJSON);
} else {
callback(err, pointGeoJSON);
}
}
}