Skip to content

Commit

Permalink
Update to v0.2-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Rein van 't Veer committed Nov 6, 2013
1 parent d515cfb commit fb082c4
Showing 1 changed file with 66 additions and 66 deletions.
132 changes: 66 additions & 66 deletions sparql-geojson.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
function wktToGeoJSON(wkt) {
//convert well-known text to GeoJSON
var geometryType;

//find substring left of first "(" occurrence
switch(wkt.substr(0, wkt.indexOf("("))){
case "POINT":
geometryType = "Point";
break;
case "MULTIPOINT":
geometryType = "MultiPoint";
break;
case "LINE":
geometryType = "Line";
break;
case "MULTILINE":
geometryType = "MultiLine";
break;
case "POLYGON":
geometryType = "Polygon";
break;
case "MULTIPOLYGON":
geometryType = "MultiPolygon";
break;
case "GEOMETRYCOLLECTION":
geometryType = "GeometryCollection";
break;
default:
//invalid wkt!
return {};
}
//chop off geometry type, already have that
var coordinates = wkt.substr(wkt.indexOf("("), wkt.length);
//add extra [ and replace ( by [
coordinates = "[" + coordinates.split("(").join("[");
//replace ) by ] and add extra ]
coordinates = coordinates.split(")").join("]") + "]";
//replace , by ],[
coordinates = coordinates.split(",").join("],[");
//replace spaces with ,
coordinates = coordinates.split(" ").join(",");
//SPARQL-GeoJSON v.0.2-alpha
function sparqlToGeoJSON(sparqlJSON) {
'use strict';
var bindingindex, varindex, geometryType, wkt, coordinates, property;
var geojson = {
"type": "FeatureCollection",
"features": []
};

return {
"type": "Feature",
"geometry": {
"type": geometryType,
"coordinates": eval('(' + coordinates + ')')
},
//todo: construct properties from sparql+json
"properties": {}
};
}
for (bindingindex = 0; bindingindex < sparqlJSON.results.bindings.length; ++bindingindex) {
for (varindex = 0; varindex < sparqlJSON.head.vars.length; ++varindex) {
if (sparqlJSON.results.bindings[bindingindex][sparqlJSON.head.vars[varindex]].datatype === "http://www.opengis.net/ont/geosparql#wktLiteral") {
//assumes the well-known text is valid!
wkt = sparqlJSON.results.bindings[bindingindex][sparqlJSON.head.vars[varindex]].value;

function sparqlToGeoJSON(sparqlJSON) {
var geojson = {
"type": "FeatureCollection",
"features": []
};

for (var bindingindex = 0; bindingindex < sparqlJSON.results.bindings.length; ++bindingindex) {
for (var varindex = 0; varindex < sparqlJSON.head.vars.length; ++varindex) {
if (sparqlJSON.results.bindings[bindingindex][sparqlJSON.head.vars[varindex]].datatype == "http://www.opengis.net/ont/geosparql#wktLiteral") {
//assumes the well-known text is valid!
geojson.features.push(wktToGeoJSON(sparqlJSON.results.bindings[bindingindex][sparqlJSON.head.vars[varindex]].value));
}
}
}
return geojson;
//find substring left of first "(" occurrence
switch (wkt.substr(0, wkt.indexOf("("))) {
case "POINT":
geometryType = "Point";
break;
case "MULTIPOINT":
geometryType = "MultiPoint";
break;
case "LINE":
geometryType = "Line";
break;
case "MULTILINE":
geometryType = "MultiLine";
break;
case "POLYGON":
geometryType = "Polygon";
break;
case "MULTIPOLYGON":
geometryType = "MultiPolygon";
break;
case "GEOMETRYCOLLECTION":
geometryType = "GeometryCollection";
break;
default:
//invalid wkt!
return {};
}

//chop off geometry type, already have that
coordinates = wkt.substr(wkt.indexOf("("), wkt.length);
//add extra [ and replace ( by [
coordinates = "[" + coordinates.split("(").join("[");
//replace ) by ] and add extra ]
coordinates = coordinates.split(")").join("]") + "]";
//replace , by ],[
coordinates = coordinates.split(",").join("],[");
//replace spaces with ,
coordinates = coordinates.split(" ").join(",");

var feature = {
"type": "Feature",
"geometry": {
"type": geometryType,
"coordinates": eval('(' + coordinates + ')')
},
"properties": sparqlJSON.results.bindings[bindingindex]
};

geojson.features.push(feature);
}
}
}
return geojson;
}

0 comments on commit fb082c4

Please sign in to comment.