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 coordinate update #1771

Merged
merged 2 commits into from
Dec 19, 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
40 changes: 40 additions & 0 deletions src/client/cypress/e2e/detailPage/coordinates.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,44 @@ describe("Tests for editing coordinates of a borehole.", () => {
checkDecimalPlaces("@LV03X-input", 5);
checkDecimalPlaces("@LV03Y-input", 5);
});

it("updates canton and municipality when changing coordinates", () => {
// Type coordinates for Samaden in LV95
cy.get("@LV95X-input").type("2789000");
cy.get("@LV95Y-input").type("1155000");
cy.wait("@location");
cy.wait(4000);

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Graubünden");
cy.get("@municipality").should("have.value", "Samaden");

// Type coordinates for Unterentfelden in LV95
cy.get("@LV95X-input").clear().type("2646000");
cy.get("@LV95Y-input").clear().type("1247000");

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Aargau");
cy.get("@municipality").should("have.value", "Unterentfelden");

// switch reference system to LV03
setSelect("originalReferenceSystem", 1);
handlePrompt("Changing the coordinate system will reset the coordinates. Do you want to continue?", "Confirm");

// Type coordinates for Samaden in LV03
cy.get("@LV03X-input").clear().type("789000");
cy.get("@LV03Y-input").clear().type("155000");

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Graubünden");
cy.get("@municipality").should("have.value", "Samaden");

// Type coordinates for Unterentfelden in LV03
cy.get("@LV03X-input").clear().type("646000");
cy.get("@LV03Y-input").clear().type("247000");

cy.get("@country").should("have.value", "Schweiz");
cy.get("@canton").should("have.value", "Aargau");
cy.get("@municipality").should("have.value", "Unterentfelden");
});
});
22 changes: 15 additions & 7 deletions src/client/src/pages/detail/form/location/locationSegment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from "react";
import { useCallback, useState } from "react";
import { UseFormReturn } from "react-hook-form";
import { Card, Grid, Stack } from "@mui/material";
import { fetchApiV2 } from "../../../../api/fetchApiV2";
Expand All @@ -17,6 +17,8 @@ interface LocationSegmentProps extends LocationBaseProps {
}

const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMethods }: LocationSegmentProps) => {
const [currentLV95X, setCurrentLV95X] = useState(borehole.locationX ? Number(borehole.locationX) : null);
const [currentLV95Y, setCurrentLV95Y] = useState(borehole.locationY ? Number(borehole.locationY) : null);
const transformCoordinates = useCallback(async (referenceSystem: string, x: number, y: number) => {
let apiUrl;
if (referenceSystem === referenceSystems.LV95.name) {
Expand Down Expand Up @@ -70,12 +72,18 @@ const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMeth
if (!response) return; // Ensure response is valid

const maxPrecision = Math.max(XPrecision, YPrecision);
const transformedX = parseFloat(response.easting).toFixed(maxPrecision);
const transformedY = parseFloat(response.northing).toFixed(maxPrecision);
const transformedX = parseFloat(response.easting);
const transformedY = parseFloat(response.northing);

const location = await fetchApiV2(`location/identify?east=${X}&north=${Y}`, "GET");
const XLV95 = sourceSystem === ReferenceSystemKey.LV95 ? X : transformedX;
const YLV95 = sourceSystem === ReferenceSystemKey.LV95 ? Y : transformedY;

setCurrentLV95X(XLV95);
setCurrentLV95Y(YLV95);

const location = await fetchApiV2(`location/identify?east=${XLV95}&north=${YLV95}`, "GET");
setValuesForCountryCantonMunicipality(location);
setValuesForReferenceSystem(targetSystem, transformedX, transformedY);
setValuesForReferenceSystem(targetSystem, transformedX.toFixed(maxPrecision), transformedY.toFixed(maxPrecision));
},
[setValuesForCountryCantonMunicipality, setValuesForReferenceSystem, transformCoordinates],
);
Expand Down Expand Up @@ -130,8 +138,8 @@ const LocationSegment = ({ borehole, editingEnabled, labelingPanelOpen, formMeth
}}
id={borehole.id}
isEditable={editingEnabled}
x={borehole.locationX ? Number(borehole.locationX) : null}
y={borehole.locationY ? Number(borehole.locationY) : null}
x={currentLV95X}
y={currentLV95Y}
/>
</FormSegmentBox>
</Grid>
Expand Down
Loading