-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.js
300 lines (279 loc) · 10.4 KB
/
filters.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
const Town_boundaries = require('./geojson/vt_towns.json')
const Vermont_regions = require('./geojson/Polygon_VT_Biophysical_Regions.json')
const CountyBarcharts = require('./data/countyBarcharts.json')
const GeoJsonGeometriesLookup = require('geojson-geometries-lookup')
const vermontTowns = new GeoJsonGeometriesLookup(Town_boundaries)
const vermontRegions = new GeoJsonGeometriesLookup(Vermont_regions)
const _ = require('lodash')
const moment = require('moment')
const provinces = require('provinces')
const helpers = require('./helpers')
const nearestPoint = require('@turf/nearest-point')
const turf = require('turf')
const centerOfMass = require('@turf/center-of-mass')
// Why eBird uses this format I have no idea.
const eBirdCountyIds = {
1: 'Addison',
3: 'Bennington',
5: 'Caledonia',
7: 'Chittenden',
9: 'Essex',
11: 'Franklin',
13: 'Grand Isle',
15: 'Lamoille',
17: 'Orange',
19: 'Orleans',
21: 'Rutland',
23: 'Washington',
25: 'Windham',
27: 'Windsor'
}
// Used more than once.
const townCentroids = getTownCentroids()
// Defaults to all
function getTownCentroids (town) {
const centers = Town_boundaries.features.map(feature => {
let center
// This center of West Haven is in New York.
if (feature.properties.town === 'West Haven'.toUpperCase()) {
center = centerOfMass.default(feature)
// TODO Unfortunately, the enclaves are broken. All Rutland counts are in Rutland City.
} else if (feature.properties.town.includes('Rutland'.toUpperCase())) {
center = turf.center(feature)
// console.log(feature.properties.town, center.geometry.coordinates.reverse())
} else {
center = turf.center(feature)
}
center.properties = feature.properties
return center
})
if (town) {
return centers.find(c => c.properties.town === town.toUpperCase())
} else {
return centers
}
}
// map: 'towns' || 'regions'
// coordinates: {
// Longitude: row.LONGITUDE,
// Latitude: row.LATITUDE
// }
// countyCode: 023
function getPoint (map, coordinates, countyCode) {
function getContainer (map, coordinates) {
let point
if (map === 'towns') {
point = pointLookup(Town_boundaries, vermontTowns, coordinates)
} else if (map === 'regions') {
point = pointLookup(Vermont_regions, vermontRegions, coordinates)
}
return point
}
let point = getContainer(map, coordinates)
// If it is on a river or across a border or something, get the nearest town
if (point === undefined) {
try {
// Only check towns in the relevant county
// TODO What if I don't have the relevant county?
const countyCenters = townCentroids.filter(f => f.properties.county === countyCode);
const long = coordinates.LONGITUDE || coordinates.Longitude
const lat = coordinates.LATITUDE || coordinates.Latitude
const newCoords = nearestPoint.default(turf.point([long, lat]), turf.featureCollection(countyCenters));
coordinates = {
Longitude: newCoords.geometry.coordinates[0],
Latitude: newCoords.geometry.coordinates[1]
};
point = getContainer(map, coordinates);
// console.log('Previously undefined point:', point);
} catch (error) {
console.error("Error occurred while processing newCoords:", error);
// You can handle the error or just log it, as done above.
// The script will continue to run even if this block throws an error.
}
}
return point
}
function pointLookup (geojson, geojsonLookup, data) {
// Shim input
let point
if (data.type === 'Point') {
point = data
} else {
point = { type: 'Point', coordinates: [data.Longitude, data.Latitude] }
}
// TODO Add a fallback if it fails
const containerArea = geojsonLookup.getContainers(point)
if (containerArea.features[0]) {
const props = containerArea.features[0].properties
return (props.town) ? props.town : props.name
}
}
function locationFilter (list, opts) {
const filterList = ['Country', 'State', 'Region', 'County', 'Town']
const intersection = _.intersection(Object.keys(opts).map(x => helpers.capitalizeFirstLetters(x)), filterList)
return list.filter(checklist => {
if (!checklist.Latitude) {
// Some audio records appear to be totally empty locationalls
if (opts.verbose) {
console.log(`Checklist discarded: ${checklist['eBird Checklist URL']}.`)
}
return false
}
if (!checklist.State) {
const [country, state] = checklist['State/Province'].split('-')
if (state === 'VT') { // Just to speed things up a bit
checklist.State = 'Vermont'
} else if (['US', 'CA'].includes(country)) { // Enable for others
if (_.findIndex(provinces, { short: state }) !== -1) { // Note that this file is larger than needed, and has more countries
checklist.State = provinces[_.findIndex(provinces, { short: state })].name
}
} else {
checklist.State = state
}
checklist.Country = country
}
if (checklist.State === 'Vermont') {
// This option takes 25 seconds to do, every time, on my data
let point
checklist.Region = pointLookup(Vermont_regions, vermontRegions, checklist)
checklist.Town = pointLookup(Town_boundaries, vermontTowns, checklist)
// These should only apply to literal edge cases
if (!checklist.Town) {
point = getPoint('towns', {
Longitude: checklist.Longitude,
Latitude: checklist.Latitude
// This is ugly but it should work.
}, Number(Object.keys(eBirdCountyIds).filter(key => eBirdCountyIds[key] === checklist.County)[0]))
checklist.Town = helpers.capitalizeFirstLetters(point)
}
if (!checklist.Region) {
point = getPoint('regions', {
Longitude: checklist.Longitude,
Latitude: checklist.Latitude
// This is ugly but it should work.
}, Number(Object.keys(eBirdCountyIds).filter(key => eBirdCountyIds[key] === checklist.County)[0]))
checklist.Region = helpers.capitalizeFirstLetters(point)
}
}
return intersection.every(filter => {
// console.log(filter.toLowerCase(), opts)
if (Array.isArray(opts[filter.toLowerCase()])) {
// console.log(opts[filter.toLowerCase()])
const test = opts[filter.toLowerCase()].find(x => {
// console.log(opts, filter, checklist)
return opts[filter.toLowerCase()].map(y => y.toLowerCase()).includes(checklist[filter].toLowerCase())
})
return !!(test)
} else {
if (opts[filter.toLowerCase()] && checklist[filter]) {
// console.log(checklist, filter)
// TODO This should also work for Arrays, I guess
return checklist[filter].toLowerCase() === opts[filter.toLowerCase()].toLowerCase()
} else {
console.log('Wrong state', checklist[filter], checklist)
}
}
return false
})
})
}
function dateFilter (list, opts) {
// Currently not documented
if (opts.after) {
return list.filter(x => {
return moment(x.Date, helpers.momentFormat(x.Date)).isAfter(moment(opts.after))
})
}
// TODO Make month and day work
if (!opts.year) {
return list
}
return list.filter(x => {
return moment(x.Date, helpers.momentFormat(x.Date)).format('YYYY') === opts.year.toString()
})
}
function durationFilter (list, opts) {
if (opts.duration && !parseInt(opts.duration)) {
console.log('Duration filter not a number!')
process.exit(1)
}
return (opts.duration) ? list.filter(x => parseInt(x['Duration (Min)']) >= opts.duration) : list
}
function completeChecklistFilter (list, opts) {
// This isn't as clear cut as it should be. There are other non-complete formats: Historical, etc.
list = (opts.noIncidental) ? list.filter(x => !['Incidental', 'Historical'].includes(x.Protocol)) : list
list = (opts.complete) ? list.filter(x => [1, '1'].includes(parseInt(x['All Obs Reported']))) : list
return list
}
function orderByDate (arr) {
return _.orderBy(arr, (e) => moment(e.Date, helpers.momentFormat(e.Date)).format())
}
function createPeriodArray (data) {
const periodArray = []
for (const period in data) {
periodArray.push({
Date: period,
SpeciesTotal: removeSpuh(_.uniqBy(data[period], 'Scientific Name')).length,
Species: removeSpuh(_.uniqBy(data[period], 'Scientific Name'))
})
}
return _.sortBy(periodArray, 'SpeciesTotal').reverse()
}
function removeSpuh (arr, reverse) {
const newArr = []
for (var i in arr) {
if (arr[i]['Scientific Name'] &&
!arr[i]['Scientific Name'].includes('sp.') &&
!arr[i]['Scientific Name'].includes(' x ') && // Get rid of hybrids
!arr[i]['Scientific Name'].includes('hybrid') && // Get rid of Lawrence's and Brewster's Warblers
!arr[i]['Scientific Name'].includes('Domestic type') && // Get rid of Domestic types
!arr[i]['Scientific Name'].split(' ').slice(0, 2).join(' ').includes('/') && // No Genus-level splits
!reverse
// !arr[i]['Scientific Name'].includes('[') &&
// !arr[i]['Scientific Name'].match(/.* .* .*/g) &&
// !arr[i]['Scientific Name'].includes('/')
) {
// Remove subspecies only entries
// For some reason, simply copying over the field before redefining it doesn't work.
// Probably due to JavaScript reference errors.
const specie = arr[i]
if (specie['Scientific Name'].split(' ').slice(2).length !== 0) {
arr[i].Subspecies = _.clone(arr[i]['Scientific Name'])
}
specie['Scientific Name'] = specie['Scientific Name'].split(' ').slice(0, 2).join(' ')
newArr.push(specie)
// } else {
// Use this to find excluded entries
// console.log(arr[i]['Scientific Name'])
} else if (reverse) {
const specie = arr[i]
newArr.push(specie)
}
}
return _.uniq(newArr)
}
function removeSpuhFromCounties (countyBarcharts) {
const newObj = {}
Object.keys(CountyBarcharts).forEach(county => {
newObj[county] = removeSpuh(Object.keys(CountyBarcharts[county].species).map(s => {
const species = CountyBarcharts[county].species[s]
// ES6 probably has a better way of doing this.
species.name = s
return species
})).map(s => s.name)
})
return newObj
}
module.exports = {
orderByDate,
durationFilter,
completeChecklistFilter,
dateFilter,
createPeriodArray,
locationFilter,
removeSpuh,
removeSpuhFromCounties,
pointLookup,
getPoint,
getTownCentroids
}