Skip to content

Commit

Permalink
fix DG.Wkt.toLatLngs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Trufi committed May 28, 2015
1 parent 66d6942 commit a8fbe86
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 55 deletions.
35 changes: 25 additions & 10 deletions src/DGWkt/DGWkt.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,32 @@ DG.Wkt.geoJsonLayer = function (data, opts) {
return DG.geoJson(DG.Wkt.toGeoJSON(data), opts);
};

DG.Wkt._coordsToLatLngs = function (coords) {
if (DG.Util.isArray(coords) && !DG.Util.isArray(coords[0])) {
return [DG.GeoJSON.coordsToLatLng(coords)];
}


return coords.map(function (el) {
return DG.Wkt._coordsToLatLngs(el);
})
.reduce(function (arr, coord) {
return arr.concat(coord);
});
};

DG.Wkt.toLatLngs = function (data) {
var coords = DG.Wkt.toGeoJSON(data).coordinates;
return DG.Util.isArray(coords) ?
coords
.map(function (coord) {
return DG.Util.isArray(coord[0]) ? DG.GeoJSON.coordsToLatLngs(coord) : [DG.GeoJSON.coordsToLatLng(coord)];
})
.reduce(function (arr, coord) {
return arr.concat(coord);
}) :
DG.GeoJSON.coordsToLatLngs(coords);
if (!DG.Util.isArray(data)) {
data = [data];
}

return data.map(function (el) {
var coords = DG.Wkt.toGeoJSON(el).coordinates;

return DG.Wkt._coordsToLatLngs(coords);
}).reduce(function (arr, coord) {
return arr.concat(coord);
});
};

