F# and Path Reduction Kit Eason - @kitlovesfsharp – – [email protected].

download F# and Path Reduction Kit Eason - @kitlovesfsharp –  – kit.eason@gmail.com.

If you can't read please download the document

Transcript of F# and Path Reduction Kit Eason - @kitlovesfsharp – – [email protected].

  • Slide 1

F# and Path Reduction Kit Eason - @kitlovesfsharp www.kiteason.com [email protected] Slide 2 Norway way too complicated! Slide 3 Way, way too complicated! Slide 4 RamerDouglasPeucker Slide 5 A record type type Point = {Long: double; Lat : double} Slide 6 Distance from line let private findPerpendicularDistance p p1 p2 = if (p1.Long = p2.Long) then abs(p.Long - p1.Long) else let slope = (p2.Lat - p1.Lat) / (p2.Long - p1.Long) let intercept = p1.Lat - (slope * p1.Long) abs(slope * p.Long - p.Lat + intercept) / sqrt((pown slope 2) + 1.) let private findPerpendicularDistance p p1 p2 = if (p1.Long = p2.Long) then abs(p.Long - p1.Long) else let slope = (p2.Lat - p1.Lat) / (p2.Long - p1.Long) let intercept = p1.Lat - (slope * p1.Long) abs(slope * p.Long - p.Lat + intercept) / sqrt((pown slope 2) + 1.) Slide 7 Recursive algorithm let rec Reduce epsilon (points : Point[]) = if points.Length < 3 || epsilon = 0. then points else let firstPoint = points.[0] let lastPoint = points.[points.Length - 1] let mutable index = -1 let mutable dist = 0.0 for i in 1..points.Length-1 do let cDist = findPerpendicularDistance points.[i] firstPoint lastPoint if (cDist > dist) then distdist) then dist let rec Reduce epsilon (points : Point[]) = if points.Length < 3 || epsilon = 0. then points else let firstPoint = points.[0] let lastPoint = points.[points.Length - 1] let mutable index = -1 let mutable dist = 0.0 for i in 1..points.Length-1 do let cDist = findPerpendicularDistance points.[i] firstPoint lastPoint if (cDist > dist) then distdist) then distArray.Parallel.map T">Array.Parallel.map ToPoints |> FilterCount 100 let private ReadData fileName = fileName |> File.ReadAllLines |> Array.breakOn (fun line -> line = "nan nan") |> Array.Parallel.map ToPoints |> FilterCount 100">Array.Parallel.map T" title="Reading the data let private ReadData fileName = fileName |> File.ReadAllLines |> Array.breakOn (fun line -> line = "nan nan") |> Array.Parallel.map T"> Reading the data let private ReadData fileName = fileName |> File.ReadAllLines |> Array.breakOn (fun line -> line = "nan nan") |> Array.Parallel.map ToPoints |> FilterCount 100 let private ReadData fileName = fileName |> File.ReadAllLines |> Array.breakOn (fun line -> line = "nan nan") |> Array.Parallel.map ToPoints |> FilterCount 100 Slide 11 Breaking into arrays let breakOn (f : 'a -> bool) (a : array ) = [| let result = ResizeArray() for x in a do if f x then yield result |> Array.ofSeq result.Clear() else result.Add(x) yield result |> Array.ofSeq |] let breakOn (f : 'a -> bool) (a : array ) = [| let result = ResizeArray() for x in a do if f x then yield result |> Array.ofSeq result.Clear() else result.Add(x) yield result |> Array.ofSeq |] Slide 12 Converting to points let private ToPoints (lines : array ) = lines |> Array.Parallel.map (fun line -> line.Split [|'\t'|]) |> Array.Parallel.map (fun items -> items |> Array.map Double.Parse) |> Array.choose (fun doubles -> match doubles with | [|long; lat|] -> Some {Long=long; Lat=lat} | _ -> None) let private ToPoints (lines : array ) = lines |> Array.Parallel.map (fun line -> line.Split [|'\t'|]) |> Array.Parallel.map (fun items -> items |> Array.map Double.Parse) |> Array.choose (fun doubles -> match doubles with | [|long; lat|] -> Some {Long=long; Lat=lat} | _ -> None) Slide 13 Eliminating tiny islands (and lakes!) let private FilterCount minCount groups = groups |> Array.filter (fun g -> Array.length g > minCount) let private FilterCount minCount groups = groups |> Array.filter (fun g -> Array.length g > minCount) Slide 14 Calling the RDP algorithm let Simplify e polyLines = polyLines |> Array.Parallel.map (fun p -> p |> Reduce.Reduce e) let Simplify e polyLines = polyLines |> Array.Parallel.map (fun p -> p |> Reduce.Reduce e) Slide 15 = 0 Slide 16 = 0.001 Slide 17 = 0.01 Slide 18 = 0.1 Slide 19 = 1 Slide 20 Out-takes Slide 21 Resources www.github.com/misterspeedy/coastline www.github.com/misterspeedy/coastline www.fsharp.org www.fsharp.org www.fsharpforfunandprofit.com www.fsharpforfunandprofit.com Chris Smiths Animal Guide www.github.com/misterspeedy/coastline www.github.com/misterspeedy/coastline www.fsharp.org www.fsharp.org www.fsharpforfunandprofit.com www.fsharpforfunandprofit.com Chris Smiths Animal Guide