-module(inlay@openstreetmap). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/inlay/openstreetmap.gleam"). -export([detect/1, render/2]). -export_type([bounding_box/0]). -type bounding_box() :: {bounding_box, float(), float(), float(), float()}. -file("src/inlay/openstreetmap.gleam", 86). -spec parse_map_coords(binary()) -> gleam@option:option(inlay@embed:embed()). parse_map_coords(Coords) -> case gleam@string:split(Coords, <<"/"/utf8>>) of [Zoom_str, Lat_str, Long_str] -> case {gleam_stdlib:parse_int(Zoom_str), gleam_stdlib:parse_float(Lat_str), gleam_stdlib:parse_float(Long_str)} of {{ok, Zoom}, {ok, Lat}, {ok, Long}} -> {some, {map_location, Zoom, Lat, Long}}; {_, _, _} -> none end; _ -> none end. -file("src/inlay/openstreetmap.gleam", 79). -spec parse_map_fragment(binary()) -> gleam@option:option(inlay@embed:embed()). parse_map_fragment(Fragment) -> case gleam@string:split(Fragment, <<"="/utf8>>) of [<<"map"/utf8>>, Rest] -> parse_map_coords(Rest); _ -> none end. -file("src/inlay/openstreetmap.gleam", 72). -spec detect_osm(gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()). detect_osm(Url) -> case erlang:element(8, Url) of {some, Fragment} -> parse_map_fragment(Fragment); none -> none end. -file("src/inlay/openstreetmap.gleam", 10). -spec detect(gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()). detect(Url) -> case erlang:element(4, Url) of {some, <<"openstreetmap.org"/utf8>>} -> detect_osm(Url); {some, <<"www.openstreetmap.org"/utf8>>} -> detect_osm(Url); _ -> none end. -file("src/inlay/openstreetmap.gleam", 59). -spec cos_deg(float()) -> float(). cos_deg(Degrees) -> Radians = (Degrees * 3.141592653589793) / 180.0, math:cos(Radians). -file("src/inlay/openstreetmap.gleam", 48). -spec bounding_box(float(), float(), integer()) -> bounding_box(). bounding_box(Lat, Long, Zoom) -> Offset = case math:pow(2.0, erlang:float(Zoom)) of +0.0 -> +0.0; -0.0 -> -0.0; Gleam@denominator -> 360.0 / Gleam@denominator end, Lon_offset = case cos_deg(Lat) of +0.0 -> +0.0; -0.0 -> -0.0; Gleam@denominator@1 -> Offset / Gleam@denominator@1 end, {bounding_box, Lat - Offset, Long - Lon_offset, Lat + Offset, Long + Lon_offset}. -file("src/inlay/openstreetmap.gleam", 17). -spec render(inlay@embed:embed(), inlay@embed:config()) -> {ok, lustre@vdom@vnode:element(any())} | {error, nil}. render(Embed, Config) -> case Embed of {map_location, Zoom, Lat, Long} -> Bbox = bounding_box(Lat, Long, Zoom), Src = <<<<<<<<<<<<<<<<<<<<<<"https://www.openstreetmap.org/export/embed.html?bbox="/utf8, (gleam_stdlib:float_to_string( erlang:element( 3, Bbox ) ))/binary>>/binary, "%2C"/utf8>>/binary, (gleam_stdlib:float_to_string( erlang:element(2, Bbox) ))/binary>>/binary, "%2C"/utf8>>/binary, (gleam_stdlib:float_to_string( erlang:element(5, Bbox) ))/binary>>/binary, "%2C"/utf8>>/binary, (gleam_stdlib:float_to_string( erlang:element(4, Bbox) ))/binary>>/binary, "&layer=mapnik&marker="/utf8>>/binary, (gleam_stdlib:float_to_string(Lat))/binary>>/binary, "%2C"/utf8>>/binary, (gleam_stdlib:float_to_string(Long))/binary>>, Aspect_ratio = case erlang:element(10, Config) of {some, {open_street_map_config, {some, R}}} -> R; _ -> <<"75%"/utf8>> end, {ok, inlay@iframe:responsive(Src, Aspect_ratio, [])}; _ -> {error, nil} end.