diff options
| author | Marc Florisson | 2013-07-24 16:13:23 +0200 |
|---|---|---|
| committer | Marc Florisson | 2013-07-24 16:13:23 +0200 |
| commit | 9aae98be82465f5fce4eac9edaf7f62b46becdc1 (patch) | |
| tree | cad13c0b408fb233e7696ca74fc185747837eb6c /app/exporters | |
| parent | ea4579c46b63be9aa3722049c72afc8b583de8c1 (diff) | |
| download | chouette-core-9aae98be82465f5fce4eac9edaf7f62b46becdc1.tar.bz2 | |
some optimisations for KML export
Diffstat (limited to 'app/exporters')
| -rw-r--r-- | app/exporters/chouette/kml/exporter.rb | 86 |
1 files changed, 63 insertions, 23 deletions
diff --git a/app/exporters/chouette/kml/exporter.rb b/app/exporters/chouette/kml/exporter.rb index aca4a1d09..a7adfab79 100644 --- a/app/exporters/chouette/kml/exporter.rb +++ b/app/exporters/chouette/kml/exporter.rb @@ -1,25 +1,48 @@ class Chouette::Kml::Exporter attr_reader :referential - attr_reader :kml_export + attr_reader :kml_export, :lines, :routes, :journey_patterns def initialize(referential, kml_export) @referential = referential @kml_export = kml_export + @lines = nil + @routes = nil + @journey_patterns = nil end - def lines(object, ids) + def select_lines(object, ids) if object == "network" - ids.present? ? referential.networks.find( ids.split(",") ).collect(&:lines).flatten : referential.networks.collect(&:lines).flatten + ids.present? ? Chouette::Line.includes(:routes).where( :network_id => ids.split(",")).order(:name) : + Chouette::Line.joins(:network).includes(:routes).order(:name) elsif object == "company" - ids.present? ? referential.companies.find( ids.split(",") ).collect(&:lines).flatten : referential.companies.collect(&:lines).flatten - elsif object == "line" - ids.present? ? referential.lines.find( ids.split(",") ): referential.lines + ids.present? ? Chouette::Line.includes(:routes).where( :company_id => ids.split(",")).order(:name) : + Chouette::Line.joins(:company).includes(:routes).order(:name) + elsif object == "line" && ids.present? + Chouette::Line.includes(:routes).where( :id => ids.split(",")).order(:name) else - referential.lines + Chouette::Line.includes(:routes).order(:name) end end + def routes_exportable? + routes && routes.size < 150 + end + + def lines_exportable? + lines && lines.size < 150 + end + + def journey_patterns_exportable? + journey_patterns && journey_patterns.size < 150 + end + + def log_overflow_warning( target_class) + kml_export.log_messages.create( :severity => "warning", :key => "EXPORT_ERROR|EXCEPTION", + :arguments => {"0" => I18n.t( 'export_log_messages.messages.overflow', + :count => 150, :data => target_class.model_name.human.pluralize)}) + end + def export(zip_file_path, options = {}) begin referential.switch @@ -30,35 +53,52 @@ class Chouette::Kml::Exporter Dir.mktmpdir(nil, "/tmp"){ |temp_dir| - lines_collected = lines( options[:o], options[:id] ).sort_by(&:name) - Chouette::Kml::LineExporter.save( lines_collected, temp_dir, kml_export) + @lines = select_lines( options[:o], options[:id] ) + @routes = lines.map(&:routes).flatten.sort {|a,b| (a.name && b.name) ? a.name <=> b.name : a.id <=> b.id} if lines_exportable? + @journey_patterns = Chouette::JourneyPattern.where( :route_id => routes.map(&:id) ).order(:name) if routes_exportable? + + if lines_exportable? + Chouette::Kml::LineExporter.save( lines, temp_dir, kml_export) + else + log_overflow_warning(Chouette::Line) + end - routes = lines_collected.collect(&:routes).flatten.uniq.sort_by(&:name) - Chouette::Kml::RouteExporter.save( routes, temp_dir, kml_export) + if routes_exportable? + Chouette::Kml::RouteExporter.save( routes, temp_dir, kml_export) + else + log_overflow_warning(Chouette::Route) if lines_exportable? + end - journey_patterns = routes.map { |r| r.journey_patterns}.flatten - Chouette::Kml::JourneyPatternExporter.save( journey_patterns, temp_dir, kml_export) + if journey_patterns_exportable? + Chouette::Kml::JourneyPatternExporter.save( journey_patterns, temp_dir, kml_export) + else + log_overflow_warning(Chouette::JourneyPattern) if routes_exportable? + end - stop_areas = lines_collected.collect(&:stop_areas).flatten.uniq.sort_by(&:name) - Chouette::Kml::StopAreaExporter.save( stop_areas, temp_dir, kml_export, "Quay") + # if too many lines + # there may be too many stop_areas + if lines_exportable? + stop_areas = Chouette::StopArea.joins(:children => [:stop_points => [:route => :line] ]).where(:lines => {:id => lines.map(&:id)}).uniq.order(:name) + Chouette::Kml::StopAreaExporter.save( stop_areas, temp_dir, kml_export, "Quay") - commercial_stop_areas = lines_collected.collect(&:commercial_stop_areas).flatten.uniq.sort_by(&:name) - Chouette::Kml::StopAreaExporter.save( commercial_stop_areas, temp_dir, kml_export, "CommercialStopPoint") + commercial_stop_areas = Chouette::StopArea.where( :id => stop_areas.map(&:parent_id).compact.uniq).order(:name) + Chouette::Kml::StopAreaExporter.save( commercial_stop_areas, temp_dir, kml_export, "CommercialStopPoint") + end - if(options[:o].present?) # Add all objects - stop_places = referential.stop_areas.stop_place.sort_by(&:name) + if( options[:o] == "line" and not options[:id].present?) # Add all objects + stop_places = referential.stop_areas.stop_place.order(:name) Chouette::Kml::StopAreaExporter.save( stop_places, temp_dir, kml_export, "StopPlace") - itls = referential.stop_areas.itl.sort_by(&:name) + itls = referential.stop_areas.itl.order(:name) Chouette::Kml::StopAreaExporter.save( itls, temp_dir, kml_export, "ITL") - connection_links = referential.connection_links.sort_by(&:name) + connection_links = referential.connection_links.order(:name) Chouette::Kml::ConnectionLinkExporter.save( connection_links, temp_dir, kml_export) - access_links = referential.access_links.sort_by(&:name) + access_links = referential.access_links.order(:name) Chouette::Kml::AccessLinkExporter.save(access_links, temp_dir, kml_export) - access_points = referential.access_points.sort_by(&:name) + access_points = referential.access_points.order(:name) Chouette::Kml::AccessPointExporter.save(access_points, temp_dir, kml_export) end |
