Skip to content

Commit

Permalink
refactoring of coordinates parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mathe05 committed Nov 20, 2024
1 parent c7f451b commit 19e2f09
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions src/packages/coordinate-search/CoordinateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ function validateInput(
}
}

const coords = parseCoords(inputString, thousandSeparator);
const coords = parseCoords(inputString, intl);

try {
if (!checkIfCoordsInProjectionsExtent(projection, coords)) return "tooltip.extent";
Expand Down Expand Up @@ -651,7 +651,10 @@ function checkIfCoordsInProjectionsExtent(projection: Projection, coords: number
);
}

function parseCoords(inputString: string, thousandSeparator: string) {
// TODO: use NumberParser from core
/* transforms the given coordinates to the given destination projection */
function parseCoords(inputString: string, intl: PackageIntl) {
const thousandSeparator = /^de-?/.test(intl.locale) ? "." : /^en-?/.test(intl.locale) ? "," : "";
const inputStringWithoutThousandSeparator = inputString.replaceAll(thousandSeparator, "");

const coordsString = inputStringWithoutThousandSeparator.replaceAll(",", ".");
Expand All @@ -675,37 +678,14 @@ function onCoordinateInput(
)
return;

// TODO: Number parse from core and unify with the validation code above
let inputStringWithoutHundredDivider = coordinateString;
if (/^de-?/.test(intl.locale)) {
inputStringWithoutHundredDivider = coordinateString.replaceAll(".", "");
} else if (/^en-?/.test(intl.locale)) {
inputStringWithoutHundredDivider = coordinateString.replaceAll(",", "");
}
const coordsForZoom = getCoordsForZoom(
inputStringWithoutHundredDivider,
projection,
mapProjection
);
const inputCoords = parseCoords(coordinateString, intl);
const coords = transformCoordinates(inputCoords, projection, mapProjection);

if (onSelect && mapProjection) {
onSelect({ coords: coordsForZoom, projection: mapProjection });
onSelect({ coords: coords, projection: mapProjection });
}
}

/* returns the given coordinates in the projection of the map */
function getCoordsForZoom(
coordinateString: string,
projection: Projection | undefined,
mapProjection: Projection | undefined
): Coordinate {
const coordsString = coordinateString.split(" ");
const coords = [
parseFloat(coordsString[0]!.replace(",", ".")),
parseFloat(coordsString[1]!.replace(",", "."))
];
return transformCoordinates(coords, projection, mapProjection);
}

/* transforms the given coordinates to the given destination projection */
function transformCoordinates(
coordinates: number[],
Expand Down

0 comments on commit 19e2f09

Please sign in to comment.