From 6ed986bfc3773b6c1267feaaaf8dbe437fa71d3a Mon Sep 17 00:00:00 2001 From: Sander Vocke Date: Thu, 6 Feb 2020 17:22:40 +0100 Subject: [PATCH] Got continents working. Very slow though - should simplify them using e.g. Concaveman. --- src/geo_store.js | 52 ++++++++++++++++++++++++++++++++++++++---- src/userquerywidget.js | 12 ++++++---- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/geo_store.js b/src/geo_store.js index 6ec155f..ea18fff 100644 --- a/src/geo_store.js +++ b/src/geo_store.js @@ -7,6 +7,7 @@ var g_GeoStore = { areas: {}, area_query_results: {}, image_index: {}, + static_areas: {}, }; export function hash_geo_area(geo_area) { @@ -147,10 +148,51 @@ export function image_in_area(image_id, area_hash) { export function load_static_geo_polygons() { return new Promise((resolve, reject) => { fetch('/continent_polygons.geojson') - .then(res => res.json()) - .then(json => { - console.log("Continents:", json); - resolve(json); - }); + .then(res => res.json()) + .then(json => { + // The continents are stored as an array of features. Change it to a + // dictionary of name to geometry. + json.features.forEach(feature => { + g_GeoStore.static_areas[feature.properties.continent] = feature.geometry; + }); + resolve(); + }); }); +} + +export function query_geometry(query) { + return new Promise((resolve, reject) => { + const query_words = query.toLowerCase().trim().split(/\s+/); + for(var key in g_GeoStore.static_areas) { + const static_words = key.toLowerCase().trim().split(/\s+/); + if(query_words.length == static_words.length) { + var match = true; + for(let i=0; i res.json()) + .then(jsonres => { + if (Array.isArray(jsonres) && jsonres.length > 0) { + resolve(jsonres[0]); + } + resolve(false); + return; + }) + .catch(e => reject(e)); + }) } \ No newline at end of file diff --git a/src/userquerywidget.js b/src/userquerywidget.js index 76efcaa..525c9a2 100644 --- a/src/userquerywidget.js +++ b/src/userquerywidget.js @@ -34,6 +34,7 @@ import { TimeFilter, ImageTypeFilter, ImageTypeEnum, LocationFilter } from './queries.js' import { Typography } from '@material-ui/core'; +import { query_geometry } from './geo_store.js'; const useStyles = makeStyles(theme => ({ root: {}, @@ -90,11 +91,12 @@ export function EditLocationFilterExpression(props) { return; } - fetch("https://nominatim.openstreetmap.org/search?polygon_geojson=1&polygon_threshold=0.001&format=json&limit=5&q=" + query) - .then(res => res.json()) - .then(jsonres => { - if (Array.isArray(jsonres) && jsonres.length > 0) { - updateProposal(jsonres[0]); + // Do a geometry query. + query_geometry(query) + .then(result => { + console.log("Query result:", result); + if (result) { + updateProposal(result); } }); }