Skip to content

Commit

Permalink
📦 Added support for FeatureCollections
Browse files Browse the repository at this point in the history
* Added support for converting FeatureCollections to GEOMETRYCOLLECTIONS
* Added protection for converting GeoJSON features
* Added option to parse WKT as Features and embed properties
  • Loading branch information
alrico88 committed Feb 15, 2020
1 parent 14062f2 commit 7f2a27d
Show file tree
Hide file tree
Showing 6 changed files with 3,152 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"object-curly-newline": "off",
"object-curly-spacing": "error",
"object-property-newline": "error",
"object-shorthand": "off",
"object-shorthand": "error",
"one-var": "off",
"one-var-declaration-per-line": "error",
"operator-assignment": ["error", "always"],
Expand Down
44 changes: 35 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

- [WktParserHelper](#WktParserHelper)
- [new WktParserHelper()](#new_WktParserHelper_new)
- [.convertPolygonToWK(GeoJSON, [type])](#WktParserHelper.convertPolygonToWK) ⇒ <code>string</code> \| <code>Buffer</code>
- [.parseFromWK(item)](#WktParserHelper.parseFromWK) ⇒ <code>object</code>
- ~~[.convertPolygonToWK()](#WktParserHelper.convertPolygonToWK)~~
- [.convertToWK(GeoJSON, [type])](#WktParserHelper.convertToWK) ⇒ <code>string</code> \| <code>Buffer</code>
- [.parseFromWK(item, [asFeature], [properties])](#WktParserHelper.parseFromWK) ⇒ <code>object</code>
- [.convertFeatureCollection(GeoJSON)](#WktParserHelper.convertFeatureCollection) ⇒ <code>string</code>

<a name="new_WktParserHelper_new"></a>

Expand All @@ -17,27 +19,51 @@ WktParserHelper class

<a name="WktParserHelper.convertPolygonToWK"></a>

### WktParserHelper.convertPolygonToWK(GeoJSON, [type]) ⇒ <code>string</code> \| <code>Buffer</code>
### ~~WktParserHelper.convertPolygonToWK()~~

**_Deprecated_**

Converts GeoJSON to Wkt or Wkb

**Kind**: static method of [<code>WktParserHelper</code>](#WktParserHelper)
**Returns**: <code>string</code> \| <code>Buffer</code> - WKT or WKT
<a name="WktParserHelper.convertToWK"></a>

### WktParserHelper.convertToWK(GeoJSON, [type]) ⇒ <code>string</code> \| <code>Buffer</code>

Converts GeoJSON to WKT or WKB

**Kind**: static method of [<code>WktParserHelper</code>](#WktParserHelper)
**Returns**: <code>string</code> \| <code>Buffer</code> - WKT or WKB

| Param | Type | Default | Description |
| ------- | ------------------------------------------------------------ | ---------------------------- | -------------------------------- |
| GeoJSON | <code>object</code> | | GeoJSON to convert to WKT or WKB |
| GeoJSON | <code>Object</code> | | GeoJSON to convert to WKT or WKB |
| [type] | <code>&#x27;wkt&#x27;</code> \| <code>&#x27;wkb&#x27;</code> | <code>&#x27;wkt&#x27;</code> | |

<a name="WktParserHelper.parseFromWK"></a>

### WktParserHelper.parseFromWK(item) ⇒ <code>object</code>
### WktParserHelper.parseFromWK(item, [asFeature], [properties]) ⇒ <code>object</code>

Parses from WKT or WKB

**Kind**: static method of [<code>WktParserHelper</code>](#WktParserHelper)
**Returns**: <code>object</code> - GeoJSON

| Param | Type | Description |
| ----- | ------------------------------------------ | -------------------------------- |
| item | <code>string</code> \| <code>Buffer</code> | WKT or WKB to convert to GeoJSON |
| Param | Type | Default | Description |
| ------------ | ------------------------------------------ | ------------------ | --------------------------------------------- |
| item | <code>string</code> \| <code>Buffer</code> | | WKT or WKB to convert to GeoJSON |
| [asFeature] | <code>boolean</code> | <code>false</code> | |
| [properties] | <code>object</code> | <code>{}</code> | Properties to embed resulting GeoJSON feature |

<a name="WktParserHelper.convertFeatureCollection"></a>

### WktParserHelper.convertFeatureCollection(GeoJSON) ⇒ <code>string</code>

Converts GeoJSON FeatureCollection to WKT GEOMETRYCOLLECTION

**Kind**: static method of [<code>WktParserHelper</code>](#WktParserHelper)
**Returns**: <code>string</code> - WKT or WKT

| Param | Type | Description |
| ------- | ------------------- | ------------------------------------------- |
| GeoJSON | <code>Object</code> | GeoJSON FeatureCollection to convert to WKT |
50 changes: 44 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,25 @@ class WktParserHelper {
* Converts GeoJSON to Wkt or Wkb
*
* @static
* @param {object} GeoJSON GeoJSON to convert to WKT or WKB
* @deprecated Use convertToWK instead
* @memberof WktParserHelper
*/
static convertPolygonToWK() {
throw new Error('method is deprecated, use convertToWK instead');
}

/**
* Converts GeoJSON to WKT or WKB
*
* @static
* @param {{type: string}} GeoJSON GeoJSON to convert to WKT or WKB
* @param {'wkt'|'wkb'} [type='wkt']
* @returns {string|Buffer} WKT or WKT
* @returns {string|Buffer} WKT or WKB
* @memberof WktParserHelper
*/
static convertPolygonToWK(GeoJSON, type = 'wkt') {
const parsed = wkx.Geometry.parseGeoJSON(GeoJSON);
static convertToWK(GeoJSON, type = 'wkt') {
const toParse = GeoJSON.type === 'Feature' ? GeoJSON.geometry : GeoJSON;
const parsed = wkx.Geometry.parseGeoJSON(toParse);
return type === 'wkt' ? parsed.toWkt() : parsed.toWkb();
}

Expand All @@ -26,11 +38,37 @@ class WktParserHelper {
*
* @static
* @param {string|Buffer} item WKT or WKB to convert to GeoJSON
* @param {boolean} [asFeature=false]
* @param {object} [properties={}] Properties to embed resulting GeoJSON feature
* @returns {object} GeoJSON
* @memberof WktParserHelper
*/
static parseFromWK(item) {
return wkx.Geometry.parse(item).toGeoJSON();
static parseFromWK(item, asFeature = false, properties = {}) {
const GeoJSON = wkx.Geometry.parse(item).toGeoJSON();
return asFeature
? {
type: 'Feature',
geometry: GeoJSON,
properties,
}
: GeoJSON;
}

/**
* Converts GeoJSON FeatureCollection to WKT GEOMETRYCOLLECTION
*
* @static
* @param {{type: 'FeatureCollection', features: object[]}} GeoJSON GeoJSON FeatureCollection to convert to WKT
* @returns {string} WKT or WKT
* @memberof WktParserHelper
*/
static convertFeatureCollection(GeoJSON) {
if (GeoJSON.type !== 'FeatureCollection') {
throw new Error('GeoJSON is not a FeatureCollection');
}
return `GEOMETRYCOLLECTION(${GeoJSON.features
.map((d) => WktParserHelper.convertToWK(d))
.join(',')})`;
}
}

Expand Down
57 changes: 57 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const WktParser = require('./index');

// TEST VARIABLES

const TEST_FEATURE = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[-3.706512451171875, 40.420074462890625],
[-3.70513916015625, 40.420074462890625],
[-3.70513916015625, 40.42144775390625],
[-3.706512451171875, 40.42144775390625],
[-3.706512451171875, 40.420074462890625],
],
],
},
};

const TEST_FEATURE_WITH_PROPERTIES = Object.assign(TEST_FEATURE, {
properties: {
test: 'Test',
},
});

const TEST_FEATURE_AS_WKT =
'POLYGON((-3.706512451171875 40.420074462890625,-3.70513916015625 40.420074462890625,-3.70513916015625 40.42144775390625,-3.706512451171875 40.42144775390625,-3.706512451171875 40.420074462890625))';

const TEST_FEATURE_COLLECTION = {
type: 'FeatureCollection',
features: [TEST_FEATURE, TEST_FEATURE],
};

const TEST_FEATURE_COLLECTION_AS_WKT =
'GEOMETRYCOLLECTION(POLYGON((-3.706512451171875 40.420074462890625,-3.70513916015625 40.420074462890625,-3.70513916015625 40.42144775390625,-3.706512451171875 40.42144775390625,-3.706512451171875 40.420074462890625)),POLYGON((-3.706512451171875 40.420074462890625,-3.70513916015625 40.420074462890625,-3.70513916015625 40.42144775390625,-3.706512451171875 40.42144775390625,-3.706512451171875 40.420074462890625)))';

// TESTS

test('Same GeoJSON should always return same WKT', () => {
expect(WktParser.convertToWK(TEST_FEATURE)).toBe(TEST_FEATURE_AS_WKT);
});

test('Same WKT should always return same Geometry', () => {
expect(WktParser.parseFromWK(TEST_FEATURE_AS_WKT)).toStrictEqual(TEST_FEATURE.geometry);
});

test('Same WKT should always return same Feature, with desired properties embedded', () => {
expect(WktParser.parseFromWK(TEST_FEATURE_AS_WKT, true, {
test: 'Test',
})).toStrictEqual(TEST_FEATURE_WITH_PROPERTIES);
});

test('Same GeoJSON FeatureCollection should always return same wkt GEOMETRYCOLLECTION', () => {
expect(WktParser.convertFeatureCollection(TEST_FEATURE_COLLECTION)).toBe(TEST_FEATURE_COLLECTION_AS_WKT);
});
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "wkt-parser-helper",
"version": "1.0.0",
"version": "2.0.0",
"keywords": [
"wkt",
"wkb",
"geojson",
"parser"
"parser",
"FeatureCollection"
],
"description": "Module to help parse GeoJSONs to WKT and back",
"main": "index.js",
Expand All @@ -16,6 +17,11 @@
"wkx": "^0.4.8"
},
"devDependencies": {
"eslint": "^5.4"
"eslint": "^5.4",
"jest": "^25.1.0"
},
"scripts": {
"test": "jest",
"prepublish": "jest"
}
}
Loading

0 comments on commit 7f2a27d

Please sign in to comment.