DG.Wkt.toPoints = function (data) {
Expand Down
47 changes: 2 additions & 45 deletions src/DGWkt/test/DGWktSpec.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,14 @@
describe('DG.Wkt', function() {
var wktExamples = [
'POINT(82.918299581655 55.0414363815388)',
'POLYGON((82.916999581655 55.0421363815388, 82.9175228440822 55.0401876099625, 82.9180633669606 55.0402351774059, 82.9175402952564 55.0421841282563,82.916999581655 55.0421363815388))',
'LINESTRING (300 100, 100 300, 400 400)',
'MULTIPOINT (1000 400, 1400 300, 1200 200, 1300 100)',
'MULTILINESTRING ((600 100, 700 200, 600 400), (900 400, 800 300, 900 200, 800 100))',
'MULTIPOLYGON (((1300 200, 1450 400, 1100 400, 1300 200)), ((1150 50, 1400 100, 1100 200, 1150 100, 1150 50)))',
'GEOMETRYCOLLECTION(MULTIPOINT(100 60, 70 300), LINESTRING(100 60, 70 100, 70 300))'
];

after(function() {
wktExamples = null;
});

describe('check init', function() {
it('should be DG.Wkt object', function() {
expect(DG.Wkt).to.be.a('object');
});
});

describe('#toGeoJSON', function() {
wktExamples.forEach(function(el) {
it(el.slice(0, el.indexOf('(')), function() {
var val = DG.Wkt.toGeoJSON(el);

// throw Error if not be ok
DG.GeoJSON.geometryToLayer(val);
});
});
});

describe('#geoJsonLayer', function() {
it('should return DG.GeoJSON', function() {
expect(DG.Wkt.geoJsonLayer(wktExamples[1])).to.be.a(DG.GeoJSON);
});
});

describe('#toLatLngs', function() {
it('should equel example latLngs', function() {
var latlngs = [
DG.latLng(55.0421363815388, 82.916999581655),
DG.latLng(55.0401876099625, 82.9175228440822),
DG.latLng(55.0402351774059, 82.9180633669606)
],
example;

example = 'POLYGON((';
example += latlngs.map(function(el) {
return el.lng + ' ' + el.lat;
}).join(', ');
example += '))';

expect(DG.Wkt.toLatLngs(example)).to.eql(latlngs);
expect(DG.Wkt.geoJsonLayer('POLYGON((82.916999581655 55.0421363815388, 82.9175228440822 55.0401876099625, 82.9180633669606 55.0402351774059, 82.9175402952564 55.0421841282563,82.916999581655 55.0421363815388))'))
.to.be.a(DG.GeoJSON);
});
});

Expand Down
157 changes: 157 additions & 0 deletions src/DGWkt/test/DGWktToGeoJSONSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
describe('DG.Wkt#toLatlngs', function() {
// test cases took from https://github.com/mapbox/wellknown

it('POINT (1 1)', function() {
expect(DG.Wkt.toGeoJSON('POINT (1 1)')).to.be.eql({
type: 'Point',
coordinates: [1, 1]
});
});

it('POINT(1 1)', function() {
expect(DG.Wkt.toGeoJSON('POINT(1 1)')).to.be.eql({
type: 'Point',
coordinates: [1, 1]
});
});

it('POINT\n\r(1 1))', function() {
expect(DG.Wkt.toGeoJSON('POINT\n\r(1 1)')).to.be.eql({
type: 'Point',
coordinates: [1, 1]
});
});

it('POINT(1.1 1.1)', function() {
expect(DG.Wkt.toGeoJSON('POINT(1.1 1.1)')).to.be.eql({
type: 'Point',
coordinates: [1.1, 1.1]
});
});

it('point(1.1 1.1)', function() {
expect(DG.Wkt.toGeoJSON('point(1.1 1.1)')).to.be.eql({
type: 'Point',
coordinates: [1.1, 1.1]
});
});

it('point(1 2 3)', function() {
expect(DG.Wkt.toGeoJSON('point(1 2 3)')).to.be.eql({
type: 'Point',
coordinates: [1, 2, 3]
});
});

it('LINESTRING (30 10, 10 30, 40 40)', function() {
expect(DG.Wkt.toGeoJSON('LINESTRING (30 10, 10 30, 40 40)')).to.be.eql({
type: 'LineString',
coordinates: [[30, 10], [10, 30], [40, 40]]
});
});

it('LINESTRING(30 10, 10 30, 40 40)', function() {
expect(DG.Wkt.toGeoJSON('LINESTRING(30 10, 10 30, 40 40)')).to.be.eql({
type: 'LineString',
coordinates: [[30, 10], [10, 30], [40, 40]]
});
});

it('LineString(30 10, 10 30, 40 40)', function() {
expect(DG.Wkt.toGeoJSON('LineString(30 10, 10 30, 40 40)')).to.be.eql({
type: 'LineString',
coordinates: [[30, 10], [10, 30], [40, 40]]
});
});

it('LINESTRING (1 2 3, 4 5 6)', function() {
expect(DG.Wkt.toGeoJSON('LINESTRING (1 2 3, 4 5 6)')).to.be.eql({
type: 'LineString',
coordinates: [[1, 2, 3], [4, 5, 6]]
});
});

it('POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))', function() {
expect(DG.Wkt.toGeoJSON('POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))')).to.be.eql({
type: 'Polygon',
coordinates: [[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]]
});
});

it('POLYGON((30 10, 10 20, 20 40, 40 40, 30 10))', function() {
expect(DG.Wkt.toGeoJSON('POLYGON((30 10, 10 20, 20 40, 40 40, 30 10))')).to.be.eql({
type: 'Polygon',
coordinates: [[[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]]
});
});

it('POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))', function() {
expect(DG.Wkt.toGeoJSON('POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30))')).to.be.eql({
type: 'Polygon',
coordinates:
[
[
[ 35, 10 ],
[ 10, 20 ],
[ 15, 40 ],
[ 45, 45 ],
[ 35, 10 ]
], [
[ 20, 30 ],
[ 35, 35 ],
[ 30, 20 ],
[ 20, 30 ]
]
]
});
});

it('MULTIPOINT (1 1, 2 3)', function() {
expect(DG.Wkt.toGeoJSON('MULTIPOINT (1 1, 2 3)')).to.be.eql({
type: 'MultiPoint',
coordinates: [[1, 1], [2, 3]]
});
});

it('MultiPoint (1 1, 2 3)', function() {
expect(DG.Wkt.toGeoJSON('MultiPoint (1 1, 2 3)')).to.be.eql({
type: 'MultiPoint',
coordinates: [[1, 1], [2, 3]]
});
});

it('MULTILINESTRING ((30 10, 10 30, 40 40), (30 10, 10 30, 40 40))', function() {
expect(DG.Wkt.toGeoJSON('MULTILINESTRING ((30 10, 10 30, 40 40), (30 10, 10 30, 40 40))')).to.be.eql({
type: 'MultiLineString',
coordinates: [
[[30, 10], [10, 30], [40, 40]],
[[30, 10], [10, 30], [40, 40]]]
});
});

it('MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))', function() {
expect(DG.Wkt.toGeoJSON('MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))')).to.be.eql({
type: 'MultiPolygon',
coordinates: [
[[[30, 20], [10, 40], [45, 40], [30, 20]]],
[[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]]]
});
});

it('MULTIPOLYGON (((-74.03349399999999 40.688348)))', function() {
expect(DG.Wkt.toGeoJSON('MULTIPOLYGON (((-74.03349399999999 40.688348)))')).to.be.eql(
{"type":"MultiPolygon","coordinates":[[[[-74.03349399999999,40.688348]]]]}
);
});

it('MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5), (10 10, 15 10, 15 15, 10 10)))', function() {
expect(DG.Wkt.toGeoJSON('MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5), (10 10, 15 10, 15 15, 10 10)))')).to.be.eql({
type: 'MultiPolygon',
coordinates: [
[[[30, 20], [10, 40], [45, 40], [30, 20]]],
[
[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]],
[[10, 10], [15, 10], [15, 15], [10, 10]]]]
});
});
});

0 comments on commit a8fbe86

Please sign in to comment.