diff options
| author | Luc Donnet | 2012-03-21 16:23:45 +0100 |
|---|---|---|
| committer | Luc Donnet | 2012-03-21 16:23:45 +0100 |
| commit | f1d2a1e8af696d648c2256633ed795db5bb6978c (patch) | |
| tree | e7f9aeb1fdc91c894dea389fcef64d435f6a531d /app/maps | |
| parent | 0742e3c5f66c7e395ad0c02e1d659ed30338b204 (diff) | |
| download | chouette-core-f1d2a1e8af696d648c2256633ed795db5bb6978c.tar.bz2 | |
Add map to line
Diffstat (limited to 'app/maps')
| -rw-r--r-- | app/maps/application_map.rb | 50 | ||||
| -rw-r--r-- | app/maps/line_map.rb | 31 | ||||
| -rw-r--r-- | app/maps/stop_area_map.rb | 55 | ||||
| -rw-r--r-- | app/maps/style_map/edit_stop_area_style_map.rb | 82 | ||||
| -rw-r--r-- | app/maps/style_map/generic_style_map.rb | 5 | ||||
| -rw-r--r-- | app/maps/style_map/line_style_map.rb | 27 | ||||
| -rw-r--r-- | app/maps/style_map/stop_areas_style_map.rb | 83 |
7 files changed, 333 insertions, 0 deletions
diff --git a/app/maps/application_map.rb b/app/maps/application_map.rb new file mode 100644 index 000000000..c5546f4d9 --- /dev/null +++ b/app/maps/application_map.rb @@ -0,0 +1,50 @@ +class ApplicationMap + + include MapLayers + include MapLayers::ViewHelpers + #delegate :url_helpers, :to => :'Rails.application.routes' + include Rails.application.routes.url_helpers + + def projection(name) + OpenLayers::Projection.new(name) + end + + def id + "map" + end + + def controls + [] + end + + def map + @map ||= MapLayers::Map.new(id, :projection => projection("EPSG:900913"), :controls => controls) + end + + def default_class + self.class.name.underscore.gsub(/_map$/, '') + end + + def name + self.class.name.underscore.gsub(/_map$/, '') + end + + def to_html(options = {}) + "<div id=\"#{id}\" class=\"#{default_class}\"></div> #{map.to_html(options)}" + end + + def kml + OpenLayers::Format::KML.new :extractStyles => true, :extractAttributes => true, :maxDepth => 2 + end + + def strategy_fixed + OpenLayers::Strategy::Fixed.new :preload => true + end + + def kml_layer(url, options = {}) + url = polymorphic_path([url.referential, url], :format => :kml) unless String === url + protocol = OpenLayers::Protocol::HTTP.new :url => url, :format => kml + OpenLayers::Layer::Vector.new name, {:projection => projection("EPSG:4326"), :strategies => [strategy_fixed], :protocol => protocol}.merge(options) + end + +end diff --git a/app/maps/line_map.rb b/app/maps/line_map.rb new file mode 100644 index 000000000..6ac671ae4 --- /dev/null +++ b/app/maps/line_map.rb @@ -0,0 +1,31 @@ + +class LineMap < ApplicationMap + + attr_reader :referential, :line, :line_style + + def initialize(referential, line, line_style = nil) + @referential = referential + @line = line + @line_style = line_style + end + + def controls + [ OpenLayers::Control::Navigation.new ] + end + + def map + @map ||= MapLayers::Map.new(id, :projection => projection("EPSG:900913"), :controls => controls) do |map, page| + page << map.add_layer(MapLayers::OSM_MAPNIK) + #page << map.add_layer(kml_layer(line, :styleMap => StyleMap::LineStyleMap.new( :style => line_style).style_map)) + page << map.add_layer(kml_layer(polymorphic_path([referential, line, :stop_areas], :format => :kml), :styleMap => StyleMap::StopAreasStyleMap.new(true).style_map)) + page << map.zoom_to_extent(bounds) if bounds + end + end + + def bounds + wgs84_bounds = Chouette::StopArea.bounds + @bounds ||= OpenLayers::Bounds.new(wgs84_bounds.lower_corner.x, wgs84_bounds.lower_corner.y, wgs84_bounds.upper_corner.x, wgs84_bounds.upper_corner.y).transform(OpenLayers::Projection.new("EPSG:4326"), OpenLayers::Projection.new("EPSG:900913")) + + end + +end diff --git a/app/maps/stop_area_map.rb b/app/maps/stop_area_map.rb new file mode 100644 index 000000000..009a370ee --- /dev/null +++ b/app/maps/stop_area_map.rb @@ -0,0 +1,55 @@ +class StopAreaMap < ApplicationMap + + attr_reader :stop_area + + attr_accessor :editable + alias_method :editable?, :editable + + def initialize(stop_area) + @stop_area = stop_area + end + + def controls + [ OpenLayers::Control::Navigation.new ] + end + + def map + @map ||= MapLayers::Map.new(id, :projection => projection("EPSG:900913"), :controls => controls) do |map, page| + page << map.add_layer(MapLayers::OSM_MAPNIK) + + stop_area.lines.each do |line| + page << map.add_layer(kml_layer(line, :styleMap => StyleMap::LineStyleMap.new({:line_color => line.color}).style_map)) + page << map.add_layer(kml_layer(polymorphic_path([line.network, line, :stop_areas], :format => :kml), :styleMap => StyleMap::StopAreasStyleMap.new(true).style_map)) + end + + styles = {"default" => {:strokeColor => "red"}, + "select" => {:strokeColor => "red", + :strokeWidth => 4} + } + + page.assign "edit_stop_area_layer", kml_layer( polymorphic_path( [stop_area.network, stop_area], :format => :kml, :default => editable?), :style_map => StyleMap::EditStopAreaStyleMap.new(false, false, {}, styles).style_map) + page << map.add_layer(:edit_stop_area_layer) + + if editable? + # TODO virer ce code inline + page << <<EOF + edit_stop_area_layer.events.on({ + 'afterfeaturemodified': function(event) { + geometry = event.feature.geometry.clone().transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326")); + $('#potimart_stop_area_position').val(geometry.y + ',' + geometry.x); + } + }); +EOF + page << map.add_control(OpenLayers::Control::ModifyFeature.new(:edit_stop_area_layer, :mode => 8, :autoActivate => true)) + + end + + page << map.set_center(center.to_google.to_openlayers, 16, false, true) + end + end + + def center + stop_area.position or stop_area.default_position + end + +end diff --git a/app/maps/style_map/edit_stop_area_style_map.rb b/app/maps/style_map/edit_stop_area_style_map.rb new file mode 100644 index 000000000..5b340cef9 --- /dev/null +++ b/app/maps/style_map/edit_stop_area_style_map.rb @@ -0,0 +1,82 @@ +class StyleMap::EditStopAreaStyleMap < StyleMap::GenericStyleMap + + attr_accessor :styles, :display_label, :context, :terminus + alias_method :terminus?, :terminus + + def initialize(display_label = false, terminus = false, context = {}, styles = {}) + @display_label = display_label + @terminus = terminus + @context = context + @styles = styles + end + + def default_style + style = { + :label => ("${label}" if display_label), + :fontColor => "black", + :fontSize => "11px", + :fontWeight => "bold", + :labelAlign => "ct", + :labelXOffset => 0, + :labelYOffset => -15, + :pointRadius => 4, + :fillColor => "white", + :fillOpacity => 1, + :strokeColor => "black", + :strokeOpacity => 1, + :strokeWidth => 2, + :display => "${display}" + }.tap do |style_basic| + style_basic.merge! :fontSize =>"11px", + :pointRadius => 16, + :externalGraphic => "/images/stop_area_hover.png" if terminus? + end + + if styles["default"].present? + style.merge styles["default"] + else + style + end + end + + def select_style + select_style = { + :label => ("${label}" if display_label), + :fontColor => "black", + :fontSize => "11px", + :fontWeight => "bold", + :labelAlign => "ct", + :labelXOffset => 0, + :labelYOffset => -15, + :pointRadius => 4, + :fillColor => "white", + :fillOpacity => 1, + :strokeColor => "black", + :strokeOpacity => 1, + :strokeWidth => 2, + :display => "${display}" + }.tap do |style_basic| + style_basic.merge! :fontSize =>"11px", + :pointRadius => 16, + :externalGraphic => "/images/stop_area_hover.png" if terminus? + end + if styles["select"].present? + select_style.merge styles["select"] + else + select_style + end + end + + def context + context = { + :label => :"function(feature) { if(feature.layer.map.getZoom() > 15) { return feature.attributes.name;} else {return '';}}" + } + end + + def style_map + OpenLayers::StyleMap.new(:default => OpenLayers::Style.new(default_style, { :context => context}), + :select => OpenLayers::Style.new(select_style, { :context => context}) + ) + end + +end diff --git a/app/maps/style_map/generic_style_map.rb b/app/maps/style_map/generic_style_map.rb new file mode 100644 index 000000000..c905cd00d --- /dev/null +++ b/app/maps/style_map/generic_style_map.rb @@ -0,0 +1,5 @@ +class StyleMap::GenericStyleMap + include MapLayers + include MapLayers::ViewHelpers + +end diff --git a/app/maps/style_map/line_style_map.rb b/app/maps/style_map/line_style_map.rb new file mode 100644 index 000000000..e86e8570f --- /dev/null +++ b/app/maps/style_map/line_style_map.rb @@ -0,0 +1,27 @@ +class StyleMap::LineStyleMap < StyleMap::GenericStyleMap + attr_accessor :style, :line_priority, :line_color + + def initialize(options = {}) + @line_color = options[:line_color] + @style = options[:style].present? ? default_style.merge(options[:style]) : default_style + end + + def stroke_width + stroke_width = 3 + end + + def default_style + style = { + :strokeColor => line_color || "#000000", + :strokeOpacity => 1, + :strokeWidth => stroke_width, + :strokeLineCap => "round", + :strokeDashstyle => "solid", + } + end + + def style_map + OpenLayers::StyleMap.new(:default => OpenLayers::Style.new(style)) + end + +end diff --git a/app/maps/style_map/stop_areas_style_map.rb b/app/maps/style_map/stop_areas_style_map.rb new file mode 100644 index 000000000..4cf169345 --- /dev/null +++ b/app/maps/style_map/stop_areas_style_map.rb @@ -0,0 +1,83 @@ +class StyleMap::StopAreasStyleMap < StyleMap::GenericStyleMap + + attr_accessor :styles, :display_label, :context, :terminus + alias_method :terminus?, :terminus + + def initialize(display_label = false, terminus = false, context = {}, styles = {}) + @display_label = display_label + @terminus = terminus + @context = context + @styles = styles + end + + def default_style + style = { + :label => ("${label}" if display_label), + :fontColor => "black", + :fontSize => "11px", + :fontWeight => "bold", + :labelAlign => "ct", + :labelXOffset => 0, + :labelYOffset => -15, + :pointRadius => 4, + :fillColor => "white", + :fillOpacity => 1, + :strokeColor => "black", + :strokeOpacity => 1, + :strokeWidth => 2, + :display => true + }.tap do |style_basic| + style_basic.merge! :fontSize =>"11px", + :pointRadius => 16, + :externalGraphic => "/assets/icons/stop_area_hover.png" if terminus? + end + + if styles["default"].present? + style.merge styles["default"] + else + style + end + end + + def select_style + select_style = { + :label => ("${label}" if display_label), + :fontColor => "black", + :fontSize => "11px", + :fontWeight => "bold", + :labelAlign => "ct", + :labelXOffset => 0, + :labelYOffset => -15, + :pointRadius => 4, + :fillColor => "white", + :fillOpacity => 1, + :strokeColor => "black", + :strokeOpacity => 1, + :strokeWidth => 2, + :display => true + }.tap do |style_basic| + style_basic.merge! :fontSize =>"11px", + :pointRadius => 16, + :externalGraphic => "/assets/icons/stop_area_hover.png" if terminus? + end + if styles["select"].present? + select_style.merge styles["select"] + else + select_style + end + end + + def context + context = { + :label => :"function(feature) {if(feature.layer.map.getZoom() > 15) { return feature.attributes.name;} else {return '';}}", + #:display => :"function(feature) {if(feature.layer.map.getZoom() > 13) { return true;} else {return 'none';}}" + } + end + + def style_map + OpenLayers::StyleMap.new(:default => OpenLayers::Style.new(default_style, { :context => context}), + :select => OpenLayers::Style.new(select_style, { :context => context}) + ) + end + +end |
