Skip to content

Commit

Permalink
Added toJson() method on Wkt.Wkt prototypes which currently returns a…
Browse files Browse the repository at this point in the history
… JSON object with the type string
  • Loading branch information
arthur-e committed Jan 12, 2014
1 parent 6d46fda commit f1e4ee5
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 107 deletions.
208 changes: 104 additions & 104 deletions tests/wicket-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1104,154 +1104,154 @@ describe('Merged WKT Test Cases: ', function () {
});

describe('GeoJSON Construction Cases:', function () {
var cases = { // See: http://en.wikipedia.org/wiki/GeoJSON#Geometries
var cases = { // See: http://en.wikipedia.org/wiki/GeoJSON#Geometries

point: {
str: 'POINT(30 10)',
json: {
'type': 'Point',
'coordinates': [30, 10]
}
'type': 'Point',
'coordinates': [30, 10]
}
},

linestring: {
str: 'LINESTRING(30 10,10 30,40 40)',
json: {
'type': 'LineString',
'coordinates': [
[30, 10], [10, 30], [40, 40]
]
}
'type': 'LineString',
'coordinates': [
[30, 10], [10, 30], [40, 40]
]
}
},

polygon: {
str: 'POLYGON((30 10,10 20,20 40,40 40,30 10))',
json: {
'type': 'Polygon',
'coordinates': [
[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]
]
}
'type': 'Polygon',
'coordinates': [
[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]
]
}
},

polygon2: {
str: 'POLYGON((35 10,45 45,15 40,10 20,35 10),(20 30,35 35,30 20,20 30))',
json: {
'type': 'Polygon',
'coordinates': [
[[35, 10], [45, 45], [15, 40], [10, 20], [35, 10]],
[[20, 30], [35, 35], [30, 20], [20, 30]]
]
}
str: 'POLYGON((35 10,45 45,15 40,10 20,35 10),(20 30,35 35,30 20,20 30))',
json: {
'type': 'Polygon',
'coordinates': [
[[35, 10], [45, 45], [15, 40], [10, 20], [35, 10]],
[[20, 30], [35, 35], [30, 20], [20, 30]]
]
}
},

multipolygon: {
str: 'MULTIPOLYGON(((30 20,10 40,45 40,30 20)),((15 5,40 10,10 20,5 10,15 5)))',
json: {
'type': 'MultiPolygon',
'coordinates': [
[
[[30, 20], [45, 40], [10, 40], [30, 20]]
], [
[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]
]
]
}
'type': 'MultiPolygon',
'coordinates': [
[
[[30, 20], [45, 40], [10, 40], [30, 20]]
], [
[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]
]
]
}
},

multipolygon2: {
str: 'MULTIPOLYGON(((40 40,20 45,45 30,40 40)),((20 35,45 20,30 5,10 10,10 30,20 35),(30 20,20 25,20 15,30 20)))',
json: {
'type': 'MultiPolygon',
'coordinates': [
[
[[40, 40], [20, 45], [45, 30], [40, 40]]
], [
[[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]],
[[30, 20], [20, 15], [20, 25], [30, 20]]
]
]
}
'type': 'MultiPolygon',
'coordinates': [
[
[[40, 40], [20, 45], [45, 30], [40, 40]]
], [
[[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]],
[[30, 20], [20, 15], [20, 25], [30, 20]]
]
]
}
},

multipoint: {
str: 'MULTIPOINT((10 40),(40 30),(20 20),(30 10))',
json: {
'type': 'MultiPoint',
'coordinates': [
[10, 40], [40, 30], [20, 20], [30, 10]
]
}
'type': 'MultiPoint',
'coordinates': [
[10, 40], [40, 30], [20, 20], [30, 10]
]
}
},

multilinestring: {
str: 'MULTILINESTRING((10 10,20 20,10 40),(40 40,30 30,40 20,30 10))',
json: {
'type': 'MultiLineString',
'coordinates': [
[[10, 10], [20, 20], [10, 40]],
[[40, 40], [30, 30], [40, 20], [30, 10]]
]
}
json: {
'type': 'MultiLineString',
'coordinates': [
[[10, 10], [20, 20], [10, 40]],
[[40, 40], [30, 30], [40, 20], [30, 10]]
]
}
},

box: {
str: 'BOX(0 0,20 20)',
json: {
'type': 'Polygon',
'bbox': [0, 0, 20, 20],
'coordinates': [
[[0, 0], [0, 20], [20, 20], [20, 0], [0, 0]]
]
'type': 'Polygon',
'bbox': [0, 0, 20, 20],
'coordinates': [
[[0, 0], [0, 20], [20, 20], [20, 0], [0, 0]]
]
}
}

};
it('should create valid JSON for WKT Point type', function () {
var a = new Wkt.Wkt(cases.point.str);
expect(a.json()).toEqual(cases.point.json);
});

it('should create valid JSON for WKT LineString type', function () {
var a = new Wkt.Wkt(cases.linestring.str);
expect(a.json()).toEqual(cases.linestring.json);
});

it('should create valid JSON for WKT Polygon type', function () {
var a = new Wkt.Wkt(cases.polygon.str);
expect(a.json()).toEqual(cases.polygon.json);
});

