-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add geoJSONtoWKT conversion and fixed error in sidebar (#9331)
- Loading branch information
1 parent
6ee71b7
commit af949e5
Showing
5 changed files
with
253 additions
and
83 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
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright 2023, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import expect from 'expect'; | ||
import toGeoJSON from '../toGeoJSON'; | ||
import toWKT from '../toWKT'; | ||
const WKT_TESTS = [{ | ||
wkt: 'POINT(30 10)', | ||
geojson: { | ||
type: 'Point', | ||
coordinates: [30, 10] | ||
} | ||
}, { | ||
wkt: 'LINESTRING(30 10, 10 30, 40 40)', | ||
geojson: { | ||
type: 'LineString', | ||
coordinates: [[30, 10], [10, 30], [40, 40]] | ||
} | ||
}, { | ||
wkt: 'POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', | ||
geojson: { | ||
type: 'Polygon', | ||
coordinates: [[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]] | ||
} | ||
}, | ||
{ | ||
wkt: 'MULTIPOINT(10 40, 40 30, 20 20, 30 10)', | ||
geojson: { | ||
type: 'MultiPoint', | ||
coordinates: [[10, 40], [40, 30], [20, 20], [30, 10]] | ||
} | ||
}, | ||
{ | ||
|
||
wkt: 'MULTILINESTRING((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))', | ||
geojson: { | ||
type: 'MultiLineString', | ||
coordinates: [[[10, 10], [20, 20], [10, 40]], [[40, 40], [30, 30], [40, 20], [30, 10]]] | ||
} | ||
}, | ||
{ | ||
wkt: 'MULTIPOLYGON(((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))', | ||
geojson: { | ||
type: 'MultiPolygon', | ||
coordinates: [ | ||
[[[30, 20], [45, 40], [10, 40], [30, 20]]], | ||
[[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5] | ||
]] | ||
] | ||
} | ||
}, { | ||
wkt: 'GEOMETRYCOLLECTION(POINT(4 6), LINESTRING(4 6, 7 10))', | ||
geojson: { | ||
type: 'GeometryCollection', | ||
geometries: [{ | ||
type: 'Point', | ||
coordinates: [4, 6] | ||
}, { | ||
type: 'LineString', | ||
coordinates: [[4, 6], [7, 10]] | ||
}] | ||
} | ||
}]; | ||
|
||
|
||
// alternative syntax to check parsing | ||
const ALTERNATIVE_SYNTAX_TESTS = [ | ||
// MULTIPOINT HAS 2 EQUIVALENT FORMS | ||
{ | ||
wkt: 'MULTIPOINT((10 40), (40 30), (20 20), (30 10))', | ||
geojson: { | ||
type: 'MultiPoint', | ||
coordinates: [[10, 40], [40, 30], [20, 20], [30, 10]] | ||
} | ||
}, | ||
// spaces after comma are optional | ||
{ | ||
wkt: 'GEOMETRYCOLLECTION(POINT(4 6), LINESTRING(4 6,7 10))', | ||
geojson: { | ||
type: 'GeometryCollection', | ||
geometries: [{ | ||
type: 'Point', | ||
coordinates: [4, 6] | ||
}, { | ||
type: 'LineString', | ||
coordinates: [[4, 6], [7, 10]] | ||
}] | ||
} | ||
}, | ||
// space after geometry name is allowed | ||
{ | ||
wkt: 'GEOMETRYCOLLECTION (POINT (4 6), LINESTRING (4 6, 7 10))', | ||
geojson: { | ||
type: 'GeometryCollection', | ||
geometries: [{ | ||
type: 'Point', | ||
coordinates: [4, 6] | ||
}, { | ||
type: 'LineString', | ||
coordinates: [[4, 6], [7, 10]] | ||
}] | ||
} | ||
} | ||
]; | ||
|
||
describe('WKT convert to geoJSON (toGeoJSON)', function() { | ||
[...WKT_TESTS, ...ALTERNATIVE_SYNTAX_TESTS].forEach((test) => { | ||
it(test.wkt, () => { | ||
expect(toGeoJSON(test.wkt)).toEqual(test.geojson); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
describe('geoJSON convert to WKT (toWKT)', function() { | ||
WKT_TESTS.forEach((test) => { | ||
it(test.wkt, () => { | ||
expect(toWKT(test.geojson)).toEqual(test.wkt); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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 +1,2 @@ | ||
export {default as toGeoJSON} from './toGeoJSON'; | ||
export {default as toWKT} from './toWKT'; |
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
const coordinateArrayToWKT = (coordinates) => { | ||
const [x, y] = coordinates; | ||
return `${x} ${y}`; | ||
}; | ||
|
||
/** | ||
* Convert a geoJSON Point to a WKT Point | ||
* @private | ||
* @param {object} point the geoJSON Point | ||
* @returns {string} WKT string | ||
*/ | ||
const pointToWKT = (point) => { | ||
const {coordinates} = point; | ||
return `POINT(${coordinateArrayToWKT(coordinates)})`; | ||
}; | ||
|
||
/** | ||
* Convert a geoJSON MultiPoint to a WKT MultiPoint | ||
* @private | ||
* @param {object} multiPoint the geoJSON MultiPoint | ||
* @returns {string} WKT string | ||
* | ||
*/ | ||
const multiPointToWKT = (multiPoint) => { | ||
const {coordinates} = multiPoint; | ||
const points = coordinates.map(coordinateArrayToWKT); | ||
return `MULTIPOINT(${points.join(', ')})`; | ||
}; | ||
|
||
/** | ||
* Convert a geoJSON LineString to a WKT LineString | ||
* @private | ||
* @param {object} lineString the geoJSON LineString | ||
* @returns {string} WKT string | ||
*/ | ||
const lineStringToWKT = (lineString) => { | ||
const {coordinates} = lineString; | ||
const points = coordinates.map(coordinateArrayToWKT); | ||
return `LINESTRING(${points.join(', ')})`; | ||
}; | ||
|
||
/** | ||
* Convert a geoJSON MultiLineString to a WKT MultiLineString | ||
* @private | ||
* @param {object} multiLineString the geoJSON MultiLineString | ||
* @returns {string} WKT string | ||
*/ | ||
const multiLineStringToWKT = (multiLineString) => { | ||
const {coordinates} = multiLineString; | ||
const lines = coordinates.map(line => `(${line.map(coordinateArrayToWKT).join(', ')})`); | ||
return `MULTILINESTRING(${lines.join(', ')})`; | ||
}; | ||
|
||
/** | ||
* Convert a geoJSON Polygon to a WKT Polygon | ||
* @private | ||
* @param {object} polygon the geoJSON Polygon | ||
* @returns {string} WKT string | ||
*/ | ||
const polygonToWKT = (polygon) => { | ||
const {coordinates} = polygon; | ||
const rings = coordinates.map(ring => `(${ring.map(coordinateArrayToWKT).join(', ')})`); | ||
return `POLYGON(${rings.join(', ')})`; | ||
}; | ||
|
||
/** | ||
* Convert a geoJSON MultiPolygon to a WKT MultiPolygon | ||
* @private | ||
* @param {object} multiPolygon the geoJSON MultiPolygon | ||
* @returns {string} WKT string | ||
*/ | ||
const multiPolygonToWKT = (multiPolygon) => { | ||
const {coordinates} = multiPolygon; | ||
const polygons = coordinates.map(polygon => `(${polygon.map(ring => `(${ring.map(coordinateArrayToWKT).join(', ')})`).join(', ')})`); | ||
return `MULTIPOLYGON(${polygons.join(', ')})`; | ||
}; | ||
|
||
let toWKT; | ||
|
||
/** | ||
* Convert a geoJSON GeometryCollection to a WKT GeometryCollection | ||
* @private | ||
* @param {object} geometryCollection the geoJSON GeometryCollection | ||
* @returns {string} WKT string | ||
*/ | ||
const geometryCollectionToWKT = (geometryCollection) => { | ||
const {geometries} = geometryCollection; | ||
const geometriesWKT = geometries.map(geoJSON => toWKT(geoJSON)); | ||
return `GEOMETRYCOLLECTION(${geometriesWKT.join(', ')})`; | ||
}; | ||
|
||
/** | ||
* Converts a geoJSON geometry to a WKT geometry | ||
* @param {object} geoJSON the geoJSON geometry | ||
* @returns {string} WKT string | ||
* @example | ||
* const geoJSON = { | ||
* type: 'Point', | ||
* coordinates: [30, 10] | ||
* }; | ||
* const wkt = toWKT(geoJSON); | ||
* // wkt = 'POINT(30 10)' | ||
*/ | ||
toWKT = (geoJSON) => { | ||
switch (geoJSON.type) { | ||
case 'Point': | ||
return pointToWKT(geoJSON); | ||
case 'MultiPoint': | ||
return multiPointToWKT(geoJSON); | ||
case 'LineString': | ||
return lineStringToWKT(geoJSON); | ||
case 'MultiLineString': | ||
return multiLineStringToWKT(geoJSON); | ||
case 'Polygon': | ||
return polygonToWKT(geoJSON); | ||
case 'MultiPolygon': | ||
return multiPolygonToWKT(geoJSON); | ||
case 'GeometryCollection': | ||
return geometryCollectionToWKT(geoJSON); | ||
default: | ||
return ''; | ||
} | ||
}; | ||
|
||
export default toWKT; |