Skip to content

Commit 8bd76a8

Browse files
committed
transform polygons to LineStrings on KML import
1 parent 4ef60f0 commit 8bd76a8

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@turf/helpers": "7.2.0",
4848
"@turf/invariant": "7.2.0",
4949
"@turf/meta": "7.2.0",
50+
"@turf/polygon-to-line": "7.2.0",
5051
"geographiclib-dms": "2.1.1",
5152
"geographiclib-geodesic": "patch:geographiclib-geodesic@npm%3A2.1.1#~/.yarn/patches/geographiclib-geodesic-npm-2.1.1-2da4df7f31.patch",
5253
"papaparse": "5.5.3",

src/parseData.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { feature, featureCollection, point } from '@turf/helpers';
22
import { collectionOf, featureOf, geojsonType } from '@turf/invariant';
3+
import polygonToLine from '@turf/polygon-to-line';
34
import type {
45
Feature,
56
FeatureCollection,
67
GeoJSON,
78
Geometry,
89
LineString,
910
Point,
11+
Polygon,
1012
} from 'geojson';
1113
import Papa from 'papaparse';
1214
import { kml } from './togeojson';
@@ -140,11 +142,16 @@ export const parseLineString = async (
140142
throw new Error('Need exactly one feature');
141143
}
142144
}
143-
if ((json as GeoJSON).type === 'LineString') {
144-
geojsonType(json, 'LineString', 'LineString');
145-
return feature(json as LineString);
145+
if (
146+
(json as GeoJSON).type === 'Polygon' ||
147+
(json as GeoJSON).type === 'LineString'
148+
) {
149+
json = feature(json as Polygon | LineString);
146150
}
147151
if ((json as GeoJSON).type === 'Feature') {
152+
if ((json as Feature).geometry.type === 'Polygon') {
153+
json = polygonToLine(json as Feature<Polygon>);
154+
}
148155
featureOf(json as Feature<any>, 'LineString', 'Feature');
149156
return json as Feature<LineString>;
150157
}

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,18 @@ __metadata:
24532453
languageName: node
24542454
linkType: hard
24552455

2456+
"@turf/polygon-to-line@npm:7.2.0":
2457+
version: 7.2.0
2458+
resolution: "@turf/polygon-to-line@npm:7.2.0"
2459+
dependencies:
2460+
"@turf/helpers": "npm:^7.2.0"
2461+
"@turf/invariant": "npm:^7.2.0"
2462+
"@types/geojson": "npm:^7946.0.10"
2463+
tslib: "npm:^2.8.1"
2464+
checksum: 10c0/b840b218e631a252655d233b2c569607d4d3e8d3715a1eab7d26394e4acc1f325b736837d2a82e5c4f678600b2ddc32004782ed3825ca5b1027dce12a4e4cde9
2465+
languageName: node
2466+
linkType: hard
2467+
24562468
"@types/geojson@npm:7946.0.16, @types/geojson@npm:^7946.0.10":
24572469
version: 7946.0.16
24582470
resolution: "@types/geojson@npm:7946.0.16"
@@ -4905,6 +4917,7 @@ __metadata:
49054917
"@turf/helpers": "npm:7.2.0"
49064918
"@turf/invariant": "npm:7.2.0"
49074919
"@turf/meta": "npm:7.2.0"
4920+
"@turf/polygon-to-line": "npm:7.2.0"
49084921
"@types/geojson": "npm:7946.0.16"
49094922
"@types/node": "npm:22.16.0"
49104923
"@types/papaparse": "npm:5.3.16"

0 commit comments

Comments
 (0)