-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr.elm
131 lines (97 loc) · 3.84 KB
/
pr.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Html exposing (Html, Attribute, button, div, text)
import Html.Events exposing (onClick)
import Svg exposing (Svg, svg, g, polygon)
import Svg.Attributes exposing (points, fill, stroke, strokeWidth )
import Http
import Json.Decode as Decode exposing (Decoder, at, list, maybe, map, map2, field, float, oneOf, string)
import Html.Attributes exposing (style)
main =
Html.program { init = init, view = view, update = update , subscriptions = subscriptions }
type Msg = GetMap ( Result Http.Error ( List ( Maybe Town ) ) ) | AskMap
-- INIT
type alias Model = { note: String, polygons: List ( Maybe Town ) }
type alias Polygon = { name: String, coordinates: List ( List ( List Float )) }
type alias MultiPolygon = { name: String , coordinates: List ( List ( List ( List Float ))) }
-- > type Qqq = Abc String | Xyz Int
-- > Abc
-- <function> : String -> Qqq
type Town = Foo MultiPolygon | Bar Polygon
-- type Poly = Muni Polygon | MuniPoly MultiPolygon
init : ( Model, Cmd Msg )
init = ( Model "PR info" ( [] ) , Cmd.none )
-- UPDATE
update msg model =
case msg of
AskMap -> ( model , getPRData )
GetMap (Ok x ) -> ( { model | polygons = x } , Cmd.none )
GetMap (Err _) -> ( { model | note = "error" } , Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model = Sub.none
-- VIEW
divStyle : Html.Attribute Msg
divStyle = style [("width", "250px"), ("display", "inline-block"), ("margin-left", "5px")]
svgStyle : Html.Attribute Msg
svgStyle = style [ ("width", "600px"), ("height", "200px"), ("background-color", "#FAFAFA"), ("margin", "5px") ]
f : List ( List Float ) -> Int
f x = ( List.sum << ( List.map (\a -> 1 ) ) ) x
h: Polygon -> String
h x =
( String.concat
<< List.map ( String.join " " )
<< ( List.map << List.map ) ( String.join "," )
<< ( List.map << List.map )
(\z -> case z of
[] -> []
_ :: [] -> []
[a,b] -> [ toString ( (1.25*(450 - ( 200*(-1*a - 65) ) ) )/0.95 ) , toString (1.25*(150 - ( 200*(b - 17.85) ) )) ]
_ :: _ :: _ :: _ -> []
)
) x.coordinates
drawTiles : Maybe Town -> Svg Msg
drawTiles x = case x of
Nothing -> polygon [] []
Just ( Bar y ) -> polygon [ points <| h y , fill "none", stroke "#000000", strokeWidth "1" ] []
Just ( Foo y ) -> polygon [] []
view model =
div [ ]
[ div [ divStyle ] [ text model.note ]
, div [ divStyle ] [ button [ onClick AskMap ] [ text "get Map" ] ]
, div [ ] [ svg [ svgStyle ] [ g [] <| List.map drawTiles model.polygons ] ]
, div []
<| List.map
(\x ->
case x of
Just ( Bar y ) -> div [ divStyle ]
[ text "# elements: "
, ( text << toString << List.sum << ( List.map f ) ) y.coordinates
]
Just ( Foo y ) -> div [ divStyle ] [ text "nothing"]
Nothing -> div [ divStyle ] [ text "nothing"]
) model.polygons
]
-- HTTP
getPRData : Cmd Msg
getPRData =
let
url = "http://localhost:8000/PR.json"
in
Http.send GetMap <| Http.get url decodePR
--Http.send GetMap <| Http.getString url
-- LANDMARK: this decoder works !
-- discussion on Elm decoders rather limited
-- Fajardo is Multipolygon
-- (resolved via http://miguelrios.github.io/atlaspr/)
-- no monads!!
decodePR : Decoder ( List ( Maybe Town ))
decodePR = ( ( at ["pueblos", "features"] ) << list << maybe ) ( oneOf [ polygonDecoder, multiPolygonDecoder ] )
polygonDecoder : Decoder Town
polygonDecoder = map Bar
<| map2 Polygon
( at [ "properties", "NAME" ] string )
( at [ "geometry", "coordinates" ] <| ( list << list << list ) ( float ) )
multiPolygonDecoder : Decoder Town
multiPolygonDecoder = map Foo
<| map2 MultiPolygon
( at [ "properties", "NAME" ] string )
( at [ "geometry", "coordinates" ] <| ( list << list << list << list ) ( float ) )