Instantly share code, notes, and snippets.

@swingley swingley/index.js
Created Nov 14, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

igorkosta commented Nov 15, 2016

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