From 546bb919e64b85e2fa792e7f86fddaa2ee1a64be Mon Sep 17 00:00:00 2001 From: Emil Balitzki Date: Tue, 2 Jul 2024 17:08:15 +0200 Subject: [PATCH] Moved some functions to utils Signed-off-by: Emil Balitzki --- .../MapView/MapDatasetVisualizer.tsx | 35 ++----------------- frontend/src/utils/polgonsToMarkers.ts | 33 +++++++++++++++++ 2 files changed, 35 insertions(+), 33 deletions(-) create mode 100644 frontend/src/utils/polgonsToMarkers.ts diff --git a/frontend/src/components/MapView/MapDatasetVisualizer.tsx b/frontend/src/components/MapView/MapDatasetVisualizer.tsx index 6c3e2595..1db0fba3 100644 --- a/frontend/src/components/MapView/MapDatasetVisualizer.tsx +++ b/frontend/src/components/MapView/MapDatasetVisualizer.tsx @@ -1,12 +1,6 @@ import { useMap } from "react-leaflet"; import { useCallback, useContext, useEffect } from "react"; -import { - Feature, - FeatureCollection, - GeoJsonProperties, - Point, - Position, -} from "geojson"; +import { FeatureCollection } from "geojson"; import { MapContext } from "../../contexts/MapContext"; import { TabsContext } from "../../contexts/TabsContext"; import GeoDataFetcher from "./GeoDataFetcher"; @@ -20,6 +14,7 @@ import { MapPin } from "@phosphor-icons/react"; import { Dataset } from "../../types/DatasetTypes"; import { MarkersTypes } from "../../types/MarkersTypes"; import { createDivIcon } from "../../utils/mergeIcons"; +import { convertPolygonsToMarkers } from "../../utils/polgonsToMarkers"; interface MapDatasetVisualizerProps { dataset: Dataset; @@ -42,32 +37,6 @@ const divIcondefault: DivIcon = L.divIcon({ iconAnchor: [17, 17], // Adjust the anchor point as needed }); -// Function to convert polygon GeoJSON to marker GeoJSON -const convertPolygonsToMarkers = ( - geoData: FeatureCollection -): FeatureCollection => { - const markerFeatures: Feature[] = - geoData.features.map((feature) => { - if (feature.geometry.type === "Polygon") { - const firstCoord = feature.geometry.coordinates[0][0]; - return { - type: "Feature", - geometry: { - type: "Point", - coordinates: firstCoord as Position, - }, - properties: feature.properties, - } as Feature; - } - return feature as Feature; - }); - - return { - type: "FeatureCollection", - features: markerFeatures, - }; -}; - const MapDatasetVisualizer: React.FC = ({ dataset, }) => { diff --git a/frontend/src/utils/polgonsToMarkers.ts b/frontend/src/utils/polgonsToMarkers.ts new file mode 100644 index 00000000..38eab585 --- /dev/null +++ b/frontend/src/utils/polgonsToMarkers.ts @@ -0,0 +1,33 @@ +import { + Feature, + FeatureCollection, + GeoJsonProperties, + Point, + Position, +} from "geojson"; + +// Function to convert polygon GeoJSON to marker GeoJSON +export const convertPolygonsToMarkers = ( + geoData: FeatureCollection +): FeatureCollection => { + const markerFeatures: Feature[] = + geoData.features.map((feature) => { + if (feature.geometry.type === "Polygon") { + const firstCoord = feature.geometry.coordinates[0][0]; + return { + type: "Feature", + geometry: { + type: "Point", + coordinates: firstCoord as Position, + }, + properties: feature.properties, + } as Feature; + } + return feature as Feature; + }); + + return { + type: "FeatureCollection", + features: markerFeatures, + }; +};