Class: GdalFile

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

Overview

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

Instance Method Summary collapse

Methods inherited from GdalStuff

#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']) ⇒ GdalFile

Returns a new instance of GdalFile.



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/gdal_helper.rb', line 312

def initialize ( name, mode="r", xsize=nil, ysize=nil,bands=3, driver="GTiff", data_type=String, options=['TILED=YES','COMPRESS=DEFLATE'] )
  if ( mode == "r" )
    @gdalfile = Gdal::Gdal.open(name)
  else
    if ( mode == "w")
      if (File.exists?(name))
        @gdalfile = 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})")
        @gdalfile = 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(@gdalfile.RasterCount).each {|x| @bands << GdalBand.new(@gdalfile.get_raster_band(x))}
end

Instance Method Details

#data_typeObject

types of bands



385
386
387
# File 'lib/gdal_helper.rb', line 385

def data_type()
  @bands.first.data_type
end

#each_lineObject

iterator over each line..



422
423
424
# File 'lib/gdal_helper.rb', line 422

def each_line( )
  0.upto(ysize-1){|y| yield(read_bands(0,y,xsize,1))}
end

#each_line_with_indexObject

iterator over each line, with index



427
428
429
# File 'lib/gdal_helper.rb', line 427

def each_line_with_index( )
  0.upto(ysize-1){|y| yield(y,read_bands(0,y,xsize,1))}
end

#get_geo_transformObject

gets the geo transform (wld file traditionally)



417
418
419
# File 'lib/gdal_helper.rb', line 417

def get_geo_transform()
   @gdalfile.get_geo_transform
end

#get_projectionObject

gets the projection



395
396
397
# File 'lib/gdal_helper.rb', line 395

def get_projection
  @gdalfile.get_projection
end

#number_of_bandsObject

number of bands



380
381
382
# File 'lib/gdal_helper.rb', line 380

def number_of_bands()
  @bands.length
end

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

reads a band



345
346
347
# File 'lib/gdal_helper.rb', line 345

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



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

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.



412
413
414
# File 'lib/gdal_helper.rb', line 412

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

#set_projection(proj_str) ⇒ Object

sets the projection



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

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

#set_projection_epsg(epsg) ⇒ Object

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



405
406
407
408
409
# File 'lib/gdal_helper.rb', line 405

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

#sizeObject

returns basic size info as a hash



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

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

#to_sObject

for pping or other .to_s action..



390
391
392
# File 'lib/gdal_helper.rb', line 390

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



353
354
355
# File 'lib/gdal_helper.rb', line 353

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



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

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



370
371
372
# File 'lib/gdal_helper.rb', line 370

def xsize()
  @gdalfile.RasterXSize
end

#ysizeObject

y dim size



375
376
377
# File 'lib/gdal_helper.rb', line 375

def ysize()
  @gdalfile.RasterYSize
end