Skip to content

Commit

Permalink
Added new feature: windy style layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Suizer98 committed Apr 19, 2024
1 parent 0f6ae49 commit f836a25
Show file tree
Hide file tree
Showing 5 changed files with 811,934 additions and 6 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data/
26 changes: 23 additions & 3 deletions app/components/MapView.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import GeoJSON from "ol/format/GeoJSON";
import OSM from "ol/source/OSM";
import { fromLonLat } from "ol/proj";
import { defaults as defaultControls } from "ol/control";
import { WindLayer } from "ol-wind";

import Sidebar from "./Sidebar";

type LayersVisible = {
osm: boolean;
vector: boolean;
wind: boolean;
};

const MapView = () => {
Expand All @@ -25,10 +27,13 @@ const MapView = () => {
const [osmLayer, setOsmLayer] = useState<TileLayer<OSM> | null>(null);
const [vectorLayer, setVectorLayer] =
useState<VectorLayer<VectorSource> | null>(null);
const [windLayer, setWindLayer] = useState<any>();
const [layersVisible, setLayersVisible] = useState<LayersVisible>({
osm: true,
vector: true,
wind: true,
});
const [windData, setWindData] = useState<any>(null);

useEffect(() => {
if (!mapElementRef.current) return;
Expand Down Expand Up @@ -66,9 +71,20 @@ const MapView = () => {
}),
});

const initialWindLayer = new WindLayer(windData, {
forceRender: false,
windOptions: {
velocityScale: 1 / 100,
paths: 5000,
colorScale: (velocity: number) => "#C71585",
width: 3,
generateParticleOption: false,
},
});

const initialMap = new Map({
target: mapElementRef.current,
layers: [initialOsmLayer, initialVectorLayer],
layers: [initialOsmLayer, initialVectorLayer, initialWindLayer],
view: new View({
center: fromLonLat([107, 3.5]),
zoom: 6,
Expand All @@ -79,14 +95,16 @@ const MapView = () => {
setMap(initialMap);
setOsmLayer(initialOsmLayer);
setVectorLayer(initialVectorLayer);
setWindLayer(initialWindLayer);

return () => initialMap.setTarget(undefined);
}, []);
}, [windData]);

useEffect(() => {
if (osmLayer) osmLayer.setVisible(layersVisible.osm);
if (vectorLayer) vectorLayer.setVisible(layersVisible.vector);
}, [layersVisible, osmLayer, vectorLayer]);
if (windLayer) windLayer.setVisible(layersVisible.wind);
}, [layersVisible, osmLayer, vectorLayer, windLayer]);

const toggleSidebar = () => {
setSidebarExpanded(!sidebarExpanded);
Expand All @@ -99,6 +117,8 @@ const MapView = () => {
sidebarExpanded={sidebarExpanded}
layersVisible={layersVisible}
setLayersVisible={setLayersVisible}
windData={windData}
setWindData={setWindData}
/>
<div
ref={mapElementRef}
Expand Down
48 changes: 45 additions & 3 deletions app/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import React from "react";
import React, { useEffect, useState } from "react";

type SidebarProps = {
toggleSidebar: () => void;
sidebarExpanded: boolean;
layersVisible: { osm: boolean; vector: boolean };
layersVisible: { osm: boolean; vector: boolean; wind: boolean };
setLayersVisible: React.Dispatch<
React.SetStateAction<{ osm: boolean; vector: boolean }>
React.SetStateAction<{ osm: boolean; vector: boolean; wind: boolean }>
>;
windData: any;
setWindData: React.Dispatch<React.SetStateAction<any>>;
};

const Sidebar: React.FC<SidebarProps> = ({
toggleSidebar,
sidebarExpanded,
layersVisible,
setLayersVisible,
windData,
setWindData,
}) => {
useEffect(() => {
if (layersVisible.wind && !windData) {
fetchWindData();
}
}, [layersVisible.wind]);

const fetchWindData = async () => {
try {
const response = await fetch(`api/windy`);
const data = await response.json();
setWindData(data);
} catch (error) {
console.error("Error fetching wind data:", error);
}
};

const handleWindLayerToggle = () => {
setLayersVisible((prev) => ({ ...prev, wind: !prev.wind }));
if (!layersVisible.wind && !windData) {
fetchWindData();
}
};

return (
<div>
<button
Expand Down Expand Up @@ -83,6 +110,21 @@ const Sidebar: React.FC<SidebarProps> = ({
className="toggle-checkbox"
/>
</div>
<div className="flex items-center justify-between">
<label
htmlFor="wind-toggle"
className="text-sm font-medium text-gray-700"
>
Wind Layer
</label>
<input
id="wind-toggle"
type="checkbox"
checked={layersVisible.wind}
onChange={handleWindLayerToggle}
className="toggle-checkbox"
/>
</div>
</div>
</div>
)}
Expand Down
Loading

0 comments on commit f836a25

Please sign in to comment.