Skip to content

Commit

Permalink
Add terrain
Browse files Browse the repository at this point in the history
  • Loading branch information
cristineguadelupe committed Jan 11, 2024
1 parent d370024 commit 5995989
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/maplibre.ex
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ defmodule MapLibre do

validate_layer_id!(ml, id)
validate_layer_type!(type)
if type != :background, do: validate_layer_source!(ml, source)
if type != :background, do: validate_source!(ml, source, :layer)
end

defp validate_layer_update!(index, id, layers, ml, opts) do
Expand All @@ -456,7 +456,7 @@ defmodule MapLibre do
type = opts[:type]
source = opts[:source]
if type, do: validate_layer_type!(type)
if source, do: validate_layer_source!(ml, source)
if source, do: validate_source!(ml, source, :layer)
end

defp validate_layer_id!(_ml, nil) do
Expand Down Expand Up @@ -497,17 +497,17 @@ defmodule MapLibre do
end
end

defp validate_layer_source!(_ml, nil) do
defp validate_source!(_ml, nil, type) do
raise ArgumentError,
"layer source is required"
"#{type} source is required"
end

defp validate_layer_source!(ml, source) do
defp validate_source!(ml, source, type) do
if not Map.has_key?(ml.spec["sources"], source) do
sources = Map.keys(ml.spec["sources"]) |> Enum.map_join(", ", &inspect/1)

raise ArgumentError,
"source #{inspect(source)} was not found. The source must be present in the style before it can be associated with a layer. Current available sources are: #{sources}"
"source #{inspect(source)} was not found. The source must be present in the style before it can be associated with a #{type}. Current available sources are: #{sources}"
end
end

Expand Down Expand Up @@ -588,6 +588,21 @@ defmodule MapLibre do
update_in(ml.spec, fn spec -> Map.put(spec, "transition", transition) end)
end

@doc """
Add 3D terrain to the map.
The source must have been previously added to the map.
See [the docs](https://maplibre.org/maplibre-style-spec/terrain/) for more
details.
"""
@spec terrain(t(), keyword()) :: t()
def terrain(ml, opts) do
validate_source!(ml, opts[:source], :terrain)
terrain = opts_to_ml_props(opts)
update_in(ml.spec, fn spec -> Map.put(spec, "terrain", terrain) end)
end

@doc """
Adds or updates the map metadata properties. Metadata are arbitrary properties useful to track
with the style, but do not influence rendering. Properties should be prefixed to avoid
Expand Down
28 changes: 28 additions & 0 deletions test/maplibre_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,32 @@ defmodule MapLibreTest do
assert ml.spec["metadata"]["meta:data"] == "updated"
end
end

describe "terrain/2" do
test "add a terrain to the map" do
ml =
Ml.new(style: %{})
|> Ml.add_source("terrain",
type: :raster_dem,
url: "https://demotiles.maplibre.org/terrain-tiles/tiles.json"
)
|> Ml.terrain(source: "terrain")

assert ml.spec["terrain"] == %{"source" => "terrain"}
end

test "raises an error if the terrain source does not exist" do
assert_raise ArgumentError,
~s(source "invalid" was not found. The source must be present in the style before it can be associated with a terrain. Current available sources are: "crimea", "maplibre"),
fn ->
Ml.new() |> Ml.terrain(source: "invalid")
end
end

test "raises an error if the terrain source is not given" do
assert_raise ArgumentError, "terrain source is required", fn ->
Ml.new() |> Ml.terrain(exaggeration: 1)
end
end
end
end

0 comments on commit 5995989

Please sign in to comment.