1 /**********************************************************
2  * Version $Id: srtm30.cpp 1921 2014-01-09 10:24:11Z oconrad $
3  *********************************************************/
4 
5 ///////////////////////////////////////////////////////////
6 //                                                       //
7 //                         SAGA                          //
8 //                                                       //
9 //      System for Automated Geoscientific Analyses      //
10 //                                                       //
11 //                     Tool Library                      //
12 //                        Grid_IO                        //
13 //                                                       //
14 //-------------------------------------------------------//
15 //                                                       //
16 //                      SRTM30.cpp                       //
17 //                                                       //
18 //                 Copyright (C) 2004 by                 //
19 //                      Olaf Conrad                      //
20 //                                                       //
21 //-------------------------------------------------------//
22 //                                                       //
23 // This file is part of 'SAGA - System for Automated     //
24 // Geoscientific Analyses'. SAGA is free software; you   //
25 // can redistribute it and/or modify it under the terms  //
26 // of the GNU General Public License as published by the //
27 // Free Software Foundation, either version 2 of the     //
28 // License, or (at your option) any later version.       //
29 //                                                       //
30 // SAGA is distributed in the hope that it will be       //
31 // useful, but WITHOUT ANY WARRANTY; without even the    //
32 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
33 // PARTICULAR PURPOSE. See the GNU General Public        //
34 // License for more details.                             //
35 //                                                       //
36 // You should have received a copy of the GNU General    //
37 // Public License along with this program; if not, see   //
38 // <http://www.gnu.org/licenses/>.                       //
39 //                                                       //
40 //-------------------------------------------------------//
41 //                                                       //
42 //    e-mail:     oconrad@saga-gis.org                   //
43 //                                                       //
44 //    contact:    Olaf Conrad                            //
45 //                Institute of Geography                 //
46 //                University of Goettingen               //
47 //                Goldschmidtstr. 5                      //
48 //                37077 Goettingen                       //
49 //                Germany                                //
50 //                                                       //
51 ///////////////////////////////////////////////////////////
52 
53 //---------------------------------------------------------
54 
55 
56 ///////////////////////////////////////////////////////////
57 //														 //
58 //														 //
59 //														 //
60 ///////////////////////////////////////////////////////////
61 
62 //---------------------------------------------------------
63 #include "srtm30.h"
64 
65 
66 ///////////////////////////////////////////////////////////
67 //														 //
68 //														 //
69 //														 //
70 ///////////////////////////////////////////////////////////
71 
72 //---------------------------------------------------------
CSRTM30_Import(void)73 CSRTM30_Import::CSRTM30_Import(void)
74 {
75 	CSG_Parameter	*pNode_0;
76 	CSG_Parameters	*pParameters;
77 
78 	//-----------------------------------------------------
79 	Set_Name(_TL("Import SRTM30 DEM"));
80 
81 	Set_Author		(SG_T("(c) 2004 by O.Conrad"));
82 
83 	Set_Description	(_TW(
84 		"Extracts elevation grids from SRTM30 data.\n\n"
85 
86 		"<i>\"SRTM30 is a near-global digital elevation model (DEM) comprising a "
87 		"combination of data from the Shuttle Radar Topography Mission, flown "
88 		"in February, 2000 and the the U.S. Geological Survey's GTOPO30 data "
89 		"set. It can be considered to be either an SRTM data set enhanced with "
90 		"GTOPO30, or as an upgrade to GTOPO30.\"</i> (NASA)\n\n"
91 
92 		"Further information about the GTOPO30 data set:\n"
93 		"<a target=\"_blank\" href=\"http://edcdaac.usgs.gov/gtopo30/gtopo30.html\">"
94 		"http://edcdaac.usgs.gov/gtopo30/gtopo30.html</a>\n\n"
95 
96 		"SRTM30 data can be downloaded from:\n"
97 		"<a target=\"_blank\" href=\"ftp://e0srp01u.ecs.nasa.gov/srtm/version2/SRTM30/\">"
98 		"ftp://e0srp01u.ecs.nasa.gov/srtm/version2/SRTM30/</a>\n\n"
99 
100 		"A directory, that contains the uncompressed SRTM30 DEM files, can be located using "
101 		"the \"Path\" Parameter of this tool.")
102 	);
103 
104 	//-----------------------------------------------------
105 	pNode_0	= Parameters.Add_Grid_Output(
106 		NULL	, "GRID"		, _TL("Grid"),
107 		_TL("")
108 	);
109 
110 	pNode_0	= Parameters.Add_FilePath(
111 		NULL	, "PATH"		, _TL("Path"),
112 		_TL(""),
113 		NULL, NULL, false, true
114 	);
115 
116 	//-----------------------------------------------------
117 	pNode_0	= Parameters.Add_Value(
118 		NULL	, "XMIN"		, _TL("West []"),
119 		_TL(""),
120 		PARAMETER_TYPE_Int, 60.0
121 	);
122 
123 	pNode_0	= Parameters.Add_Value(
124 		NULL	, "XMAX"		, _TL("East []"),
125 		_TL(""),
126 		PARAMETER_TYPE_Int, 120.0
127 	);
128 
129 	pNode_0	= Parameters.Add_Value(
130 		NULL	, "YMIN"		, _TL("South []"),
131 		_TL(""),
132 		PARAMETER_TYPE_Int, 20.0
133 	);
134 	pNode_0	= Parameters.Add_Value(
135 		NULL	, "YMAX"		, _TL("North []"),
136 		_TL(""),
137 		PARAMETER_TYPE_Int, 50.0
138 	);
139 
140 	//-----------------------------------------------------
141 	pParameters	= Add_Parameters("TILE", _TL(""), _TL(""));
142 
143 	pNode_0	= pParameters->Add_Info_String(
144 		NULL	, "INFO"		, _TL("File does not exist:"),
145 		_TL(""),
146 		_TL("")
147 	);
148 
149 	pNode_0	= pParameters->Add_FilePath(
150 		NULL	, "PATH"		, _TL("Select file"),
151 		_TL(""),
152 		_TL("SRTM30 DEM Tiles (*.dem)|*.dem|All Files|*.*")
153 	);
154 }
155 
156 //---------------------------------------------------------
~CSRTM30_Import(void)157 CSRTM30_Import::~CSRTM30_Import(void)
158 {}
159 
160 
161 ///////////////////////////////////////////////////////////
162 //														 //
163 //														 //
164 //														 //
165 ///////////////////////////////////////////////////////////
166 
167 //---------------------------------------------------------
168 #define	X_WIDTH		4800
169 #define	Y_WIDTH		6000
170 
171 //---------------------------------------------------------
On_Execute(void)172 bool CSRTM30_Import::On_Execute(void)
173 {
174 	char	x_sTile[9][5]	= {	"W180", "W140", "W100", "W060", "W020", "E020", "E060", "E100", "E140"	},
175 			y_sTile[3][4]	= {	"S10", "N40", "N90"	};
176 
177 	double	dSize			= 30.0 / (60.0 * 60.0);
178 
179 	//-----------------------------------------------------
180 	int			xTile, yTile;
181 	double		xMin, xMax, yMin, yMax;
182 	TSG_Rect	rOut, rTile;
183 	CSG_String	sTile;
184 	CSG_Grid	*pOut;
185 
186 	//-----------------------------------------------------
187 	xMin		= Parameters("XMIN")->asInt();
188 	xMax		= Parameters("XMAX")->asInt();
189 	yMin		= Parameters("YMIN")->asInt();
190 	yMax		= Parameters("YMAX")->asInt();
191 
192 	rOut.xMin	= (180 + xMin) / 40.0 * X_WIDTH;
193 	rOut.xMax	= rOut.xMin + (int)((xMax - xMin) / dSize);
194 	rOut.yMin	= ( 60 + yMin) / 50.0 * Y_WIDTH;
195 	rOut.yMax	= rOut.yMin + (int)((yMax - yMin) / dSize);
196 
197 	//-----------------------------------------------------
198 	pOut		= SG_Create_Grid(SG_DATATYPE_Short,
199 					(int)(rOut.xMax - rOut.xMin),
200 					(int)(rOut.yMax - rOut.yMin),
201 					dSize,
202 					xMin + 0.5 * dSize,
203 					yMin + 0.5 * dSize
204 				);
205 
206 	pOut->Set_NoData_Value(-9999);
207 	pOut->Assign_NoData();
208 	pOut->Set_Name(SG_T("SRTM30"));
209 	pOut->Get_Projection().Create(SG_T("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"));
210 
211 	//-----------------------------------------------------
212 	for(yTile=0, rTile.yMin=0, rTile.yMax=Y_WIDTH; yTile<3; yTile++, rTile.yMin+=Y_WIDTH, rTile.yMax+=Y_WIDTH)
213 	{
214 		for(xTile=0, rTile.xMin=0, rTile.xMax=X_WIDTH; xTile<9; xTile++, rTile.xMin+=X_WIDTH, rTile.xMax+=X_WIDTH)
215 		{
216 			sTile.Printf(SG_T("Tile: %s%s"), x_sTile[xTile], y_sTile[yTile]);
217 			Process_Set_Text(sTile);
218 
219 			sTile.Printf(SG_T("%s%s%s.dem"), Parameters("PATH")->asString(), x_sTile[xTile], y_sTile[yTile]);
220 			Tile_Load(sTile, rTile, pOut, rOut);
221 		}
222 	}
223 
224 	//-----------------------------------------------------
225 	Parameters("GRID")->Set_Value(pOut);
226 
227 	return( true );
228 }
229 
230 
231 ///////////////////////////////////////////////////////////
232 //														 //
233 //														 //
234 //														 //
235 ///////////////////////////////////////////////////////////
236 
237 //---------------------------------------------------------
Tile_Load(const SG_Char * sTile,TSG_Rect & rTile,CSG_Grid * pOut,TSG_Rect & rOut)238 bool CSRTM30_Import::Tile_Load(const SG_Char *sTile, TSG_Rect &rTile, CSG_Grid *pOut, TSG_Rect &rOut)
239 {
240 	short		Value;
241 	int			x, y, xOut, yOut;
242 	FILE		*Stream;
243 	CSG_Rect	r(rTile);
244 
245 	if( r.Intersects(rOut) != INTERSECTION_None && (Stream = Tile_Open(sTile)) != NULL )
246 	{
247 		for(y=0, yOut=(int)(rTile.yMax-rOut.yMin); y<Y_WIDTH && yOut>=0 && Set_Progress(y, Y_WIDTH); y++, yOut--)
248 		{
249 			for(x=0, xOut=(int)(rTile.xMin-rOut.xMin); x<X_WIDTH; x++, xOut++)
250 			{
251 				fread(&Value, 1, sizeof(short), Stream);
252 
253 				if(	xOut >= 0 && xOut < pOut->Get_NX() && yOut >= 0 && yOut < pOut->Get_NY() )
254 				{
255 					SG_Swap_Bytes(&Value, sizeof(short));
256 
257 					pOut->Set_Value(xOut, yOut, Value);
258 				}
259 			}
260 		}
261 
262 		fclose(Stream);
263 
264 		return( true );
265 	}
266 
267 	return( false );
268 }
269 
270 
271 ///////////////////////////////////////////////////////////
272 //														 //
273 //														 //
274 //														 //
275 ///////////////////////////////////////////////////////////
276 
277 //---------------------------------------------------------
Tile_Open(const SG_Char * sTile)278 FILE * CSRTM30_Import::Tile_Open(const SG_Char *sTile)
279 {
280 	const SG_Char	*sPath;
281 	FILE			*Stream;
282 	CSG_String		fName;
283 	CSG_Parameters	*pParameters;
284 
285 	fName	= sTile;
286 
287 	if( (Stream = fopen(fName.b_str(), "rb")) == NULL )
288 	{
289 		pParameters	= Get_Parameters("TILE");
290 		pParameters->Get_Parameter("INFO")->Set_Value(sTile);
291 
292 		if( Dlg_Parameters(pParameters, _TL("Locate STRM30 Data File")) && (sPath = pParameters->Get_Parameter("PATH")->asString()) != NULL )
293 		{
294 			fName	= sPath;
295 			Stream	= fopen(fName.b_str(), "rb");
296 		}
297 	}
298 
299 	return( Stream );
300 }
301