Skip to content

Commit

Permalink
fix (frontend): projection validity check using coordinates of extent (
Browse files Browse the repository at this point in the history
  • Loading branch information
NSUWAL123 authored Jan 18, 2024
1 parent 8ad3ec1 commit 1c5420b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
22 changes: 12 additions & 10 deletions src/frontend/src/components/createnewproject/UploadArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
47 changes: 28 additions & 19 deletions src/frontend/src/utilfunctions/checkWGS84Projection.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
Expand Down

0 comments on commit 1c5420b

Please sign in to comment.