# 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