-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb_to_jpeg_tif.rb
executable file
·113 lines (83 loc) · 3.47 KB
/
rgb_to_jpeg_tif.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env ruby
require 'yaml'
require 'pp'
def runner ( command, opts)
puts("Running: #{command.join(" ")}") if (opts[:verbrose])
start_time = Time.now
system(*command)
puts("Done in #{(Time.now - start_time)/60.0}m.") if (opts[:verbrose])
end
## Command line parsing action..
require 'trollop'
parser = Trollop::Parser.new do
version "0.0.1 [email protected]"
banner <<-EOS
This util takes a rgb file readable by gdal and converts it to a rgb jpeg compressed tiff with a nodata mask.
It assumes the nodata values are 0 for each band.
Usage:
rgb_to_jpeg_tif.rb [options] <infile> <outfile>
where [options] is:
EOS
opt :generate_thumbnail, "Generate an overview thumbnail"
opt :internal_mask, "Use interal nodata masks"
opt :verbrose, "Maxium Verbrosity.", :short => "V"
opt :skip_cache_check, "Skip check of GDAL_CACHEMAX"
opt :small_tiff, "Don't use bigtiff option."
end
opts = Trollop::with_standard_exception_handling(parser) do
o = parser.parse ARGV
raise Trollop::HelpNeeded if ARGV.length != 2 # show help screen
o
end
if (!opts[:skip_cache_check] )
if (!ENV["GDAL_CACHEMAX"])
puts("Error: Please set GDAL_CACHEMAX to something useful by doing something like this \"export GDAL_CACHEMAX=2048\" and try again. ")
puts("(This check can be skipped by using the \"--skip_cache_check\" flag)")
exit(-1)
else
puts("Warning: GDAL_CACHEMAX is set to somthing pretty small #{ENV["GDAL_CACHEMAX"].to_i} (thats in mbytes)") if (ENV["GDAL_CACHEMAX"].to_i < 512)
end
end
# first, verify that the required subprograms exist..
errors=false;
["add_mask", "add_overviews.rb"].each do |t|
if (!File.exists?(File.dirname(__FILE__) +"/" + t))
errors=true
puts("Error: Could not find #{t}, which should be here: #{File.dirname(__FILE__)}")
end
end
exit(-1) if (errors)
infile = ARGV[0]
outfile = ARGV[1]
tmpfile = ARGV[1] +".tmp"
puts("Info: using \"#{infile}\" as source ") if (opts[:verbrose])
puts("Info: using \"#{outfile}\" as output ") if (opts[:verbrose])
#get info of source file..
input_cfs = YAML.load(`gdal_list_corners #{infile}`)
#check the bands, make sure its reasonable..
if ( input_cfs["num_bands"] !=3 )
puts("Error: #{infile} has #{input_cfs["num_bands"]}, in order for this to work it needs to have 3.")
exit(-1)
end
#make temp image, compressed + tiled
puts("Info: generating temp image..")
runner(["gdal_translate", "-co", "TILED=YES", "-co","BIGTIFF=YES","-co", "COMPRESS=LZW", infile, tmpfile ], opts)
#add mask
puts("Info: Adding mask to temp image..")
runner([File.dirname(__FILE__)+"/add_mask", tmpfile], opts)
#add overviews..
puts("Info: Adding overviews to temp image..")
runner([File.dirname(__FILE__)+"/add_overviews.rb", "-m", tmpfile], opts)
puts("Info: Generating #{outfile}..")
additional_options=[]
additional_options += ["--config", "GDAL_TIFF_INTERNAL_MASK", "TRUE"] if (opts[:internal_mask])
additional_options += ["-co", "BIGTIFF=YES"] if (!opts[:small_tiff])
runner(["gdal_translate","-co", "TILED=YES", "-co", "COMPRESS=JPEG","-co","COPY_SRC_OVERVIEWS=YES","-co","PHOTOMETRIC=YCBCR", "-co", "JPEG_QUALITY=90"] + additional_options + [tmpfile, outfile], opts)
if (!opts[:internal_mask])
puts("Info: Adding overviews to mask..")
runner([File.dirname(__FILE__)+"/add_overviews.rb", "-m", outfile +".msk"], opts)
end
puts("Info: Deleting #{tmpfile} && #{tmpfile}.msk") if (opts[:verbrose])
runner(["rm", tmpfile], opts)
runner(["rm", tmpfile+".msk"], opts) #if(!opts[:internal_mask])
puts("Info: Done.")