-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:manuelbieh/geolib
- Loading branch information
Showing
6 changed files
with
75 additions
and
8 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
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 |
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
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,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.'); | ||
}); | ||
}); |
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,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; |
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