Skip to content
Open
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
52 changes: 37 additions & 15 deletions specifyweb/frontend/js_src/lib/components/WbToolkit/GeoLocate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { useBooleanState } from '../../hooks/useBooleanState';
import { commonText } from '../../localization/common';
import { localityText } from '../../localization/locality';
import { wbText } from '../../localization/workbench';
import { Coord } from '../../utils/latLong';
import { f } from '../../utils/functools';
import type { IR, RA } from '../../utils/types';
import { filterArray } from '../../utils/types';
import { sortFunction } from '../../utils/utils';
import { Button } from '../Atoms/Button';
import { getLocalityField } from '../Leaflet/helpers';
import {
getLocalityCoordinate,
getSelectedLocalityColumns,
} from '../Leaflet/wbLocalityDataExtractor';
import type { GeoLocatePayload } from '../Molecules/GeoLocate';
Expand Down Expand Up @@ -255,24 +256,45 @@ function getGeoLocateData(
readonly visualRow: number;
}
): IR<string> {
const visualHeaders = getVisualHeaders(hot, columns);
return buildGeoLocateData(
hot.getDataAtRow(visualRow),
getVisualHeaders(hot, columns),
localityColumns
);
}

export function buildGeoLocateData(
row: RA<string>,
headers: RA<string>,
localityColumns: IR<string>
): IR<string> {
const getValue = (fieldName: string): string =>
getLocalityField(row, headers, localityColumns, fieldName);

const parseCoordinate = (value: string): number | undefined => {
const trimmedValue = value.trim();
if (trimmedValue === '') return undefined;
if (trimmedValue === '0') return 0;

const parsedCoordinate = Coord.parse(trimmedValue)?.toDegs();
return parsedCoordinate === undefined
? undefined
: parsedCoordinate.components[0] * parsedCoordinate.sign;
};

const localityData =
getLocalityCoordinate(
hot.getDataAtRow(visualRow),
visualHeaders,
localityColumns
) || {};
const latitude = getValue('locality.latitude1');
const longitude = getValue('locality.longitude1');
const parsedLatitude = parseCoordinate(latitude);
const parsedLongitude = parseCoordinate(longitude);

const rawData = {
country: localityData['locality.geography.$country.name']?.value,
state: localityData['locality.geography.$state.name']?.value,
county: localityData['locality.geography.$county.name']?.value,
locality: localityData['locality.localityname']?.value,
country: getValue('locality.geography.$country.name') || undefined,
state: getValue('locality.geography.$state.name') || undefined,
county: getValue('locality.geography.$county.name') || undefined,
locality: getValue('locality.localityname') || undefined,
points:
typeof localityData['locality.latitude1'] === 'object' &&
typeof localityData['locality.longitude1'] === 'object'
? `${localityData['locality.latitude1'].value}|${localityData['locality.longitude1'].value}`
parsedLatitude !== undefined && parsedLongitude !== undefined
? `${parsedLatitude}|${parsedLongitude}`
: undefined,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { buildGeoLocateData } from '../GeoLocate';

test('keeps geography and locality when coordinates are blank', () => {
const headers = ['Country', 'State', 'County', 'Locality', 'Lat', 'Lon'];
const localityColumns = {
'locality.geography.$country.name': 'Country',
'locality.geography.$state.name': 'State',
'locality.geography.$county.name': 'County',
'locality.localityname': 'Locality',
'locality.latitude1': 'Lat',
'locality.longitude1': 'Lon',
};

expect(
buildGeoLocateData(
['USA', 'Kansas', 'Douglas', 'Prairie Park', '', ''],
headers,
localityColumns
)
).toEqual({
country: 'USA',
state: 'Kansas',
county: 'Douglas',
locality: 'Prairie Park',
});
});

test('includes formatted point data when coordinates are present', () => {
const headers = ['Country', 'Locality', 'Lat', 'Lon'];
const localityColumns = {
'locality.geography.$country.name': 'Country',
'locality.localityname': 'Locality',
'locality.latitude1': 'Lat',
'locality.longitude1': 'Lon',
};

expect(
buildGeoLocateData(
['USA', 'Prairie Park', '38:58:48 N', '95:14:24 W'],
headers,
localityColumns
)
).toEqual({
country: 'USA',
locality: 'Prairie Park',
points: '38.98|-95.24',
});
});
Loading