Last active
February 10, 2016 22:22
-
-
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.
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
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