Class: Natour::MapGeoAdmin::MapServlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/natour/map_geo_admin.rb

Instance Method Summary collapse

Instance Method Details

#do_GET(request, response) ⇒ Object

rubocop:disable Naming/MethodName

Raises:

  • (WEBrick::HTTPStatus::NotFound)


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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/natour/map_geo_admin.rb', line 82

def do_GET(request, response) # rubocop:disable Naming/MethodName
  raise WEBrick::HTTPStatus::NotFound unless request.path == '/map'

  files = request.query.fetch('gps-files', '').split(',')
  colors = request.query.fetch('gps-colors', '').split(',')
  (files.size - colors.size).times { colors << (colors.last || 'blueviolet') }
  layers = ['ch.swisstopo.pixelkarte-farbe']
  layers |= request.query.fetch('map-layers', '').split(',')

  width, height = request.query.fetch('map-size', '').split(',')
  raise WEBrick::HTTPStatus::BadRequest unless width && height

  doc = []
  doc << '<html><head>'
  doc << '<link rel="icon" href="data:;base64,iVBORw0KGgo=">'
  doc << '<script src="js/jquery-3.5.1.slim.min.js"></script>'
  doc << '<script src="js/bootstrap.min.js"></script>'
  doc << '<script src="js/loader.js"></script>'
  doc << '<script type="text/javascript">'
  doc << '  $(function() {'
  doc << '    let map = new ga.Map({'
  doc << '      controls: [],'
  doc << '      target: "map",'
  doc << '      view: new ol.View()'
  doc << '    })'
  doc << "    let layers = [#{layers.map { |layer| "\"#{layer}\"" }.join(', ')}]"
  doc << '    layers.forEach(function(layer) {'
  doc << '      map.addLayer(ga.layer.create(layer))'
  doc << '    })'
  doc << '    let getStyles = function(color) {'
  doc << '      return {'
  doc << '        "Point": new ol.style.Style({'
  doc << '          image: new ol.style.Circle({'
  doc << '            fill: new ol.style.Fill({'
  doc << '              color: function() {'
  doc << '                let [r, g, b, a] = ol.color.asArray(color)'
  doc << '                a = 0.3'
  doc << '                return ol.color.asString([r, g, b, a])'
  doc << '              }()'
  doc << '            }),'
  doc << '            radius: 6,'
  doc << '            stroke: new ol.style.Stroke({'
  doc << '              color: color,'
  doc << '              width: 1.5'
  doc << '            })'
  doc << '          })'
  doc << '        }),'
  doc << '        "LineString": new ol.style.Style({'
  doc << '          stroke: new ol.style.Stroke({'
  doc << '            color: color,'
  doc << '            width: 3'
  doc << '          })'
  doc << '        }),'
  doc << '        "MultiLineString": new ol.style.Style({'
  doc << '          stroke: new ol.style.Stroke({'
  doc << '            color: color,'
  doc << '            width: 3'
  doc << '          })'
  doc << '        })'
  doc << '      }'
  doc << '    }'
  doc << "    let sources = [#{files.zip(colors)
                                    .map { |file, color| "{ file: \"#{file}\", color: \"#{color}\" }" }
                                    .join(', ')}]"
  doc << '    let vectors = sources.map(function(source) {'
  doc << '      return new ol.layer.Vector({'
  doc << '        source: new ol.source.Vector({'
  doc << '          format: function() {'
  doc << '            if (source.file.endsWith(".gpx")) {'
  doc << '              return new ol.format.GPX()'
  doc << '            } else if (source.file.endsWith(".kml")) {'
  doc << '              return new ol.format.KML({'
  doc << '                extractStyles: false'
  doc << '              })'
  doc << '            } else {'
  doc << '              return null'
  doc << '            }'
  doc << '          }(),'
  doc << '          url: source.file'
  doc << '        }),'
  doc << '        style: function(feature) {'
  doc << '          return getStyles(source.color)[feature.getGeometry().getType()]'
  doc << '        }'
  doc << '      })'
  doc << '    })'
  doc << '    vectors.forEach(function(vector) {'
  doc << '      map.addLayer(vector)'
  doc << '      vector.getSource().on("change", function(evt) {'
  doc << '        let extent = ol.extent.createEmpty()'
  doc << '        vectors.forEach(function(vector) {'
  doc << '          ol.extent.extend(extent, vector.getSource().getExtent())'
  doc << '        })'
  doc << '        map.getView().fit(extent, map.getSize())'
  doc << '      })'
  doc << '    })'
  doc << '  })'
  doc << '</script>'
  doc << '<style type="text/css">'
  doc << '  .map {'
  doc << "    width: #{width};"
  doc << "    height: #{height};"
  doc << '  }'
  doc << '</style>'
  doc << '</head><body><div id="map" class="map"></div></body></html>'
  response.body = doc.join("\n")
  response['Content-Type'] = 'text/html'
end