gdal-helper 0.0.1.1 → 0.0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "pp"
4
+ require "gdal_helper"
5
+
6
+ #basic plan - open image,dim image by 25%, set the projection and geo_trans, then quit, job done.
7
+ # Open a tiff to write to, with default create options (TILED=YES, COMPRESS=LZW) to write to..
8
+ if (ARGV.length != 2)
9
+ puts("Usage: ./basic_example_using_each_line.rb (infile) (outfile)")
10
+ return -1
11
+ end
12
+
13
+ infile = GdalFile.new(ARGV[0])
14
+ outfile = GdalFile.new(ARGV[1], "w", infile.xsize,infile.ysize,infile.number_of_bands,"GTiff", infile.data_type, ["COMPRESS=DEFLATE", "TILED=YES"])
15
+ y = 0
16
+ infile.each_line do |data|
17
+ data.each do |band|
18
+ band.each_index do |x_index|
19
+ band[x_index] = band[x_index] - band[x_index]/4
20
+ end
21
+ end
22
+ outfile.write_bands(0,y,infile.xsize,1,data)
23
+ y += 1
24
+ end
25
+ # Set the projection
26
+ outfile.set_projection(infile.get_projection)
27
+ # set the geo transform (world file)
28
+ outfile.set_geo_transform(infile.get_geo_transform)
29
+
30
+
31
+
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "pp"
4
+ require "gdal_helper"
5
+
6
+ #basic plan - open image,dim image by 25%, set the projection and geo_trans, then quit, job done.
7
+ # Open a tiff to write to, with default create options (TILED=YES, COMPRESS=LZW) to write to..
8
+ if (ARGV.length != 2)
9
+ puts("Usage: ./basic_example_using_each_line_with_index.rb (infile) (outfile)")
10
+ return -1
11
+ end
12
+
13
+ infile = GdalFile.new(ARGV[0])
14
+ outfile = GdalFile.new(ARGV[1], "w", infile.xsize,infile.ysize,infile.number_of_bands,"GTiff", infile.data_type, ["COMPRESS=DEFLATE", "TILED=YES"])
15
+ y = 0
16
+ infile.each_line_with_index do |y_index,data|
17
+ data.each do |band|
18
+ band.each_index do |x_index|
19
+ band[x_index] = band[x_index] - band[x_index]/4
20
+ end
21
+ end
22
+ outfile.write_bands(0,y_index,infile.xsize,1,data)
23
+ end
24
+ # Set the projection
25
+ outfile.set_projection(infile.get_projection)
26
+ # set the geo transform (world file)
27
+ outfile.set_geo_transform(infile.get_geo_transform)
28
+
29
+
30
+
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "gdal_helper"
4
+ require "pp"
5
+
6
+ ##
7
+ # This is a very basic example using the helper library to do some real work - in this case taking a MODIS ndvi product
8
+ # that is scaled from 10,000 to -1000 and creating a nice color presentation of it.
9
+ # This is just an example, so YMMV. Have fun - jc@alaska.edu
10
+
11
+ ##
12
+ # Finds the correct item in a colormap for a particular value
13
+ def find_color_range ( value, colormap )
14
+ ##
15
+ # Keep last, incase it is needed again - speed up
16
+ if (@last && @last[0] <= value && value <= @last[1])
17
+ return @last
18
+ end
19
+
20
+ colormap.each do |color_set|
21
+ if ( color_set[0] <= value && value <= color_set[1] )
22
+ @last = color_set
23
+ return color_set
24
+ end
25
+ end
26
+
27
+ # outside the range of the color map, this is an error..
28
+ raise(ArgumentError, "value requested is not in colormap..", caller)
29
+ end
30
+
31
+
32
+ ##
33
+ # Maps v to a range, given a min and max source and desitancation values.. wow, I can't spell..
34
+ def map_color(color_set, value)
35
+ #give the color table some more reasonable names..
36
+ # [min, max, [min r,g,b], [max r,g,b]]
37
+ min = color_set[0]
38
+ max = color_set[1]
39
+ min_c = color_set[2]
40
+ max_c = color_set[3]
41
+ rgb=[] #requested color
42
+ perc = (value.to_f - min.to_f)/(max - min) #percent between
43
+ 0.upto(2) { |zang| rgb[zang] = ((max_c[zang] - min_c[zang]) * perc + min_c[zang] ).to_i }
44
+ rgb
45
+ end
46
+
47
+ ##
48
+ # Maps a value to a color set
49
+ def colorize(value, colormap)
50
+ @cache={} if (!@cache)
51
+ return @cache[value] if (@cache[value])
52
+ color_set = find_color_range(value, colormap)
53
+
54
+ rgb = map_color(color_set, value)
55
+ @cache[value]=rgb if (@cache.length < 10000) #cache first x items - deleting from the hash is slow
56
+ rgb
57
+ end
58
+
59
+ ##
60
+ # A color map for ndvi, adapted from GRASS. the format is [min value, max value, [starting r,g,b], [ending r,g,b]]
61
+ # this could of course be improved..
62
+ ndvi_colormap = [
63
+ [-1.0000, -0.3000, [255,255,255], [0,0,255]],
64
+ [-0.3000, -0.2000, [0,0,255], [205,193, 173]],
65
+ [-0.2000, 0.0000, [205,193, 173], [150,150,150]],
66
+ [0.0000, 0.1000,[150,150,150], [120,100,51]],
67
+ [0.1000, 0.3000,[120,100,51], [120, 200, 100]],
68
+ [0.3000, 0.4000,[120,200,100],[28, 144, 3]],
69
+ [0.4000, 0.6000,[28, 144, 3],[ 6, 55, 0]],
70
+ [0.6000, 0.8000,[ 6, 55, 0],[10, 30, 25]],
71
+ [0.8000, 1.0000,[10, 30, 25],[ 6, 27, 7]]
72
+ ]
73
+
74
+ if (ARGV.length != 2)
75
+ puts("Usage: ./ndvi_color.rb (infile) (outfile)")
76
+ return -1
77
+ end
78
+
79
+ #input file
80
+ infile = GdalFile.new(ARGV[0])
81
+ #output file
82
+ outfile = GdalFile.new(ARGV[1], "w", infile.xsize,infile.ysize,3,"GTiff", String, ["COMPRESS=DEFLATE", "TILED=YES"])
83
+ #set the projection related details on the output file
84
+ outfile.set_projection(infile.get_projection)
85
+ outfile.set_geo_transform(infile.get_geo_transform)
86
+
87
+ # Loop though each line, colorizing the data
88
+ infile.each_line_with_index do |y_index, data|
89
+ out_data = [[],[],[]] #rbg array..
90
+
91
+ # print some status details..
92
+ if (y_index%100 == 0)
93
+ STDOUT.write(".")
94
+ STDOUT.flush
95
+ end
96
+
97
+ # loop though each sample, colorzing them.
98
+ data[0].each_index do |x_index|
99
+ # take each value, map it to a rgb, then put this in the bands to be output
100
+ rgb = colorize(data[0][x_index].to_f*0.0001, ndvi_colormap)
101
+ out_data[0][x_index] = rgb[0]
102
+ out_data[1][x_index] = rgb[1]
103
+ out_data[2][x_index] = rgb[2]
104
+ end
105
+ outfile.write_bands(0,y_index, infile.xsize, 1, out_data)
106
+ end
107
+
108
+ STDOUT.write("Done!\n")
data/lib/gdal_helper.rb CHANGED
@@ -302,16 +302,16 @@ end
302
302
  class GdalFile < GdalStuff
