Skip to content

Commit

Permalink
geosolutions-it#10136: Search for Map CRS coordinates (geosolutions-i…
Browse files Browse the repository at this point in the history
…t#10305)

* geosolutions-it#10136: Search for Map CRS coordinates
Description:
- resolve a threshold in CRS coordinate in switch

* geosolutions-it#10136: Search for Map CRS coordinates
Description:
- resolve not update the X/Y coods in case switch between map crs by storing the currentMapCRS into coordinate object
  • Loading branch information
mahmoudadel54 authored May 14, 2024
1 parent 99a8d5b commit d8ab7af
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
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

0 comments on commit d8ab7af

Please sign in to comment.