Module: NSWTopo::Raster
- Included in:
- ArcGISRaster, Import, Relief, Vegetation
- Defined in:
- lib/nswtopo/layer/raster.rb
Instance Method Summary collapse
- #create ⇒ Object
- #empty? ⇒ Boolean
- #filename ⇒ Object
- #render(group, defs) ⇒ Object
- #size_resolution ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#create ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/nswtopo/layer/raster.rb', line 3 def create tif = Dir.mktmppath do |temp_dir| tif_path = temp_dir / "final.tif" tfw_path = temp_dir / "final.tfw" out_path = temp_dir / "output.tif" resolution, raster_path = get_raster(temp_dir) dimensions, ppi, resolution = @map.raster_dimensions_at resolution: resolution density = 0.01 * @map.scale / resolution = %W[-mo TIFFTAG_XRESOLUTION=#{density} -mo TIFFTAG_YRESOLUTION=#{density} -mo TIFFTAG_RESOLUTIONUNIT=3] @map.write_world_file tfw_path, resolution: resolution OS.convert "-size", dimensions.join(?x), "canvas:none", "-type", "TrueColorMatte", "-depth", 8, tif_path OS.gdalwarp "-t_srs", @map.projection, "-r", "bilinear", raster_path, tif_path OS.gdal_translate "-a_srs", @map.projection, *, tif_path, out_path @map.write filename, out_path.binread end end |
#empty? ⇒ Boolean
26 27 28 |
# File 'lib/nswtopo/layer/raster.rb', line 26 def empty? false end |
#filename ⇒ Object
22 23 24 |
# File 'lib/nswtopo/layer/raster.rb', line 22 def filename "#{@name}.tif" end |
#render(group, defs) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/nswtopo/layer/raster.rb', line 47 def render(group, defs) (width, height), resolution = size_resolution group.add_attributes "style" => "opacity:%s" % params.fetch("opacity", 1) transform = "scale(#{1000.0 * resolution / @map.scale})" png = Dir.mktmppath do |temp_dir| tif_path = temp_dir / "raster.tif" png_path = temp_dir / "raster.png" tif_path.binwrite @map.read(filename) OS.gdal_translate "-of", "PNG", "-co", "ZLEVEL=9", tif_path, png_path png_path.binread end href = "data:image/png;base64,#{Base64.encode64 png}" group.add_element "image", "transform" => transform, "width" => width, "height" => height, "image-rendering" => "optimizeQuality", "xlink:href" => href group.add_attribute "mask", "url(#raster-mask)" if defs.elements["mask[@id='raster-mask']"] end |
#size_resolution ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/nswtopo/layer/raster.rb', line 30 def size_resolution json = OS.gdalinfo "-json", "/vsistdin/" do |stdin| stdin.binmode stdin.write @map.read(filename) end size, geotransform = JSON.parse(json).values_at "size", "geoTransform" resolution = geotransform.values_at(1, 2).norm return size, resolution end |
#to_s ⇒ Object
40 41 42 43 44 45 |
# File 'lib/nswtopo/layer/raster.rb', line 40 def to_s size, resolution = size_resolution megapixels = size.inject(&:*) / 1024.0 / 1024.0 ppi = 0.0254 * @map.scale / resolution "%s: %i×%i (%.1fMpx) @ %.1fm/px (%.0f ppi)" % [@name, *size, megapixels, resolution, ppi] end |