Created
November 14, 2015 00:51
-
-
Save swingley/e05a6c8193c2a21ba304 to your computer and use it in GitHub Desktop.
List of California reservoirs from wikipedia to geojson (example resulting geojson file: https://gist.github.com/swingley/25d2bb374a344958ac7d).
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
#!/usr/local/bin/node | |
var fs = require('fs'); | |
var Xray = require('x-ray'); | |
var xray = new Xray(); | |
var turfPoint = require('turf-point'); | |
var turfFeatureCollection = require('turf-featurecollection'); | |
var wikiReservoirs = 'https://en.wikipedia.org/wiki/List_of_largest_reservoirs_of_California'; | |
var reservoirs = {}; | |
var out = 'california-reservoirs-wikipedia.geojson'; | |
// Scrape reservoir info. | |
xray(wikiReservoirs, 'table.wikitable.sortable tr', [{ | |
name: 'td:nth-child(1)', | |
county: 'td:nth-child(2)', | |
coordinates: 'td:nth-child(3) span.geo-nondefault span span.geo', | |
volumeAcreFeet: 'td:nth-child(4)', | |
volumeKm: 'td:nth-child(5)', | |
outflow: 'td:nth-child(6)', | |
dam: 'td:nth-child(7)', | |
image: 'td:nth-child(8) img@src' | |
}])(function(err, obj) { | |
if ( err ) { console.log('xray error: ' + err); return; } | |
// Add numerical latitude and longitude properties. | |
// Remove stuff like [12] and [n 4] since it's meaningless now. | |
var features = obj.map(function(info, index) { | |
for ( var prop in info ) { | |
if ( prop === 'coordinates' ) { | |
info.latitude = parseFloat(info[prop].split('; ')[0]); | |
info.longitude = parseFloat(info[prop].split('; ')[1]); | |
} | |
info[prop] = info[prop].replace(/\[.*\]/g, '').replace('\n', ' '); | |
} | |
return turfPoint([info.longitude, info.latitude], info); | |
}); | |
var featureCollection = turfFeatureCollection(features); | |
fs.writeFile(out, JSON.stringify(featureCollection, null, 2), function(err) { | |
if ( err ) { console.log('write failed', err); } | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/swingley/e05a6c8193c2a21ba304#file-index-js-L10
var reservoirs = {}
is never used, right?