Debian Codegraph @

utils/demconvert/demconvert.cxx

raw symbols SCIP
1 // demconvert.cxx -- convert dem into lower resolutions
2 //
3 // Copyright (C) 2016 Peter Sadrozinski
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #include <fstream>
23 #include <sstream>
24 #include <iostream>
25 #include <iomanip>
26 #include <cmath>
27
28 #include <osg/ArgumentParser>
29
30 #include <simgear/misc/stdint.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/debug/logstream.hxx>
33
34 #include <simgear/scene/dem/SGDem.hxx>
35 #include <simgear/scene/dem/SGDemSession.hxx>
36
37 int main(int argc, char** argv)
38 {
39 std::string demroot;
40 std::string inputvfp;
41 int tileWidth;
42 int tileHeight;
43 int resx, resy;
44 int overlap = 0;
45 SGDem dem;
46
47 osg::ApplicationUsage* usage = new osg::ApplicationUsage();
48 usage->setApplicationName("demconvert");
49 usage->setCommandLineUsage(
50 "Convert high resolution DEM to low res suitable for terrasync dl.");
51 usage->addCommandLineOption("--inputvfp <dir>", "input VFP root directory");
52 usage->addCommandLineOption("--demroot <dir>", "input/ouput DEM root directory");
53 usage->addCommandLineOption("--width <N>", "width (in degrees) of created tiles");
54 usage->addCommandLineOption("--height <N>", "height (in degrees) of created tiles");
55 usage->addCommandLineOption("--resx <N>", "resolution of created tiles (w/o overlap)");
56 usage->addCommandLineOption("--resy <N>", "resolution of created tiles (w/o overlap)");
57 usage->addCommandLineOption("--overlap <N>", "number of pixels of overlap");
58
59 // use an ArgumentParser object to manage the program arguments.
60 osg::ArgumentParser arguments(&argc, argv);
61 arguments.setApplicationUsage(usage);
62
63 sglog().setLogLevels( SG_TERRAIN, SG_INFO );
64
65 arguments.read("--inputvfp", inputvfp);
66 printf( "--inputvfp is %s\n", inputvfp.c_str() );
67
68 arguments.read("--demroot", demroot);
69 if ( inputvfp.empty() && demroot.empty() ) {
70 arguments.reportError("--inputvfp or --demroot argument required.");
71 } else if ( !demroot.empty() ) {
72 SGPath s(demroot);
73 if (!s.isDir()) {
74 arguments.reportError(
75 "--demroot directory does not exist or is not directory.");
76 } else if (!s.canRead()) {
77 arguments.reportError(
78 "--demroot directory cannot be read. Check permissions.");
79 } else if (!s.canWrite()) {
80 arguments.reportError(
81 "--demroot directory cannot be written. Check permissions.");
82 } else if ( !dem.addRoot(s) ) {
83 // see if we specified input as raw directory
84 if ( inputvfp.empty() ) {
85 arguments.reportError(
86 "--demroot directory is not a DEM heiarchy");
87 } else {
88 // create a new dem heiarchy
89 printf("Creating new dem heiarchy at %s\n", s.c_str() );
90 dem.createRoot(s);
91 }
92 }
93 } else {
94 SGPath s(inputvfp);
95 if (!s.isDir()) {
96 arguments.reportError(
97 "--inputvfp directory does not exist or is not directory.");
98 } else if (!s.canRead()) {
99 arguments.reportError(
100 "--inputvfp directory cannot be read. Check permissions.");
101 }
102 }
103
104 if (!arguments.read("--width", tileWidth)) {
105 arguments.reportError("--width argument required.");
106 } else {
107 if ( tileWidth < 1 || tileWidth > 60 )
108 arguments.reportError(
109 "--width must be between 1 and 60");
110 }
111
112 if (!arguments.read("--height", tileHeight)) {
113 arguments.reportError("--height argument required.");
114 } else {
115 if ( tileHeight < 1 || tileHeight > 60 )
116 arguments.reportError(
117 "--height must be between 1 and 60");
118 }
119
120 if (!arguments.read("--resx", resx)) {
121 arguments.reportError("--resx argument required.");
122 } else {
123 if ( resx < 2 )
124 arguments.reportError(
125 "--resx must be between greater than 2");
126 }
127
128 if (!arguments.read("--resy", resy)) {
129 arguments.reportError("--resy argument required.");
130 } else {
131 if ( resy < 2 )
132 arguments.reportError(
133 "--resy must be between greater than 2");
134 }
135
136 if (arguments.read("--overlap", overlap)) {
137 if ( overlap > (resx/2) || overlap > (resy/2) )
138 arguments.reportError(
139 "--overlap is greater than half tile");
140 }
141
142 if (arguments.errors()) {
143 arguments.writeErrorMessages(std::cout);
144 arguments.getApplicationUsage()->write(std::cout,
145 osg::ApplicationUsage::COMMAND_LINE_OPTION |
146 osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE, 80, true);
147 return EXIT_FAILURE;
148 }
149
150 double lat_dec = (double)tileHeight / (double)resy;
151 double lon_inc = (double)tileWidth / (double)resx;
152
153 SG_LOG( SG_TERRAIN, SG_INFO, "tileWidth: " << tileWidth);
154 SG_LOG( SG_TERRAIN, SG_INFO, "tileHeight: " << tileHeight);
155 SG_LOG( SG_TERRAIN, SG_INFO, "lon_inc: " << lon_inc);
156 SG_LOG( SG_TERRAIN, SG_INFO, "lat_dec: " << lat_dec);
157
158 #define MIN_X -180
159 #define MIN_Y -90
160 #define MAX_X 180
161 #define MAX_Y 90
162
163 // create a new dem level in demRoot
164 SGDemRoot* root = dem.getRoot(0);
165 if ( root ) {
166 int outLvl = root->createLevel( tileWidth, tileHeight, resx, resy, overlap, ".tiff" );
167 if ( outLvl >= 0 ) {
168 printf("SGDem::createLevel success\n");
169
170 // traverse the new tiles, 1 at a time
171 for ( int tilex = MIN_X; tilex < MAX_X; tilex += tileWidth ) {
172 for ( int tiley = MAX_Y; tiley > MIN_Y; tiley -= tileHeight ) {
173 // traverse rows from north to south, then columns west to east
174 double lonmin = (double)tilex;
175 double lonmax = lonmin + (double)tileWidth;
176 double latmax = (double)tiley;
177 double latmin = latmax - (double)tileHeight;
178
179 unsigned wo = SGDem::longitudeDegToOffset(lonmin);
180 unsigned eo = SGDem::longitudeDegToOffset(lonmax);
181 unsigned so = SGDem::latitudeDegToOffset(latmin);
182 unsigned no = SGDem::latitudeDegToOffset(latmax);
183
184 if ( !inputvfp.empty() ) {
185 // read from vfp files
186 printf("open session from raw directory\n");
187 SGDemSession s = dem.openSession( SGGeod::fromDeg(lonmin, latmin), SGGeod::fromDeg(lonmax, latmax), SGPath(inputvfp) );
188 printf("opened session from raw directory\n");
189
190 // create a new dem tile for the new level
191 SGDemTileRef tile = root->createTile( outLvl, (int)lonmin, (int)latmin, overlap, s );
192
193 s.close();
194 } else {
195 // read session from DEM root - don't cache - include adjacent tiles
196 fprintf( stderr, "open session from DEM level %d\n", outLvl-1);
197 SGDemSession s = dem.openSession( wo, so, eo, no, outLvl-1, false );
198 fprintf( stderr, "session has %d tiles\n", s.size() );
199
200 // create a new dem tile for the new level
201 SGDemTileRef tile = root->createTile( outLvl, (int)lonmin, (int)latmin, overlap, s );
202
203 s.close();
204 }
205 }
206 }
207
208 printf("SGDem::close Level \n");
209
210 root->closeLevel( outLvl );
211 } else {
212 printf("SGDem::createLevel failed\n");
213 }
214 } else {
215 printf("SGDem::getRoot failed\n");
216 }
217
218 return EXIT_SUCCESS;
219 }