Skip to content

Commit

Permalink
Merge pull request #1070 from RobAndrewHurst/gazetteer_bug
Browse files Browse the repository at this point in the history
Provided missing gazetteer to Coordinates Entry
  • Loading branch information
RobAndrewHurst authored Jan 25, 2024
2 parents f1b9f8f + a893046 commit 73527a9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 41 deletions.
13 changes: 7 additions & 6 deletions lib/layer/decorate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export default async layer => {

// Spread in defaults.
...params?.defaults,
...layer.draw?.defaults})
...layer.draw?.defaults
})
})

// Check whether feature is loaded on MVT update.
Expand All @@ -79,9 +80,9 @@ export default async layer => {
setTimeout(checkFeature, 1000)

function checkFeature() {

let found = layer.features?.find(F => F.properties?.id === location.id)

if (found) {

layer.source.un('tileloadend', concatFeatures);
Expand Down Expand Up @@ -134,7 +135,7 @@ export default async layer => {

// Keep object theme.
layer.style.theme = typeof layer.style.theme === 'object'

? layer.style.theme

// Assign theme from key [string], or first theme.
Expand All @@ -148,7 +149,7 @@ export default async layer => {
layer.style.label = layer.style.labels[layer.style.theme.setLabel]
}

// Warn if outdated layer.hover configuration is used.
// Warn if outdated layer.hover configuration is used.
// Set layer.style.hover and remove layer.hover.
if (layer.hover) {

Expand Down Expand Up @@ -252,7 +253,7 @@ export default async layer => {
// Or plugin method and provide the layer object as argument.
typeof mapp.plugins[key] === 'function' && mapp.plugins[key]?.(layer);

// It is possible to have a plugin method of the same name as a layer method.
// It is possible to have a plugin method of the same name as a layer method.
});

return layer;
Expand Down
12 changes: 4 additions & 8 deletions lib/location/decorate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,12 @@ async function syncFields(fields) {
}

function flyTo(maxZoom) {

const sourceVector = new ol.source.Vector();

this.Layers.forEach(layer => {

const source = layer.getSource()

typeof source.getFeatures === 'function'
&& sourceVector.addFeatures(source.getFeatures())
})
this.Layers.forEach((layer) => {
const source = layer.getSource();
typeof source.getFeatures === 'function' && sourceVector.addFeatures(source.getFeatures());
});

this.layer.mapview.fitView(sourceVector.getExtent(), {
maxZoom
Expand Down
18 changes: 7 additions & 11 deletions lib/mapview/fitView.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function (extent, opt_options = {}){
export default function (extent, options = {}){
/**
* Fits the view to a given extent
* @param {Array} extent - geo extent, array of 4 numbers, must be [minX, minY, maxX, maxY]
* @param {Object} opt_options - Defaults to empty object
* @param {Object} options - Defaults to empty object
*/

// Extent must be an array of finite values.
Expand All @@ -11,13 +11,9 @@ export default function (extent, opt_options = {}){
return;
}

this.Map.getView().fit(
extent,
Object.assign(
{
padding: [50, 50, 50, 50],
duration: 1000
},
opt_options)
)
this.Map.getView().fit(extent, {
padding: [50, 50, 50, 50],
duration: 1000,
...options
})
}
52 changes: 37 additions & 15 deletions lib/ui/Gazetteer.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
export default gazetteer => {

mapp.utils.merge(mapp.dictionaries, {
en: {
invalid_lat_long_range: 'Invalid coordinates: Latitude and longitude values must be within valid ranges.',
invalid_lat_lon: 'The provided Coordinates do not fall within the selected Locale.'
},
de: {
invalid_lat_long_range: 'Falsche Eingabe von Latitude / Longitude.',
invalid_lat_lon: 'Koordinate liegt außerhalb der Lokale.'
}
})

gazetteer.input = mapp.utils.html.node`<input
type="search"
placeholder=${gazetteer.placeholder}>`
Expand Down Expand Up @@ -27,24 +38,35 @@ export default gazetteer => {
// Get possible coordinates from input.
let ll = e.target.value.split(',').map(parseFloat)

// Check whether coordinates are float values.
if (ll.length === 2 && ll.every(n => !isNaN(n))) {
// Check whether coordinates are valid float values.
if (ll.length === 2 && ll.every(n => typeof n === 'number' && !isNaN(n) && isFinite(n))) {

// Check if both coordinates are within valid range (latitude: -90 to 90, longitude: -180 to 180).
const [lat, lng] = ll;

gazetteer.list.appendChild(mapp.utils.html.node`
<li
onclick=${e => {
if (lat >= -90 && lat <= 90 && lng >= -180 && lng <= 180) {
gazetteer.list.appendChild(mapp.utils.html.node`
<li onclick=${e => {
mapp.utils.gazetteer.getLocation({
label: `Latitude:${ll[0]}, Longitude:${ll[1]}`,
source: 'Coordinates',
lng: ll[1],
lat: ll[0]
}, gazetteer);
}}><span>Latitude:${ll[0]}, Longitude:${ll[1]}</span>`);

mapp.utils.gazetteer.getLocation({
label: `Latitude:${ll[0]}, Longitude:${ll[1]}`,
source: 'Coordinates',
lng: ll[1],
lat: ll[0]
})
// Do not search if coordinates are provided.
return;

}}><span>Latitude:${ll[0]}, Longitude:${ll[1]}`)
} else {

// Do not search if coordinates are provided.
return;
// Handle the case where coordinates are not within valid ranges.
// Add the error as a list item
gazetteer.list.appendChild(mapp.utils.html.node`
<li style="color: red;">
<span>${mapp.dictionary.invalid_lat_long_range}</span>`);

}
}

// An external gazetteer provider is requested
Expand All @@ -64,5 +86,5 @@ export default gazetteer => {
// Request datasets gazetteer
mapp.utils.gazetteer.datasets(e.target.value, gazetteer)
}

}
16 changes: 15 additions & 1 deletion lib/utils/gazetteer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export function datasets(term, gazetteer) {
if (!gazetteer.provider) {

// The default gazetteer config is for a dataset search.
// Abort current dataset query. Onload will not be called.
gazetteer.xhr?.abort()
gazetteer.xhr = new XMLHttpRequest()
gazetteer.qterm && search(gazetteer)
}

Expand Down Expand Up @@ -46,7 +49,7 @@ export function datasets(term, gazetteer) {
console.warn('No table definition for gazetteer search.')
return;
};

dataset.xhr.open('GET', gazetteer.mapview.host + '/api/query?' +
mapp.utils.paramString({
template: dataset.query || 'gaz_query',
Expand Down Expand Up @@ -112,6 +115,17 @@ export function getLocation(location, gazetteer) {
return;
}

const coord = ol.proj.transform(
[location.lng, location.lat],
`EPSG:4326`,
`EPSG:${gazetteer.mapview.srid}`
)

if (!ol.extent.containsCoordinate(gazetteer.mapview.extent, coord)) {
alert(mapp.dictionary.invalid_lat_lon);
return;
}

Object.assign(location, {
layer: {
mapview: gazetteer.mapview
Expand Down

0 comments on commit 73527a9

Please sign in to comment.