303
303
  def initialize ( name, mode="r", xsize=nil, ysize=nil,bands=3, driver="GTiff", data_type=String, options=['TILED=YES','COMPRESS=DEFLATE'] )
304
304
  if ( mode == "r" )
305
- @Gdalfile = Gdal::Gdal.open(name)
305
+ @gdalfile = Gdal::Gdal.open(name)
306
306
  else
307
307
  if ( mode == "w")
308
308
  if (File.exists?(name))
309
- @Gdalfile = Gdal::Gdal.open(name,Gdal::Gdalconst::GA_UPDATE )
309
+ @gdalfile = Gdal::Gdal.open(name,Gdal::Gdalconst::GA_UPDATE )
310
310
  else
311
311
  driver = Gdal::Gdal.get_driver_by_name(driver)
312
312
  #puts(driver.class)
313
313
  #puts("Creating create(#{name}, #{xsize}, #{ysize}, #{bands}, #{data_type_to_gdal(data_type).to_s})")
314
- @Gdalfile = driver.create(name, xsize, ysize, bands, data_type_to_gdal(data_type), options)
314
+ @gdalfile = driver.create(name, xsize, ysize, bands, data_type_to_gdal(data_type), options)
315
315
  end
316
316
  else
317
317
  raise ArgumentError, "mode of \"#{mode}\" is not useful (not r|w) not sure what to do here folks", caller
