Skip to content

Instantly share code, notes, and snippets.

@chriszs
Last active February 10, 2016 22:22
Show Gist options
  • Save chriszs/0dce7c7658d58b0b7206 to your computer and use it in GitHub Desktop.
Save chriszs/0dce7c7658d58b0b7206 to your computer and use it in GitHub Desktop.
Compute a GeoJSON of centroids for markets with stations using lodash and turf.js.
var turf = require('turf'),
_ = require('lodash');
// iterate through markets
markets = markets.map(function (stations) {
// and stations within those markets
var transmitters = stations.map(function (station) {
// reduce the stations to coordinates, or null
if (station.fcc_station && station.fcc_station.transmitter && station.fcc_station.transmitter.coordinates) {
return turf.point(station.fcc_station.transmitter.coordinates);
}
else {
return null;
}
});
// remove null locations, package into GeoJSON feature collection
transmitters = turf.featurecollection(_.compact(transmitters));
// compute centroid for market
var centroid = turf.centroid(transmitters);
// set properties on the market GeoJSON object, including a total, name and ID
centroid.properties.id = stations[0].market;
centroid.properties.name = stations[0].market;
centroid.properties.total = _.sum(stations,'count');
return centroid;
});
// package markets into GeoJSON feature collection
markets = turf.featurecollection(markets);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment