Skip to content

Instantly share code, notes, and snippets.

@cjwinchester
Created July 20, 2015 03:14
Show Gist options
  • Save cjwinchester/81dc655717da46bccd71 to your computer and use it in GitHub Desktop.
Save cjwinchester/81dc655717da46bccd71 to your computer and use it in GitHub Desktop.
gis depository thinger
import os
import json
import re
import collections
def sub(text):
return re.sub("ia-|ne-|us-|-topojson|-geojson|.json|.zip", "", text)
def getType(x):
if "topojson" in x:
return "topojson"
elif "geojson" in x:
return "geojson"
elif "zip" in x:
return "shapefile"
ls = []
os.chdir("g")
root = os.getcwd()
dirs = os.listdir(root)
for d in dirs:
obj = {}
obj['geo'] = d
obj['data'] = []
fs = os.listdir(d)
os.chdir(d)
uniques = [x for x, y in collections.Counter([sub(v) for v in fs]).items() if y > 1]
for thing in uniques:
dude = {}
dude['name'] = thing
cowbell = []
for file in fs:
if thing in file:
cowbell.append({"filename": file, "filetype": getType(file), "filesize": "{:,}".format(os.stat(file).st_size / 1000) + " KB"})
dude['sets'] = cowbell
obj['data'].append(dude)
ls.append(obj)
os.chdir(root)
os.chdir("../js")
f = open("gis.json", "wb")
f.write(json.dumps(ls))
f.close()
<html>
<div id="out"></div>
<script type="text/html" class="template">
<% _.each( crunk, function( thing ){ %>
<h1><%= lookup[thing.geo]["verbose"] %></h1>
<% _.each( thing.data, function( womp ){ %>
<h3><%= lookup[womp.name] %><% if ( womp.date ) { %><%= womp.date %><% }; %></h3>
<% _.each( _.sortBy(womp.sets, function(d) {return lookup[d["filetype"]]["order"]}) , function( giraffe ){ %>
<li><span class="label label-<%= lookup[giraffe.filetype]["label"] %>"><a href="/static/gis/<%= thing.geo %>/<%= giraffe.filename %>"><%= giraffe.filetype %> (<%= giraffe.filesize %>)</a></span></li>
<% }); %>
<% }); %>
<% }); %>
</script>
<script src="jquery.min.js"></script>
<script src="underscore-min.js"></script>
<script src="js/main.js"></script>
</html>
var lookup = {
"us": {"order": 0, "verbose": "U.S."},
"ne": {"order": 1, "verbose": "Nebraska"},
"ia": {"order": 2, "verbose": "Iowa"},
"shapefile": {"label": "danger", "order": 0},
"geojson": {"label": "primary", "order": 1},
"topojson": {"label": "info", "order": 2},
"counties": "Counties",
"leg-dists": "Legislative districts",
"school-dists": "School districts",
"reservations": "Tribal land"
};
$.getJSON('js/gis.json').success(function(d) {
var sorted = _.sortBy(d, function(z) {
return lookup[z["geo"]]["order"]
});
_.templateSettings.variable = "crunk";
var template = _.template($( "script.template" ).html());
$('#out').html(template(sorted));
}).then(function() {
console.log("Success!");
}).fail(function() {
alert("Something went wrong.");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment