Skip to content

Commit

Permalink
Merge branch 'dev' into multilayer_active_editing
Browse files Browse the repository at this point in the history
  • Loading branch information
wlorenzetti committed Nov 24, 2023
2 parents 2f4edae + cab94f6 commit f4075cf
Show file tree
Hide file tree
Showing 17 changed files with 415 additions and 10 deletions.
8 changes: 8 additions & 0 deletions g3w-admin/base/settings/base_geo_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@
}
}

# Geocoding providers
GEOCODING_PROVIDERS = {
"nominatim": {
"label": "Nominatim (OSM)",
"url": "https://nominatim.openstreetmap.org/search"
},
}

2 changes: 1 addition & 1 deletion g3w-admin/client/static/client/css/app.min.css

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions g3w-admin/client/static/client/geocoding-providers/bing_places.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @file
* @since 3.9.0
*/

(function() {

const geocoding = initConfig.group.mapcontrols.geocoding || {};
const provider = document.currentScript.src.split('/').reverse()[0].replace('.js', '') || 'bing_places';

// skip when disabled
if (!provider in geocoding.providers) {
return;
}

/**
* @example https://dev.virtualearth.net/REST/v1/LocalSearch/?query={query}&userMapView={lat,lon,lat,lon}&key={BingMapsKey}
*
* @see https://learn.microsoft.com/en-us/bingmaps/rest-services/locations/local-search
*/
geocoding.providers[provider].fetch = async function(opts) {
const { XHR } = g3wsdk.core.utils;
const { vendorkeys } = g3wsdk.core.ApplicationState.keys;

// fallback to generic bing vendor key
vendorkeys[provider] = vendorkeys[provider] || vendorkeys.bing;

const key = undefined !== vendorkeys[provider] ? vendorkeys[provider] : opts && (new URL(opts.url)).searchParams.get('key');

// whether can fetch data from Bing Local Search API
if (!opts || !key) {
return Promise.reject();
}

const url = opts.url || 'https://dev.virtualearth.net/REST/v1/LocalSearch/';
const params = {
query: opts.query, // textual search
userMapView: [opts.extent[1], opts.extent[0], opts.extent[3], opts.extent[2]].join(','),
};

// get fallback key from url
if (undefined === vendorkeys.bing) {
params.key = key;
}

const response = await XHR.get({ url, params });

return {
provider,
label: 'Bing Places',
icon: undefined !== opts.icon ? opts.icon : 'poi',
results: 200 === response.statusCode
? response.resourceSets[0].resources
.filter(({ point: { coordinates } })=> ol.extent.containsXY(opts.extent, coordinates[1], coordinates[0]))
.map(result => {
return {
name: result.name,
lon: result.point.coordinates[1],
lat: result.point.coordinates[0],
type: result.entityType,
address: {
road: result.Address.addressLine,
postcode: result.Address.postalCode,
city: result.Address.locality,
state: result.Address.adminDistrict,
country: result.Address.countryRegion,
formatted: result.Address.formattedAddress,
},
bing: result,
};
})
: [],
};

};

})();
76 changes: 76 additions & 0 deletions g3w-admin/client/static/client/geocoding-providers/bing_streets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @file
* @since 3.9.0
*/

