Class: Map::Gdal::PolygonizeService

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/map/gdal/polygonize_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_path) ⇒ PolygonizeService

Returns a new instance of PolygonizeService.



8
9
10
# File 'lib/map/gdal/polygonize_service.rb', line 8

def initialize(tif_path)
  @tif_path = tif_path
end

Instance Method Details

#call(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/map/gdal/polygonize_service.rb', line 12

def call(options = {})
  if options[:kml]
    @tif_path = Map::Gdal::CropService.new(@tif_path).call(reference: IO.binread(options[:kml]))
    add_to_clean(@tif_path)
  end

  file_id = "#{Time.current.to_i}-#{(rand * 1_000).to_i}"
  destination_shp = get_path_to_temp_file('shapefile', 'shp', file_id)
  zip_file = get_path_to_temp_file('shapefile', 'zip', file_id)

  run_command(%{gdal_polygonize.py #{@tif_path} -f "ESRI Shapefile" #{destination_shp}})

  Zip::File.open(zip_file, Zip::File::CREATE) do |zip|
    %w(dbf prj shp shx).each do |extension|
      file_name = "shapefile-#{file_id}.#{extension}"
      file_with_path = get_path_to_temp_file('shapefile', extension, file_id)
      zip.add(file_name, file_with_path)
      add_to_clean(file_with_path)
    end
  end

  add_to_clean(zip_file)
  zip_file
end