add tile_layer_opt; untested

This commit is contained in:
Swrup 2024-01-29 22:49:31 +01:00
parent 3332d0fb7c
commit ccddd550f9
3 changed files with 78 additions and 19 deletions

View file

@ -120,22 +120,65 @@ let create_geojson : Jv.t -> geojson_opt list -> [ `Geojson ] t =
(** Tile layers *)
let create_tile_osm : string option -> [ `Tile ] t =
fun url ->
let url =
Option.value url
~default:"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
type tile_layer_opt =
| Min_zoom of int
| Max_zoom of int
| Subdomains of string array
| Error_tile_url of string
| Zoom_offset of int
| Tms of bool
| Zoom_reverse of bool
| Detect_retina of bool
(* TODO allow string for those two
"Whether the crossOrigin attribute will be added to the tiles. If a String is provided, all tiles will have their crossOrigin attribute set to the String provided. This is needed if you want to access tile pixel data. Refer to CORS Settings for valid String values."
*)
| Cross_origin of bool
| Referrer_policy of bool
let tile_layer_opt_to_string = function
| Min_zoom _ -> "minZoom"
| Max_zoom _ -> "maxZoom"
| Subdomains _ -> "subdomains"
| Error_tile_url _ -> "errorTileUrl"
| Zoom_offset _ -> "zoomOffset"
| Tms _ -> "tms"
| Zoom_reverse _ -> "zoomReverse"
| Detect_retina _ -> "detectRetina"
| Cross_origin _ -> "crossOrigin"
| Referrer_policy _ -> "referrerPolicy"
let tile_layer_opt_to_jv = function
| Min_zoom o | Max_zoom o | Zoom_offset o -> Jv.of_int o
| Tms o
| Zoom_reverse o
| Detect_retina o
| Cross_origin o
| Referrer_policy o ->
Jv.of_bool o
| Error_tile_url o -> Jv.of_string o
| Subdomains o -> Jv.of_array Jv.of_string o
let create_tile :
string -> attribution:string -> tile_layer_opt list -> [ `Tile ] t =
fun url ~attribution options ->
let arr =
Array.of_list
@@ ("attribution", Jv.of_string attribution)
:: List.map
(fun o -> (tile_layer_opt_to_string o, tile_layer_opt_to_jv o))
options
in
let jv_t =
Jv.call Global.leaflet "tileLayer"
[| Jv.of_string url
; Jv.obj
[| ( "attribution"
, Jv.of_string
"&copy; <a \
href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> \
contributors" )
|]
|]
Jv.call Global.leaflet "tileLayer" [| Jv.of_string url; Jv.obj arr |]
in
Tile jv_t
let create_tile_osm : tile_layer_opt list -> [ `Tile ] t =
fun options ->
let url = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" in
let attribution =
"&copy; <a \
href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> \
contributors"
in
create_tile url ~attribution options

View file

@ -64,7 +64,23 @@ val create_geojson : Jv.t -> geojson_opt list -> [ `Geojson ] t
(** Tile layers *)
(** [create_tile_osm Some(url)] is a new tile layer with tile server specified
by [url]. Tile server default to [openstreetmap.org]. See
type tile_layer_opt =
| Min_zoom of int
| Max_zoom of int
| Subdomains of string array
| Error_tile_url of string
| Zoom_offset of int
| Tms of bool
| Zoom_reverse of bool
| Detect_retina of bool
| Cross_origin of bool
| Referrer_policy of bool
(** [create_tile url attribution opts] create a new tile layer *)
val create_tile :
string -> attribution:string -> tile_layer_opt list -> [ `Tile ] t
(** [create_tile_osm opts] create a new tile layer with tile server and
attribution set to [openstreetmap.org]. See
{:https://wiki.openstreetmap.org/wiki/Tile_servers} *)
val create_tile_osm : string option -> [ `Tile ] t
val create_tile_osm : tile_layer_opt list -> [ `Tile ] t