Skip to content

Commit 5ea7770

Browse files
add --geojson option (#63)
1 parent 4608ac3 commit 5ea7770

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.14.0 (2025-03-20)
2+
3+
* add `--geojson` option to add a GeoJSON Feature or FeatureCollection on the map viewer
4+
15
# 0.13.1 (2025-03-19)
26

37
* relaxe titiler requirement to `>=0.20,<0.22`

rio_viz/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class viz:
9393
layers: Optional[List[str]] = attr.ib(default=None)
9494
nodata: Optional[Union[str, int, float]] = attr.ib(default=None)
9595

96+
geojson: Optional[Dict] = attr.ib(default=None)
97+
9698
# cog / bands / assets
9799
reader_type: str = attr.ib(init=False)
98100

@@ -803,6 +805,7 @@ def viewer(request: Request):
803805
"info_endpoint": str(request.url_for("info_geojson")),
804806
"point_endpoint": str(request.url_for("point")),
805807
"allow_3d": has_mvt,
808+
"geojson": self.geojson,
806809
},
807810
media_type="text/html",
808811
)

rio_viz/scripts/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""rio_viz.cli."""
22

33
import importlib
4+
import json
45
import os
56
import tempfile
67
import warnings
@@ -135,6 +136,11 @@ def convert(self, value, param, ctx):
135136
callback=options_to_dict,
136137
help="Reader Options.",
137138
)
139+
@click.option(
140+
"--geojson",
141+
type=click.File(mode="r"),
142+
help="GeoJSON Feature or FeatureCollection path to display on viewer.",
143+
)
138144
def viz(
139145
src_path,
140146
nodata,
@@ -148,6 +154,7 @@ def viz(
148154
server_only,
149155
config,
150156
reader_params,
157+
geojson,
151158
):
152159
"""Rasterio Viz cli."""
153160
if reader:
@@ -187,6 +194,7 @@ def viz(
187194
maxzoom=maxzoom,
188195
nodata=nodata,
189196
layers=layers,
197+
geojson=json.load(geojson) if geojson else None,
190198
)
191199
if not server_only:
192200
click.echo(f"Viewer started at {application.template_url}", err=True)

rio_viz/templates/assets.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,21 @@
365365
document.getElementById('zoom').textContent = z
366366
})
367367

368+
const add_geojson = () => {
369+
if (map.getSource('geojson')) map.removeSource('geojson')
370+
if (map.getLayer('geojson')) map.removeLayer('geojson')
371+
if ({{ geojson }} !== undefined) {
372+
map.addSource('geojson', {'type': 'geojson', 'data': {{ geojson|safe }}})
373+
map.addLayer({
374+
id: 'geojson',
375+
type: 'line',
376+
source: 'geojson',
377+
layout: {'line-cap': 'round', 'line-join': 'round'},
378+
paint: {'line-color': '#FF0000', 'line-width': 4}
379+
})
380+
}
381+
}
382+
368383
const add_raster = (tilejson) => {
369384
if (map.getLayer('raster-l')) map.removeLayer('raster-l')
370385
if (map.getSource('raster-l')) map.removeSource('raster-l')
@@ -442,6 +457,9 @@
442457
document.getElementById('loader').classList.add('off')
443458
if (scope.metadata) addHisto1Band()
444459
})
460+
.then(() => {
461+
add_geojson()
462+
})
445463
.catch(err => {
446464
console.warn(err)
447465
})
@@ -485,6 +503,9 @@
485503
document.getElementById('loader').classList.add('off')
486504
if (scope.metadata) addHisto3Bands()
487505
})
506+
.then(() => {
507+
add_geojson()
508+
})
488509
.catch(err => {
489510
console.warn(err)
490511
})
@@ -712,6 +733,10 @@
712733
if (map.getLayer('raster')) map.removeLayer('raster')
713734
if (map.getSource('raster')) map.removeSource('raster')
714735

736+
// remove GeoJSON layers/sources
737+
if (map.getSource('geojson')) map.removeSource('geojson')
738+
if (map.getLayer('geojson')) map.removeLayer('geojson')
739+
715740
const rasterType = document.getElementById('toolbar').querySelector(".active").id
716741
switch (rasterType) {
717742
case '1b':

rio_viz/templates/bands.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,21 @@
380380
document.getElementById('zoom').textContent = z
381381
})
382382

383+
const add_geojson = () => {
384+
if (map.getSource('geojson')) map.removeSource('geojson')
385+
if (map.getLayer('geojson')) map.removeLayer('geojson')
386+
if ({{ geojson }} !== undefined) {
387+
map.addSource('geojson', {'type': 'geojson', 'data': {{ geojson|safe }}})
388+
map.addLayer({
389+
id: 'geojson',
390+
type: 'line',
391+
source: 'geojson',
392+
layout: {'line-cap': 'round', 'line-join': 'round'},
393+
paint: {'line-color': '#FF0000', 'line-width': 4}
394+
})
395+
}
396+
}
397+
383398
const add_raster = (tilejson) => {
384399
if (map.getLayer('raster-l')) map.removeLayer('raster-l')
385400
if (map.getSource('raster-l')) map.removeSource('raster-l')
@@ -586,6 +601,9 @@
586601
.then(data => {
587602
add_raster(data)
588603
})
604+
.then(() => {
605+
add_geojson()
606+
})
589607
.catch(err => {
590608
console.warn(err)
591609
})
@@ -601,6 +619,9 @@
601619
add_vector_source(data)
602620
add_vector_layer(vizType)
603621
})
622+
.then(() => {
623+
add_geojson()
624+
})
604625
.catch(err => {
605626
console.warn(err)
606627
})
@@ -616,6 +637,9 @@
616637
add_vector_source(data)
617638
add_vector_layer(vizType)
618639
})
640+
.then(() => {
641+
add_geojson()
642+
})
619643
.catch(err => {
620644
console.warn(err)
621645
})
@@ -654,6 +678,9 @@
654678
add_raster(data)
655679
if (scope.dataset_statistics) addHisto3Bands()
656680
})
681+
.then(() => {
682+
add_geojson()
683+
})
657684
.catch(err => {
658685
console.warn(err)
659686
})
@@ -676,6 +703,10 @@
676703
if (map.getLayer('vector')) map.removeLayer('vector')
677704
if (map.getSource('vector')) map.removeSource('vector')
678705

706+
// remove GeoJSON layers/sources
707+
if (map.getSource('geojson')) map.removeSource('geojson')
708+
if (map.getLayer('geojson')) map.removeLayer('geojson')
709+
679710
const rasterType = document.getElementById('toolbar').querySelector(".active").id
680711
switch (rasterType) {
681712
case '1b':
@@ -948,6 +979,7 @@
948979
switchViz()
949980
} else {
950981
add_vector_layer(newViz)
982+
add_geojson()
951983
if (scope.dataset_statistics) addHisto1Band()
952984
}
953985
}

rio_viz/templates/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,21 @@
381381
document.getElementById('zoom').textContent = z
382382
})
383383

384+
const add_geojson = () => {
385+
if (map.getSource('geojson')) map.removeSource('geojson')
386+
if (map.getLayer('geojson')) map.removeLayer('geojson')
387+
if ({{ geojson }} !== undefined) {
388+
map.addSource('geojson', {'type': 'geojson', 'data': {{ geojson|safe }}})
389+
map.addLayer({
390+
id: 'geojson',
391+
type: 'line',
392+
source: 'geojson',
393+
layout: {'line-cap': 'round', 'line-join': 'round'},
394+
paint: {'line-color': '#FF0000', 'line-width': 4}
395+
})
396+
}
397+
}
398+
384399
const add_raster = (tilejson) => {
385400
if (map.getLayer('raster-l')) map.removeLayer('raster-l')
386401
if (map.getSource('raster-l')) map.removeSource('raster-l')
@@ -583,6 +598,9 @@
583598
.then(data => {
584599
add_raster(data)
585600
})
601+
.then(() => {
602+
add_geojson()
603+
})
586604
.catch(err => {
587605
console.warn(err)
588606
})
@@ -598,6 +616,9 @@
598616
add_vector_source(data)
599617
add_vector_layer(vizType)
600618
})
619+
.then(() => {
620+
add_geojson()
621+
})
601622
.catch(err => {
602623
console.warn(err)
603624
})
@@ -613,6 +634,9 @@
613634
add_vector_source(data)
614635
add_vector_layer(vizType)
615636
})
637+
.then(() => {
638+
add_geojson()
639+
})
616640
.catch(err => {
617641
console.warn(err)
618642
})
@@ -651,6 +675,9 @@
651675
add_raster(data)
652676
if (scope.dataset_statistics) addHisto3Bands()
653677
})
678+
.then(() => {
679+
add_geojson()
680+
})
654681
.catch(err => {
655682
console.warn(err)
656683
})
@@ -673,6 +700,10 @@
673700
if (map.getLayer('vector')) map.removeLayer('vector')
674701
if (map.getSource('vector')) map.removeSource('vector')
675702

703+
// remove GeoJSON layers/sources
704+
if (map.getSource('geojson')) map.removeSource('geojson')
705+
if (map.getLayer('geojson')) map.removeLayer('geojson')
706+
676707
const rasterType = document.getElementById('toolbar').querySelector(".active").id
677708
switch (rasterType) {
678709
case '1b':
@@ -945,6 +976,7 @@
945976
switchViz()
946977
} else {
947978
add_vector_layer(newViz)
979+
add_geojson()
948980
if (scope.dataset_statistics) addHisto1Band()
949981
}
950982
}

0 commit comments

Comments
 (0)