-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |