Skip to content

Commit

Permalink
Moved some functions to utils
Browse files Browse the repository at this point in the history
Signed-off-by: Emil Balitzki <[email protected]>
  • Loading branch information
Corgam committed Jul 2, 2024
1 parent 6bf7429 commit 546bb91
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
35 changes: 2 additions & 33 deletions frontend/src/components/MapView/MapDatasetVisualizer.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand All @@ -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<Point, GeoJsonProperties> => {
const markerFeatures: Feature<Point, GeoJsonProperties>[] =
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<Point, GeoJsonProperties>;
}
return feature as Feature<Point, GeoJsonProperties>;
});

return {
type: "FeatureCollection",
features: markerFeatures,
};
};

const MapDatasetVisualizer: React.FC<MapDatasetVisualizerProps> = ({
dataset,
}) => {
Expand Down
33 changes: 33 additions & 0 deletions frontend/src/utils/polgonsToMarkers.ts
Original file line number Diff line number Diff line change
@@ -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<Point, GeoJsonProperties> => {
const markerFeatures: Feature<Point, GeoJsonProperties>[] =
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<Point, GeoJsonProperties>;
}
return feature as Feature<Point, GeoJsonProperties>;
});

return {
type: "FeatureCollection",
features: markerFeatures,
};
};

0 comments on commit 546bb91

Please sign in to comment.