Skip to content

Instantly share code, notes, and snippets.

@swingley
Created November 14, 2015 00:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swingley/e05a6c8193c2a21ba304 to your computer and use it in GitHub Desktop.
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).
#!/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); }
});
});
@igorkosta
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment