diff --git a/src/frontend/src/components/createnewproject/UploadArea.tsx b/src/frontend/src/components/createnewproject/UploadArea.tsx index b7dce9264c..0742646ee1 100644 --- a/src/frontend/src/components/createnewproject/UploadArea.tsx +++ b/src/frontend/src/components/createnewproject/UploadArea.tsx @@ -113,17 +113,19 @@ const UploadArea = ({ flag, geojsonFile, setGeojsonFile, setCustomLineUpload, se }; useEffect(() => { - const isWGS84 = () => { - if (uploadAreaSelection === 'upload_file') { - const isWGS84Projection = checkWGS84Projection(drawnGeojson); - setIsGeojsonWG84(isWGS84Projection); - return isWGS84Projection; + if (drawnGeojson) { + const isWGS84 = () => { + if (uploadAreaSelection === 'upload_file') { + const isWGS84Projection = checkWGS84Projection(drawnGeojson); + setIsGeojsonWG84(isWGS84Projection); + return isWGS84Projection; + } + setIsGeojsonWG84(true); + return true; + }; + if (!isWGS84() && drawnGeojson) { + showSpatialError(); } - setIsGeojsonWG84(true); - return true; - }; - if (!isWGS84() && drawnGeojson) { - showSpatialError(); } return () => {}; }, [drawnGeojson]); diff --git a/src/frontend/src/utilfunctions/checkWGS84Projection.js b/src/frontend/src/utilfunctions/checkWGS84Projection.js index 2699ea0ef8..638126b19c 100644 --- a/src/frontend/src/utilfunctions/checkWGS84Projection.js +++ b/src/frontend/src/utilfunctions/checkWGS84Projection.js @@ -1,26 +1,35 @@ -function checkWGS84Projection(geojson) { +import OLVectorLayer from 'ol/layer/Vector'; +import GeoJSON from 'ol/format/GeoJSON'; +import { Vector as VectorSource } from 'ol/source'; + +function checkWGS84Projection(drawnGeojson) { + const vectorLyr = new OLVectorLayer({ + source: new VectorSource({ + features: new GeoJSON().readFeatures(drawnGeojson), + }), + declutter: true, + }); + + const extent = vectorLyr.getSource()?.getExtent(); + try { - for (const feature of geojson.features) { - const coordinates = feature.geometry.coordinates; - for (const coord of coordinates[0]) { - const [longitude, latitude] = coord; - if ( - isNaN(latitude) || - isNaN(longitude) || - latitude < -90 || - latitude > 90 || - longitude < -180 || - longitude > 180 - ) { - // setIsGeojsonWG84(false); - return false; // Coordinates are out of WGS 84 range - } + if (extent?.length > 0) { + const longitude = extent[0]; + const latitude = extent[1]; + if ( + isNaN(latitude) || + isNaN(longitude) || + latitude < -90 || + latitude > 90 || + longitude < -180 || + longitude > 180 + ) { + return false; } + return true; // All coordinates are within WGS 84 range } - // setIsGeojsonWG84(true); - return true; // All coordinates are within WGS 84 range + return false; } catch (error) { - // setIsGeojsonWG84(false); return false; } }