Last active
January 25, 2019 17:56
-
-
Save AustinHunt/c2395862dcc8d423531726501214624b to your computer and use it in GitHub Desktop.
example largest impacting hail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as turf from '@turf/turf'; | |
import _ from 'lodash'; | |
const geometry = { | |
"type": "Point", | |
"coordinates": [ | |
-73.92714109999997, | |
40.8657281 | |
] | |
}; | |
const data = //Fetched map data.... | |
const stage1 = []; //All impacting polygons. | |
data.forEach(map => { | |
const { features, properties } = map; | |
const impactingFeatures = []; | |
features.filter(feature => !!feature.properties.hailSize).forEach(feature => { | |
if (feature.geometry.type !== 'Polygon' || !turf.booleanWithin(geometry, feature)) { | |
return; | |
} | |
const hailSize = feature.properties && feature.properties.hailSize; | |
impactingFeatures.push(feature); | |
}); | |
stage1.push([map, impactingFeatures]); | |
}); | |
/* Largest impacting polygons (hailSize) per map (storm_date). | |
* stage2 = [ Date, HailSizeNumber ]; | |
*/ | |
const stage2 = []; | |
const hailSizeSelector = obj => Number(_.property('properties.hailSize')(obj)) | |
stage1.forEach(([ map, impactingFeatures ]) => { | |
const maxFeature = _.maxBy(impactingFeatures, hailSizeSelector); | |
stage2.push([ | |
map.properties.storm_date, | |
hailSizeSelector( | |
maxFeature, | |
), | |
_.pick(map, ['_id', 'properties', 'type']), | |
]) | |
}); | |
// Final Stage | |
results = _.unzip(stage2); // => Array<Array<StormDate>,Array<HailSize>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment