Skip to content

Commit 03bbc42

Browse files
author
Stefan Kuethe
committed
Add geotiff example
1 parent 6095d91 commit 03bbc42

File tree

6 files changed

+84
-7
lines changed

6 files changed

+84
-7
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import openlayers as ol
2+
import openlayers.express as ox
3+
4+
data = "https://openlayers.org/data/vector/populated-places.json"
5+
6+
m = ox.Vector(
7+
data, fit_bounds=False, style=ox.default_style(circle_fill_color="red")
8+
).explore()
9+
# m.save()
10+
11+
m = ox.Vector(
12+
data="https://openlayers.org/en/latest/examples/data//kml/states.kml",
13+
# style=None,
14+
webgl=True
15+
# data="https://openlayers.org/en/latest/examples/data/gpx/fells_loop.gpx"
16+
).explore()
17+
m.save()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# See https://openlayers.org/en/latest/examples/cog-overviews.html
2+
import openlayers as ol
3+
4+
layer = ol.WebGLTileLayer(
5+
source=ol.GeoTIFFSource(
6+
sources=[
7+
dict(
8+
url="https://openlayers.org/data/raster/no-overviews.tif",
9+
overviews=["https://openlayers.org/data/raster/no-overviews.tif.ovr"],
10+
)
11+
]
12+
)
13+
)
14+
15+
m = ol.Map(layers=[layer])
16+
m.set_view_from_source(layer.id)
17+
m.save()

examples/standalone/layers/kml_vector_layer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
)
99

1010
m = ol.Map()
11-
# m.add_layer(kml_layer)
11+
m.add_layer(kml_layer)
1212
# m.add_tooltip()
1313
# m.add_call("addDragAndDropVectorLayers")
1414
m.add_drag_and_drop_vector_layers_interaction(
@@ -17,5 +17,4 @@
1717
stroke_color="red", stroke_width=3, circle_radius=5, circle_stroke_color="green"
1818
),
1919
)
20-
m.add_call("addDrawInteraction", "LineString")
2120
m.save("/tmp/ol-example.html")

examples/standalone/layers/mvt_vector_layer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# VectorTileLayer: https://unpkg.com/world-atlas@1.1.4/world/50m.json
12
import openlayers as ol
23

34
mvt_layer = ol.layers.VectorTileLayer(

src/openlayers/express.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .models.sources import GeoTIFFSource, VectorSource
1313
from .models.view import View
1414
from .styles import FlatStyle, default_style
15+
from .models.formats import Format, GeoJSON, KML, GPX
1516

1617

1718
class GeoTIFFTileLayer(LayerLike):
@@ -33,13 +34,55 @@ def to_map(self, *args, **kwargs) -> Map:
3334
return m
3435

3536

36-
#class VectorLayer(LayerLike): ...
37+
# class VectorLayer(LayerLike): ...
3738

38-
#class GPXLayer(VectorLayer): ...
39+
# class GPXLayer(VectorLayer): ...
3940

40-
#class GeoJSONLayer(VectorLayer): ...
41+
# class GeoJSONLayer(VectorLayer): ...
4142

42-
#class TopoJSONLayer(VectorLayer): ...
43+
# class TopoJSONLayer(VectorLayer): ...
44+
45+
46+
class Vector(LayerLike):
47+
def __init__(
48+
self,
49+
data: str | dict,
50+
id: str = None,
51+
style=default_style(),
52+
fit_bounds: bool = True,
53+
format: Format = None,
54+
webgl: bool = True,
55+
**kwargs,
56+
) -> None:
57+
source = (
58+
VectorSource(url=data)
59+
if isinstance(data, str)
60+
else VectorSource(geojson=data)
61+
)
62+
if not format and source.url:
63+
url = source.url.lower()
64+
if url.endswith(".kml"):
65+
format = KML()
66+
elif url.endswith(".gpx"):
67+
format = GPX()
68+
69+
source.format = format or GeoJSON()
70+
Layer = WebGLVectorLayer if webgl else VectorLayer
71+
self._model = Layer(
72+
source=source, id=id, fit_bounds=fit_bounds, style=style, **kwargs
73+
)
74+
75+
def explore(self, tooltip: bool = True, **kwargs) -> Map | MapWidget:
76+
m = Map(**kwargs)
77+
m.add_layer(self._model)
78+
if tooltip:
79+
m.add_default_tooltip()
80+
81+
return m
82+
83+
@property
84+
def model(self) -> VectorLayer | WebGLVectorLayer:
85+
return self._model
4386

4487

4588
# TODO: Move to VectorLayer

src/openlayers/models/layers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class TileLayer(Layer): ...
3131

3232

3333
class VectorLayer(Layer):
34-
style: dict | FlatStyle = default_style()
34+
style: dict | FlatStyle | None = default_style()
3535
fit_bounds: bool = Field(False, serialization_alias="fitBounds")
3636

3737
@field_validator("style")

0 commit comments

Comments
 (0)