Class: GdalFile
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
-
#data_type ⇒ Object
types of bands.
-
#each_line ⇒ Object
iterator over each line..
-
#each_line_with_index ⇒ Object
iterator over each line, with index.
-
#get_extents ⇒ Object
returns the extents as a => min, “ymin” => ymin, “xmax” => xmax, “ymax” => ymax sort of construct..
-
#get_geo_transform ⇒ Object
gets the geo transform (wld file traditionally) Returns an array with this information: [Origin (top left corner), X pixel size, Rotation (0 if north is up),Y Origin (top left corner), Rotation (0 if north is up), Y pixel size *-1 (its negitive)].
-
#get_projection ⇒ Object
gets the projection.
-
#initialize(name, mode = "r", xsize = nil, ysize = nil, bands = 3, driver = "GTiff", data_type = String, options = ['TILED=YES','COMPRESS=DEFLATE']) ⇒ GdalFile
constructor
A new instance of GdalFile.
-
#number_of_bands ⇒ Object
number of bands.
-
#read_band(bandno, start_x, start_y, width_x, width_y) ⇒ Object
reads a band.
-
#read_bands(start_x, start_y, width_x, width_y) ⇒ Object
reads bands.
-
#set_geo_transform(srs) ⇒ Object
sets the geo_transform, the wld file generally.
-
#set_projection(proj_str) ⇒ Object
sets the projection.
-
#set_projection_epsg(epsg) ⇒ Object
looks up the projection in the epsg database, give it a number like 102006.
-
#size ⇒ Object
returns basic size info as a hash.
-
#to_s ⇒ Object
for pping or other .to_s action..
-
#write_band(bandno, start_x, start_y, end_x, end_y, band) ⇒ Object
writes a band.
-
#write_bands(start_x, start_y, width_x, width_y, bands) ⇒ Object
writes bands.
-
#xsize ⇒ Object
x dimention size.
-
#ysize ⇒ Object
y dim size.
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.
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/gdal_helper.rb', line 340 def initialize ( name, mode="r", xsize=nil, ysize=nil,bands=3, driver="GTiff", data_type=String, =['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), ) 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_type ⇒ Object
types of bands
413 414 415 |
# File 'lib/gdal_helper.rb', line 413 def data_type() @bands.first.data_type end |
#each_line ⇒ Object
iterator over each line..
451 452 453 |
# File 'lib/gdal_helper.rb', line 451 def each_line( ) 0.upto(ysize-1){|y| yield(read_bands(0,y,xsize,1))} end |
#each_line_with_index ⇒ Object
iterator over each line, with index
456 457 458 |
# File 'lib/gdal_helper.rb', line 456 def each_line_with_index( ) 0.upto(ysize-1){|y| yield(y,read_bands(0,y,xsize,1))} end |
#get_extents ⇒ Object
returns the extents as a => min, “ymin” => ymin, “xmax” => xmax, “ymax” => ymax sort of construct..
461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/gdal_helper.rb', line 461 def get_extents transform = @gdalfile.get_geo_transform #[274785.0, 30.0, 0.0, 4906905.0, 0.0, -30.0] #[(0)Origin (top left corner), (1) X pixel size, (2) Rotation (0 if north is up),(3)Y Origin (top left corner), (4) Rotation (0 if north is up), (5) Y pixel size] { "xmin" => transform[0].to_f, "ymin"=> transform[3].to_f + ysize().to_f * transform[5].to_f, "ymax" => transform[3].to_f, "xmax" => transform[0].to_f + xsize().to_f * transform[1].to_f } end |
#get_geo_transform ⇒ Object
gets the geo transform (wld file traditionally) Returns an array with this information: [Origin (top left corner), X pixel size, Rotation (0 if north is up),Y Origin (top left corner), Rotation (0 if north is up), Y pixel size *-1 (its negitive)]
446 447 448 |
# File 'lib/gdal_helper.rb', line 446 def get_geo_transform() @gdalfile.get_geo_transform end |
#get_projection ⇒ Object
gets the projection
423 424 425 |
# File 'lib/gdal_helper.rb', line 423 def get_projection @gdalfile.get_projection end |
#number_of_bands ⇒ Object
number of bands
408 409 410 |
# File 'lib/gdal_helper.rb', line 408 def number_of_bands() @bands.length end |
#read_band(bandno, start_x, start_y, width_x, width_y) ⇒ Object
reads a band
373 374 375 |
# File 'lib/gdal_helper.rb', line 373 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
367 368 369 370 371 |
# File 'lib/gdal_helper.rb', line 367 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.
440 441 442 |
# File 'lib/gdal_helper.rb', line 440 def set_geo_transform(srs) @gdalfile.set_geo_transform(srs) end |
#set_projection(proj_str) ⇒ Object
sets the projection
428 429 430 |
# File 'lib/gdal_helper.rb', line 428 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.
433 434 435 436 437 |
# File 'lib/gdal_helper.rb', line 433 def set_projection_epsg(epsg) srs = Gdal::Osr::SpatialReference.new() srs.import_from_epsg(epsg) @gdalfile.set_projection(srs.export_to_wkt) end |
#size ⇒ Object
returns basic size info as a hash
390 391 392 393 394 395 |
# File 'lib/gdal_helper.rb', line 390 def size() { "x"=> @gdalfile.RasterXSize, "y" => @gdalfile.RasterYSize, "bands" => @bands.length, "data_type" => @bands[0].data_type()} end |
#to_s ⇒ Object
for pping or other .to_s action..
418 419 420 |
# File 'lib/gdal_helper.rb', line 418 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
381 382 383 |
# File 'lib/gdal_helper.rb', line 381 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
377 378 379 |
# File 'lib/gdal_helper.rb', line 377 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 |
#xsize ⇒ Object
x dimention size
398 399 400 |
# File 'lib/gdal_helper.rb', line 398 def xsize() @gdalfile.RasterXSize end |
#ysize ⇒ Object
y dim size
403 404 405 |
# File 'lib/gdal_helper.rb', line 403 def ysize() @gdalfile.RasterYSize end |