From e0cbf35f12d4519bb877dc5afd102ec4505feb9d Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Sun, 8 Apr 2018 00:34:22 +0000 Subject: [PATCH] update item html templates, GeoJSON identifiers in test data --- pygeoapi-config.yml | 3 +- pygeoapi/api.py | 15 ++++ pygeoapi/log.py | 3 +- pygeoapi/provider/geojson.py | 25 ++++--- pygeoapi/provider/sqlite.py | 1 + pygeoapi/templates/items.html | 13 +++- tests/data/ne_110m_lakes.geojson | 124 +++++++++++++++---------------- 7 files changed, 105 insertions(+), 79 deletions(-) diff --git a/pygeoapi-config.yml b/pygeoapi-config.yml index c23c522..1f6a840 100644 --- a/pygeoapi-config.yml +++ b/pygeoapi-config.yml @@ -123,10 +123,9 @@ datasets: provider: name: GeoJSON data: tests/data/ne_110m_lakes.geojson - id_field: null # null indicates use feature enumeration + id_field: id countries: - id_field: ogc_fid # null indicates use feature enumeration title: Countries in the world description: Countries of the world keywords: diff --git a/pygeoapi/api.py b/pygeoapi/api.py index 0fc007c..677aafd 100644 --- a/pygeoapi/api.py +++ b/pygeoapi/api.py @@ -434,6 +434,18 @@ class API(object): return headers_, 200, json.dumps(content) +def to_json(dict_): + """ + serialize dict to json + + :param dict_: dict_ + + :returns: JSON string representation + """ + import json + return json.dumps(dict_) + + def _render_j2_template(config, template, data): """ render Jinja2 template @@ -446,5 +458,8 @@ def _render_j2_template(config, template, data): """ env = Environment(loader=FileSystemLoader(TEMPLATES)) + env.filters['to_json'] = to_json + env.globals.update(to_json=to_json) + template = env.get_template(template) return template.render(config=config, data=data, version=__version__) diff --git a/pygeoapi/log.py b/pygeoapi/log.py index cf02484..d3a134f 100644 --- a/pygeoapi/log.py +++ b/pygeoapi/log.py @@ -59,7 +59,8 @@ def setup_logger(logging_config): if 'logfile' in logging_config: logging.basicConfig(level=loglevel, datefmt=date_format, - format=log_format, filename=logging_config['logfile']) + format=log_format, + filename=logging_config['logfile']) else: logging.basicConfig(level=loglevel, datefmt=date_format, format=log_format, stream=sys.stdout) diff --git a/pygeoapi/provider/geojson.py b/pygeoapi/provider/geojson.py index 6d3deea..67324b4 100644 --- a/pygeoapi/provider/geojson.py +++ b/pygeoapi/provider/geojson.py @@ -27,12 +27,15 @@ # # ================================================================= -import os import json +import logging +import os import uuid from pygeoapi.provider.base import BaseProvider +LOGGER = logging.getLogger(__name__) + class GeoJSONProvider(BaseProvider): """Provider class backed by local GeoJSON files @@ -81,7 +84,8 @@ class GeoJSONProvider(BaseProvider): # Must be a FeatureCollection assert data['type'] == 'FeatureCollection' # All features must have ids, TODO must be unique strings - assert all(f.get('id') for f in data['features']) + for i in data['features']: + i['ID'] = i['properties'][self.id_field] return data @@ -115,13 +119,12 @@ class GeoJSONProvider(BaseProvider): """ all_data = self._load() for feature in all_data['features']: - if feature['id'] == identifier: - return { - 'type': 'FeatureCollection', - 'features': [feature]} + if str(feature['properties'][self.id_field]) == identifier: + return feature # default, no match - raise RuntimeError("Should be a 404 error") + LOGGER.error('feature {} not found'.format(identifier)) + return None def create(self, new_feature): """Create a new feature @@ -131,7 +134,7 @@ class GeoJSONProvider(BaseProvider): all_data = self._load() # Hijack the feature id and make sure it's unique - new_feature['id'] = str(uuid.uuid4()) + new_feature['properties']['id'] = str(uuid.uuid4()) all_data['features'].append(new_feature) @@ -146,9 +149,9 @@ class GeoJSONProvider(BaseProvider): """ all_data = self._load() for i, feature in enumerate(all_data['features']): - if feature['id'] == identifier: + if feature['properties']['id'] == identifier: # ensure new_feature retains id - new_feature['id'] = identifier + new_feature['properties']['id'] = identifier all_data['features'][i] = new_feature break @@ -162,7 +165,7 @@ class GeoJSONProvider(BaseProvider): """ all_data = self._load() for i, feature in enumerate(all_data['features']): - if feature['id'] == identifier: + if feature['properties']['id'] == identifier: all_data['features'].pop(i) break diff --git a/pygeoapi/provider/sqlite.py b/pygeoapi/provider/sqlite.py index 6a54163..b392b9b 100644 --- a/pygeoapi/provider/sqlite.py +++ b/pygeoapi/provider/sqlite.py @@ -76,6 +76,7 @@ class SQLiteProvider(BaseProvider): geom = geojson.loads(row_data['AsGeoJSON(geometry)']) del row_data['AsGeoJSON(geometry)'] feature = geojson.Feature(geometry=geom, properties=row_data) + feature['ID'] = feature['properties'][self.id_field] feature_list.append(feature) feature_collection = geojson.FeatureCollection(feature_list) diff --git a/pygeoapi/templates/items.html b/pygeoapi/templates/items.html index 447a082..b13c231 100644 --- a/pygeoapi/templates/items.html +++ b/pygeoapi/templates/items.html @@ -45,8 +45,15 @@ subdomains: '1234' } )); - {% for feature in data['features'] %} - L.marker([{{ feature['geometry']['coordinates'][1] }}, {{ feature['geometry']['coordinates'][0] }}]).addTo(map).bindPopup('{{ feature['ID'] }} ') - {% endfor %} + var geojson_data = {{ data['features'] |to_json }}; + var items = new L.GeoJSON(geojson_data, { + onEachFeature: function (feature, layer) { + var html_ = '' + feature.ID + ''; + layer.bindPopup(html_); + } + }); + + map.addLayer(items); + map.fitBounds(items.getBounds()); {% endblock %} diff --git a/tests/data/ne_110m_lakes.geojson b/tests/data/ne_110m_lakes.geojson index 50a6821..a5fb82e 100644 --- a/tests/data/ne_110m_lakes.geojson +++ b/tests/data/ne_110m_lakes.geojson @@ -4,8 +4,9 @@ { "type": "Feature", "properties": { + "id": 0, "scalerank": 0, - "name": "Lake\rBaikal", + "name": "Lake Baikal", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -172,14 +173,14 @@ ] ] ] - }, - "id": "0" + } }, { "type": "Feature", "properties": { + "id": 1, "scalerank": 0, - "name": "Lake\rWinnipeg", + "name": "Lake Winnipeg", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -238,14 +239,14 @@ ] ] ] - }, - "id": "1" + } }, { "type": "Feature", "properties": { + "id": 2, "scalerank": 0, - "name": "Great\rSlave Lake", + "name": "Great Slave Lake", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -356,12 +357,12 @@ ] ] ] - }, - "id": "2" + } }, { "type": "Feature", "properties": { + "id": 3, "scalerank": 0, "name": "L. Ontario", "name_alt": null, @@ -442,12 +443,12 @@ ] ] ] - }, - "id": "3" + } }, { "type": "Feature", "properties": { + "id": 4, "scalerank": 0, "name": "L. Erie", "name_alt": null, @@ -536,12 +537,12 @@ ] ] ] - }, - "id": "4" + } }, { "type": "Feature", "properties": { + "id": 5, "scalerank": 0, "name": "Lake Superior", "name_alt": null, @@ -678,14 +679,14 @@ ] ] ] - }, - "id": "5" + } }, { "type": "Feature", "properties": { + "id": 6, "scalerank": 0, - "name": "Lake\rVictoria", + "name": "Lake Victoria", "name_alt": null, "admin": "admin-0", "featureclass": "Lake" @@ -772,14 +773,14 @@ ] ] ] - }, - "id": "6" + } }, { "type": "Feature", "properties": { + "id": 7, "scalerank": 0, - "name": "Lake\rLadoga", + "name": "Lake Ladoga", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -850,12 +851,12 @@ ] ] ] - }, - "id": "7" + } }, { "type": "Feature", "properties": { + "id": 8, "scalerank": 0, "name": "Balqash K\u00f6li", "name_alt": "Lake\rBalkhash", @@ -972,14 +973,14 @@ ] ] ] - }, - "id": "8" + } }, { "type": "Feature", "properties": { + "id": 9, "scalerank": 0, - "name": "Lake\rTanganyika", + "name": "Lake Tanganyika", "name_alt": null, "admin": "admin-0", "featureclass": "Lake" @@ -1078,12 +1079,12 @@ ] ] ] - }, - "id": "9" + } }, { "type": "Feature", "properties": { + "id": 10, "scalerank": 0, "name": "Lake Malawi", "name_alt": "Lake Nyasa", @@ -1176,14 +1177,14 @@ ] ] ] - }, - "id": "10" + } }, { "type": "Feature", "properties": { + "id": 11, "scalerank": 0, - "name": "Aral\rSea", + "name": "Aral Sea", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -1290,12 +1291,12 @@ ] ] ] - }, - "id": "11" + } }, { "type": "Feature", "properties": { + "id": 12, "scalerank": 1, "name": "V\u00e4nern", "name_alt": null, @@ -1364,12 +1365,12 @@ ] ] ] - }, - "id": "12" + } }, { "type": "Feature", "properties": { + "id": 13, "scalerank": 1, "name": "Lake Okeechobee", "name_alt": null, @@ -1406,14 +1407,14 @@ ] ] ] - }, - "id": "13" + } }, { "type": "Feature", "properties": { + "id": 14, "scalerank": 1, - "name": "Lago de\rNicaragua", + "name": "Lago de Nicaragua", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -1456,12 +1457,12 @@ ] ] ] - }, - "id": "14" + } }, { "type": "Feature", "properties": { + "id": 15, "scalerank": 1, "name": "Lake Tana", "name_alt": null, @@ -1506,12 +1507,12 @@ ] ] ] - }, - "id": "15" + } }, { "type": "Feature", "properties": { + "id": 16, "scalerank": 1, "name": "Lago Titicaca", "name_alt": null, @@ -1592,14 +1593,14 @@ ] ] ] - }, - "id": "16" + } }, { "type": "Feature", "properties": { + "id": 17, "scalerank": 1, - "name": "Lake\rWinnipegosis", + "name": "Lake Winnipegosis", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -1690,14 +1691,14 @@ ] ] ] - }, - "id": "17" + } }, { "type": "Feature", "properties": { + "id": 18, "scalerank": 1, - "name": "Lake\rOnega", + "name": "Lake Onega", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -1804,12 +1805,12 @@ ] ] ] - }, - "id": "18" + } }, { "type": "Feature", "properties": { + "id": 19, "scalerank": 1, "name": "Great Salt Lake", "name_alt": null, @@ -1862,14 +1863,14 @@ ] ] ] - }, - "id": "19" + } }, { "type": "Feature", "properties": { + "id": 20, "scalerank": 1, - "name": "Great\rBear Lake", + "name": "Great Bear Lake", "name_alt": null, "admin": null, "featureclass": "Lake" @@ -1980,12 +1981,12 @@ ] ] ] - }, - "id": "20" + } }, { "type": "Feature", "properties": { + "id": 21, "scalerank": 1, "name": "Lake Athabasca", "name_alt": null, @@ -2034,12 +2035,12 @@ ] ] ] - }, - "id": "21" + } }, { "type": "Feature", "properties": { + "id": 22, "scalerank": 1, "name": "Reindeer\rLake", "name_alt": null, @@ -2112,12 +2113,12 @@ ] ] ] - }, - "id": "22" + } }, { "type": "Feature", "properties": { + "id": 23, "scalerank": 0, "name": "Lake\rHuron", "name_alt": null, @@ -2258,12 +2259,12 @@ ] ] ] - }, - "id": "23" + } }, { "type": "Feature", "properties": { + "id": 24, "scalerank": 0, "name": "Lake\rMichigan", "name_alt": null, @@ -2404,8 +2405,7 @@ ] ] ] - }, - "id": "24" + } } ] -} \ No newline at end of file +}