it('should create valid JSON for WKT Polygon type with a hole', function () {
var a = new Wkt.Wkt(cases.polygon2.str);
expect(a.json()).toEqual(cases.polygon2.json);
});
it('should create valid JSON for WKT MultiPolygon type', function () {
var a = new Wkt.Wkt(cases.multipolygon.str);
expect(a.json()).toEqual(cases.multipolygon.json);
});
it('should create valid JSON for WKT MultiPolygon type with a hole', function () {
var a = new Wkt.Wkt(cases.multipolygon2.str);
expect(a.json()).toEqual(cases.multipolygon2.json);
});
it('should create valid JSON for WKT MultiPoint type', function () {
var a = new Wkt.Wkt(cases.multipoint.str);
expect(a.json()).toEqual(cases.multipoint.json);
});
it('should create valid JSON for WKT MultiLineString type', function () {
var a = new Wkt.Wkt(cases.multilinestring.str);
expect(a.json()).toEqual(cases.multilinestring.json);
});
it('should create valid JSON for WKT Box type', function () {
var a = new Wkt.Wkt(cases.box.str);
expect(a.json()).toEqual(cases.box.json);
});
};
it('should create valid JSON for WKT Point type', function () {
var a = new Wkt.Wkt(cases.point.str);
expect(a.toJson()).toEqual(cases.point.json);
});

it('should create valid JSON for WKT LineString type', function () {
var a = new Wkt.Wkt(cases.linestring.str);
expect(a.toJson()).toEqual(cases.linestring.json);
});

it('should create valid JSON for WKT Polygon type', function () {
var a = new Wkt.Wkt(cases.polygon.str);
expect(a.toJson()).toEqual(cases.polygon.json);
});

it('should create valid JSON for WKT Polygon type with a hole', function () {
var a = new Wkt.Wkt(cases.polygon2.str);
expect(a.toJson()).toEqual(cases.polygon2.json);
});
it('should create valid JSON for WKT MultiPolygon type', function () {
var a = new Wkt.Wkt(cases.multipolygon.str);
expect(a.toJson()).toEqual(cases.multipolygon.json);
});
it('should create valid JSON for WKT MultiPolygon type with a hole', function () {
var a = new Wkt.Wkt(cases.multipolygon2.str);
expect(a.toJson()).toEqual(cases.multipolygon2.json);
});
it('should create valid JSON for WKT MultiPoint type', function () {
var a = new Wkt.Wkt(cases.multipoint.str);
expect(a.toJson()).toEqual(cases.multipoint.json);
});
it('should create valid JSON for WKT MultiLineString type', function () {
var a = new Wkt.Wkt(cases.multilinestring.str);
expect(a.toJson()).toEqual(cases.multilinestring.json);
});
it('should create valid JSON for WKT Box type', function () {
var a = new Wkt.Wkt(cases.box.str);
expect(a.toJson()).toEqual(cases.box.json);
});

});

2 changes: 1 addition & 1 deletion tests/wicket.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>Jasmine Spec Runner v2.0.0-alpha</title>

<link rel="shortcut icon" type="image/png" href="/static/jasmine/dist/lib/jasmine-2.0.0/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="/static/jasmine/dist/lib/jasmine-2.0.0/jasmine.css">
<link rel="stylesheet" type="text/css" href="/static/jasmine/dist/lib/jasmine-2.0.0 /jasmine.css">

<!-- Get Jasmine: https://github.com/pivotal/jasmine -->
<script type="text/javascript" src="/static/jasmine/dist/lib/jasmine-2.0.0/jasmine.js"></script>
Expand Down
37 changes: 35 additions & 2 deletions wicket.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ var Wkt = (function () { // Execute function immediately
'parenComma': /\)\s*,\s*\(/,
'coord': /-*\d+\.*\d+ -*\d+\.*\d+/, // e.g. "24 -14"
'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/,
'trimParens': /^\s*\(?(.*?)\)?\s*$/
'trimParens': /^\s*\(?(.*?)\)?\s*$/,
'ogcTypes': /^(multi)?(point|line|polygon)?(string)?$/i // Captures e.g. "Multi","Line","String"
};

/**
Expand Down Expand Up @@ -222,6 +223,38 @@ Wkt.Wkt.prototype.toString = function (config) {
return this.write();
};

/**
* Creates a JSON representation, with the GeoJSON schema, of the geometry.
* @return {Object} The corresponding GeoJSON representation
* @memberof Wkt.Wkt
* @method
*/
Wkt.Wkt.prototype.toJson = function () {
var json;

json = {
type: (function () {
var t, type, s;

type = this.regExes.ogcTypes.exec(this.type).slice(1);
s = [];

for (t in type) {
if (type.hasOwnProperty(t)) {
if (type[t] !== undefined) {
s.push(type[t].toLowerCase().slice(0,1).toUpperCase()
+ type[t].toLowerCase().slice(1));
}
}
}

return s;
}.call(this)).join('')
}

return json;
};

/**
* Absorbs the geometry of another Wkt.Wkt instance, merging it with its own,
* creating a collection (MULTI-geometry) based on their types, which must agree.
Expand Down Expand Up @@ -351,7 +384,7 @@ Wkt.Wkt.prototype.extract = {
* @instance
*/
point: function (point) {
return String(point.x) + this.delimiter + String(point.y);
return String(point.x) + this.delimiter + String(point.y);
},

/**
Expand Down

0 comments on commit f1e4ee5

Please sign in to comment.