import "/std/ns.rql"; var nsbase = "http://www.census.gov/tiger/2002/"; var TIGER_DIR = "c:/tiger/files/"; var TIGER_FEAT_DIRECTION = nsbase + "featid/direction/"; var TIGER_FEAT_TYPE = nsbase + "featid/type/"; var TIGER_STATE_BASE = nsbase + "fips/"; var TIGER_SCHOOLDISTRICT_BASE = nsbase + "school_district/"; var TIGER_FIPSCC_BASE = nsbase + "fips/class_code/"; var TIGER_PLACEDC_BASE = nsbase + "fips/place_description/"; var TIGER_LSADC_BASE = nsbase + "fips/area_description/"; session.namespaces["tiger"] = nsbase + "vocab#"; session.namespaces["fipscc"] = TIGER_FIPSCC_BASE ; session.namespaces["fipspd"] = TIGER_PLACEDC_BASE ; session.namespaces["fipsad"] = TIGER_LSADC_BASE ; //classes var TIGER_LINE = resource("tiger:Line"); var TIGER_POINT = resource("tiger:Point"); var TIGER_POLYGON = resource("tiger:Polygon"); var TIGER_LANDMARK = resource("tiger:Landmark"); //props var TIGER_ATLEFT = resource("tiger:atLeft"); var TIGER_ATRIGHT = resource("tiger:atRight"); var TIGER_AREA = resource("tiger:area"); var TIGER_PATH = resource("tiger:path"); var TIGER_LOCATION = resource("tiger:location"); var TIGER_LAT = resource("tiger:lat"); var TIGER_LONG = resource("tiger:long"); var TIGER_IDENTIFIER = resource("tiger:identifier"); var TIGER_DIRECTIONPREFIX = resource("tiger:directionPrefix"); var TIGER_DIRECTIONSUFFIX = resource("tiger:directionSuffix"); var TIGER_NAME = resource("tiger:name"); var TIGER_START = resource("tiger:start"); var TIGER_END = resource("tiger:end"); var RDF_TYPE = resource("rdf:type"); drop table geo; create table geo context none (NOTRANS); var ds = new datasource("inet?parsetype=rdf&url=c:/tiger/tiger_features.rdf"); load {?p ?s ?o} into geo using #ds; ds = new datasource("inet?parsetype=rdf&url=c:/tiger/tiger_fipscc.rdf"); load {?p ?s ?o} into geo using #ds; ds = new datasource("inet?parsetype=rdf&url=c:/tiger/tiger_fipspd.rdf"); load {?p ?s ?o} into geo using #ds; ds = new datasource("inet?parsetype=rdf&url=c:/tiger/tiger_fipsad.rdf"); load {?p ?s ?o} into geo using #ds; ds = new datasource("inet?parsetype=rdf&url=c:/tiger/tiger_vocab.rdf"); load {?p ?s ?o} into geo using #ds; importCounty("tgr50001"); importCounty("tgr50003"); importCounty("tgr50005"); importCounty("tgr50007"); importCounty("tgr50009"); importCounty("tgr50011"); /* importCounty("tgr50013"); importCounty("tgr50015"); importCounty("tgr50017"); importCounty("tgr50019"); importCounty("tgr50021"); importCounty("tgr50023"); importCounty("tgr50025"); importCounty("tgr50027"); */ /* insert {[tiger:intersectsArea] ?x ?y} into geo using geo where {[tiger:area] ?x ?p} and {[tiger:area] ?y ?p} insert {[tiger:boundary] ?x ?l} into geo using geo where {[tiger:area] ?x ?p} and ( ({[tiger:atLeft] ?l ?p} and not ({[tiger:atRight] ?l ?p2} and {[tiger:area] ?x ?p2})) or ({[tiger:atRight] ?l ?p} and not ({[tiger:atLeft] ?l ?p2} and {[tiger:area] ?x ?p2})) ); */ function importCounty(county) { response.write("Import county: " + county); var dst = new datasource(); //load feature identifiers // importFile(dst, TIGER_DIR + county + ".rt5"); //load lines importFile(dst, TIGER_DIR + county + ".rt1"); //load landmarks importFile(dst, TIGER_DIR + county + ".rt7"); //landmark areas (polygons) importFile(dst, TIGER_DIR + county + ".rt8"); //get geo polygons and relationships to legal entities importFile(dst, TIGER_DIR + county + ".rta"); //get geo entity names importFile(dst, TIGER_DIR + county + ".rtc"); //get lines to polygons relationships importFile(dst, TIGER_DIR + county + ".rti"); (insert {?p ?s ?o} into geo using #dst where {?p ?s ?o}); } function importFile(dst, file) { response.write("importing file: " + file); var ds = new datasource("inet?parsetype=line&url=" + file); for (var rs = (select ?p ?s ?o using #ds where {?p ?s ?o}); !rs.EOF; rs.MoveNext()) { if (typeof(rs[2]) != "string") continue; importLine(rs[2], dst); } } function importLine(ln, ds) { switch(getField(ln, "A", 1, 1)) { case "1": importRT1(ln, ds); break; case "5": importRT5(ln, ds); break; case "7": importRT7(ln, ds); break; case "8": importRT8(ln, ds); break; case "A": importRTA(ln, ds); break; case "C": importRTC(ln, ds); break; case "I": importRTI(ln, ds); break; } } function importRTC(ln, ds) { //associate a polygon with legal entities - skip statistical entities for now var entity = getField(ln,"A", 25, 25); var st = getField(ln,"N", 6, 7); var co = getField(ln,"N", 8, 10); var fips55 = getField(ln,"N", 15, 19); var fipscc = getField(ln,"A", 20, 21); var placedc = getField(ln,"A", 22, 22); var lsadc = getField(ln, "A", 23, 24); var sd = getField(ln, "N", 30, 34); var name = getField(ln, "A", 63, 122); switch (entity) { case "S": // state var res = resource(TIGER_STATE_BASE + st); ds.Add(RDF_TYPE, res, resource(TIGER_LSADC_BASE + lsadc)); ds.Add(TIGER_NAME, res, name); break; case "C": // county var res = resource(TIGER_STATE_BASE + st + "/" + co); ds.Add(RDF_TYPE, res, resource(TIGER_FIPSCC_BASE + fipscc)); ds.Add(RDF_TYPE, res, resource(TIGER_LSADC_BASE + lsadc)); ds.Add(TIGER_NAME, res, name); break; case "A": // consolidated city case "G": // new consolidated city case "M": // county subdivision var res = resource(TIGER_STATE_BASE + st + "/" + fips55); ds.Add(RDF_TYPE, res, resource(TIGER_FIPSCC_BASE + fipscc)); ds.Add(RDF_TYPE, res, resource(TIGER_PLACEDC_BASE + placedc)); ds.Add(RDF_TYPE, res, resource(TIGER_LSADC_BASE + lsadc)); ds.Add(TIGER_NAME, res, name); break; case "3": // unified school district case "4": // secondary school district case "5": // elementary school district var res = resource(TIGER_SCHOOLDISTRICT_BASE + sd); ds.Add(RDF_TYPE, res, resource(TIGER_LSADC_BASE + lsadc)); ds.Add(TIGER_NAME, res, name); break; } } function importRTA(ln, ds) { //associate a polygon with legal entities var cenid = getField(ln,"A", 11, 15); var polyid = getField(ln,"N", 16, 25); var resPoly = resource(nsbase + "polygon/" + cenid + "/" + polyid); var st = getField(ln,"N", 26, 27); var co = getField(ln,"N", 28, 30); var subco = getField(ln,"N", 71, 75); var concity = getField(ln,"N", 66, 70); var inccity = getField(ln,"N", 81, 85); var elemsch = getField(ln, "A", 86, 90); var secsch = getField(ln, "A", 91, 95); var unisch = getField(ln, "A", 96, 100); var subbar = getField(ln, "N", 76, 80); ds.Add(RDF_TYPE, resPoly, TIGER_POLYGON); ds.Add(TIGER_AREA, resource(TIGER_STATE_BASE + st), resPoly); ds.Add(TIGER_AREA, resource(TIGER_STATE_BASE + st + "/" + co), resPoly); ds.Add(TIGER_AREA, resource(TIGER_STATE_BASE + st + "/" + subco), resPoly); if (concity != null) ds.Add(TIGER_AREA, resource(TIGER_STATE_BASE + st + "/" + concity), resPoly); if (inccity != null) ds.Add(TIGER_AREA, resource(TIGER_STATE_BASE + st + "/" + inccity), resPoly); if (subbar != null) ds.Add(TIGER_AREA, resource(TIGER_STATE_BASE + st + "/" + subbar), resPoly); if (elemsch != null) ds.Add(TIGER_AREA, resource(TIGER_SCHOOLDISTRICT_BASE + elemsch), resPoly); if (secsch != null) ds.Add(TIGER_AREA, resource(TIGER_SCHOOLDISTRICT_BASE + secsch), resPoly); if (unisch != null) ds.Add(TIGER_AREA, resource(TIGER_SCHOOLDISTRICT_BASE + unisch), resPoly); } function importRTI(ln, ds) { //associate a polygon with the lines that make it up var resLine = resource(nsbase + "tlid/" + getField(ln,"N", 11, 20)); var cenid = getField(ln,"A", 41, 45); var polyid = getField(ln,"N", 46, 55); if (cenid != null && polyid != null) { var resPoly = resource(nsbase + "polygon/" + cenid + "/" + polyid); ds.Add(TIGER_ATLEFT, resLine, resPoly); } cenid = getField(ln,"A", 56, 60); polyid = getField(ln,"N", 61, 70); if (cenid != null && polyid != null) { var resPoly = resource(nsbase + "polygon/" + cenid + "/" + polyid); ds.Add(TIGER_ATRIGHT, resLine, resPoly); } } function importRT8(ln, ds) { //associate area landmarks with polygons var file = getField(ln,"N", 6, 10); var land = getField(ln,"N", 26, 35); var cenid = getField(ln,"A", 11, 15); var polyid = getField(ln,"N", 16, 25); var resLand = resource(nsbase + "landmark/" + file + "/" + land); var resPoly = resource(nsbase + "polygon/" + cenid + "/" + polyid); ds.Add(TIGER_AREA, resLand, resPoly); } function importRT7(ln, ds) { //import landmarks, get their location if present var file = getField(ln,"N", 6, 10); var land = getField(ln,"N", 11, 20); var res = resource(nsbase + "landmark/" + file + "/" + land); insertPropMakeUri(ds, ln, RDF_TYPE, res, "A", 22, 24, nsbase + "CFCC/"); insertProp(ds, ln, TIGER_NAME, res, "A", 25, 54); ds.Add(RDF_TYPE, res, TIGER_LANDMARK); var lat = getField(ln,"N", 65, 73); var long = getField(ln,"N", 55, 64); if (lat != null && long != null) { var resPoint = new resource(); ds.Add(TIGER_LAT, resPoint, lat); ds.Add(TIGER_LONG, resPoint, long); ds.Add(TIGER_LOCATION, res, resPoint); } } /* function importRT5(ln, ds) { //import feature identifier var file = getField(ln,"N", 6, 10); var feat = getField(ln,"N", 11, 18); var res = resource(nsbase + "featid/" + file + "/" + feat); insertPropMakeUri(ds, ln, TIGER_DIRECTIONPREFIX, res, "A", 19, 20, TIGER_FEAT_DIRECTION); insertProp(ds, ln, TIGER_NAME, res, "A", 21, 50); insertPropMakeUri(ds, ln, RDF_TYPE, res, "A", 51, 54, TIGER_FEAT_TYPE); insertPropMakeUri(ds, ln, TIGER_DIRECTIONSUFFIX, res, "A", 55, 56, TIGER_FEAT_DIRECTION); } */ function importRT1(l, ds) { var res = new Resource(nsbase + "tlid/" + getField(l,"N", 6, 15)); // the model: // a line has start and end points // a feature has the line as it's path // a feature has a feature identifier and a type //lat, lon, cfcc can't be blank in rt1 file var resStart = new Resource(); var frlong = getField(l,"N", 191, 200); var frlat = getField(l,"N", 201, 209); var resEnd = new Resource(); var tolong = getField(l,"N", 210, 219); var tolat = getField(l,"N", 220, 228); var cfcc = getField(l,"A", 56, 58); ds.Add(RDF_TYPE, res, TIGER_LINE); ds.Add(TIGER_START, res, resStart); ds.Add(TIGER_END, res, resEnd); ds.Add(TIGER_LAT, resStart, frlat); ds.Add(TIGER_LONG, resStart, frlong); ds.Add(TIGER_LAT, resEnd, tolat); ds.Add(TIGER_LONG, resEnd, tolong); // get the feature identifier var prefix = getField(l,"A", 18, 19); var name = getField(l,"A", 20, 49); var type = getField(l,"A", 50, 53); var suffix = getField(l,"A", 54, 55); var resFeature; if (prefix == null && name == null && type == null && suffix == null) resFeature = new Resource(); else { resFeature = newid(prefix + name + type + suffix); if (prefix != null) ds.Add(TIGER_DIRECTIONPREFIX, resFeature, resource(TIGER_FEAT_DIRECTION + prefix)); if (name!= null) ds.Add(TIGER_NAME, resFeature, name); if (type != null) ds.Add(RDF_TYPE, resFeature, resource(TIGER_FEAT_DIRECTION + type)); if (suffix != null) ds.Add(TIGER_DIRECTIONSUFFIX, resFeature, resource(TIGER_FEAT_DIRECTION + suffix)); } ds.Add(TIGER_PATH, resFeature, res); ds.Add(RDF_TYPE, resFeature, resource(nsbase + "CFCC/" + cfcc)); } function insertProp(ds, ln, p, s, type, _beg, _end) { var val = getField(ln, type, _beg, _end); if (val != null && val != "") ds.Add(p, s, val); } function insertPropMakeUri(ds, ln, p, s, type, _beg, _end, uribase) { var val = getField(ln, type, _beg, _end); if (val != null && val != "") { ds.Add(p, s, resource(uribase + val)); return val; } return null; } function getField(l, type, _beg, _end) { var val = trim(l.substring(_beg - 1, _end)); if (type == "N") val = Integer(val); return val==""?null:val; } function escapeLiteral(s) { var re = new RegExp("([\'\"])","g"); return string(s).replace(re, "\\$1"); }