SAGA API Version 9.14
Loading...
Searching...
No Matches
map_basemap.cpp
Go to the documentation of this file.
1
3// //
4// SAGA //
5// //
6// System for Automated Geoscientific Analyses //
7// //
8// Application Programming Interface //
9// //
10// Library: SAGA_API //
11// //
12//-------------------------------------------------------//
13// //
14// map_graticule.cpp //
15// //
16// Copyright (C) 2026 by //
17// Olaf Conrad //
18// //
19//-------------------------------------------------------//
20// //
21// This file is part of 'SAGA - System for Automated //
22// Geoscientific Analyses'. SAGA is free software; you //
23// can redistribute it and/or modify it under the terms //
24// of the GNU General Public License as published by the //
25// Free Software Foundation, either version 2 of the //
26// License, or (at your option) any later version. //
27// //
28// SAGA is distributed in the hope that it will be //
29// useful, but WITHOUT ANY WARRANTY; without even the //
30// implied warranty of MERCHANTABILITY or FITNESS FOR A //
31// PARTICULAR PURPOSE. See the GNU General Public //
32// License for more details. //
33// //
34// You should have received a copy of the GNU General //
35// Public License along with this program; if not, see //
36// <http://www.gnu.org/licenses/>. //
37// //
38//-------------------------------------------------------//
39// //
40// e-mail: oconrad@saga-gis.org //
41// //
42// contact: Olaf Conrad //
43// Institute of Geography //
44// University of Hamburg //
45// Germany //
46// //
48
49//---------------------------------------------------------
50#include "map_dc.h"
51#include "map_layer.h"
52
53#include "../dc_helper.h"
54#include "../tool_library.h"
55
56
58// //
59// //
60// //
62
63//---------------------------------------------------------
65{
66 m_pTool = NULL; // remember the last created base map tool instance
67
68 m_Parameters.Set_Name ("BASEMAP");
69 m_Parameters.Set_Identifier("BASEMAP");
70
71 //-----------------------------------------------------
72 m_Parameters.Add_Node("", "NODE_GENERAL",_TL("General") , _TL(""));
73
74 m_Parameters.Add_String("NODE_GENERAL",
75 "NAME" , _TL("Name"),
76 _TL(""),
77 _TL("Base Map")
78 );
79
80 m_Parameters.Add_Choice("NODE_GENERAL",
81 "SERVER" , _TL("Server"),
82 _TL(""),
83 CSG_String::Format("%s|%s|%s|%s|%s|%s|%s|%s|%s|%s",
84 SG_T("Open Street Map"),
85 SG_T("Google Map"),
86 SG_T("Google Satellite"),
87 SG_T("Google Hybrid"),
88 SG_T("Google Terrain"),
89 SG_T("Google Terrain, Streets and Water"),
90 SG_T("ArcGIS MapServer Tiles"),
91 SG_T("TopPlusOpen"),
92 SG_T("EMODnet Bathymetry WMTS service"),
93 SG_T("user defined")
94 ), 0
95 );
96
97 m_Parameters.Add_String("SERVER",
98 "SERVER_USER", _TL("Server"),
99 _TL(""),
100 "tile.openstreetmap.org/${z}/${x}/${y}.png"
101 );
102
103 m_Parameters.Add_Int("SERVER",
104 "SERVER_EPSG" , _TL("EPSG"),
105 _TL(""),
106 3857
107 );
108
109 //-----------------------------------------------------
110 m_Parameters.Add_Bool("NODE_GENERAL",
111 "CACHE" , _TL("Cache"),
112 _TL("Enable local disk cache. Allows for offline operation."),
113 false
114 );
115
116 m_Parameters.Add_FilePath("CACHE",
117 "CACHE_DIR" , _TL("Cache Directory"),
118 _TL("If not specified the cache will be created in the current user's temporary directory."),
119 NULL, NULL, false, true
120 );
121
122 //-----------------------------------------------------
123 m_Parameters.Add_Node("", "NODE_DISPLAY",_TL("Display") , _TL(""));
124
125 m_Parameters.Add_Double("NODE_DISPLAY",
126 "TRANSPARENCY", _TL("Transparency"),
127 _TL("Transparency specified as percentage."),
128 0., 0., true, 100., true
129 );
130
131 m_Parameters.Add_Double("NODE_DISPLAY",
132 "BRIGHTNESS" , _TL("Maximum Brightness"),
133 _TL("Brightness threshold [%] below a pixel is displayed. Set to 100% to display all (default)."),
134 100., 0., true, 100., true
135 );
136
137 m_Parameters.Add_Choice("NODE_DISPLAY",
138 "GRAYSCALE" , _TL("Gray Scale"),
139 _TL(""),
140 CSG_String::Format("%s|%s|%s",
141 _TL("no"),
142 _TL("yes"),
143 _TL("yes, contrastet")
144 ), 0
145 );
146
147 m_Parameters.Add_Double("NODE_DISPLAY",
148 "RESOLUTION" , _TL("Resolution"),
149 _TL("resolution measured in screen pixels"),
150 1., 1., true
151 );
152}
153
154//---------------------------------------------------------
159
160
162// //
164
165//---------------------------------------------------------
167{
168 int n = 0;
169
170 for(int i=0; i<Parameters.Get_Count(); i++)
171 {
172 CSG_Parameter *pParameter = m_Parameters(Parameters[i].Get_Identifier());
173
174 if( pParameter && pParameter->Get_Type() == Parameters[i].Get_Type() )
175 {
176 CSG_String oldValue = pParameter->asString();
177
178 if( pParameter->Assign(Parameters(i)) && oldValue.Cmp(pParameter->asString())
179 && !pParameter->Cmp_Identifier("NAME" )
180 && !pParameter->Cmp_Identifier("CACHE" )
181 && !pParameter->Cmp_Identifier("CACHE_DIR") )
182 {
183 n++;
184 }
185 }
186 }
187
188 if( n > 0 )
189 {
190 m_BaseMap.Destroy(); // forcing a base map refresh
191 }
192
193 return( true );
194}
195
196//---------------------------------------------------------
198{
199 if( Flags & PARAMETER_CHECK_ENABLE )
200 {
201 if( Parameter.Cmp_Identifier("SERVER") )
202 {
203 bool bUser = Parameter.asInt() >= Parameter.asChoice()->Get_Count() - 1;
204
205 Parameters.Set_Enabled("SERVER_USER", bUser);
206 Parameters.Set_Enabled("SERVER_EPSG", bUser);
207 }
208
209 if( Parameter.Cmp_Identifier("CACHE") )
210 {
211 Parameters.Set_Enabled("CACHE_DIR" , Parameter.asBool());
212 }
213 }
214
215 return( true );
216}
217
218//---------------------------------------------------------
220{
221 m_BaseMap.Destroy(); // forcing a base map refresh
222
223 return( true );
224}
225
226
228// //
230
231//---------------------------------------------------------
233{
234 CSG_Grid_System System(dc_Map.DC2World(), dc_Map.rWorld().Get_XMin(), dc_Map.rWorld().Get_YMin(), dc_Map.rDC().GetWidth(), dc_Map.rDC().GetHeight());
235
236 if( System != m_BaseMap.Get_System() )
237 {
238 if( !Get_BaseMap(System, Projection) )
239 {
240 wxColour oldColour(dc_Map.Get_DC().GetTextForeground()); dc_Map.Get_DC().SetTextForeground(*wxLIGHT_GREY);
241 wxFont oldFont (dc_Map.Get_DC().GetFont ()); dc_Map.Get_DC().SetFont(wxSystemSettings::GetFont(wxSYS_SYSTEM_FONT));
242
243 wxString Text(_TL("Base Map Error")); wxSize Size(dc_Map.Get_DC().GetTextExtent(Text));
244
245 for(int y=dc_Map.rDC().GetTop(), x0=0; y<=dc_Map.rDC().GetBottom(); y+=2*Size.y, x0=x0?0:Size.x)
246 {
247 for(int x=dc_Map.rDC().GetLeft()+x0; x<=dc_Map.rDC().GetRight(); x+=2*Size.x)
248 {
249 SG_DC_Draw_Text(dc_Map.Get_DC(), SG_DC_TEXTALIGN_CENTER, x, y, 0., Text, SG_DC_TEXTEFFECT_FRAME, *wxWHITE, 1);
250 }
251 }
252
253 dc_Map.Get_DC().SetTextForeground(oldColour);
254 dc_Map.Get_DC().SetFont (oldFont );
255
256 return( false );
257 }
258 }
259
260 //-----------------------------------------------------
261 if( dc_Map.Draw_Image_Begin(m_Parameters["TRANSPARENCY"].asDouble() / 100.) )
262 {
263 #pragma omp parallel for
264 for(int y=0; y<m_BaseMap.Get_NY(); y++) for(int x=0, yy=m_BaseMap.Get_NY()-y-1; x<m_BaseMap.Get_NX(); x++)
265 {
266 if( !m_BaseMap.is_NoData(x, yy) )
267 {
268 dc_Map.Draw_Image_Pixel(x, y, m_BaseMap.asInt(x, yy));
269 }
270 }
271
272 dc_Map.Draw_Image_End();
273 }
274
275 //-----------------------------------------------------
276 return( true );
277}
278
279
281// //
283
284//---------------------------------------------------------
285bool CSG_Map_BaseMap::Get_BaseMap(const CSG_Grid_System &System, const CSG_Projection &Projection)
286{
287 CSG_Tool *pTool;
288
289 if( !Projection.is_Okay() || !(pTool = SG_Get_Tool_Library_Manager().Create_Tool("io_gdal", 9)) )
290 {
291 m_BaseMap.Destroy();
292
293 return( false );
294 }
295
297
298 //-----------------------------------------------------
299 CSG_Grid_System _System(System);
300
301 if( m_Parameters["RESOLUTION"].asDouble() > 1. )
302 {
303 _System.Create(m_Parameters["RESOLUTION"].asDouble() * System.Get_Cellsize(), System.Get_Extent(true));
304 }
305
306 if( _System != m_BaseMap.Get_System() )
307 {
308 m_BaseMap.Create(_System, SG_DATATYPE_Int);
309 }
310
311 m_BaseMap.Get_Projection() = Projection;
312
313 m_pTool = pTool; // remember the last created base map tool instance
314
315 pTool->Set_Manager(NULL); pTool->Set_Callback(false);
316
317 if( pTool->Set_Parameter("TARGET" , &m_BaseMap)
318 && pTool->Set_Parameter("TARGET_MAP" , &m_BaseMap)
319 && pTool->Set_Parameter("SERVER" , m_Parameters("SERVER" ))
320 && pTool->Set_Parameter("SERVER_USER", m_Parameters("SERVER_USER"))
321 && pTool->Set_Parameter("SERVER_EPSG", m_Parameters("SERVER_EPSG"))
322 && pTool->Set_Parameter("CACHE" , m_Parameters("CACHE" ))
323 && pTool->Set_Parameter("CACHE_DIR" , m_Parameters("CACHE_DIR" ))
324 && pTool->Set_Parameter("GRAYSCALE" , m_Parameters["GRAYSCALE" ].asInt() != 0)
325 && pTool->On_Before_Execution() && pTool->Execute() && pTool == m_pTool )
326 {
327 m_pTool = NULL;
328
329 int bGrayscale = m_Parameters["GRAYSCALE"].asInt();
330
331 if( System != m_BaseMap.Get_System() )
332 {
333 CSG_Grid BaseMap(m_BaseMap); m_BaseMap.Create(System, SG_DATATYPE_DWord);
334
335 #pragma omp parallel for
336 for(int y=0; y<m_BaseMap.Get_NY(); y++) for(int x=0; x<m_BaseMap.Get_NX(); x++)
337 {
338 double c;
339
340 if( BaseMap.Get_Value(System.Get_Grid_to_World(x, y), c, CSG_Grid_Resampling::Bicubic_2, false, !bGrayscale) )
341 {
342 m_BaseMap.Set_Value(x, y, c);
343 }
344 else
345 {
346 m_BaseMap.Set_NoData(x, y);
347 }
348 }
349 }
350
351 if( bGrayscale )
352 {
353 double offset = 0., scale = 1.;
354
355 if( bGrayscale == 2 && m_BaseMap.Get_Range() > 0. )
356 {
357 offset = m_BaseMap.Get_Min(); scale = 255. / m_BaseMap.Get_Range();
358 }
359
360 #pragma omp parallel for
361 for(int y=0; y<m_BaseMap.Get_NY(); y++) for(int x=0; x<m_BaseMap.Get_NX(); x++)
362 {
363 if( m_BaseMap.is_NoData(x, y) == false )
364 {
365 int c = (int)(scale * (m_BaseMap.asDouble(x, y) - offset));
366
367 m_BaseMap.Set_Value(x, y, SG_GET_RGB(c, c, c));
368 }
369 }
370 }
371
372 if( m_Parameters["BRIGHTNESS"].asDouble() < 100. )
373 {
374 int Threshold = (int)(0.5 + 3 * 255 * m_Parameters["BRIGHTNESS"].asDouble() / 100.);
375
376 #pragma omp parallel for
377 for(sLong i=0; i<m_BaseMap.Get_NCells(); i++)
378 {
379 int c = m_BaseMap.asInt(i);
380
381 if( Threshold < (SG_GET_R(c) + SG_GET_G(c) + SG_GET_B(c)) )
382 {
383 m_BaseMap.Set_NoData(i);
384 }
385 }
386 }
387 }
388 else if( pTool == m_pTool )
389 {
390 m_pTool = NULL;
391
392 m_BaseMap.Destroy();
393 }
394
395 //-----------------------------------------------------
397
399
400 return( m_BaseMap.is_Valid() );
401}
402
403
405// //
406// //
407// //
409
410//---------------------------------------------------------
void SG_UI_ProgressAndMsg_Lock(bool bOn)
#define SG_GET_B(rgb)
Definition api_core.h:1350
#define SG_GET_G(rgb)
Definition api_core.h:1349
signed long long sLong
Definition api_core.h:158
#define SG_T(s)
Definition api_core.h:537
#define SG_GET_R(rgb)
Definition api_core.h:1348
@ SG_DATATYPE_Int
Definition api_core.h:1050
@ SG_DATATYPE_DWord
Definition api_core.h:1049
#define SG_GET_RGB(r, g, b)
Definition api_core.h:1345
#define _TL(s)
Definition api_core.h:1619
TSG_Point Get_Grid_to_World(int xGrid, int yGrid) const
Definition grid.h:306
double Get_Cellsize(void) const
Definition grid.h:255
const CSG_Rect & Get_Extent(bool bCells=false) const
Definition grid.h:262
virtual bool Destroy(void)
Definition grid.cpp:379
virtual bool Parameters_Changed(void)
virtual bool Draw(CSG_Map_DC &dc, const CSG_Projection &Projection)
virtual bool Set_Parameters(const CSG_Parameters &Parameters)
virtual bool Parameter_Changed(CSG_Parameters &Parameters, CSG_Parameter &Parameter, int Flags)
virtual ~CSG_Map_BaseMap(void)
bool Draw_Image_Begin(double Transparency, Mode Mode=Mode::Transparent)
Definition map_dc.cpp:374
const CSG_Rect & rWorld(void) const
Definition map_dc.h:124
wxMemoryDC & Get_DC(void)
Definition map_dc.h:96
const wxRect & rDC(void) const
Definition map_dc.h:129
void Draw_Image_Pixel(int x, int y, int Color)
Definition map_dc.h:186
bool Draw_Image_End(void)
Definition map_dc.cpp:418
const double & DC2World(void) const
Definition map_dc.h:154
int Get_Count(void) const
Definition parameters.h:692
bool Assign(CSG_Parameter *pSource)
class CSG_Parameter_Choice * asChoice(void) const
virtual TSG_Parameter_Type Get_Type(void) const =0
bool asBool(void) const
Definition parameters.h:286
const SG_Char * asString(void) const
Definition parameters.h:290
bool Cmp_Identifier(const CSG_String &Identifier) const
int asInt(void) const
Definition parameters.h:287
int Get_Count(void) const
void Set_Enabled(bool bEnabled=true)
double Get_XMin(void) const
Definition geo_tools.h:505
double Get_YMin(void) const
Definition geo_tools.h:507
int Cmp(const CSG_String &String) const
static CSG_String Format(const char *Format,...)
bool Delete_Tool(CSG_Tool *pTool) const
bool Set_Manager(class CSG_Data_Manager *pManager)
Definition tool.cpp:874
bool Set_Parameter(const CSG_String &ID, CSG_Parameter *pValue)
Definition tool.cpp:1659
virtual bool On_Before_Execution(void)
Definition tool.h:242
void Set_Callback(bool bActive=true)
Definition tool.cpp:863
bool Execute(bool bAddHistory=false)
Definition tool.cpp:560
void SG_DC_Draw_Text(wxDC &dc, int Align, int x, int y, const wxString &Text)
#define SG_DC_TEXTALIGN_CENTER
Definition dc_helper.h:135
#define SG_DC_TEXTEFFECT_FRAME
Definition dc_helper.h:150
#define asDouble(i)
#define PARAMETER_CHECK_ENABLE
Definition parameters.h:191
CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)