@@ -320,7 +320,7 @@ class GdalFile < GdalStuff
320
320
 
321
321
  @bands=[]
322
322
  #1 is not a mistake - the raster bands start at 1 no 0. just a fyi.
323
- 1.upto(@Gdalfile.RasterCount).each {|x| @bands << GdalBand.new(@Gdalfile.get_raster_band(x))}
323
+ 1.upto(@gdalfile.RasterCount).each {|x| @bands << GdalBand.new(@gdalfile.get_raster_band(x))}
324
324
  end
325
325
 
326
326
  ###
@@ -351,20 +351,20 @@ class GdalFile < GdalStuff
351
351
 
352
352
  #returns basic size info as a hash
353
353
  def size()
354
- { "x"=> @Gdalfile.RasterXSize,
355
- "y" => @Gdalfile.RasterYSize,
354
+ { "x"=> @gdalfile.RasterXSize,
355
+ "y" => @gdalfile.RasterYSize,
356
356
  "bands" => @bands.length,
357
357
  "data_type" => @bands[0].data_type()}
358
358
  end
359
359
 
360
360
  #x dimention size
361
361
  def xsize()
362
- @Gdalfile.RasterXSize
362
+ @gdalfile.RasterXSize
363
363
  end
364
364
 
365
365
  #y dim size
366
366
  def ysize()
367
- @Gdalfile.RasterYSize
367
+ @gdalfile.RasterYSize
368
368
  end
369
369
 
370
370
  #number of bands
@@ -384,29 +384,39 @@ class GdalFile < GdalStuff
384
384
 
385
385
  # gets the projection
386
386
  def get_projection
387
- @Gdalfile.get_projection
387
+ @gdalfile.get_projection
388
388
  end
389
389
 
390
390
  #sets the projection
391
391
  def set_projection(proj_str)
392
- @Gdalfile.set_projection(proj_str)
392
+ @gdalfile.set_projection(proj_str)
393
393
  end
394
394
 
395
395
  #looks up the projection in the epsg database, give it a number like 102006.
396
396
  def set_projection_epsg(epsg)
397
397
  srs = Gdal::Osr::SpatialReference.new()
398
398
  srs.import_from_epsg(epsg)
399
- @Gdalfile.set_projection(srs.export_to_wkt)
399
+ @gdalfile.set_projection(srs.export_to_wkt)
400
400
  end
401
401
 
402
402
  #sets the geo_transform, the wld file generally.
403
403
  def set_geo_transform(srs)
404
- @Gdalfile.set_geo_transform(srs)
404
+ @gdalfile.set_geo_transform(srs)
405
405
  end
406
406
 
407
407
  #gets the geo transform (wld file traditionally)
408
408
  def get_geo_transform()
409
- @Gdalfile.get_geo_transform
409
+ @gdalfile.get_geo_transform
410
+ end
411
+
412
+ #iterator over each line..
413
+ def each_line( )
414
+ 0.upto(ysize-1){|y| yield(read_bands(0,y,xsize,1))}
415
+ end
416
+
417
+ #iterator over each line, with index
418
+ def each_line_with_index( )
419
+ 0.upto(ysize-1){|y| yield(y,read_bands(0,y,xsize,1))}
410
420
  end
411
421
 
412
422
  end
data/lib/version.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Gdal_Helper
2
- Version = "0.0.1.1"
1
+ module GdalHelper
2
+ Version = "0.0.1.2"
3
3
  end
4
4
 
metadata CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 0
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.0.1.1
9
+ - 2
10
+ version: 0.0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - JC
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-04-20 00:00:00 -08:00
18
+ date: 2010-04-21 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -31,8 +31,11 @@ files:
31
31
  - lib/version.rb
32
32
  - lib/gdal_helper.rb
33
33
  - test/ruby_gem_test.rb
34
+ - examples/basic_example_using_each_line_with_index.rb
34
35
  - examples/basic_example.rb
35
36
  - examples/print_info_example.rb
37
+ - examples/ndvi_color.rb
38
+ - examples/basic_example_using_each_line.rb
36
39
  - examples/gdal_helper_test.rb
37
40
  has_rdoc: true
38
41
  homepage: http://www.gina.alaska.edu