Convert WKT to JSON for ESRI v1.1
by mac1175
HTML
<table id='results'>
<tr>
<th>Type</th>
<th>Raw</th>
<th>JSON</th>
</tr>
</table>
CSS
th {
background-color: #333;
color: skyblue;
width: 120px;
}
table {
border-left: 1px solid #333;
border-top: 1px solid #333;
}
td {
border-right: 1px solid #333;
border-bottom: 1px solid #333;
}
JavaScript
function fromWKT2Json(WKTstr) {
var mods = {};
var convertToPointArray = function (ptArrayString) {
var points = [],
ptStringArray = ptArrayString.replace(/\)|\(/gi, "").split(",");
ptStringArray.forEach(function (pt) {
var splitpt = pt.trim().split(" "),
x = parseFloat(splitpt[0], 10),
y = parseFloat(splitpt[1], 10);
points[points.length] = [x, y];
});
return points;
};
mods.POINT = function (tailStr) {
///point should be in the following format:
// (xxxx yyyy)
console.log(tailStr);
var point = tailStr.replace(/\)|\(/gi, "").trim().split(" ");
return {
type: 'point',
x: parseFloat(point[0], 10),
y: parseFloat(point[1], 10)
};
};
mods.MULTILINESTRING = function (tailStr) {
//should be in the following format:
// MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
///strip outermost parenthesis
tailStr = tailStr.replace(/(\(\()|(\)\))/gi, '');
//split on tailing parenthesis and comma
var paths = [],
pathsRaw = tailStr.split("),"); ///show now have ['(x1 y1, x2 y2,....)','(x1 y1, x2 y2,....)',...]
pathsRaw.forEach(function (p) {
paths[paths.length] = convertToPointArray(p);
});
return {
type: 'polyline',
paths: paths
};
};
mods.POLYGON = function (tailStr) {
var ml = mods.MULTILINESTRING(tailStr);
///DIFFERENCES BETWEEN THIS AND MULTILINE IS THAT THE PATHS ARE RINGS
return {
type: 'polygon',
rings: ml.paths
};
};
mods.MULTIPOLYGON = function (tailStr) {
console.error('MULTIPOLYGON - not implemented');
};
mods.MULTIPOINT = function (tailStr) {
return {
type: 'multipoint',
points:...