Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid geojson #1112

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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