Class: CartoCSSHelper::VisualDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/cartocss_helper/visualise_changes_image_generation.rb

Defined Under Namespace

Classes: FileDataSource, MapGenerationJob

Constant Summary collapse

@@job_pooling =
false
@@jobs =
[]

Class Method Summary collapse

Class Method Details

.add_job(filename, latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size, image_size, prefix) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 48

def self.add_job(filename, latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size, image_size, prefix)
  print prefix
  new_job = MapGenerationJob.new(filename, latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size, image_size)
  new_job.print

  raise "#{filename} does not exists" unless File.exist?(filename)
  raise "#{latitude} is not a number" unless latitude.is_a? Numeric
  raise "#{longitude} is not a number" unless longitude.is_a? Numeric
  raise "#{zlevels} is not a range" unless zlevels.class == Range
  raise "#{header} is not a string" unless header.class == String
  raise "#{new_branch} is not a string" unless new_branch.class == String
  raise "#{old_branch} is not a string" unless old_branch.class == String
  raise "#{download_bbox_size} is not a number" unless download_bbox_size.is_a? Numeric
  raise "#{image_size} is not a integer" unless image_size.is_a? Integer

  @@jobs.push(new_job)
end

.collect_images_for_real_data_test(latitude, longitude, zlevels, source, image_size = 400) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 178

def self.collect_images_for_real_data_test(latitude, longitude, zlevels, source, image_size = 400)
  collection = []
  zlevels.each do |zlevel|
    render_bbox_size = VisualDiff.get_render_bbox_size(zlevel, image_size, latitude)
    filename = "#{latitude} #{longitude} #{zlevel}zlevel #{image_size}px #{source.get_timestamp} #{source.download_bbox_size}.png"
    unless RendererHandler.image_available_from_cache(latitude, longitude, zlevel, render_bbox_size, image_size, filename)
      source.load
    end
    file_location = RendererHandler.request_image_from_renderer(latitude, longitude, zlevel, render_bbox_size, image_size, filename)
    collection.push(ImageForComparison.new(file_location, "z#{zlevel}"))
  end
  return collection
end

.collect_images_for_synthethic_test(tags, type, on_water, zlevel_range) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 102

def self.collect_images_for_synthethic_test(tags, type, on_water, zlevel_range)
  collection = []
  zlevel_range.each do |zlevel|
    scene = Scene.new(tags, zlevel, on_water, type)
    collection.push(ImageForComparison.new(scene.get_image_filename, "z#{zlevel}"))
  end
  return collection
end

.disable_job_poolingObject



21
22
23
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 21

def self.disable_job_pooling
  @@job_pooling = false
end

.enable_job_poolingObject



14
15
16
17
18
19
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 14

def self.enable_job_pooling
  # it results in avoiding loading the same database mutiple times
  # useful if the same database will be used multiple times (for example the same place in multiple comparisons)
  # use run_jobs function to run jobs
  @@job_pooling = true
end

.get_render_bbox_size(zlevel, wanted_image_size, latitude) ⇒ Object



171
172
173
174
175
176
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 171

def self.get_render_bbox_size(zlevel, wanted_image_size, latitude)
  longitude_equator_rendered_length_in_pixels = 256 * 2**zlevel
  longitude_size = 360 * wanted_image_size.to_f / longitude_equator_rendered_length_in_pixels
  latitude_size = longitude_size * Math.cos(latitude * Math::PI / 180)
  return [latitude_size, longitude_size]
end

.make_header(tags, type, on_water) ⇒ Object



86
87
88
89
90
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 86

def self.make_header(tags, type, on_water)
  on_water_string = ''
  on_water_string = ' on water' if on_water
  return "#{VisualDiff.tag_dict_to_string(tags)} #{type}#{on_water_string}"
end

.pack_image_sets(old, new, header, new_branch, old_branch, image_size) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 192

def self.pack_image_sets(old, new, header, new_branch, old_branch, image_size)
  old_branch = FileHelper.make_string_usable_as_filename(old_branch)
  new_branch = FileHelper.make_string_usable_as_filename(new_branch)
  header_for_filename = FileHelper.make_string_usable_as_filename(header)
  filename_sufix = "#{old_branch} -> #{new_branch}"
  filename = CartoCSSHelper::Configuration.get_path_to_folder_for_output + "#{header_for_filename} #{filename_sufix} #{image_size}px #{RendererHandler.renderer_marking}.png"
  diff = FullSetOfComparedImages.new(old, new, header, filename, image_size)
  diff.save
end

.run_jobsObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 66

def self.run_jobs
  new_job_array = []
  return if @@jobs == []
  @@jobs[0].run_job
  for x in 1..@@jobs.length - 1
    if @@jobs[0].filename == @@jobs[x].filename
      # requires loading the same file as just run job
      # it may be safely run without reloading database
      @@jobs[x].run_job
    else
      new_job_array << @@jobs[x].filename
    end
  end
  @@jobs = new_job_array
end

.shuffle_jobs(seed) ⇒ Object



82
83
84
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 82

def self.shuffle_jobs(seed)
  @@jobs.shuffle!(random: Random.new(seed))
end

.tag_dict_to_string(dict) ⇒ Object



202
203
204
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 202

def self.tag_dict_to_string(dict)
  return OverpassQueryGenerator.turn_list_of_tags_in_overpass_filter(dict)
end

.visualise_for_given_source(latitude, longitude, zlevels, header, new_branch, old_branch, image_size, source) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 163

def self.visualise_for_given_source(latitude, longitude, zlevels, header, new_branch, old_branch, image_size, source)
  Git.checkout old_branch
  old = VisualDiff.collect_images_for_real_data_test(latitude, longitude, zlevels, source, image_size)
  Git.checkout new_branch
  new = VisualDiff.collect_images_for_real_data_test(latitude, longitude, zlevels, source, image_size)
  VisualDiff.pack_image_sets old, new, header, new_branch, old_branch, image_size
end

.visualise_for_location(latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size = 0.4, image_size = 400) ⇒ Object



151
152
153
154
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 151

def self.visualise_for_location(latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size = 0.4, image_size = 400)
  filename = OverpassQueryGenerator.get_file_with_downloaded_osm_data_for_location(latitude, longitude, download_bbox_size)
  visualise_for_location_from_file(filename, latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size, image_size)
end

.visualise_for_location_from_file(filename, latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size = 0.4, image_size = 400) ⇒ Object



156
157
158
159
160
161
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 156

def self.visualise_for_location_from_file(filename, latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size = 0.4, image_size = 400)
  prefix = ''
  prefix = 'pool <- ' if @@job_pooling
  add_job(filename, latitude, longitude, zlevels, header, new_branch, old_branch, download_bbox_size, image_size, prefix)
  run_jobs unless @@job_pooling
end

.visualise_on_overpass_data(tags, type, wanted_latitude, wanted_longitude, zlevels, new_branch, old_branch = 'master') ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 134

def self.visualise_on_overpass_data(tags, type, wanted_latitude, wanted_longitude, zlevels, new_branch, old_branch = 'master')
  # special support for some tag values - see CartoCSSHelper::OverpassQueryGenerator.turn_list_of_tags_in_overpass_filter for details
  header_prefix = "#{VisualDiff.tag_dict_to_string(tags)} #{type} [#{wanted_latitude}, #{wanted_longitude}] -> "
  target_location = '[?, ?]'
  header_sufix = " #{old_branch}->#{new_branch} #{zlevels}"
  puts "visualise_on_overpass_data <#{header_prefix}#{header_sufix}> #{old_branch} -> #{new_branch}"
  begin
    latitude, longitude = OverpassQueryGenerator.locate_element_with_given_tags_and_type tags, type, wanted_latitude, wanted_longitude
    target_location = "[#{latitude}, #{longitude}]"
  rescue OverpassQueryGenerator::NoLocationFound, OverpassDownloader::OverpassRefusedResponse
    puts 'No nearby instances of tags and tag is not extremely rare - no generation of nearby location and wordwide search was impossible. No diff image will be generated for this location.'
    return false
  end
  visualise_for_location(latitude, longitude, zlevels, header_prefix + target_location + header_sufix, new_branch, old_branch)
  return true
end

.visualise_on_synthethic_data(tags, type, on_water, zlevel_range, new_branch, old_branch) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/cartocss_helper/visualise_changes_image_generation.rb', line 92

def self.visualise_on_synthethic_data(tags, type, on_water, zlevel_range, new_branch, old_branch)
  header = make_header(tags, type, on_water)
  puts "visualise_on_synthethic_data <#{header}> #{old_branch} -> #{new_branch}"
  Git.checkout(old_branch)
  old = VisualDiff.collect_images_for_synthethic_test(tags, type, on_water, zlevel_range)
  Git.checkout(new_branch)
  new = VisualDiff.collect_images_for_synthethic_test(tags, type, on_water, zlevel_range)
  VisualDiff.pack_image_sets old, new, header, new_branch, old_branch, 200
end