Skip to content

Commit

Permalink
Basic GPX support via Leaflet
Browse files Browse the repository at this point in the history
  • Loading branch information
spilth committed Oct 29, 2024
1 parent 8be8857 commit 502d404
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ recursive-include docs Makefile
recursive-include invenio_previewer_geospatial *.html
recursive-include invenio_previewer_geospatial/translations *.po *.pot *.mo
recursive-include tests *.py

# added by check-manifest
recursive-include invenio_previewer_geospatial *.css
recursive-include invenio_previewer_geospatial *.js
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,30 @@
import L from "leaflet"
import 'leaflet-gpx';
import "leaflet/dist/leaflet.css";

document.addEventListener("DOMContentLoaded", () => {
const mapElement = document.getElementById("map");
const gpxFileUri = mapElement.getAttribute("data-file-uri")
const gpxUrl = new URL(gpxFileUri, window.location.href);

const map = L.map('map').setView([0, 0], 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);

new L.GPX(gpxUrl.href, {
async: true,
gpx_options: {
parseElements: ['track', 'route']
},
markers: {
// TODO: Figure out how to display stock images from leaflet-gpx
startIcon: null,
endIcon: null
}
}).on('loaded', (e) => {
map.fitBounds(e.target.getBounds());
}).addTo(map);
});
3 changes: 3 additions & 0 deletions invenio_previewer_geospatial/extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

"""Geospatial Previewers."""
21 changes: 21 additions & 0 deletions invenio_previewer_geospatial/extensions/gpx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""GPX Previewer."""

from flask import render_template
from invenio_previewer.proxies import current_previewer

previewable_extensions = ["gpx"]


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


def preview(file):
"""Render the GPX template."""
return render_template(
"invenio_previewer_geospatial/gpx.html",
file=file,
js_bundles=current_previewer.js_bundles + ["gpx_js.js"],
css_bundles=current_previewer.css_bundles + ["gpx_css.css"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{%- extends config.PREVIEWER_ABSTRACT_TEMPLATE %}

{% block panel %}
<div id="map" data-file-uri="{{ file.uri }}"></div>
{% endblock %}
18 changes: 18 additions & 0 deletions invenio_previewer_geospatial/webpack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""JS/CSS Webpack bundles for Invenio Previewer Geospatial."""

from invenio_assets.webpack import WebpackThemeBundle

theme = WebpackThemeBundle(
__name__,
"assets",
default="semantic-ui",
themes={
"semantic-ui": dict(
entry={
"gpx_js": "./js/invenio_previewer_geospatial/gpx.js",
"gpx_css": "./css/invenio_previewer_geospatial/gpx.css",
},
dependencies={"leaflet": "^1.9.4", "leaflet-gpx": "^2.1.2"},
),
},
)
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ zip_safe = False
install_requires =
invenio-base>=1.2.5,<2.0.0
invenio-i18n>=2.0.0,<3.0.0
invenio-previewer>=2.0.0,<3.0.0

[options.extras_require]
tests =
Expand All @@ -44,6 +45,10 @@ invenio_base.blueprints =
invenio_previewer_geospatial = invenio_previewer_geospatial.views:blueprint
invenio_i18n.translations =
messages = invenio_previewer_geospatial
invenio_assets.webpack =
invenio_previewer_geospatial = invenio_previewer_geospatial.webpack:theme
invenio_previewer.previewers =
gpx = invenio_previewer_geospatial.extensions.gpx

[build_sphinx]
source-dir = docs/
Expand Down

0 comments on commit 502d404

Please sign in to comment.