(function() {

const geocoding = initConfig.group.mapcontrols.geocoding || {};
const provider = document.currentScript.src.split('/').reverse()[0].replace('.js', '') || 'bing_streets';

// skip when disabled
if (!provider in geocoding.providers) {
return;
}

/**
* @example https://dev.virtualearth.net/REST/v1/Locations/?query={query}&userMapView={lat,lon,lat,lon}&key={BingMapsKey}
*
* @see https://learn.microsoft.com/en-us/bingmaps/rest-services/locations/find-a-location-by-query
*/
geocoding.providers[provider].fetch = async function(opts) {
const { XHR } = g3wsdk.core.utils;
const { vendorkeys } = g3wsdk.core.ApplicationState.keys;

// fallback to generic bing vendor key
vendorkeys[provider] = vendorkeys[provider] || vendorkeys.bing;

const key = undefined !== vendorkeys.bing ? vendorkeys.bing : opts && (new URL(opts.url)).searchParams.get('key');

// whether can fetch data from Bing Locations API
if (!opts || !key /*|| !active*/) {
return Promise.reject();
}

const url = opts.url || 'https://dev.virtualearth.net/REST/v1/Locations/';
const params = {
query: opts.query, // textual search
userMapView: [opts.extent[1], opts.extent[0], opts.extent[3], opts.extent[2]].join(','),
};

// get fallback key from url
if (undefined === vendorkeys.bing) {
params.key = key;
}

const response = await XHR.get({ url, params });

return {
provider,
label: 'Bing Streets',
icon: undefined !== opts.icon ? opts.icon : 'road',
results: 200 === response.statusCode
? response.resourceSets[0].resources
.filter(({ point: { coordinates } })=> ol.extent.containsXY(opts.extent, coordinates[1], coordinates[0]))
.map(result => {
return {
name: result.name,
lon: result.point.coordinates[1],
lat: result.point.coordinates[0],
type: result.entityType,
address: {
road: result.address.addressLine,
postcode: result.address.postalCode,
city: result.address.locality,
state: result.address.adminDistrict,
country: result.address.countryRegion,
formatted: result.address.formattedAddress,
},
bing: result,
};
})
: [],
};
};

})();
87 changes: 87 additions & 0 deletions g3w-admin/client/static/client/geocoding-providers/google.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @file
* @since 3.9.0
*/

(function() {

const geocoding = initConfig.group.mapcontrols.geocoding || {};
const provider = document.currentScript.src.split('/').reverse()[0].replace('.js', '') || 'google';

// skip when disabled
if (!provider in geocoding.providers) {
return;
}

let active = true;

geocoding.providers[provider].fetch = async function(opts) {

const { XHR } = g3wsdk.core.utils;
const { vendorkeys } = g3wsdk.core.ApplicationState.keys;

// fallback to generic google vendor key
vendorkeys[provider] = vendorkeys[provider] || vendorkeys.google;

const key = undefined !== vendorkeys[provider] ? vendorkeys[provider] : opts && (new URL(opts.url)).searchParams.get('key');

// whether can fetch data from Google Geocode API
if (!opts || !active || !key) {
return Promise.reject();
}

const url = opts.url || 'https://maps.googleapis.com/maps/api/geocode/json';
const params = {
address: opts.query, // textual search
bounds: [opts.extent[1], opts.extent[0], opts.extent[3], opts.extent[2]].join(','),
language: opts.lang,
};

// get fallback key from url
if (undefined === vendorkeys.bing) {
params.key = key;
}

const response = await XHR.get({ url, params });

// disable google provider on invalid API key
if (response.status === 'REQUEST_DENIED') {
active = false;
return Promise.reject();
}

return {
provider,
label: 'Google',
icon: undefined !== opts.icon ? opts.icon : 'poi',
results: 'OK' === response.status
? response.results
.filter(({ geometry: { location } })=> ol.extent.containsXY(opts.extent, location.lng, location.lat))
.map(result => {
let name, city, country;
result.address_components.forEach(({ types, long_name }) => {
if (types.find(t => 'route' === t)) name = long_name;
else if (types.find( t => 'locality' === t)) city = long_name;
else if (types.find( t => 'country' === t)) country = long_name
});
return {
lon : result.geometry.location.lng,
lat : result.geometry.location.lat,
address: {
name,
road: undefined,
postcode: '',
city,
state: undefined,
country,
formatted: result.display_name,
},
google: result,
};
})
: [],
};

};

})();
64 changes: 64 additions & 0 deletions g3w-admin/client/static/client/geocoding-providers/nominatim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @file
* @since 3.9.0
*/

