Sha256: c84ac596b3bbb6fdd01f633184e0db546195130238df1389ca231f90e6cd730c

Contents?: true

Size: 1.88 KB

Versions: 8

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

# Converte um shape file para tif
# 1. Rasteriza o shape
# 2. Faz resample
# 3. Aplica escala de cores
class Map::ShapeToTifService
  attr_reader :options, :shape_file

  def initialize(shape_file)
    @shape_file = shape_file
    @services = []
  end

  def call(options)
    @options = options
    color_result = color
    crop_service = Map::Gdal::CropService.new(color_result[:file])
    original_file = color_result[:file]
    color_result[:file] = crop_service.call({
      reference: IO.read(options[:kml]),
      s_srs: 'WGS84'
    })
    FileUtils.rm_rf(original_file)
    color_result
  ensure
    clean_services
  end

  private

  def raster
    @services << Map::Gdal::RasterService.new(@shape_file)
    raster_options = {
      a_nodata: 0,
      format: 'tif',
      ot: 'Float',
      a: options[:field],
      ts: options[:ts] || '500 500'
    }
    raster_options[:te] = te if te
    @services.last.call(raster_options)
  end

  def te
    if options[:te]
      options[:te]
    elsif options[:kml]
      Map::Gdal::OgriInfoService.new(options[:kml]).limits
    end
  end

  def color
    service = Map::Gdal::ColorizeService.new(resample)
    color_table = options[:color_table] || service.generate_color_table
    {
      colors: colors(color_table),
      json: json,
      file: service.call(color_table:  color_table)
    }
  end

  def resample
    @services << Map::Gdal::TranslateService.new(raster)
    @services.last.call({
      tr: '0.00001 0.00001',
      r: 'cubicspline'
    })
  end

  def clean_services
    @services.map(&:clean)
  end

  def json
    service = Map::Gdal::Ogr2ogrService.new(@shape_file)
    out = service.call({
      f: 'GeoJSON',
      select: options[:field]
    })
    parsed = JSON.parse(IO.read(out))
    service.clean
    parsed
  end

  def colors(color_table)
    Map::Gdal::ColorizeService.format_color_table(color_table)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
aqila-mapas-0.4.12 lib/map/shape_to_tif_service.rb
aqila-mapas-0.4.11 lib/map/shape_to_tif_service.rb
aqila-mapas-0.4.9 lib/map/shape_to_tif_service.rb
aqila-mapas-0.4.8 lib/map/shape_to_tif_service.rb
aqila-mapas-0.4.7 lib/map/shape_to_tif_service.rb
aqila-mapas-0.4.6 lib/map/shape_to_tif_service.rb
aqila-mapas-0.4.5 lib/map/shape_to_tif_service.rb
aqila-mapas-0.4.4 lib/map/shape_to_tif_service.rb