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

#10136: Search for Map CRS coordinates #10305

Merged
merged 2 commits into from
May 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Message from "../../I18N/Message";
import CoordinateEntry from "../../misc/coordinateeditors/CoordinateEntry";
import DropdownToolbarOptions from "../../misc/toolbar/DropdownToolbarOptions";
import { zoomAndAddPoint, changeCoord } from '../../../actions/search';
import { reproject} from '../../../utils/CoordinatesUtils';

/**
* CoordinateOptions for Search bar
Expand All @@ -26,7 +27,7 @@ import { zoomAndAddPoint, changeCoord } from '../../../actions/search';
export const CoordinateOptions = ({
clearCoordinates: (onClearCoordinatesSearch, onChangeCoord) =>{
onClearCoordinatesSearch({owner: "search"});
const clearedFields = ["lat", "lon", "xCoord", "yCoord"];
const clearedFields = ["lat", "lon", "xCoord", "yCoord", "currentMapXYCRS"];
const resetVal = '';
clearedFields.forEach(field => onChangeCoord(field, resetVal));
},
Expand Down Expand Up @@ -138,6 +139,7 @@ const CoordinatesSearch = ({
onZoomToPoint,
onChangeCoord,
defaultZoomLevel,
currentMapCRS,
aeronauticalOptions = {
seconds: {
decimals: 4,
Expand All @@ -161,9 +163,30 @@ const CoordinatesSearch = ({

const changeCoordinates = (coord, value) => {
onChangeCoord(coord, parseFloat(value));
// set current map crs to coordinate object
if (coordinate?.currentMapXYCRS !== currentMapCRS && currentMapCRS !== "EPSG:4326") onChangeCoord('currentMapXYCRS', currentMapCRS);
if (!areValidCoordinates()) {
onClearCoordinatesSearch({owner: "search"});
}
// if there is mapCRS available --> calculate X/Y values by reproject to display in case switch to MapCRS
if (currentMapCRS !== 'EPSG:4326') {
// if there are lat, lon values --> reproject the point and get xCoord and yCoord for map CRS
const latNumVal = coord === 'lat' ? parseFloat(value) : coordinate.lat;
const lonNumVal = coord === 'lon' ? parseFloat(value) : coordinate.lon;
const isLatNumberVal = isNumber(latNumVal) && !isNaN(latNumVal);
const isLonNumberVal = isNumber(lonNumVal) && !isNaN(lonNumVal);
if (isLatNumberVal && isLonNumberVal) {
const reprojectedValue = reproject([lonNumVal, latNumVal], 'EPSG:4326', currentMapCRS, true);
const parsedXCoord = parseFloat((reprojectedValue?.x));
const parsedYCoord = parseFloat((reprojectedValue?.y));
onChangeCoord('xCoord', parsedXCoord);
onChangeCoord('yCoord', parsedYCoord);

return;
}
coordinate.xCoord && onChangeCoord('xCoord', '');
coordinate.yCoord && onChangeCoord('yCoord', '');
}
};

const onZoom = () => {
Expand Down Expand Up @@ -226,7 +249,8 @@ CoordinatesSearch.propTypes = {
onClearCoordinatesSearch: PropTypes.func,
onZoomToPoint: PropTypes.func,
onChangeCoord: PropTypes.func,
defaultZoomLevel: PropTypes.number
defaultZoomLevel: PropTypes.number,
currentMapCRS: PropTypes.string
};

export default connect((state)=>{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,35 @@ const CurrentMapCRSCoordinatesSearch = ({
return true;
};
React.useEffect(() => {
if (!currentMapCRS || currentMapCRS === 'EPSG:4326') return;
// if there are lat, lon values --> reproject the point and get xCoord and yCoord for map CRS
const isLatNumberVal = isNumber(coordinate.lat) && !isNaN(coordinate.lat);
const isLonNumberVal = isNumber(coordinate.lon) && !isNaN(coordinate.lon);
if (isLatNumberVal && isLonNumberVal) {
const reprojectedValue = reproject([coordinate.lon, coordinate.lat], 'EPSG:4326', currentMapCRS, true);
const parsedXCoord = parseFloat((reprojectedValue?.x));
const parsedYCoord = parseFloat((reprojectedValue?.y));
onChangeCoord('xCoord', parsedXCoord);
onChangeCoord('yCoord', parsedYCoord);
// if coords are out of crs extent --> clear the marker
if (!isCoordWithinCrs(parsedXCoord, 'xCoord') || !isCoordWithinCrs(parsedYCoord, 'yCoord')) onClearCoordinatesSearch({owner: "search"});
return;
// if currentMapCRS = 4326 or undefined --> nothing to do
let prevCRS = coordinate?.currentMapXYCRS;
let currentCRS = currentMapCRS;
// set currentCRS to ref
if (!currentCRS || currentCRS === 'EPSG:4326') return;
// set current map crs to coordinate object
if (prevCRS !== currentMapCRS) onChangeCoord('currentMapXYCRS', currentMapCRS);
// if the current map crs is changed from one to another --> get new coords
if (currentCRS && prevCRS && prevCRS !== currentCRS) {

// if there are lat, lon values --> reproject the point and get xCoord and yCoord for map CRS
const isLatNumberVal = isNumber(coordinate.lat) && !isNaN(coordinate.lat);
const isLonNumberVal = isNumber(coordinate.lon) && !isNaN(coordinate.lon);
if (isLatNumberVal && isLonNumberVal) {
const reprojectedValue = reproject([coordinate.lon, coordinate.lat], 'EPSG:4326', currentCRS, true);
const parsedXCoord = parseFloat((reprojectedValue?.x));
const parsedYCoord = parseFloat((reprojectedValue?.y));
onChangeCoord('xCoord', parsedXCoord);
onChangeCoord('yCoord', parsedYCoord);
// if coords are out of crs extent --> clear the marker
if (!isCoordWithinCrs(parsedXCoord, 'xCoord') || !isCoordWithinCrs(parsedYCoord, 'yCoord')) onClearCoordinatesSearch({owner: "search"});
return;
}
coordinate.xCoord && onChangeCoord('xCoord', '');
coordinate.yCoord && onChangeCoord('yCoord', '');
}
coordinate.xCoord && onChangeCoord('xCoord', '');
coordinate.yCoord && onChangeCoord('yCoord', '');
// else just check the crs bounds
if (!isCoordWithinCrs(coordinate?.xCoord, 'xCoord') || !isCoordWithinCrs(coordinate?.yCoord, 'yCoord')) onClearCoordinatesSearch({owner: "search"});

}, [currentMapCRS]);

const changeCoordinates = (coord, value) => {
Expand All @@ -95,24 +108,15 @@ const CurrentMapCRSCoordinatesSearch = ({
const numValue = parseFloat(value);
onChangeCoord(coord, numValue);
// reproject the new point and set lat/lon
if (coord === 'yCoord') {
const yCoordValidNum = isNumber(numValue) && !isNaN(numValue) && isCoordWithinCrs(numValue, 'yCoord');
const xCoordValidNum = isNumber(coordinate.xCoord) && !isNaN(coordinate.xCoord) && isCoordWithinCrs(coordinate.xCoord, 'xCoord');
if (yCoordValidNum && xCoordValidNum) {
const projectedPt = reproject([coordinate.xCoord, numValue], currentMapCRS, 'EPSG:4326', true);
onChangeCoord('lat', (projectedPt.y));
onChangeCoord('lon', (projectedPt.x));
return;
}
} else {
const xCoordValidNum = isNumber(numValue) && !isNaN(numValue) && isCoordWithinCrs(numValue, 'xCoord');
const yCoordValidNum = isNumber(coordinate.yCoord) && !isNaN(coordinate.yCoord) && isCoordWithinCrs(coordinate.yCoord, 'yCoord');
if (yCoordValidNum && xCoordValidNum) {
const projectedPt = reproject([numValue, coordinate.yCoord], currentMapCRS, 'EPSG:4326', true);
onChangeCoord('lat', (projectedPt.y));
onChangeCoord('lon', (projectedPt.x));
return;
}
const yCoodNumVal = coord === 'yCoord' ? numValue : coordinate.yCoord;
const xCoodNumVal = coord === 'xCoord' ? numValue : coordinate.xCoord;
const yCoordValidNum = isNumber(yCoodNumVal) && !isNaN(yCoodNumVal) && isCoordWithinCrs(yCoodNumVal, 'yCoord');
const xCoordValidNum = isNumber(xCoodNumVal) && !isNaN(xCoodNumVal) && isCoordWithinCrs(xCoodNumVal, 'xCoord');
if (yCoordValidNum && xCoordValidNum) {
const projectedPt = reproject([xCoodNumVal, yCoodNumVal], currentMapCRS, 'EPSG:4326', true);
onChangeCoord('lat', (projectedPt.y));
onChangeCoord('lon', (projectedPt.x));
return;
}
const resetValue = '';
onChangeCoord('lat', resetValue);
Expand Down
Loading