From 8a49fcaa47610418657be68444db8080f5a93c0a Mon Sep 17 00:00:00 2001 From: jywarren Date: Fri, 12 Apr 2019 14:30:15 -0500 Subject: [PATCH 1/3] fully externalize exporter; untested --- Gemfile | 2 + app/models/map.rb | 5 +- app/models/warpable.rb | 5 +- lib/exporter.rb | 370 ------------------------------------- test/unit/exporter_test.rb | 57 ------ 5 files changed, 7 insertions(+), 432 deletions(-) delete mode 100644 lib/exporter.rb delete mode 100644 test/unit/exporter_test.rb diff --git a/Gemfile b/Gemfile index 1a4e65be5..2f1af2be2 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,8 @@ group :dependencies do gem "autoprefixer-rails" gem "uglifier" + # switch to main version once ready: + gem "mapknitter-exporter", git: 'https://github.com/publiclab/mapknitter-exporter', branch: 'add_cartagen', glob: '*.gemspec' end group :test do diff --git a/app/models/map.rb b/app/models/map.rb index a21a93486..48da64104 100755 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -210,13 +210,12 @@ def run_export(user, resolution) :map_id => id }) end - Exporter.run_export(user, + MapKnitterExporter.run_export( + user.id, resolution, self.export, - self.id, self.slug, Rails.root.to_s, - self.average_scale, self.placed_warpables, key) end diff --git a/app/models/warpable.rb b/app/models/warpable.rb index 68f0153f8..5ab232e25 100755 --- a/app/models/warpable.rb +++ b/app/models/warpable.rb @@ -1,3 +1,5 @@ +require "mapknitterExporter" + class Warpable < ActiveRecord::Base attr_accessible :image @@ -141,9 +143,8 @@ def url=(uri) self.uploaded_data = io end - # TODO: simplify/reduce # of parameters needed here: def generate_perspectival_distort(pxperm, path) - Exporter.generate_perspectival_distort(pxperm, path, nodes_array, id, image_file_name, image, height, width) + MapKnitterExporter.generate_perspectival_distort(pxperm, path, nodes_array, image_file_name, image.url(:original), height, width, Rails.root) end def user_id diff --git a/lib/exporter.rb b/lib/exporter.rb deleted file mode 100644 index 057cdf50e..000000000 --- a/lib/exporter.rb +++ /dev/null @@ -1,370 +0,0 @@ -class Exporter - - def self.ulimit - # use ulimit to restrict to 7200 CPU seconds and 5gb virtual memory, and 5gB file storage: - #"ulimit -t 7200 && ulimit -v 5000000 && ulimit -f 5000000 && " - "ulimit -t 14400 && ulimit -v 5000000 && ulimit -f 10000000 && nice -n 19 " - end - - def self.get_working_directory(path) - "public/warps/" + path + "-working/" - end - - def self.warps_directory(path) - "public/warps/" + path + "/" - end - - def self.delete_temp_files(path) - system('rm -r ' + get_working_directory(path)) - system('rm ' + warps_directory(path) + '*.png') - end - - ######################## - ## Run on each image: - - # pixels per meter = pxperm - def self.generate_perspectival_distort(pxperm, path, nodes_array, id, image_file_name, image, height, width) - require 'net/http' - - # everything in -working/ can be deleted; - # this is just so we can use the files locally outside of s3 - working_directory = get_working_directory(path) - Dir.mkdir(working_directory) unless (File.exists?(working_directory) && File.directory?(working_directory)) - local_location = working_directory+id.to_s+'-'+image_file_name.to_s - - directory = warps_directory(path) - Dir.mkdir(directory) unless (File.exists?(directory) && File.directory?(directory)) - completed_local_location = directory+id.to_s+'.png' - - # everything -masked.png can be deleted - masked_local_location = directory+id.to_s+'-masked.png' - # everything -mask.png can be deleted - mask_location = directory+id.to_s+'-mask.png' - #completed_local_location = directory+id.to_s+'.tif' - # know everything -unwarped can be deleted - geotiff_location = directory+id.to_s+'-geo-unwarped.tif' - # everything -geo WITH AN ID could be deleted, but there is a feature request to preserve these - warped_geotiff_location = directory+id.to_s+'-geo.tif' - - northmost = nodes_array.first.lat - southmost = nodes_array.first.lat - westmost = nodes_array.first.lon - eastmost = nodes_array.first.lon - - nodes_array.each do |node| - northmost = node.lat if node.lat > northmost - southmost = node.lat if node.lat < southmost - westmost = node.lon if node.lon < westmost - eastmost = node.lon if node.lon > eastmost - end - - # puts northmost.to_s+','+southmost.to_s+','+westmost.to_s+','+eastmost.to_s - - scale = 20037508.34 - y1 = pxperm*Cartagen.spherical_mercator_lat_to_y(northmost,scale) - x1 = pxperm*Cartagen.spherical_mercator_lon_to_x(westmost,scale) - y2 = pxperm*Cartagen.spherical_mercator_lat_to_y(southmost,scale) - x2 = pxperm*Cartagen.spherical_mercator_lon_to_x(eastmost,scale) - # puts x1.to_s+','+y1.to_s+','+x2.to_s+','+y2.to_s - - # should determine if it's stored in s3 or locally: - if (image.url[0..3] == 'http') - Net::HTTP.start('s3.amazonaws.com') { |http| - #Net::HTTP.start('localhost') { |http| - puts (image.url) - resp = http.get(image.url) - open(local_location, "wb") { |file| - file.write(resp.body) - } - } - else - require "fileutils" - FileUtils.cp(Rails.root.to_s+'/public'+image.to_s,local_location) - end - - points = "" - maskpoints = "" - coordinates = "" - first = true - -#EXIF orientation values: -#Value 0th Row 0th Column -#1 top left side -#2 top right side -#3 bottom right side -#4 bottom left side -#5 left side top -#6 right side top -#7 right side bottom -#8 left side bottom - - rotation = (`identify -format %[exif:Orientation] #{local_location}`).to_i - #stdin, stdout, stderr = Open3.popen3('identify -format %[exif:Orientation] #{local_location}') - #rotation = stdout.readlines.first.to_s.to_i - #puts stderr.readlines - - if rotation == 6 - puts 'rotated CCW' - source_corners = source_corners = [[0,height],[0,0],[width,0],[width,height]] - elsif rotation == 8 - puts 'rotated CW' - source_corners = [[width,0],[width,height],[0,height],[0,0]] - elsif rotation == 3 - puts 'rotated 180 deg' - source_corners = [[width,height],[0,height],[0,0],[width,0]] - else - source_corners = [[0,0],[width,0],[width,height],[0,height]] - end - - maxdimension = 0 - - nodes_array.each do |node| - corner = source_corners.shift - nx1 = corner[0] - ny1 = corner[1] - nx2 = -x1+(pxperm*Cartagen.spherical_mercator_lon_to_x(node.lon,scale)) - ny2 = y1-(pxperm*Cartagen.spherical_mercator_lat_to_y(node.lat,scale)) - - points = points + ' ' unless first - maskpoints = maskpoints + ' ' unless first - points = points + nx1.to_s + ',' + ny1.to_s + ' ' + nx2.to_i.to_s + ',' + ny2.to_i.to_s - maskpoints = maskpoints + nx2.to_i.to_s + ',' + ny2.to_i.to_s - first = false - # we need to find an origin; find northwestern-most point - coordinates = coordinates+' -gcp '+nx2.to_s+', '+ny2.to_s+', '+node.lon.to_s + ', ' + node.lat.to_s - - # identify largest dimension to set canvas size for ImageMagick: - maxdimension = nx1.to_i if maxdimension < nx1.to_i - maxdimension = ny1.to_i if maxdimension < ny1.to_i - maxdimension = nx2.to_i if maxdimension < nx2.to_i - maxdimension = ny2.to_i if maxdimension < ny2.to_i - end - - # close mask polygon: - maskpoints = maskpoints + ' ' - nx2 = -x1+(pxperm*Cartagen.spherical_mercator_lon_to_x(nodes_array.first.lon,scale)) - ny2 = y1-(pxperm*Cartagen.spherical_mercator_lat_to_y(nodes_array.first.lat,scale)) - maskpoints = maskpoints + nx2.to_i.to_s + ',' + ny2.to_i.to_s - - height = (y1-y2).to_i.to_s - width = (-x1+x2).to_i.to_s - - # http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=11319 - # http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=8764 - # read about equalization - # -equalize - # -contrast-stretch 0 - - imageMagick = "convert " - imageMagick += "-contrast-stretch 0 " - imageMagick += local_location+" " - imageMagick += "-crop "+maxdimension.to_i.to_s+"x"+maxdimension.to_i.to_s+"+0+0! " - imageMagick += "-flatten " - imageMagick += "-distort Perspective '"+points+"' " - imageMagick += "-flatten " - if width > height - imageMagick += "-crop "+width+"x"+width+"+0+0\! " - else - imageMagick += "-crop "+height+"x"+height+"+0+0\! " - end - imageMagick += "+repage " - imageMagick += completed_local_location - puts imageMagick - system(self.ulimit+imageMagick) - - # create a mask (later we can blur edges here) - imageMagick2 = 'convert +antialias ' - if width > height - imageMagick2 += "-size "+width+"x"+width+" " - else - imageMagick2 += "-size "+height+"x"+height+" " - end - # attempt at blurred edges in masking, but I've given up, as gdal_merge doesn't seem to respect variable-opacity alpha channels - imageMagick2 += ' xc:none -draw "fill black stroke red stroke-width 30 polyline ' - imageMagick2 += maskpoints + '" ' - imageMagick2 += ' -alpha set -channel A -transparent red -blur 0x8 -channel R -evaluate set 0 +channel '+mask_location - #imageMagick2 += ' xc:none -draw "fill black stroke none polyline ' - #imageMagick2 += maskpoints + '" ' - #imageMagick2 += ' '+mask_location - puts imageMagick2 - system(self.ulimit+imageMagick2) - - imageMagick3 = 'composite '+mask_location+' '+completed_local_location+' -compose DstIn -alpha Set '+masked_local_location - puts imageMagick3 - system(self.ulimit+imageMagick3) - - gdal_translate = "gdal_translate -of GTiff -a_srs EPSG:4326 "+coordinates+' -co "TILED=NO" '+masked_local_location+' '+geotiff_location - puts gdal_translate - system(self.ulimit+gdal_translate) - - #gdalwarp = 'gdalwarp -srcnodata "255" -dstnodata 0 -cblend 30 -of GTiff -t_srs EPSG:4326 '+geotiff_location+' '+warped_geotiff_location - gdalwarp = 'gdalwarp -of GTiff -t_srs EPSG:4326 '+geotiff_location+' '+warped_geotiff_location - puts gdalwarp - system(self.ulimit+gdalwarp) - - # deletions could happen here; do it in distinct method so we can run it independently - delete_temp_files(path) - - [x1,y1] - end - - ######################## - ## Run on maps: - - # distort all warpables, returns upper left corner coords in x,y - def self.distort_warpables(scale, warpables, export, slug) - - puts '> generating geotiffs of each warpable in GDAL' - lowest_x=0 - lowest_y=0 - warpable_coords = [] - current = 0 - warpables.each do |warpable| - current += 1 - - ## TODO: refactor to generate static status file: - export.status = 'warping '+current.to_s+' of '+warpables.length.to_s - puts 'warping '+current.to_s+' of '+warpables.length.to_s - export.save - ## - - my_warpable_coords = warpable.generate_perspectival_distort(scale,slug) - puts '- '+my_warpable_coords.to_s - warpable_coords << my_warpable_coords - lowest_x = my_warpable_coords.first if (my_warpable_coords.first < lowest_x || lowest_x == 0) - lowest_y = my_warpable_coords.last if (my_warpable_coords.last < lowest_y || lowest_y == 0) - end - [lowest_x,lowest_y,warpable_coords] - end - - # generate a tiff from all warpable images in this set - def self.generate_composite_tiff(coords, origin, placed_warpables, slug, ordered) - directory = "public/warps/"+slug+"/" - composite_location = directory+slug+'-geo.tif' - geotiffs = '' - minlat = nil - minlon = nil - maxlat = nil - maxlon = nil - placed_warpables.each do |warpable| - warpable.nodes_array.each do |n| - minlat = n.lat if minlat == nil || n.lat < minlat - minlon = n.lon if minlon == nil || n.lon < minlon - maxlat = n.lat if maxlat == nil || n.lat > maxlat - maxlon = n.lon if maxlon == nil || n.lon > maxlon - end - end - first = true - if ordered != true - # sort by area; this would be overridden by a provided order - warpables = placed_warpables.sort{|a,b|b.poly_area <=> a.poly_area} - end - warpables.each do |warpable| - geotiffs += ' '+directory+warpable.id.to_s+'-geo.tif' - if first - gdalwarp = "gdalwarp -s_srs EPSG:3857 -te "+minlon.to_s+" "+minlat.to_s+" "+maxlon.to_s+" "+maxlat.to_s+" "+directory+warpable.id.to_s+'-geo.tif '+directory+slug+'-geo.tif' - first = false - else - gdalwarp = "gdalwarp "+directory+warpable.id.to_s+'-geo.tif '+directory+slug+'-geo.tif' - end - puts gdalwarp - system(self.ulimit+gdalwarp) - end - composite_location - end - - # generates a tileset at root/public/tms// - # root is something like https://mapknitter.org - def self.generate_tiles(key, slug, root) - key = "AIzaSyAOLUQngEmJv0_zcG1xkGq-CXIPpLQY8iQ" if key == "" # ugh, let's clean this up! - key = key || "AIzaSyAOLUQngEmJv0_zcG1xkGq-CXIPpLQY8iQ" - gdal2tiles = 'gdal2tiles.py -k --s_srs EPSG:3857 -t "'+slug+'" -g "'+key+'" '+root+'/public/warps/'+slug+'/'+slug+'-geo.tif '+root+'/public/tms/'+slug+"/" - puts gdal2tiles - system(self.ulimit+gdal2tiles) - end - - # zips up tiles at root/public/tms/.zip; - def self.zip_tiles(slug) - rmzip = 'cd public/tms/ && rm '+slug+'.zip && cd ../../' - system(rmzip) - zip = 'cd public/tms/ && ' + self.ulimit + 'zip -rq '+slug+'.zip '+slug+'/ && cd ../../' - system(zip) - end - - # generates a tileset at root/public/tms// - def self.generate_jpg(slug, root) - imageMagick = 'convert -background white -flatten '+root+'/public/warps/'+slug+'/'+slug+'-geo.tif '+root+'/public/warps/'+slug+'/'+slug+'.jpg' - system(self.ulimit+imageMagick) - end - - # runs the above map functions while maintaining a record of state in an Export model; - # we'll be replacing the export model state with a flat status file - def self.run_export(user,resolution,export,id,slug,root,average_scale,placed_warpables,key) - begin - export.user_id = user.id if user - export.status = 'starting' - export.tms = false - export.geotiff = false - export.zip = false - export.jpg = false - export.save - - directory = "#{root}/public/warps/"+slug+"/" - stdin, stdout, stderr = Open3.popen3('rm -r '+directory.to_s) - puts stdout.readlines - puts stderr.readlines - stdin, stdout, stderr = Open3.popen3("rm -r #{root}/public/tms/#{slug}") - puts stdout.readlines - puts stderr.readlines - - puts '> averaging scales; resolution: ' + resolution.to_s - pxperm = 100/(resolution).to_f || average_scale # pixels per meter - puts '> scale: ' + pxperm.to_s + 'pxperm' - - puts '> distorting warpables' - - origin = self.distort_warpables(pxperm, placed_warpables, export, slug) - warpable_coords = origin.pop - - export.status = 'compositing' - export.save - - puts '> generating composite tiff' - composite_location = self.generate_composite_tiff(warpable_coords,origin,placed_warpables,slug,false) # no ordering yet - - info = (`identify -quiet -format '%b,%w,%h' #{composite_location}`).split(',') - puts info - - if info[0] != '' - export.geotiff = true - export.size = info[0] - export.width = info[1] - export.height = info[2] - export.cm_per_pixel = 100.0000/pxperm - export.status = 'tiling' - export.save - end - - puts '> generating tiles' - export.tms = true if self.generate_tiles(key, slug, root) - export.status = 'zipping tiles' - export.save - - puts '> zipping tiles' - export.zip = true if self.zip_tiles(slug) - export.status = 'creating jpg' - export.save - - puts '> generating jpg' - export.jpg = true if self.generate_jpg(slug, root) - export.status = 'complete' - export.save - - rescue SystemCallError - export.status = 'failed' - export.save - end - return export.status - end - -end diff --git a/test/unit/exporter_test.rb b/test/unit/exporter_test.rb deleted file mode 100644 index d372e6688..000000000 --- a/test/unit/exporter_test.rb +++ /dev/null @@ -1,57 +0,0 @@ -require 'test_helper' - -class ExporterTest < ActiveSupport::TestCase - test "isolated exporter lib" do - - # make a sample image - system('mkdir -p public/system/images/1/original') - system('cp test/fixtures/demo.png public/system/images/1/original/') - system('mkdir -p public/warps/saugus-landfill-incinerator') - system('mkdir -p public/tms/saugus-landfill-incinerator') - system('touch public/warps/saugus-landfill-incinerator/folder') - assert File.exist?('public/warps/saugus-landfill-incinerator/folder') - - scale = 2 - - w = warpables(:one) - coords = Exporter.generate_perspectival_distort(scale, w.map.slug, w.nodes_array, w.id, w.image_file_name, w.image, w.height, w.width) - assert coords - assert Exporter.get_working_directory(w.map.slug) - assert Exporter.warps_directory(w.map.slug) - - map = Map.first - - # get rid of existing geotiff - system('rm -r public/warps/saugus-landfill-incinerator/1-geo.tif') - # make a sample image - system('mkdir -p public/system/images/2/original/') - system('cp test/fixtures/demo.png public/system/images/2/original/test.png') - origin = Exporter.distort_warpables(scale, map.warpables, map.export, map.slug) - lowest_x, lowest_y, warpable_coords = origin - assert origin - ordered = false - - system('mkdir -p public/warps/saugus-landfill-incinerator') - system('mkdir -p public/tms/saugus-landfill-incinerator') - # these params could be compressed - warpable coords is part of origin; are coords and origin required? - assert Exporter.generate_composite_tiff(warpable_coords, origin, map.placed_warpables, map.slug, ordered) - assert Exporter.generate_tiles('', map.slug, Rails.root.to_s) - assert Exporter.zip_tiles(map.slug) - assert Exporter.generate_jpg(map.slug, Rails.root.to_s) - resolution = 20 - assert Exporter.run_export(User.last, resolution, map.export, map.id, map.slug, Rails.root.to_s, map.average_scale, map.placed_warpables, '') - - # test deletion of the files; they were already deleted in run_export, so let's make sample ones: - # make a sample image - system('mkdir -p public/system/images/2/original/') - system('touch public/system/images/2/original/test.png') - system('mkdir -p public/warps/saugus-landfill-incinerator') - system('mkdir -p public/tms/saugus-landfill-incinerator') - system('touch public/warps/saugus-landfill-incinerator/folder') - assert File.exist?('public/warps/saugus-landfill-incinerator/folder') - system('mkdir -p public/warps/saugus-landfill-incinerator-working') - system('touch public/warps/saugus-landfill-incinerator/test.png') - assert Exporter.delete_temp_files(w.map.slug) - end -end - From 8d6b835d8852581c7ab70d0fefe078b58ecdce14 Mon Sep 17 00:00:00 2001 From: jywarren Date: Fri, 12 Apr 2019 14:56:11 -0500 Subject: [PATCH 2/3] main branch exporter gem --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 2f1af2be2..302079d0b 100644 --- a/Gemfile +++ b/Gemfile @@ -38,7 +38,7 @@ group :dependencies do gem "uglifier" # switch to main version once ready: - gem "mapknitter-exporter", git: 'https://github.com/publiclab/mapknitter-exporter', branch: 'add_cartagen', glob: '*.gemspec' + gem "mapknitter-exporter", git: 'https://github.com/publiclab/mapknitter-exporter', glob: '*.gemspec' end group :test do From 74141b18af5af08ae706fe429decb730d3e6e826 Mon Sep 17 00:00:00 2001 From: jywarren Date: Fri, 12 Apr 2019 15:24:16 -0500 Subject: [PATCH 3/3] Gemfile.lock --- Gemfile.lock | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 4289884bb..77b18be4c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: https://github.com/publiclab/mapknitter-exporter + revision: e46886898d0c5b36863ce5a066b85c4c4a5cd0da + glob: *.gemspec + specs: + mapknitter-exporter (0.0.1) + GEM remote: https://rubygems.org/ specs: @@ -195,6 +202,7 @@ DEPENDENCIES geokit-rails (= 1.1.4) image_science (= 1.2.6) jshintrb + mapknitter-exporter! mysql2 (< 0.4) oa-openid (= 0.3.2) open_id_authentication @@ -220,4 +228,4 @@ RUBY VERSION ruby 2.4.6p354 BUNDLED WITH - 1.17.1 + 1.17.3