(function() {

const geocoding = initConfig.group.mapcontrols.geocoding || {};
const provider = document.currentScript.src.split('/').reverse()[0].replace('.js', '') || 'nominatim';

// skip when disabled
if (!provider in geocoding.providers) {
return;
}

geocoding.providers[provider].fetch = async function(opts) {

const { XHR } = g3wsdk.core.utils;

if (!opts) {
return Promise.reject();
}

return {
provider,
label: 'Nominatim (OSM)',
icon: undefined !== opts.icon ? opts.icon : 'road',
results:
(
await XHR.get({
url: opts.url || 'https://nominatim.openstreetmap.org/search',
params: {
q: opts.query, // textual search
format: 'json',
addressdetails: 1,
limit: opts.limit || 10,
viewbox: opts.extent.join(','),
bounded: 1,
}
})
)
.filter(place => ol.extent.containsXY(opts.extent, place.lon, place.lat))
.map(result => ({
name: result.name,
lon: result.lon,
lat: result.lat,
type: result.type,
address: {
name: result.address.neighbourhood || '',
road: result.address.road || '',
city: result.address.city || result.address.town,
postcode: result.address.postcode,
state: result.address.state,
country: result.address.country,
formatted: result.display_name,
},
nominatim: result,
})
),
};

};

})();
1 change: 1 addition & 0 deletions g3w-admin/client/static/client/images/pushpin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion g3w-admin/client/static/client/js/app.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion g3w-admin/client/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def testGroupConfigApiView(self):
'extent': [-180.0, -90.0, 180.0, 90.0]
})
print(resp["group"]["mapcontrols"])
self.assertEqual(resp["group"]["mapcontrols"], ['zoom', 'zoombox', 'zoomtoextent', 'query', 'querybbox', 'querybypolygon', 'overview', 'scaleline', 'geolocation', 'streetview', 'nominatim', 'addlayers', 'length', 'area', 'mouseposition', 'scale'])
self.assertEqual(resp["group"]["mapcontrols"], {'zoom': {}, 'zoombox': {}, 'zoomtoextent': {}, 'query': {}, 'querybbox': {}, 'querybypolygon': {}, 'overview': {}, 'scaleline': {}, 'geolocation': {}, 'streetview': {}, 'nominatim': {}, 'addlayers': {}, 'length': {}, 'area': {}, 'mouseposition': {}, 'scale': {}})
self.assertEqual(resp["group"]["header_logo_img"], "logo_img/qgis-logo.png")
self.assertEqual(resp["group"]["name"], "Gruppo 1")
self.assertIsNone(resp["group"]["header_logo_link"])
Expand Down
20 changes: 17 additions & 3 deletions g3w-admin/core/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsCoordinateTransformContext
from copy import copy
import json


def update_serializer_data(serializer_data, data):
Expand Down Expand Up @@ -116,9 +117,6 @@ def to_representation(self, instance):
'extent': extent
}

# map controls
ret['mapcontrols'] = [mapcontrol.name for mapcontrol in instance.mapcontrols.all()]

# add projects to group
ret['projects'] = []
self.projects = {}
Expand Down Expand Up @@ -258,6 +256,22 @@ def to_representation(self, instance):
if layout_right_panel:
ret['layout']['rightpanel'] = layout_right_panel

# Mapcontrols
ret['mapcontrols'] = {}
for mapcontrol in instance.mapcontrols.all():
options = {}
if mapcontrol.name in ('nominatim', 'geocoding') and self.project.geocoding_providers:
options = {'providers': {}}
for gp in json.loads(self.project.geocoding_providers):
if gp in settings.GEOCODING_PROVIDERS:
options['providers'].update({
gp: settings.GEOCODING_PROVIDERS[gp]
})

ret['mapcontrols'].update({
mapcontrol.name: options
})

return ret

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion g3w-admin/editing/static/editing/js/plugin.js

Large diffs are not rendered by default.

Loading

0 comments on commit f4075cf

Please sign in to comment.