Skip to content
Browse files

initial commit of a more shareable JS library for data access

  • Loading branch information...
1 parent b0e7112 commit a7f75101e5976513214c0868e2e83e1adf846893 @JoeGermuska JoeGermuska committed Aug 24, 2011
Showing with 202 additions and 1 deletion.
  1. +7 −1 tools/README.rst
  2. +20 −0 tools/js/example.html
  3. +175 −0 tools/js/ire_census.js
View
8 tools/README.rst
@@ -29,4 +29,10 @@ Metadata
========
These files provide machine-readable distillations of table and field codes and names. The label codes should match with column codes in the SQL script above.
-All files in this directory are UTF-8 encoded.
+All files in this directory are UTF-8 encoded.
+
+JS (JavaScript)
+===============
+A first stab at encapsulating basic operations in JavaScript to make it easier for more people to use this. In need of
+more examples and documentation, but should be somewhat straightforward for people accustomed to doing AJAX with jQuery.
+Depends upon jQuery for ajax boilerplate. Please feel free to help make this more awesome.
View
20 tools/js/example.html
@@ -0,0 +1,20 @@
+<html>
+ <head>
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
+ <script type="text/javascript" src="ire_census.js"></script>
+ </head>
+ <body>
+ <input id="geoid-input" type="text" /> <button id="pop_lookup" type="button">Lookup population for GeoID.</button>
+
+ <script type="text/javascript" charset="utf-8">
+ function alert_handler(sf1) {
+ alert("The 2010 population of "+ sf1['metadata']['NAME'] + " was " + sf1['data']['2010']['P1']['P001001'] + ".");
+ }
+ $(document).ready(
+ $("#pop_lookup").click(function() {
+ ire_census.do_with_sf1_data($("#geoid-input").val(),alert_handler);
+ })
+ );
+ </script>
+ </body>
+</html>
View
175 tools/js/ire_census.js
@@ -0,0 +1,175 @@
+// Dependencies:
+// jQuery
+var ire_census = {};
+(function() {
+ this.API_URL = "http://censusdata.ire.org"
+ this.GEOAPI_URL = "http://census.ire.org/geo/1.0"
+
+ var table_comparator = function (table) {
+ parts = table.match(/([A-Z]+)(\d+)([A-Z]+)?/);
+
+ key = parts[1];
+ num = parts[2];
+
+ if (key[0] == 'P') {
+ key = "A" + key; // sort P tables first as Census does
+ }
+ while (num.length < 3) {
+ num = '0' + num;
+ }
+
+ key += num;
+
+ if (!_.isUndefined(parts[3])) {
+ key += parts[3];
+ }
+
+ return key;
+ }
+
+ function apiRequest(path, callback, handler) {
+ $.ajax(this.API_URL + path, {
+ dataType: "jsonp",
+ jsonpCallback: callback,
+ success: handler
+ });
+ }
+
+ this.build_bulk_data_url = function(state,sumlev,table,format) {
+ if (!format) {
+ format = 'csv';
+ }
+ if (table) {
+ table = "." + table;
+ } else {
+ table = "";
+ }
+ return this.API_URL + "/" + state + "/all_" + sumlev + "_in_"+state+table+"." + format;
+ }
+
+ this.build_shapefile_url = function(state,sumlev) {
+ for (var i = 0; i < SUMLEVS.length; i++) {
+ if (SUMLEVS[i][0] == sumlev) {
+ return SUMLEVS[i][2]({'state_fips': state});
+ }
+ }
+ throw "Invalid summary level";
+ }
+
+ this.do_with_available_states = function(handler) {
+ apiRequest("/states.jsonp", "states", handler);
+ }
+
+ this.do_with_labels = function(handler) {
+ apiRequest("/" + window.DATASET + "_labels.jsonp", "labels_" + window.DATASET, handler);
+ }
+
+ this.do_with_sf1_data = function(geoid, callback) {
+ var state = geoid.substring(0, 2);
+ apiRequest("/" + state + "/" + geoid + ".jsonp", "geoid_" + geoid, callback);
+ }
+
+ GEOGRAPHY_TYPES = [
+ [/^\d{11}$/,'tracts'],
+ [/^\d{10}$/,'county-subdivisions'],
+ [/^\d{5}$/,'counties'],
+ [/^\d{2}$/,'states'],
+ [/^\d{7}$/,'places']
+ ];
+ // For now, if geoid begins with a slash, it will be treated as a complete type/geoid combo. This
+ // leaves it fairly flexible to add more geographies in case sometimes lengths aren't distinctive.
+ // But also for now, we can infer the geography type by the length of the geoID, so the value for
+ // geoid may also simply be a string of 2-11 digits, or technically any pattern defined in GEOGRAPHY_TYPES.
+ //
+ // See http://census.ire.org/docs/boundary.html for more on the API, or to see what data
+ // gets returned, look at http://census.ire.org/geo/1.0/boundary-set/places/1714000
+ // TK: examples demonstrating how to put the shape which is returned on a map.
+ this.get_geojson = function (geoid,success_handler) {
+ if (geoid[0] != '/') {
+ for (var i=0; i < GEOGRAPHY_TYPES.length; i++) {
+ if (geoid.match(GEOGRAPHY_TYPES[i][0])) {
+ geoid = "/" + GEOGRAPHY_TYPES[i][1] + "/" + geoid;
+ break
+ }
+ }
+ }
+ if (geoid.substr(0,'/boundary-set'.length) != '/boundary-set') {
+ geoid = '/boundary-set' + geoid;
+ }
+ $.ajax(this.GEOAPI_URL + geoid, {
+ dataType: "jsonp",
+ success: success_handler
+ });
+ }
+
+ this.STATE_FIPS = {
+ "Alabama": "01",
+ "Alaska": "02",
+ "Arizona": "04",
+ "Arkansas": "05",
+ "California": "06",
+ "Colorado": "08",
+ "Connecticut": "09",
+ "Delaware": "10",
+ "District of Columbia": "11",
+ "Florida": "12",
+ "Georgia": "13",
+ "Hawaii": "15",
+ "Idaho": "16",
+ "Illinois": "17",
+ "Indiana": "18",
+ "Iowa": "19",
+ "Kansas": "20",
+ "Kentucky": "21",
+ "Louisiana": "22",
+ "Maine": "23",
+ "Maryland": "24",
+ "Massachusetts": "25",
+ "Michigan": "26",
+ "Minnesota": "27",
+ "Mississippi": "28",
+ "Missouri": "29",
+ "Montana": "30",
+ "Nebraska": "31",
+ "Nevada": "32",
+ "New Hampshire": "33",
+ "New Jersey": "34",
+ "New Mexico": "35",
+ "New York": "36",
+ "North Carolina": "37",
+ "North Dakota": "38",
+ "Ohio": "39",
+ "Oklahoma": "40",
+ "Oregon": "41",
+ "Pennsylvania": "42",
+ "Puerto Rico": "72",
+ "Rhode Island": "44",
+ "South Carolina": "45",
+ "South Dakota": "46",
+ "Tennessee": "47",
+ "Texas": "48",
+ "Utah": "49",
+ "Vermont": "50",
+ "Virginia": "51",
+ "Washington": "53",
+ "West Virginia": "54",
+ "Wisconsin": "55",
+ "Wyoming": "56"
+ }
+
+ this.get_state_fips = function(state) {
+ return this.STATE_FIPS[state];
+ }
+
+ this.FIPS_STATE = {}
+
+ for (name in this.STATE_FIPS) {
+ var fips = this.get_state_fips(name);
+ this.FIPS_STATE[fips] = name;
+ }
+
+ this.get_fips_state = function(fips) {
+ return this.FIPS_STATE[fips];
+ }
+
+}).apply(ire_census);

0 comments on commit a7f7510

Please sign in to comment.
Something went wrong with that request. Please try again.