Skip to content

Commit

Permalink
Merge branch 'master' of github.com:manuelbieh/geolib
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbieh committed Dec 11, 2019
2 parents 256d3b3 + aac0feb commit 00a1935
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# These are supported funding model platforms

# github: [manuelbieh]
open_collective: geolib
issuehunt: manuelbieh/geolib
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,23 @@ geolib.convertArea(298678, 'km2'));
```

Returns the converted area as number.


### `wktToPolygon(wkt)`

Converts the Well-known text (a.k.a WKT) to polygon that Geolib understands.
[https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Geometric_Objects](WKT)

```js
geolib.wktToPolygon('POLYGON ((30 10.54321, 40 40, 20 40, 10 20, 30 10))');
// [
// { latitude: 10.54321, longitude: 30 },
// { latitude: 40, longitude: 40 },
// { latitude: 40, longitude: 20 },
// { latitude: 20, longitude: 10 },
// { latitude: 10, longitude: 30 },}
// ]
```

Returns the array of coordinates.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "geolib",
"version": "3.0.3",
"version": "3.1.0",
"description": "",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
19 changes: 19 additions & 0 deletions src/wktToPolygon.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import wktToPolygon from './wktToPolygon';

describe('wktToPolygon', () => {
it('converts a WKT to Polygon', () => {
const wkt = 'POLYGON ((30 10.54321, 40 40, 20 40, 10 20, 30 10))';
expect(wktToPolygon(wkt)).toEqual([
{ latitude: 10.54321, longitude: 30 },
{ latitude: 40, longitude: 40 },
{ latitude: 40, longitude: 20 },
{ latitude: 20, longitude: 10 },
{ latitude: 10, longitude: 30 },
]);
});

it('throw error when is not a POLYGON', () => {
const wkt = 'MULTIPOLYGON (((3 2, 45 4, 3 2)), ((15 5, 4 1, 15 5)))';
expect(() => wktToPolygon(wkt)).toThrowError('Invalid wkt.');
});
});
21 changes: 21 additions & 0 deletions src/wktToPolygon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Converts a wkt text to polygon
const wktToPolygon = (wkt: string) => {
if (!wkt.startsWith('POLYGON')) {
throw new Error('Invalid wkt.');
}
const coordsText = wkt
.slice(wkt.indexOf('(') + 2, wkt.indexOf(')'))
.split(', ');

const polygon = coordsText.map((coordText) => {
const [longitude, latitude] = coordText.split(' ');
return {
longitude: parseFloat(longitude),
latitude: parseFloat(latitude),
};
});

return polygon;
};

export default wktToPolygon;
16 changes: 9 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3151,14 +3151,16 @@ eslint-scope@^4.0.0, eslint-scope@^4.0.3:
estraverse "^4.1.1"

eslint-utils@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-1.3.1.tgz#9a851ba89ee7c460346f97cf8939c7298827e512"
integrity sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q==
version "1.4.3"
resolved "https://registry.yarnpkg.com/eslint-utils/-/eslint-utils-1.4.3.tgz#74fec7c54d0776b6f67e0251040b5806564e981f"
integrity sha512-fbBN5W2xdY45KulGXmLHZ3c3FHfVYmKg0IrAKGOkT/464PQsx2UeIzfz1RmEci+KLm1bBaAzZAh8+/E+XAeZ8Q==
dependencies:
eslint-visitor-keys "^1.1.0"

eslint-visitor-keys@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#3f3180fb2e291017716acb4c9d6d5b5c34a6a81d"
integrity sha512-qzm/XxIbxm/FHyH341ZrbnMUpe+5Bocte9xkmFMzPMjRaZMcXww+MpBptFvtU+79L362nqiLhekCxCxDPaUMBQ==
eslint-visitor-keys@^1.0.0, eslint-visitor-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz#e2a82cea84ff246ad6fb57f9bde5b46621459ec2"
integrity sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==

eslint@^5.16.0:
version "5.16.0"
Expand Down

0 comments on commit 00a1935

Please sign in to comment.