Skip to content

Instantly share code, notes, and snippets.

@burritojustice
Last active October 4, 2016 04:41
Show Gist options
  • Save burritojustice/5bb590b333522cc5c4bbbb778f058bc6 to your computer and use it in GitHub Desktop.
Save burritojustice/5bb590b333522cc5c4bbbb778f058bc6 to your computer and use it in GitHub Desktop.
WOF descender
<!DOCTYPE html>
<html>
<head>
<title>WOF descender</title>
<script type="text/javascript" src="fileSaver.js"></script>
</head>
<body>
<!-- experiments in a UI for entering a WOF ID and level -->
<!--
<form name="wof_data">
WOF ID:<br>
<input type="text" name="wof_id" value="85633041">
Descendant type:
<select name="wof_level" value='region'>
<option value="region">region</option>
<option value="county">county</option>
<option value="locality">locality</option>
<option value="neighborhood">neighborhood</option>
</select><br>
<input
type="button"
onclick="wof_descender(form.wof_id.value, form.wof_level.value)"
value="Descend">
</form>
-->
<!--
<a href="#" id="link" download="example.json">
Download as JSON
</a>
-->
</body>
<script>
// define WOF IDs, level, and token
var wof_access_token = 'bfa3cbc6f49de8faf4d99e41c8d328f2';
// var wof_id = 85688637; // california
// var wof_id = 85632227; // tanzania
var wof_id = 85633041; // canada
// var wof_id = 85633793; // US
// var wof_level = 'locality';
// var wof_level = 'county';
var wof_level = 'region';
// var wof_id = document.getElementById("wof_id").value;
// var wof_level = document.getElementById("wof_level").value;
// grab WOF info from UI
// var wof_level;
// var wof_id;
//
// function wof_descender(wof_id,wof_level) {
// return(wof_id,wof_level);
// }
// define XHR stuff
var wof_parent = wof_id;
var wof_parent_name;
var xhr = new XMLHttpRequest();
// counters and arrays for descendants and GeoJSON
var descendantsJSON = [];
var descendantsCount = 0;
var descendantsProcessed = 0;
// build url for list of descendants
var url = 'https://whosonfirst.mapzen.com/api/rest/?method=whosonfirst.places.getDescendants&access_token=' + wof_access_token + '&id=' + wof_id +'&placetype=' + wof_level + '&page=1&per_page=200';
// prefix and suffix for building GeoJSON feature collection
var features = [];
var featureCollectionStart = '{ "type": "FeatureCollection", "features": [ ';
var featureCollectionEnd = ' ] }';
// add featureCollection to document
//
// var t = document.createTextNode(featureCollectionStart);
// document.body.appendChild(t);
// get list of descendants
xhr.open('GET', url, true);
xhr.send();
xhr.addEventListener("readystatechange", process_wof_id, false);
// check readyState of XHR request for descendant list
function process_wof_id(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log("building URLs" + Date());
response = JSON.parse(xhr.responseText);
descendantsCount = response.results.length;
// loop through list and build URLs
for (var i = 0; i < descendantsCount; i++) {
var wof_id = response.results[i]['wof:id'];
var wof_name = response.results[i]['wof:name'];
console.log(wof_name + ": " + wof_level);
var wof_url = [];
wof_url[i] = makeWOFURL(wof_id);
console.log(wof_url[i]);
//try to grab URL of a descendant
var xhr2 = new XMLHttpRequest();
xhr2.open('GET', wof_url[i], true);
console.log("getting " + i);
xhr2.send();
xhr2.addEventListener("readystatechange", process_wof_descendant, false);
} //for
var wait = function() {
console.log("WAITING..." + descendantsProcessed + " of " + descendantsCount);
// if (descendantsProcessed != 0){
if (descendantsProcessed < descendantsCount){
setTimeout(wait, 500);
console.log("buffering...");
return;
}
// console.log("BUELLER");
feature_collection = {
'type': 'FeatureCollection',
'features': features,
}
// geojson = JSON.stringify(feature_collection);
// console.log(features.length);
console.log("starting blob " + Date());
var args = {type: "application/json"};
var blob = new Blob([JSON.stringify(feature_collection)], args);
console.log("stopping blob " + Date());
var filename = wof_parent + '_' + wof_level + '.geojson'
saveAs(blob, filename);
// var out = document.createTextNode(geojson);
// document.body.appendChild(out);
//
// hand geojson off to tangram... however you do that
}
wait();
} //if
} //function
function process_wof_descendant(e) {
if (this.readyState == 4 && this.status == 200) {
// add JSON to body
this.wofJSON = this.responseText;
feature = JSON.parse(this.responseText);
// feature = this.responseText;
features.push(feature);
// var u = document.createTextNode(this.wofJSON);
// document.body.appendChild(u);
// descendantsJSON[descendantsProcessed] = xhr2.responseText;
// var u = document.createTextNode(descendantsJSON[j]);
// document.body.appendChild(u);
// add comma to the end, unless we're done in which we close the FeatureCollection
// if (descendantsProcessed <= descendantsCount) {
// str = response + ', ';
// var c = document.createTextNode(str);
// document.body.appendChild(c);
// } else {
// var v = document.createTextNode(featureCollectionEnd);
// document.body.appendChild(v);
// } //else
console.log('processed: ' + (1 + descendantsProcessed) + ' of ' + descendantsCount);
descendantsProcessed++;
} else {
// console.log('error:' + descendantsProcessed + ':' + xhr2.readyState + ':' + xhr2.status );
}
}
function makeWOFURL(wof_id) {
// parse the ID into groups of three to build the WOF url
var id = wof_id.toString();
var wof = [];
for (var i = 0; i < id.length; i = i + 3) {
var j = ((i + 3)/3) - 1;
wof[j] = id.slice(i, i + 3);
// console.log('wofj-' + j + ": " + wof[j] + " " + wof);
}
var wof_url_prefix = 'https://whosonfirst.mapzen.com/data/';
var wof_url_suffix = id + '.geojson';
var wof_url_middle = '';
var i = 0;
while (wof[i]) {
wof_url_middle += wof[i] + '/';
i++;
}
wof_url = wof_url_prefix + wof_url_middle + wof_url_suffix;
return wof_url;
// console.log(descendants['wof:name']);
}
function topojsonify() {
// holding spot for topojson conversion
}
function buildURI() {
// holding spot for saving locally
//Get the file contents
var txtFile = "test.txt";
var file = new File(txtFile);
var str = JSON.stringify(JsonExport);
//Save the file contents as a DataURI
var dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(str);
var topojson = document.createElement("p");
var t = document.createTextNode(str);
topojson.appendChild(t);
//Write it as the href for the link
var link = document.getElementById('link').href = dataUri;
//Then just give the link an ID and a default href
}
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<title>descender</title>
</head>
<body>
<script>
var wof_access_token = 'bfa3cbc6f49de8faf4d99e41c8d328f2';
// var wof_id = 85688637; // california
// var wof_id = 85632227; // tanzania
var wof_id = 85633041; // canada
// var wof_level = 'locality';
// var wof_level = 'county';
var wof_level = 'region';
var xhr = new XMLHttpRequest();
var url = 'https://whosonfirst.mapzen.com/api/rest/?method=whosonfirst.places.getDescendants&access_token=' + wof_access_token + '&id=' + wof_id +'&placetype=' + wof_level + '&page=1&per_page=200';
xhr.open('GET', url, true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
response = JSON.parse(xhr.responseText);
for (var i = 0; i < response.results.length; i++) {
var wof_id = response.results[i]['wof:id'];
console.log(response.results[i]['wof:name'] + ": " + wof_level);
var wof_url = [];
wof_url[i] = getWOFURL(wof_id);
console.log(wof_url[i]);
// console.log(places['wof:name']);
// alert(places);
// return places;
}
}
}
function getWOFURL(wof_id) {
var id = wof_id.toString();
var wof = [];
for (var i = 0; i < id.length; i = i + 3) {
var j = ((i + 3)/3) - 1;
wof[j] = id.slice(i, i + 3);
// console.log('wofj-' + j + ": " + wof[j] + " " + wof);
}
var wof_url_prefix = 'https://whosonfirst.mapzen.com/data/';
var wof_url_suffix = id + '.geojson';
var wof_url_middle = '';
var i = 0;
while (wof[i]) {
wof_url_middle += wof[i] + '/';
i++;
}
wof_url = wof_url_prefix + wof_url_middle + wof_url_suffix;
return wof_url;
// console.log(descendants['wof:name']);
}
function topojsonify() {
}
function buildURI() {
//Get the file contents
var txtFile = "test.txt";
var file = new File(txtFile);
var str = JSON.stringify(JsonExport);
//Save the file contents as a DataURI
var dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(str);
var topojson = document.createElement("p");
var t = document.createTextNode(str);
topojson.appendChild(t);
//Write it as the href for the link
var link = document.getElementById('link').href = dataUri;
//Then just give the link an ID and a default href
}
</script>
<a href="#" id="link" download="example.json">
Download as JSON
</a>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment