Class: Gdal_File

Inherits:
Gdal_Stuff show all
Defined in:
lib/gdal_helper.rb

Overview

Class for a file - this is what most folks want Use like: infile = Gdal_File.new(“foo.tif”) bands = infile.read_bands(0,0,infile.xsize/4,infile.ysize/4) ..do something..

Instance Method Summary collapse

Methods inherited from Gdal_Stuff

#data_type_from_gdal, #data_type_to_gdal

Constructor Details

#initialize(name, mode = "r", xsize = nil, ysize = nil, bands = 3, driver = "GTiff", data_type = String, options = ['TILED=YES','COMPRESS=DEFLATE']) ⇒ Gdal_File

Returns a new instance of Gdal_File.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/gdal_helper.rb', line 299

def initialize ( name, mode="r", xsize=nil, ysize=nil,bands=3, driver="GTiff", data_type=String, options=['TILED=YES','COMPRESS=DEFLATE'] )
  if ( mode == "r" )
    @gdal_file = Gdal::Gdal.open(name)
  else
    if ( mode == "w")
      if (File.exists?(name))
        @gdal_file = Gdal::Gdal.open(name,Gdal::Gdalconst::GA_UPDATE )
      else
        driver = Gdal::Gdal.get_driver_by_name(driver)
        puts(driver.class)
        puts("Creating create(#{name}, #{xsize}, #{ysize}, #{bands}, #{data_type_to_gdal(data_type).to_s})")
        @gdal_file = driver.create(name, xsize, ysize, bands, data_type_to_gdal(data_type), options)
      end
    else
      raise ArgumentError, "mode of \"#{mode}\" is not useful (not r|w) not sure what to do here folks", caller
    end
  end
  
  @bands=[]
  #1 is not a mistake - the raster bands start at 1 no 0. just a fyi.
  1.upto(@gdal_file.RasterCount).each {|x| @bands << Gdal_Band.new(@gdal_file.get_raster_band(x))}
end

Instance Method Details

#data_typeObject

types of bands



372
373
374
# File 'lib/gdal_helper.rb', line 372

def data_type()
  @bands.first.data_type
end

#get_geo_transformObject

gets the geo transform (wld file traditionally)



404
405
406
# File 'lib/gdal_helper.rb', line 404

def get_geo_transform()
   @gdal_file.get_geo_transform
end

#get_projectionObject

gets the projection



382
383
384
# File 'lib/gdal_helper.rb', line 382

def get_projection
  @gdal_file.get_projection
end

#number_of_bandsObject

number of bands



367
368
369
# File 'lib/gdal_helper.rb', line 367

def number_of_bands()
  @bands.length
end

#read_band(bandno, start_x, start_y, width_x, width_y) ⇒ Object

reads a band



332
333
334
# File 'lib/gdal_helper.rb', line 332

def read_band(bandno,start_x, start_y,width_x, width_y  )
  @bands[bandno].read(start_x, start_y, width_x, width_y)
end

#read_bands(start_x, start_y, width_x, width_y) ⇒ Object

reads bands



326
327
328
329
330
# File 'lib/gdal_helper.rb', line 326

def read_bands(start_x, start_y, width_x, width_y)
  data = []
  @bands.each_index {|x| data[x] = @bands[x].read(start_x, start_y, width_x, width_y)}
  data
end

#set_geo_transform(srs) ⇒ Object

sets the geo_transform, the wld file generally.



399
400
401
# File 'lib/gdal_helper.rb', line 399

def set_geo_transform(srs) 
  @gdal_file.set_geo_transform(srs)
end

#set_projection(proj_str) ⇒ Object

sets the projection



387
388
389
# File 'lib/gdal_helper.rb', line 387

def set_projection(proj_str)
  @gdal_file.set_projection(proj_str)
end

#set_projection_epsg(epsg) ⇒ Object

looks up the projection in the epsg database, give it a number like 102006.



392
393
394
395
396
# File 'lib/gdal_helper.rb', line 392

def set_projection_epsg(epsg)
  srs =  Gdal::Osr::SpatialReference.new()
  srs.import_from_epsg(epsg)
  @gdal_file.set_projection(srs.export_to_wkt)
end

#sizeObject

returns basic size info as a hash



349
350
351
352
353
354
# File 'lib/gdal_helper.rb', line 349

def size()
  { "x"=> @gdal_file.RasterXSize,
    "y" => @gdal_file.RasterYSize,
    "bands" => @bands.length,
    "data_type" => @bands[0].data_type()}
end

#to_sObject

for pping or other .to_s action..



377
378
379
# File 'lib/gdal_helper.rb', line 377

def to_s
  "#{xsize}x#{ysize} with #{number_of_bands} #{@bands[0].to_s} bands"
end

#write_band(bandno, start_x, start_y, end_x, end_y, band) ⇒ Object

writes a band



340
341
342
# File 'lib/gdal_helper.rb', line 340

def write_band(bandno,start_x, start_y, end_x, end_y, band )
  @bands[bandno].write(start_x, start_y, end_x, end_y, band)
end

#write_bands(start_x, start_y, width_x, width_y, bands) ⇒ Object

writes bands



336
337
338
# File 'lib/gdal_helper.rb', line 336

def write_bands(start_x, start_y, width_x, width_y, bands)
  bands.each_index {|x| @bands[x].write(start_x, start_y, width_x, width_y, bands[x])}
end

#xsizeObject

x dimention size



357
358
359
# File 'lib/gdal_helper.rb', line 357

def xsize()
  @gdal_file.RasterXSize
end

#ysizeObject

y dim size



362
363
364
# File 'lib/gdal_helper.rb', line 362

def ysize()
  @gdal_file.RasterYSize
end