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)


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
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
# File 'lib/natour/map_geo_admin.rb', line 72

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

  files = request.query.fetch('gps-files', '').split(',')
  layers = request.query.fetch('map-layers', '').split(',')
  layers.unshift('ch.swisstopo.pixelkarte-farbe')

  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 << '    var map = new ga.Map({'
  doc << '      controls: [],'
  doc << '      target: "map",'
  doc << '      view: new ol.View()'
  doc << '    })'
  doc << "    var layers = [#{layers.map { |layer| "\"#{layer}\"" }.join(', ')}]"
  doc << '    layers.forEach(function(layer) {'
  doc << '      map.addLayer(ga.layer.create(layer))'
  doc << '    })'
  doc << '    var styles = {'
  doc << '      "Point": new ol.style.Style({'
  doc << '        image: new ol.style.Circle({'
  doc << '          fill: new ol.style.Fill({'
  doc << '            color: function() {'
  doc << '              var color = "blueviolet"'
  doc << '              var alpha = 0.3'
  doc << '              var [r, g, b] = ol.color.asArray(color)'
  doc << '              return ol.color.asString([r, g, b, alpha])'
  doc << '            }()'
  doc << '          }),'
  doc << '          radius: 6,'
  doc << '          stroke: new ol.style.Stroke({'
  doc << '            color: "blueviolet",'
  doc << '            width: 1.5'
  doc << '          })'
  doc << '        })'
  doc << '      }),'
  doc << '      "LineString": new ol.style.Style({'
  doc << '        stroke: new ol.style.Stroke({'
  doc << '          color: "blueviolet",'
  doc << '          width: 3'
  doc << '        })'
  doc << '      }),'
  doc << '      "MultiLineString": new ol.style.Style({'
  doc << '        stroke: new ol.style.Stroke({'
  doc << '          color: "blueviolet",'
  doc << '          width: 3'
  doc << '        })'
  doc << '      })'
  doc << '    }'
  doc << "    var files = [#{files.map { |file| "\"#{file}\"" }.join(', ')}]"
  doc << '    var vectors = files.map(function(file) {'
  doc << '      return new ol.layer.Vector({'
  doc << '        source: new ol.source.Vector({'
  doc << '          format: function() {'
  doc << '            if (file.endsWith(".gpx")) {'
  doc << '              return new ol.format.GPX()'
  doc << '            } else if (file.endsWith(".kml")) {'
  doc << '              return new ol.format.KML({'
  doc << '                extractStyles: false'
  doc << '              })'
  doc << '            } else {'
  doc << '              return null'
  doc << '            }'
  doc << '          }(),'
  doc << '          url: file'
  doc << '        }),'
  doc << '        style: function(feature) {'
  doc << '          return styles[feature.getGeometry().getType()]'
  doc << '        }'
  doc << '      })'
  doc << '    })'
  doc << '    vectors.forEach(function(vector) {'
  doc << '      map.addLayer(vector)'
  doc << '      vector.getSource().on("change", function(evt) {'
  doc << '        var 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