From 3d0589b226062ca1f8b377faa3a9f1a7c44e90ff Mon Sep 17 00:00:00 2001 From: Nishit Suwal <81785002+NSUWAL123@users.noreply.github.com> Date: Tue, 21 Nov 2023 16:44:10 +0545 Subject: [PATCH] =?UTF-8?q?fix=20(createNewProject):=20uploadArea=20-=20va?= =?UTF-8?q?lidation=20added=20to=20upload=20file,=E2=80=A6=20(#987)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix (createNewProject): uploadArea - validation added to upload file, where error shown if projection is not WGS84 * feat (utilityFunction): checkWGS84Projection - function that checks if uploaded file is in WGS84 projection * fix (createNewProject): uploadArea - checking WGS84 projection function seperated to utilsFunction, code refactor --- .../createnewproject/UploadArea.tsx | 44 ++++++++++++++++--- .../src/utilfunctions/checkWGS84Projection.js | 28 ++++++++++++ 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/frontend/src/utilfunctions/checkWGS84Projection.js diff --git a/src/frontend/src/components/createnewproject/UploadArea.tsx b/src/frontend/src/components/createnewproject/UploadArea.tsx index 8f8c167b6f..505f6bb3c5 100644 --- a/src/frontend/src/components/createnewproject/UploadArea.tsx +++ b/src/frontend/src/components/createnewproject/UploadArea.tsx @@ -12,6 +12,7 @@ import { useAppSelector } from '../../types/reduxTypes'; import UploadAreaValidation from './validation/UploadAreaValidation'; import FileInputComponent from '../common/FileInputComponent'; import NewDefineAreaMap from '../../views/NewDefineAreaMap'; +import { checkWGS84Projection } from '../../utilfunctions/checkWGS84Projection.js'; // @ts-ignore const DefineAreaMap = React.lazy(() => import('../../views/DefineAreaMap')); @@ -34,6 +35,7 @@ const UploadArea = ({ flag, geojsonFile, setGeojsonFile }) => { const dispatch = useDispatch(); const navigate = useNavigate(); // const [uploadAreaFile, setUploadAreaFile] = useState(null); + const [isGeojsonWGS84, setIsGeojsonWG84] = useState(true); const projectDetails: any = useAppSelector((state) => state.createproject.projectDetails); const drawnGeojson = useAppSelector((state) => state.createproject.drawnGeojson); @@ -65,11 +67,6 @@ const UploadArea = ({ flag, geojsonFile, setGeojsonFile }) => { // } // }; - // useEffect(() => { - // setGeojsonFile(null); - // dispatch(CreateProjectActions.SetDrawnGeojson(null)); - // dispatch(CreateProjectActions.SetTotalAreaSelection(null)); - // }, [uploadAreaSelection]); const convertFileToGeojson = async (file) => { if (!file) return; const fileReader = new FileReader(); @@ -101,6 +98,33 @@ const UploadArea = ({ flag, geojsonFile, setGeojsonFile }) => { dispatch(CreateProjectActions.SetTotalAreaSelection(null)); }; + useEffect(() => { + const isWGS84 = () => { + if (uploadAreaSelection === 'upload_file') { + const isWGS84Projection = checkWGS84Projection(drawnGeojson); + setIsGeojsonWG84(isWGS84Projection); + return isWGS84Projection; + } + setIsGeojsonWG84(true); + return true; + }; + if (!isWGS84() && drawnGeojson) { + showSpatialError(); + } + return () => {}; + }, [drawnGeojson]); + + const showSpatialError = () => { + dispatch( + CommonActions.SetSnackBar({ + open: true, + message: 'Invalid spatial reference system. Please only import WGS84 (EPSG: 4326).', + variant: 'error', + duration: 6000, + }), + ); + }; + const resetFile = () => { setGeojsonFile(null); handleCustomChange('uploadedAreaFile', null); @@ -122,11 +146,17 @@ const UploadArea = ({ flag, geojsonFile, setGeojsonFile }) => { The total area of the AOI is also calculated and displayed on the screen.

-
{ + e.preventDefault(); + if (!isGeojsonWGS84 && drawnGeojson) { + showSpatialError(); + } else { + handleSubmit(e); + } + }} className="fmtm-flex fmtm-flex-col fmtm-gap-6 lg:fmtm-w-[40%] fmtm-justify-between" >
diff --git a/src/frontend/src/utilfunctions/checkWGS84Projection.js b/src/frontend/src/utilfunctions/checkWGS84Projection.js new file mode 100644 index 0000000000..2699ea0ef8 --- /dev/null +++ b/src/frontend/src/utilfunctions/checkWGS84Projection.js @@ -0,0 +1,28 @@ +function checkWGS84Projection(geojson) { + 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 + } + } + } + // setIsGeojsonWG84(true); + return true; // All coordinates are within WGS 84 range + } catch (error) { + // setIsGeojsonWG84(false); + return false; + } +} + +export { checkWGS84Projection };