Skip to content

Commit

Permalink
fix for geometry collections
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterprovoost committed Nov 5, 2023
1 parent b3b9e81 commit 90d3134
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
run: npm install
- name: Build page
run: npm run build
- name: Run tests
run: npm run test
- name: Create cname file
uses: finnp/create-file-action@master
env:
Expand Down
24 changes: 16 additions & 8 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import CRC32 from "crc-32";
import { EditControl } from "react-leaflet-draw";
import { geojsonToWKT } from "@terraformer/wkt";
import ReactGA from "react-ga4";
import { transformInput, ValueError, expandCollections } from "./wkt";
import { transformInput, ValueError } from "./wkt";

const DEFAULT_EPSG = "4326";

Expand Down Expand Up @@ -140,24 +140,32 @@ function App() {
}
}, [map]); // eslint-disable-line react-hooks/exhaustive-deps

function splitGeometry(geometry) {
if (geometry.type === "GeometryCollection") {
return geometry.geometries;
} else {
return [geometry];
}
}

function handleDrawStop() {
let geometries = [];
groupRef.current.eachLayer(function(layer) {
const geo = layer.toGeoJSON();
if (geo.type === "Feature") {
geometries.push(geojsonToWKT(geo.geometry));
geometries = geometries.concat(splitGeometry(geo.geometry));
} else if (geo.type === "FeatureCollection") {
geo.features.forEach(feature => {
geometries.push(geojsonToWKT(feature.geometry));
geometries = geometries.concat(splitGeometry(feature.geometry));
});
}
});
const wktGeometries = geometries.map(geojsonToWKT);
let wkt;
geometries = expandCollections(geometries);
if (geometries.length === 1) {
wkt = geometries[0];
} else if (geometries.length > 1) {
wkt = "GEOMETRYCOLLECTION(" + geometries.join(", ") + ")";
if (wktGeometries.length === 1) {
wkt = wktGeometries[0];
} else if (wktGeometries.length > 1) {
wkt = "GEOMETRYCOLLECTION(" + wktGeometries.join(", ") + ")";
}
setEpsg(4326);
clearHash();
Expand Down
11 changes: 1 addition & 10 deletions src/wkt.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,4 @@ async function transformInput(input) {

}

function expandCollections(geometries) {
// TODO: fix for existing GEOMETRYCOLLECTION (should not be nested)
geometries.reduce(function(acc, current) {
acc.push(current);
return acc;
}, []);
return geometries;
}

export { parseWkt, transformInput, ValueError, fetchProj, extractAndParseCrs, expandCollections };
export { parseWkt, transformInput, ValueError, fetchProj, extractAndParseCrs };

0 comments on commit 90d3134

Please sign in to comment.