Skip to content

Commit

Permalink
Merge pull request #1 from nyudlts/geoserver-previewer
Browse files Browse the repository at this point in the history
Shapefile previews via GeoServer
  • Loading branch information
spilth authored Nov 5, 2024
2 parents 0961023 + fbbe518 commit 98bf508
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 3 deletions.
73 changes: 70 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package:
::

PREVIEWER_PREVIEWERS_ORDER = [
"invenio_previewer.extensions.geoserver",
"invenio_previewer.extensions.gpx",
"invenio_previewer.extensions.csv_papaparsejs",
"invenio_previewer.extensions.json_prismjs",
Expand All @@ -45,6 +46,7 @@ package:
]

PREVIEWER_PREFERENCE = [
"geoserver",
"gpx",
"csv_papaparsejs",
"json_prismjs",
Expand All @@ -59,13 +61,13 @@ package:
"txt",
]

Allow Images from Open Street Map
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allow Images from Open Street Map & GeoServers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In ``invenio.cfg`` edit ``APP_DEFAULT_SECURE_HEADERS`` to allow images
from OpenStreetMap:

::
.. code:: python
APP_DEFAULT_SECURE_HEADERS = {
'content_security_policy': {
Expand All @@ -78,12 +80,77 @@ from OpenStreetMap:
'img-src': [
"'self'",
"https://*.openstreetmap.org",
"https://maps-public.geo.nyu.edu/geoserver/sdr/wms",
'data:',
]
},
...
}
Add Custom GeoServer Fields
~~~~~~~~~~~~~~~~~~~~~~~~~~~

In ``invenio.cfg`` add the following namespace, custom fields and UI elements:

.. code:: python
# Set these if you want to override the field names below
PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_WMS_URL = "geoserver:wms_url"
PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_WFS_URL = "geoserver:wfs_url"
PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_LAYER_NAME = "geoserver:layer_name"
RDM_NAMESPACES = {
"geoserver": "https://geoserver.org/"
}
RDM_CUSTOM_FIELDS = [
TextCF(name="geoserver:wms_url"),
TextCF(name="geoserver:wfs_url"),
TextCF(name="geoserver:layer_name"),
]
RDM_CUSTOM_FIELDS_UI = [
{
"section": _("GeoServer"),
"fields": [
dict(
field="geoserver:wms_url",
ui_widget="Input",
props=dict(
label="WMS URL",
placeholder="https://maps-public.geo.nyu.edu/geoserver/sdr/wms",
icon="linkify",
description="GeoServer WMS Service Base URL",
required=False
)
),
dict(
field="geoserver:wfs_url",
ui_widget="Input",
props=dict(
label="WMS URL",
placeholder="https://maps-public.geo.nyu.edu/geoserver/sdr/wfs",
icon="linkify",
description="GeoServer WFS Service Base URL",
required=False
)
),
dict(
field="geoserver:layer_name",
ui_widget="Input",
props=dict(
label="Layer Name",
placeholder="sdr:nyu_2451_12345",
icon="pencil",
description="Name of the GeoServer Layer this data can be found in",
required=False
)
)
]
}
]
Development
-----------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "leaflet/dist/leaflet.css";

#map {
width: 100%;
height: 480px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import L from "leaflet"
import "leaflet/dist/leaflet.css";

document.addEventListener("DOMContentLoaded", () => {
const mapElement = document.getElementById("map");
const baseUrl = mapElement.getAttribute("data-base-url")
const layerName = mapElement.getAttribute("data-layer-name")

const map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);

const wmsLayer = L.tileLayer.wms(baseUrl, {
layers: layerName,
format: 'image/png',
transparent: true
});

wmsLayer.addTo(map);
wmsLayer.setOpacity(0.75);
map.fitBounds([
[17.881242, -179.14734],
[71.390482, 179.778465]
])
});
4 changes: 4 additions & 0 deletions invenio_previewer_geospatial/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@

PREVIEWER_GEOSPATIAL_BASE_TEMPLATE = "invenio_previewer_geospatial/base.html"
"""Default base template for the demo page."""

PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_WMS_URL = "geoserver:wms_url"
PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_WFS_URL = "geoserver:wfs_url"
PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_LAYER_NAME = "geoserver:layer_name"
32 changes: 32 additions & 0 deletions invenio_previewer_geospatial/extensions/geoserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""GeoServer Previewer."""

from flask import current_app, render_template
from invenio_previewer.proxies import current_previewer

previewable_extensions = ["shp"]


def can_preview(file):
"""Check if file can be previewed."""
return file.is_local() and file.has_extensions(".shp")


def preview(file):
"""Render the Geoserver template."""
return render_template(
"invenio_previewer_geospatial/geoserver.html",
file=file,
record=file.record,
wms_url_field=(
current_app.config.get(
"PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_WMS_URL"
)
),
layer_name_field=(
current_app.config.get(
"PREVIEWER_GEOSPATIAL_CUSTOM_FIELDS_GEOSERVER_LAYER_NAME"
)
),
js_bundles=current_previewer.js_bundles + ["geoserver_js.js"],
css_bundles=current_previewer.css_bundles + ["geoserver_css.css"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{%- extends config.PREVIEWER_ABSTRACT_TEMPLATE %}

{% block panel %}
{% if record.custom_fields[wms_url_field] and record.custom_fields[layer_name_field] %}
<div id="map"
data-base-url="{{ record.custom_fields[wms_url_field] }}"
data-layer-name="{{ record.custom_fields[layer_name_field] }}"
></div>
{% else %}
<p>Sorry, this Shapefile has no associated GeoServer layer.</p>
{% endif %}
{% endblock %}
2 changes: 2 additions & 0 deletions invenio_previewer_geospatial/webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
entry={
"gpx_js": "./js/invenio_previewer_geospatial/gpx.js",
"gpx_css": "./css/invenio_previewer_geospatial/gpx.css",
"geoserver_js": "./js/invenio_previewer_geospatial/geoserver.js",
"geoserver_css": "./css/invenio_previewer_geospatial/geoserver.css",
},
dependencies={"leaflet": "^1.9.4", "leaflet-gpx": "^2.1.2"},
),
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ invenio_assets.webpack =
invenio_previewer_geospatial = invenio_previewer_geospatial.webpack:theme
invenio_previewer.previewers =
gpx = invenio_previewer_geospatial.extensions.gpx
geoserver = invenio_previewer_geospatial.extensions.geoserver

[build_sphinx]
source-dir = docs/
Expand Down

0 comments on commit 98bf508

Please sign in to comment.