aboutsummaryrefslogtreecommitdiffstats
path: root/app/maps/application_map.rb
blob: 19b9038eca9c68099fac5266caaca956180e60de (plain)
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
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
class ApplicationMap

  include MapLayers
  include MapLayers::ViewHelpers

  attr_accessor :helpers
  cattr_accessor :ign_api_key

  def helpers
    @helpers ||= Helpers.new
  end

  # For example, in a controller :
  #
  #   @map = MyMap.new(...).with_helpers(self)
  #
  def with_helpers(helpers)
    self.helpers = helpers
    self
  end

  def geoportail_key
    if ( Rails.application.secrets.respond_to? :geoportail_api_key)
      return Rails.application.secrets.geoportail_api_key
    end
    return nil
  end

  class Helpers
    include ActionDispatch::Routing::UrlFor
    include Rails.application.routes.url_helpers
  end

  def projection(name)
    OpenLayers::Projection.new(name)
  end

  def controls
    [ OpenLayers::Control::LayerSwitcher.new(:ascending => true),
      OpenLayers::Control::ScaleLine.new,
      OpenLayers::Control::Navigation.new,
      OpenLayers::Control::PanZoomBar.new,
      OpenLayers::Control::Attribution.new]
  end

  def id
    "map"
  end

  def map
    @map ||= MapLayers::Map.new(id, :projection => projection("EPSG:900913"), :controls => controls) do |map, page|
      if self.geoportail_key
        page << map.add_layer(geoportail_ortho_wmts)
        page << map.add_layer(geoportail_scans_wmts)
        page << map.add_layer(geoportail_cadastre_wmts)
      end

      page << map.add_layer(MapLayers::OSM_MAPNIK)
      #page << map.add_layers([geoportail_scans, geoportail_ortho])
      page << map.add_layer(google_physical)
      page << map.add_layer(google_streets)
      page << map.add_layer(google_hybrid)
      page << map.add_layer(google_satellite)

      customize_map(map,page) if respond_to?( :customize_map)
    end
  end

  def name
    self.class.name.underscore.gsub(/_map$/, '')
  end
  alias_method :default_class, :name

  def to_html(options = {})
    if not respond_to?(:ready?) or ready?
      expand = options[:no_fullscreen] ? '' : "<button type=\"button\" data-ce-id=\"#{id}\" data-ce-action=\"map-fullscreen\" class=\"ce-MapExpandBlock\"><i class=\"fa fa-expand\"></i></button>"
      "<div id=\"#{id}\" class=\"#{default_class}\">#{expand}</div>#{map.to_html(options)}".html_safe
    end
  end

  def kml
    OpenLayers::Format::KML.new :extractStyles => true, :extractAttributes => true, :maxDepth => 2
  end
  def geoportail_cadastre_wmts
    OpenLayers::Layer::WMTS.new( geoportail_options.update(:name => I18n.t("maps.ign_cadastre"),
        :format => "image/png",
        :layer => "CADASTRALPARCELS.PARCELS"))
  end

  def geoportail_ortho_wmts
    OpenLayers::Layer::WMTS.new( geoportail_options.update(:name => I18n.t("maps.ign_ortho"),
        :layer => "ORTHOIMAGERY.ORTHOPHOTOS"))
  end

  def geoportail_scans_wmts
    OpenLayers::Layer::WMTS.new( geoportail_options.update(:name => I18n.t("maps.ign_map"),
        :layer => "GEOGRAPHICALGRIDSYSTEMS.MAPS"))
  end

  def geoportail_options
    { :url => "http://gpp3-wxs.ign.fr/#{self.geoportail_key}/wmts",
        :matrixSet => "PM",
        :style => "normal",
        :numZoomLevels => 19,
        :group => "IGN",
	:attribution => 'Map base: &copy;IGN <a href="http://www.geoportail.fr/" target="_blank"><img src="http://api.ign.fr/geoportail/api/js/2.0.0beta/theme/geoportal/img/logo_gp.gif"></a> <a href="http://www.geoportail.gouv.fr/depot/api/cgu/licAPI_CGUF.pdf" alt="TOS" title="TOS" target="_blank">Terms of Service</a>'}
  end

  def strategy_fixed
    OpenLayers::Strategy::Fixed.new :preload => true
  end

  def google_physical
    OpenLayers::Layer::Google.new(I18n.t("maps.google_physical"),
                      :type => :"google.maps.MapTypeId.TERRAIN")
  end

  def google_streets
    OpenLayers::Layer::Google.new I18n.t("maps.google_streets"), :numZoomLevels => 20
  end

  def google_hybrid
    OpenLayers::Layer::Google.new I18n.t("maps.google_hybrid"), :type => :"google.maps.MapTypeId.HYBRID", :numZoomLevels => 20
  end

  def google_satellite
    OpenLayers::Layer::Google.new I18n.t("maps.google_satellite"), :type => :"google.maps.MapTypeId.SATELLITE", :numZoomLevels => 22
  end

  def hover_control_display_name(layer)
    OpenLayers::Control::SelectFeature.new( layer, {
                                              :autoActivate => true,
                                              :hover => true,
                                              :renderIntent => "temporary",
                                              :eventListeners => {
                                                :featurehighlighted => JsExpr.new("function(e) {
          var feature = e.feature ;
          if (feature.attributes.inactive != undefined)
            return;
          var stop_area_type_label = '';
          if (feature.attributes.stop_area_type_label != undefined)
            stop_area_type_label = feature.attributes.stop_area_type_label;
          var popup = new OpenLayers.Popup.Anchored('chicken',
                                                 new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y),
                                                 null,
                                                 \"<div class='popup_hover'><p><b>\" + feature.attributes.name +\"</b></p>\" + stop_area_type_label + \"</div> \", null, false, null);
          popup.autoSize = true;
          popup.displayClass = 'popup_hover';

          feature.popup = popup;
          map.addPopup(popup);
        }"),
                                                :featureunhighlighted => JsExpr.new("function(e) {
          var feature = e.feature;
          if (feature.attributes.inactive != undefined)
            return;
          map.removePopup(feature.popup);
          feature.popup.destroy();
          feature.popup = null;
        }")
                                              } } )
  end

  def kml_layer(url_or_object, options_or_url_options = {}, options = nil)
    unless options
      url_options = {}
      options = options_or_url_options
    else
      url_options = options_or_url_options
    end

    url_options = url_options.merge(:format => :kml)

    url =
      case url_or_object
      when String
        url_or_object
      when Array
        helpers.polymorphic_path_patch( helpers.polymorphic_path(url_or_object, url_options))
      else
        helpers.polymorphic_path_patch( helpers.polymorphic_path([url_or_object.referential, url_or_object], url_options))
      end

    protocol = OpenLayers::Protocol::HTTP.new :url => url, :format => kml
    OpenLayers::Layer::Vector.new name, {:projection => projection("EPSG:4326"), :strategies => [strategy_fixed], :protocol => protocol, :displayInLayerSwitcher => false}.merge(options)
  end

end