pktools  2.6.3
Processing Kernel for geospatial data
pkdsm2shadow.cc
1 /**********************************************************************
2 pkdsm2shadow.cc: program to calculate sun shadow based on digital surface model and sun angles
3 Copyright (C) 2008-2014 Pieter Kempeneers
4 
5 This file is part of pktools
6 
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19 ***********************************************************************/
20 #include <assert.h>
21 #include <iostream>
22 #include <string>
23 #include <fstream>
24 #include <math.h>
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include "base/Optionpk.h"
28 #include "base/Vector2d.h"
29 #include "algorithms/Filter2d.h"
30 #include "imageclasses/ImgReaderGdal.h"
31 #include "imageclasses/ImgWriterGdal.h"
32 
33 using namespace std;
34 
35 /*------------------
36  Main procedure
37  ----------------*/
38 int main(int argc,char **argv) {
39  Optionpk<std::string> input_opt("i","input","input image file");
40  Optionpk<std::string> output_opt("o", "output", "Output image file");
41  Optionpk<double> sza_opt("sza", "sza", "Sun zenith angle.");
42  Optionpk<double> saa_opt("saa", "saa", "Sun azimuth angle (N=0 E=90 S=180 W=270).");
43  Optionpk<int> flag_opt("f", "flag", "Flag to put in image if pixel shadow", 0);
44  Optionpk<std::string> otype_opt("ot", "otype", "Data type for output image ({Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/CInt16/CInt32/CFloat32/CFloat64}). Empty string: inherit type from input image", "");
45  Optionpk<string> oformat_opt("of", "oformat", "Output image format (see also gdal_translate). Empty string: inherit from input image");
46  Optionpk<string> colorTable_opt("ct", "ct", "color table (file with 5 columns: id R G B ALFA (0: transparent, 255: solid)");
47  Optionpk<string> option_opt("co", "co", "Creation option for output file. Multiple options can be specified.");
48  Optionpk<double> scale_opt("s", "scale", "scale used for input dsm: height=scale*input+offset");
49  Optionpk<double> offset_opt("off", "offset", "offset used for input dsm: height=scale*input+offset");
50  Optionpk<short> verbose_opt("v", "verbose", "verbose mode if > 0", 0,2);
51 
52  scale_opt.setHide(1);
53  offset_opt.setHide(1);
54 
55  bool doProcess;//stop process when program was invoked with help option (-h --help)
56  try{
57  doProcess=input_opt.retrieveOption(argc,argv);
58  output_opt.retrieveOption(argc,argv);
59  sza_opt.retrieveOption(argc,argv);
60  saa_opt.retrieveOption(argc,argv);
61  flag_opt.retrieveOption(argc,argv);
62  scale_opt.retrieveOption(argc,argv);
63  offset_opt.retrieveOption(argc,argv);
64  option_opt.retrieveOption(argc,argv);
65  otype_opt.retrieveOption(argc,argv);
66  oformat_opt.retrieveOption(argc,argv);
67  colorTable_opt.retrieveOption(argc,argv);
68  verbose_opt.retrieveOption(argc,argv);
69  }
70  catch(string predefinedString){
71  std::cout << predefinedString << std::endl;
72  exit(0);
73  }
74  if(!doProcess){
75  cout << endl;
76  cout << "Usage: pkdsm2shadow -i input.txt -o output [-sza angle] [-saa angle]" << endl;
77  cout << endl;
78  std::cout << "short option -h shows basic options only, use long option --help to show all options" << std::endl;
79  exit(0);//help was invoked, stop processing
80  }
81 
82  ImgReaderGdal input;
83  ImgWriterGdal output;
84  assert(input_opt.size());
85  assert(output_opt.size());
86  input.open(input_opt[0]);
87  if(scale_opt.size())
88  input.setScale(scale_opt[0]);
89  if(offset_opt.size())
90  input.setOffset(offset_opt[0]);
91 
92  // output.open(output_opt[0],input);
93  GDALDataType theType=GDT_Unknown;
94  if(verbose_opt[0])
95  cout << "possible output data types: ";
96  for(int iType = 0; iType < GDT_TypeCount; ++iType){
97  if(verbose_opt[0])
98  cout << " " << GDALGetDataTypeName((GDALDataType)iType);
99  if( GDALGetDataTypeName((GDALDataType)iType) != NULL
100  && EQUAL(GDALGetDataTypeName((GDALDataType)iType),
101  otype_opt[0].c_str()))
102  theType=(GDALDataType) iType;
103  }
104  if(theType==GDT_Unknown)
105  theType=input.getDataType();
106 
107  if(verbose_opt[0])
108  std::cout << std::endl << "Output pixel type: " << GDALGetDataTypeName(theType) << endl;
109 
110  string imageType=input.getImageType();
111  if(oformat_opt.size())
112  imageType=oformat_opt[0];
113 
114  if(option_opt.findSubstring("INTERLEAVE=")==option_opt.end()){
115  string theInterleave="INTERLEAVE=";
116  theInterleave+=input.getInterleave();
117  option_opt.push_back(theInterleave);
118  }
119  try{
120  output.open(output_opt[0],input.nrOfCol(),input.nrOfRow(),input.nrOfBand(),theType,imageType,option_opt);
121  }
122  catch(string errorstring){
123  cout << errorstring << endl;
124  exit(4);
125  }
126  output.setProjection(input.getProjection());
127  double gt[6];
128  input.getGeoTransform(gt);
129  output.setGeoTransform(gt);
130 
131  if(input.getColorTable()!=NULL)
132  output.setColorTable(input.getColorTable());
133 
134  filter2d::Filter2d filter2d;
135  if(verbose_opt[0])
136  std::cout<< "class values: ";
137  if(colorTable_opt.size())
138  output.setColorTable(colorTable_opt[0]);
139  filter2d.shadowDsm(input,output,sza_opt[0],saa_opt[0],input.getDeltaX(),flag_opt[0]);
140  input.close();
141  output.close();
142  return 0;
143 }