Also do some refactoring, and start on moving barentswatch api behind auth. The manual fish health data decoding needs some work, and needs a mapping of what is interesting. The selection is also kinda wonky right now. The selection and deselection might not be what we want.
86 lines
2.7 KiB
Forth
86 lines
2.7 KiB
Forth
module Fiskeridir
|
|
|
|
open Browser
|
|
open Fable.OpenLayers
|
|
open Thoth.Fetch
|
|
open Thoth.Json
|
|
|
|
open Atlantis.Types
|
|
open Utils
|
|
|
|
|
|
/// See docs/fiskeridir-locality-borders-example-payload.json
|
|
type FiskeridirLocalityBorder = {
|
|
Id : int
|
|
SiteVersionId : int
|
|
SiteNr : int
|
|
Name : string
|
|
Type : obj
|
|
Points : Point array
|
|
AreaM2 : float
|
|
}
|
|
and Point = {
|
|
Id : int
|
|
Index : int
|
|
Latitude : float
|
|
Longitude : float
|
|
}
|
|
|
|
let fetchLocalityBorders (map: Map.Map) (localityId: int) =
|
|
Fetch.tryGet<unit, FiskeridirLocalityBorder array>(
|
|
url = $"https://api.fiskeridir.no/pub-aqua/api/v1/sites/{localityId}/borders",
|
|
headers = [
|
|
Fetch.Types.HttpRequestHeaders.Accept "application/json"
|
|
Fetch.Types.HttpRequestHeaders.AcceptCharset "UTF-8"
|
|
],
|
|
decoder =
|
|
Decode.Auto.generateDecoder<FiskeridirLocalityBorder array>(
|
|
caseStrategy = CaseStrategy.CamelCase
|
|
)
|
|
)
|
|
|> Promise.map (fun res ->
|
|
match res with
|
|
| Error err -> console.error("Could not fetch locality borders from api.fiskeridir.no:", err)
|
|
| Ok borders ->
|
|
let points =
|
|
borders
|
|
|> Array.collect (fun border ->
|
|
border.Points)
|
|
|
|
if points.Length < 1 then
|
|
console.info("This site does not have borders")
|
|
()
|
|
else
|
|
let feature =
|
|
let coords : Coordinate array =
|
|
let coords =
|
|
points
|
|
|> Array.map (fun point ->
|
|
posToCoord (point.Longitude, point.Latitude)
|
|
|> coordToEpsg3857)
|
|
let first = Array.head coords
|
|
let last = coords[coords.Length - 1]
|
|
|
|
if first |> Coord.equals last then
|
|
coords
|
|
else
|
|
let first = coords |> Array.head |> Array.singleton
|
|
let linearRing = first |> Array.append coords
|
|
linearRing
|
|
|
|
let polygon =
|
|
Geometry.polygon [
|
|
geometry.coordinates [| coords |]
|
|
geometry.layout GeometryLayout.XY
|
|
]
|
|
Feature.feature [
|
|
feature.geometryOrProperties polygon
|
|
]
|
|
|
|
feature.setId localityId
|
|
feature.set("type", "polygon")
|
|
|
|
Layers.addFeatures map MapLayer.Aquaculture [| feature |]
|
|
|
|
())
|