Skip to content

Commit

Permalink
Fixed test case for Polygon type; have only 4 more tests that need to…
Browse files Browse the repository at this point in the history
… pass
  • Loading branch information
arthur-e committed Jan 12, 2014
1 parent f1e4ee5 commit e71e956
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
38 changes: 19 additions & 19 deletions tests/wicket-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1109,100 +1109,100 @@ describe('GeoJSON Construction Cases:', function () {
point: {
str: 'POINT(30 10)',
json: {
'type': 'Point',
'coordinates': [30, 10]
'coordinates': [30, 10],
'type': 'Point'
}
},

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

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]]
]
[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]
],
'type': 'Polygon'
}
},

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]]
]
],
'type': 'Polygon'
}
},

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'
}
},

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'
}
},

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'
}
},

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]]
]
],
'type': 'MultiLineString'
}
},

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'
}
}

Expand Down
56 changes: 49 additions & 7 deletions wicket.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,22 @@ Wkt.Wkt.prototype.toString = function (config) {
* @method
*/
Wkt.Wkt.prototype.toJson = function () {
var json;
var cs, json, i, j, ring;

cs = this.components;
json = {
coordinates: [],
type: (function () {
var t, type, s;
var i, 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));
for (i in type) {
if (type.hasOwnProperty(i)) {
if (type[i] !== undefined) {
s.push(type[i].toLowerCase().slice(0,1).toUpperCase()
+ type[i].toLowerCase().slice(1));
}
}
}
Expand All @@ -252,6 +254,46 @@ Wkt.Wkt.prototype.toString = function (config) {
}.call(this)).join('')
}

// Wkt BOX type gets a special bbox property in GeoJSON
if (this.type.toLowerCase() === 'box') {
json.type = 'Polygon';
json.bbox = [];
for (i in cs) {
if (cs.hasOwnProperty(i)) {
json.bbox.concat([cs[i].x, cs[i].y]);
}
}
}

for (i in cs) {
if (cs.hasOwnProperty(i)) {

// For those nested structures
if (Wkt.isArray(cs[i])) {
ring = [];

for (j in cs[i]) {
if (cs[i].hasOwnProperty(j)) {

if (cs[i].length > 1) {
ring.push([cs[i][j].x, cs[i][j].y]);
}
}
}

json.coordinates.push(ring);

} else {
if (cs.length > 1) { // For LINESTRING type
json.coordinates.push([cs[i].x, cs[i].y]);
} else { // For POINT type
json.coordinates = json.coordinates.concat([cs[i].x, cs[i].y]);
}
}

}
}

return json;
};

Expand Down

0 comments on commit e71e956

Please sign in to comment.