Skip to content

Commit

Permalink
consume multipolygon from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
nofurtherinformation committed Feb 3, 2025
1 parent 4a2f6e4 commit a17ec93
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions app/src/app/utils/GeometryWorker/geometryWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import pointsWithinPolygon from '@turf/points-within-polygon';
import {LngLatBoundsLike, MapGeoJSONFeature} from 'maplibre-gl';
import bbox from '@turf/bbox';

const explodeMultiPolygonToPolygons = (
feature: GeoJSON.MultiPolygon
): Array<GeoJSON.Feature<GeoJSON.Polygon>> => {
return feature.coordinates.map(coords => ({
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: coords,
},
})) as Array<GeoJSON.Feature<GeoJSON.Polygon>>;
};

const GeometryWorker: GeometryWorkerClass = {
geometries: {},
getGeos() {
Expand Down Expand Up @@ -119,15 +131,17 @@ const GeometryWorker: GeometryWorkerClass = {
const geomsToDissolve: GeoJSON.Feature[] = [];
if (useBackend) {
console.log('Fetching unassigned geometries from backend');
const url = new URL(`${process.env.NEXT_PUBLIC_API_URL}/api/document/${documentId}/unassigned`);
const url = new URL(
`${process.env.NEXT_PUBLIC_API_URL}/api/document/${documentId}/unassigned`
);
if (exclude_ids?.length) {
exclude_ids.forEach(id => url.searchParams.append('exclude_ids', id));
}
await fetch(url)
.then(r => r.json())
.then(data =>
data.features.forEach((geo: GeoJSON.Feature) => geomsToDissolve.push(geo))
);
const remoteMultipolygon = await fetch(url).then(r => r.json());
remoteMultipolygon.features.forEach((geo: GeoJSON.MultiPolygon) => {
const polygons = explodeMultiPolygonToPolygons(geo);
polygons.forEach(p => geomsToDissolve.push(p));
});
} else {
for (const id in this.geometries) {
const geom = this.geometries[id];
Expand Down

0 comments on commit a17ec93

Please sign in to comment.