Skip to content

Commit

Permalink
PB-1141 : update layer list in FAQ
Browse files Browse the repository at this point in the history
replace the use of dedicated "faqlist" endpoint by the standard "layersConfig" (and removing the now unused faqlist endpoint)

Adding the layers' type (WMS/WMTS/etc...) to the table
  • Loading branch information
pakb committed Nov 11, 2024
1 parent 513a20c commit 6351f3d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 98 deletions.
1 change: 0 additions & 1 deletion chsdi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def datetime_adapter(obj, request):
config.add_route('translations', '/rest/services/translations', request_method=request_method)

config.add_route('stationboard', '/stationboard/stops/{id}', request_method=request_method)
config.add_route('faqlist', '/rest/services/{map}/faqlist', request_method=request_method)
config.add_route('color', '/color/{r},{g},{b}/{image}', request_method=request_method)

# Static route
Expand Down
77 changes: 51 additions & 26 deletions chsdi/static/doc/source/api/faq/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,59 @@ The following list contains all the free accessible layers:

.. raw:: html

<script type="text/javascript">
function init() {
$.getJSON( "../../rest/services/api/faqlist", function( data ) {
var layersApi = data;
var translationsApi = data.translations;
var names = ["notChargeableLayers", "tooltipLayers", "searchableLayers", "queryableLayers"];
for (var index=0; index < names.length; index++) {
var name = names[index];
var newInner = "<br><table border=\"0\">";
var counter = 1;
for (var layer in layersApi[name]) {
newInner += '<tr><td>' + counter + '</td><td><a href="http://map3.geo.admin.ch/?layers=' +
layersApi[name][layer] + '" target="new"> ' + layersApi[name][layer] + '</a>&nbsp('+translationsApi[layersApi[name][layer]]+')</td></tr>';
counter++;
<script type="text/javascript">
function init() {
const sections = [
{
identifier: 'notChargeableLayers',
filter: (layer) => !layer.chargeable,
},
{
identifier: 'tooltipLayers',
filter: (layer) => !!layer.tooltip
},
{
identifier: 'searchableLayers',
filter: (layer) => !!layer.searchable
},
{
identifier: 'queryableLayers',
filter: (layer) => layer.queryableAttributes?.length > 0
}
]
$.getJSON( "../../rest/services/all/MapServer/layersConfig", function( layersConfig ) {
const allLayers = layersConfig.keys().map((layerId) => {
return {
id: layerId,
...layersConfig[layerId],
}
})
// sorting all layers alphabetically by ID
allLayers.sort((layer1, layer2) => layer1.id.localeCompare(layer2.id))
sections.forEach((section) => {
const layers = allLayers.filter(section.filter)
let layerTable = `<br /><table style="border: none;">`
layerTable += layers.map((layer, counter) => {
let layerRow = `<tr>`
layerRow += `<td>${counter}</td>`
layerRow += `<td><a target="_blank" href="https://map.geo.admin.ch/?layers=${layer.id}">${layer.id}</a>&nbsp;${layer.label}</td>`
layerRow += `<td style="text-transform: capitalize">${layer.type}</td>`
layerRow += `</tr>`
return layerRow
})
layerTable += `</table>`
const sectionElement = document.getElementById(section.identifier)
if (sectionElement) {
sectionElement.innerHTML = layerTable
}
})
});
}
if (document.getElementById(name)) {
document.getElementById(name).innerHTML = newInner;
}
newInner = [];
}
});
}
</script>
</script>
<body onload="init();">
</body>
<body onload="init();">
</body>
.. _querybale_layers:
Expand Down
45 changes: 0 additions & 45 deletions chsdi/views/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,51 +147,6 @@ def insert_value_at(field, attrName, value):
return {'id': layerId, 'name': params.translate(layerId), 'fields': fields}


@view_config(route_name='faqlist', renderer='jsonp')
def faqlist(request):
params = BaseLayersValidation(request)
params.geodataStaging = 'prod'
translations = {}

# That there is a tooltip
tooltipLayers = []
# That you can search in layer search
searchableLayers = []
# That you need to pay for
chargeableLayers = []
# Free layers
notChargeableLayers = []
# queryable layer (filtering with where and layerDefs)
queryableLayers = []

query = params.request.db.query(LayersConfig)
for layer in get_layers_config_for_params(params, query, LayersConfig):
k = list(layer.keys()).pop()
lyr = list(layer.values()).pop()
if 'parentLayerId' not in lyr and not k.endswith('_3d'):
if k not in translations:
translations[k] = request.translate(k)
if 'tooltip' in lyr and lyr['tooltip'] and 'type' in lyr and lyr['type'] not in ('geojson',):
tooltipLayers.append(k)
if 'queryableAttributes' in lyr and lyr['queryableAttributes']:
queryableLayers.append(k)
if 'searchable' in lyr and lyr['searchable']:
searchableLayers.append(k)
if 'chargeable' in lyr and lyr['chargeable']:
chargeableLayers.append(k)
if 'chargeable' in lyr and not lyr['chargeable']:
notChargeableLayers.append(k)

return {
'translations': translations,
'tooltipLayers': sorted(tooltipLayers),
'searchableLayers': sorted(searchableLayers),
'chargeableLayers': sorted(chargeableLayers),
'notChargeableLayers': sorted(notChargeableLayers),
'queryableLayers': sorted(queryableLayers)
}


def _has_legend(layerId, lang):
legendsDir = os.path.join(os.path.dirname(__file__), '../static/images/legends')
image = "%s_%s.png" % (layerId, lang)
Expand Down
26 changes: 0 additions & 26 deletions tests/integration/test_layers_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,6 @@ def test_metadata_chargeable_false(self):
resp = self.testapp.get('/rest/services/blw/MapServer', params={'chargeable': 'false'}, status=200)
self.assertEqual(resp.content_type, 'application/json')

def test_faqlist(self):
resp = self.testapp.get('/rest/services/api/faqlist', status=200)
self.assertEqual(resp.content_type, 'application/json')
self.assertIn('ch.astra.ivs-nat', resp.json['searchableLayers'])
self.assertIn('ch.bafu.bundesinventare-bln', resp.json['tooltipLayers'])
self.assertIn('ch.swisstopo-karto.hangneigung', resp.json['chargeableLayers'])
self.assertIn('ch.are.alpenkonvention', resp.json['notChargeableLayers'])
self.assertGreater(len(resp.json['searchableLayers']), 20)
self.assertGreater(len(resp.json['tooltipLayers']), 20)
self.assertGreater(len(resp.json['chargeableLayers']), 20)
self.assertGreater(len(resp.json['notChargeableLayers']), 20)
self.assertGreater(len(resp.json['translations']), 20)

def test_faqlist_topic_all(self):
resp = self.testapp.get('/rest/services/all/faqlist', status=200)
self.assertEqual(resp.content_type, 'application/json')
self.assertIn('ch.astra.ivs-nat', resp.json['searchableLayers'])
self.assertIn('ch.bafu.bundesinventare-bln', resp.json['tooltipLayers'])
self.assertIn('ch.swisstopo-karto.hangneigung', resp.json['chargeableLayers'])
self.assertIn('ch.are.alpenkonvention', resp.json['notChargeableLayers'])
self.assertGreater(len(resp.json['searchableLayers']), 20)
self.assertGreater(len(resp.json['tooltipLayers']), 20)
self.assertGreater(len(resp.json['chargeableLayers']), 20)
self.assertGreater(len(resp.json['notChargeableLayers']), 20)
self.assertGreater(len(resp.json['translations']), 20)

def test_legend_valid(self):
resp = self.testapp.get('/rest/services/ech/MapServer/ch.bafu.bundesinventare-bln/legend', status=200)
self.assertEqual(resp.content_type, 'text/html')
Expand Down

0 comments on commit 6351f3d

Please sign in to comment.