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 search for coord with reprojection #485

Merged
merged 3 commits into from
Oct 29, 2019
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
39 changes: 23 additions & 16 deletions packages/geo/src/lib/map/shared/map.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export function stringToLonLat(str: string, mapProjection: string): {lonLat: [nu
const dmsCoordPattern = `${dmsCoord}([N|S]),?\\s*${dmsCoord}([E|W])`;
const dmsRegex = new RegExp(`^${dmsCoordPattern}`, 'gi');

const patternUtmMtm = '(UTM|MTM)\-?(\\d{1,2})[\\s,.]*(\\d+[\\s.,]?\\d+)[\\s,.]+(\\d+[\\s.,]?\\d+)';
const utmMtmRegex = new RegExp(`^${patternUtmMtm}`, 'gi');
const patternUtm = '(UTM)\-?(\\d{1,2})[\\s,.]*(\\d+[\\s.,]?\\d+)[\\s,.]+(\\d+[\\s.,]?\\d+)';
const utmRegex = new RegExp(`^${patternUtm}`, 'gi');

const ddCoord = '([-+])?(\\d{1,3})[,.](\\d+)';
const patternDd = `${ddCoord}[,.]?\\s*${ddCoord}`;
Expand All @@ -73,8 +73,9 @@ export function stringToLonLat(str: string, mapProjection: string): {lonLat: [nu
const mmPattern = `${mmCoord}[\\s,.]\\s*${mmCoord}`;
const mmRegex = new RegExp(`^${mmPattern}$`, 'g');

str = str.toLocaleUpperCase();
let isXYCoords = false;

str = str.toLocaleUpperCase().trim();
// Extract projection
if (projectionRegex.test(str)) {
[coordStr, projectionStr] = str.split(';');
Expand Down Expand Up @@ -111,8 +112,9 @@ export function stringToLonLat(str: string, mapProjection: string): {lonLat: [nu
lon = convertDMSToDD(parseFloat(degreesLon), parseFloat(minutesLon), parseFloat(secondsLon), directionLon);
lat = convertDMSToDD(parseFloat(degreesLat), parseFloat(minutesLat), parseFloat(secondsLat), directionLat);

} else if (utmMtmRegex.test(coordStr)) {
[, pattern, timeZone, lon, lat] = coordStr.match(patternUtmMtm);
} else if (utmRegex.test(coordStr)) {
isXYCoords = true;
[, pattern, timeZone, lon, lat] = coordStr.match(patternUtm);
const utm = '+proj=' + pattern + ' +zone=' + timeZone;
const wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs';
[lon, lat] = proj4(utm.toLocaleLowerCase(), wgs84, [parseFloat(lon), parseFloat(lat)]);
Expand Down Expand Up @@ -181,6 +183,7 @@ export function stringToLonLat(str: string, mapProjection: string): {lonLat: [nu
}

} else if (mmRegex.test(coordStr)) {
isXYCoords = true;
[, lon, decimalLon, lat, decimalLat] = coordStr.match(mmPattern);

if (decimalLon) {
Expand All @@ -195,20 +198,24 @@ export function stringToLonLat(str: string, mapProjection: string): {lonLat: [nu
return {lonLat: undefined, message: '', radius: undefined, conf: undefined};
}

// Set a negative coordinate for North America zone
if (lon > 0 && lat > 0) {
if (lon > lat) {
lon = -lon;
} else {
lat = -lat;
if (!isXYCoords) {
// Set a negative coordinate for North America zone
if (lon > 0 && lat > 0) {
if (lon > lat) {
lon = -lon;
} else {
lat = -lat;
}
}
}

// Reverse coordinate to respect lonLat convention
if (lon < lat) {
lonLat = [lon, lat] as [number, number];
// Reverse coordinate to respect lonLat convention
if (lon < lat) {
lonLat = [lon, lat] as [number, number];
} else {
lonLat = [lat, lon] as [number, number];
}
} else {
lonLat = [lat, lon] as [number, number];
lonLat = [lon, lat] as [number, number];
}

// Reproject the coordinate if projection parameter have been set and coord is not 4326
Expand Down
6 changes: 3 additions & 3 deletions packages/geo/src/lib/search/shared/sources/coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class CoordinatesReverseSearchSource extends SearchSource
extent: undefined,
properties: {
type: 'point',
coordonnees: String(data[0]) + ', ' + String(data[1]),
coordonnees: String(data[0].toFixed(5)) + ', ' + String(data[1].toFixed(5)),
format: 'degrés decimaux',
systemeCoordonnees: 'WGS84',
GoogleMaps: GoogleLinks.getGoogleMapsLink(data[0], data[1]),
Expand All @@ -96,13 +96,13 @@ export class CoordinatesReverseSearchSource extends SearchSource
},
meta: {
id: '1',
title: String(data[0]) + ', ' + String(data[1])
title: String(data[0].toFixed(5)) + ', ' + String(data[1].toFixed(5))
}
},
meta: {
dataType: FEATURE,
id: '1',
title: String(data[0]) + ', ' + String(data[1]),
title: String(data[0].toFixed(5)) + ', ' + String(data[1].toFixed(5)),
icon: 'map-marker'
}
};
Expand Down