Class: Map::Gdal::CropService

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/map/gdal/crop_service.rb

Instance Attribute Summary

Attributes included from Base

#files_to_clean

Instance Method Summary collapse

Methods included from Base

#add_to_clean, #clean, #gdal_running?, #get_file_name_with_path, #get_layer_name, #get_path_to_temp_file, #options_to_command_line, #run_command, #store_kml, #tmp_file

Constructor Details

#initialize(tif) ⇒ CropService

Returns a new instance of CropService.



6
7
8
# File 'lib/map/gdal/crop_service.rb', line 6

def initialize(tif)
  @tif = tif
end

Instance Method Details

#call(options = {}) ⇒ Object



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
# File 'lib/map/gdal/crop_service.rb', line 10

def call(options = {})
  raise 'Reference file is not defined' unless options[:reference]
  shp_folder = File.join(Dir.tmpdir, "shp-#{(Time.current.to_i * rand).to_i}")
  kml = get_path_to_temp_file('table-color', 'kml')
  add_to_clean(shp_folder)
  add_to_clean(kml)

  IO.write(kml, Map::KmlEditService.new(options[:reference]).to_xml)
  run_command(%{ogr2ogr -f "ESRI Shapefile" #{shp_folder} #{kml}})
  service = Map::Gdal::WarpService.new(@tif)
  options_to_warp = {
    '-config': 'GDALWARP_IGNORE_BAD_CUTLINE YES',
    crop_to_cutline: '',
    cutline: shp_folder
  }

  options_to_warp[:dstalpha] = options[:dstalpha] if options[:dstalpha]
  options_to_warp[:dstnodata] = options[:dstnodata] if options[:dstnodata]
  options_to_warp[:s_srs] = options[:s_srs] if options[:s_srs]
  options_to_warp[:tr] = options[:tr].gsub('-tr ', '') if options[:tr]
  out = service.call(options_to_warp)
  add_to_clean(out)

  out
end