1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //                    User Interface                     //
9 //                                                       //
10 //                    Program: SAGA                      //
11 //                                                       //
12 //-------------------------------------------------------//
13 //                                                       //
14 //                  WKSP_Map_Layer.cpp                   //
15 //                                                       //
16 //          Copyright (C) 2005 by Olaf Conrad            //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'. SAGA is free software; you   //
22 // can redistribute it and/or modify it under the terms  //
23 // of the GNU General Public License as published by the //
24 // Free Software Foundation, either version 2 of the     //
25 // License, or (at your option) any later version.       //
26 //                                                       //
27 // SAGA is distributed in the hope that it will be       //
28 // useful, but WITHOUT ANY WARRANTY; without even the    //
29 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
30 // PARTICULAR PURPOSE. See the GNU General Public        //
31 // License for more details.                             //
32 //                                                       //
33 // You should have received a copy of the GNU General    //
34 // Public License along with this program; if not, see   //
35 // <http://www.gnu.org/licenses/>.                       //
36 //                                                       //
37 //-------------------------------------------------------//
38 //                                                       //
39 //    contact:    Olaf Conrad                            //
40 //                Institute of Geography                 //
41 //                University of Goettingen               //
42 //                Goldschmidtstr. 5                      //
43 //                37077 Goettingen                       //
44 //                Germany                                //
45 //                                                       //
46 //    e-mail:     oconrad@saga-gis.org                   //
47 //                                                       //
48 ///////////////////////////////////////////////////////////
49 
50 //---------------------------------------------------------
51 #include <wx/window.h>
52 
53 #include <saga_api/saga_api.h>
54 
55 #include "res_commands.h"
56 
57 #include "wksp_data_manager.h"
58 #include "wksp_layer.h"
59 #include "wksp_layer_classify.h"
60 #include "wksp_map.h"
61 #include "wksp_map_layer.h"
62 
63 #include "wksp_grid.h"
64 #include "wksp_grids.h"
65 
66 
67 ///////////////////////////////////////////////////////////
68 //														 //
69 //														 //
70 //														 //
71 ///////////////////////////////////////////////////////////
72 
73 //---------------------------------------------------------
CWKSP_Map_Layer(CWKSP_Layer * pLayer)74 CWKSP_Map_Layer::CWKSP_Map_Layer(CWKSP_Layer *pLayer)
75 {
76 	m_pLayer		= pLayer;
77 
78 	m_bShow			= true;
79 	m_bProject		= false;
80 	m_bProject_Area	= false;
81 	m_bFitColors	= false;
82 }
83 
84 
85 ///////////////////////////////////////////////////////////
86 //														 //
87 ///////////////////////////////////////////////////////////
88 
89 //---------------------------------------------------------
Get_Name(void)90 wxString CWKSP_Map_Layer::Get_Name(void)
91 {
92 	wxString	Name(m_pLayer->Get_Name());
93 
94 	return( !m_bShow ? "* " + Name : Name );
95 }
96 
97 //---------------------------------------------------------
Get_Description(void)98 wxString CWKSP_Map_Layer::Get_Description(void)
99 {
100 	return( m_pLayer->Get_Description() );
101 }
102 
103 //---------------------------------------------------------
Get_Menu(void)104 wxMenu * CWKSP_Map_Layer::Get_Menu(void)
105 {
106 	wxMenu	*pMenu, *pMenu_Edit;
107 
108 	pMenu	= new wxMenu(m_pLayer->Get_Name());
109 
110 	//-----------------------------------------------------
111 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_WKSP_ITEM_CLOSE);
112 	CMD_Menu_Add_Item(pMenu,  true, ID_CMD_MAPS_LAYER_SHOW);
113 	pMenu->AppendSeparator();
114 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_MAPS_MOVE_TOP);
115 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_MAPS_MOVE_UP);
116 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_MAPS_MOVE_DOWN);
117 	CMD_Menu_Add_Item(pMenu, false, ID_CMD_MAPS_MOVE_BOTTOM);
118 
119 	//-----------------------------------------------------
120 	switch( m_pLayer->Get_Type() )
121 	{
122 	default:
123 		break;
124 
125 	case WKSP_ITEM_Grid:
126 	case WKSP_ITEM_Grids:
127 		pMenu->AppendSeparator();
128 		CMD_Menu_Add_Item(pMenu, true, ID_CMD_MAPS_GRID_FITCOLORS);
129 		break;
130 	}
131 
132 	//-----------------------------------------------------
133 	CSG_Projection	prj_Layer, prj_Map;
134 
135 	if( _Projected_Get_Projections(prj_Layer, prj_Map) )
136 	{
137 		CMD_Menu_Add_Item(pMenu, true, ID_CMD_MAPS_PROJECT);
138 
139 		if( m_bProject && (m_pLayer->Get_Type() == WKSP_ITEM_Grid || m_pLayer->Get_Type() == WKSP_ITEM_Grids) )
140 		{
141 			CMD_Menu_Add_Item(pMenu, true, ID_CMD_MAPS_PROJECT_AREA);
142 		}
143 	}
144 
145 	//-----------------------------------------------------
146 	if( (pMenu_Edit = m_pLayer->Edit_Get_Menu()) != NULL )
147 	{
148 		pMenu->AppendSeparator();
149 		pMenu->Append(ID_CMD_WKSP_FIRST, _TL("Edit"), pMenu_Edit);
150 	}
151 
152 	//-----------------------------------------------------
153 	return( pMenu );
154 }
155 
156 
157 ///////////////////////////////////////////////////////////
158 //														 //
159 ///////////////////////////////////////////////////////////
160 
161 //---------------------------------------------------------
On_Command(int Cmd_ID)162 bool CWKSP_Map_Layer::On_Command(int Cmd_ID)
163 {
164 	switch( Cmd_ID )
165 	{
166 	default:
167 		return( CWKSP_Base_Item::On_Command(Cmd_ID) );
168 
169 	case ID_CMD_SHAPES_EDIT_SHAPE:
170 	case ID_CMD_SHAPES_EDIT_ADD_SHAPE:
171 	case ID_CMD_SHAPES_EDIT_DEL_SHAPE:
172 	case ID_CMD_SHAPES_EDIT_ADD_PART:
173 	case ID_CMD_SHAPES_EDIT_DEL_PART:
174 	case ID_CMD_SHAPES_EDIT_DEL_POINT:
175 	case ID_CMD_SHAPES_EDIT_SEL_CLEAR:
176 	case ID_CMD_SHAPES_EDIT_SEL_INVERT:
177 		return( m_pLayer->On_Command(Cmd_ID) );
178 
179 	case ID_CMD_WKSP_ITEM_RETURN:
180 	case ID_CMD_MAPS_LAYER_SHOW:
181 		m_bShow	= !m_bShow;
182 		((wxTreeCtrl *)Get_Control())->SetItemText(GetId(), Get_Name());
183 		((CWKSP_Map *)Get_Manager())->View_Refresh(true);
184 		break;
185 
186 	case ID_CMD_MAPS_MOVE_TOP:
187 		if( Get_Manager()->Move_Top(this) )
188 			((CWKSP_Map *)Get_Manager())->View_Refresh(false);
189 		break;
190 
191 	case ID_CMD_MAPS_MOVE_BOTTOM:
192 		if( Get_Manager()->Move_Bottom(this) )
193 			((CWKSP_Map *)Get_Manager())->View_Refresh(false);
194 		break;
195 
196 	case ID_CMD_MAPS_MOVE_UP:
197 		if( Get_Manager()->Move_Up(this) )
198 			((CWKSP_Map *)Get_Manager())->View_Refresh(false);
199 		break;
200 
201 	case ID_CMD_MAPS_MOVE_DOWN:
202 		if( Get_Manager()->Move_Down(this) )
203 			((CWKSP_Map *)Get_Manager())->View_Refresh(false);
204 		break;
205 
206 	case ID_CMD_MAPS_GRID_FITCOLORS:
207 		if( (m_bFitColors = !m_bFitColors) == true )
208 		{
209 			Fit_Colors(((CWKSP_Map *)Get_Manager())->Get_Extent());
210 		}
211 		break;
212 
213 	case ID_CMD_MAPS_PROJECT:
214 		m_bProject	= !m_bProject; ((CWKSP_Map *)Get_Manager())->View_Refresh(false);
215 		break;
216 
217 	case ID_CMD_MAPS_PROJECT_AREA:
218 		if( m_bProject )
219 		{
220 			m_bProject_Area	= !m_bProject_Area; ((CWKSP_Map *)Get_Manager())->View_Refresh(false);
221 		}
222 		break;
223 	}
224 
225 	return( true );
226 }
227 
228 //---------------------------------------------------------
On_Command_UI(wxUpdateUIEvent & event)229 bool CWKSP_Map_Layer::On_Command_UI(wxUpdateUIEvent &event)
230 {
231 	switch( event.GetId() )
232 	{
233 	default:
234 		return( CWKSP_Base_Item::On_Command_UI(event) );
235 
236 	case ID_CMD_SHAPES_EDIT_SHAPE:
237 	case ID_CMD_SHAPES_EDIT_ADD_SHAPE:
238 	case ID_CMD_SHAPES_EDIT_DEL_SHAPE:
239 	case ID_CMD_SHAPES_EDIT_ADD_PART:
240 	case ID_CMD_SHAPES_EDIT_DEL_PART:
241 	case ID_CMD_SHAPES_EDIT_DEL_POINT:
242 	case ID_CMD_SHAPES_EDIT_SEL_CLEAR:
243 	case ID_CMD_SHAPES_EDIT_SEL_INVERT:
244 		return( m_pLayer->On_Command_UI(event) );
245 
246 	case ID_CMD_MAPS_LAYER_SHOW:
247 		event.Check(m_bShow);
248 		break;
249 
250 	case ID_CMD_MAPS_MOVE_TOP:
251 	case ID_CMD_MAPS_MOVE_UP:
252 		event.Enable(Get_Index() > 0);
253 		break;
254 
255 	case ID_CMD_MAPS_MOVE_DOWN:
256 	case ID_CMD_MAPS_MOVE_BOTTOM:
257 		event.Enable(Get_Index() < Get_Manager()->Get_Count() - 1);
258 		break;
259 
260 	case ID_CMD_MAPS_GRID_FITCOLORS:
261 		event.Check(m_bFitColors);
262 		break;
263 
264 	case ID_CMD_MAPS_PROJECT:
265 		event.Check(m_bProject);
266 		break;
267 
268 	case ID_CMD_MAPS_PROJECT_AREA:
269 		event.Check(m_bProject_Area);
270 		break;
271 	}
272 
273 	return( true );
274 }
275 
276 
277 ///////////////////////////////////////////////////////////
278 //														 //
279 ///////////////////////////////////////////////////////////
280 
281 //---------------------------------------------------------
Load_Settings(CSG_MetaData * pEntry)282 bool CWKSP_Map_Layer::Load_Settings(CSG_MetaData *pEntry)
283 {
284 	if( pEntry )
285 	{
286 		m_bShow         = !(*pEntry)("SHOW"        ) || (*pEntry)["SHOW"        ].Get_Content().CmpNoCase("true") == 0;
287 		m_bProject      =  (*pEntry)("PROJECT"     ) && (*pEntry)["PROJECT"     ].Get_Content().CmpNoCase("true") == 0;
288 		m_bProject_Area =  (*pEntry)("PROJECT_AREA") && (*pEntry)["PROJECT_AREA"].Get_Content().CmpNoCase("true") == 0;
289 		m_bFitColors    =  (*pEntry)("FITCOLORS"   ) && (*pEntry)["FITCOLORS"   ].Get_Content().CmpNoCase("true") == 0;
290 
291 		if( !m_bShow )
292 		{
293 			((wxTreeCtrl *)Get_Control())->SetItemText(GetId(), Get_Name());
294 		}
295 
296 		return( true );
297 	}
298 
299 	return( false );
300 }
301 
302 //---------------------------------------------------------
Save_Settings(CSG_MetaData * pEntry)303 bool CWKSP_Map_Layer::Save_Settings(CSG_MetaData *pEntry)
304 {
305 	if( pEntry )
306 	{
307 		pEntry->Add_Child("SHOW"        , m_bShow         ? "true" : "false");
308 		pEntry->Add_Child("PROJECT"     , m_bProject      ? "true" : "false");
309 		pEntry->Add_Child("PROJECT_AREA", m_bProject_Area ? "true" : "false");
310 		pEntry->Add_Child("FITCOLORS"   , m_bFitColors    ? "true" : "false");
311 
312 		return( true );
313 	}
314 
315 	return( false );
316 }
317 
318 
319 ///////////////////////////////////////////////////////////
320 //														 //
321 ///////////////////////////////////////////////////////////
322 
323 //---------------------------------------------------------
Get_Parameters(void)324 CSG_Parameters * CWKSP_Map_Layer::Get_Parameters(void)
325 {
326 	return( m_pLayer->Get_Parameters() );
327 }
328 
329 //---------------------------------------------------------
Parameters_Changed(void)330 void CWKSP_Map_Layer::Parameters_Changed(void)
331 {
332 	m_pLayer->Parameters_Changed();
333 
334 	CWKSP_Base_Item::Parameters_Changed();
335 }
336 
337 
338 ///////////////////////////////////////////////////////////
339 //														 //
340 ///////////////////////////////////////////////////////////
341 
342 //---------------------------------------------------------
Fit_Colors(const CSG_Rect & rWorld)343 bool CWKSP_Map_Layer::Fit_Colors(const CSG_Rect &rWorld)
344 {
345 	if( m_bFitColors )
346 	{
347 		CSG_Rect	_rWorld(rWorld);	CSG_Projection	prj_Layer, prj_Map;
348 
349 		if( m_bProject && _Projected_Get_Projections(prj_Layer, prj_Map) )
350 		{
351 			SG_Get_Projected(prj_Map, prj_Layer, _rWorld.m_rect);
352 		}
353 
354 		switch( m_pLayer->Get_Type() )
355 		{
356 		default:
357 			return( false );
358 
359 		case WKSP_ITEM_Grids:
360 			((CWKSP_Grids *)m_pLayer)->Fit_Colors(_rWorld);
361 			break;
362 
363 		case WKSP_ITEM_Grid :
364 			((CWKSP_Grid  *)m_pLayer)->Fit_Colors(_rWorld);
365 
366 			if( m_pLayer->Get_Parameter("COLORS_TYPE")->asInt() == CLASSIFY_OVERLAY )
367 			{
368 				#define	FIT_OVERLAY_GRID_COLORS(band, extent)	{\
369 					CWKSP_Grid	*pGrid	= (CWKSP_Grid *)g_pData->Get(m_pLayer->Get_Parameter(band)->asGrid());\
370 					if( pGrid && m_pLayer->Get_Parameter(band)->is_Enabled() )\
371 					{	pGrid->Fit_Colors(extent);	}\
372 				}
373 
374 				FIT_OVERLAY_GRID_COLORS("OVERLAY_R", rWorld);
375 				FIT_OVERLAY_GRID_COLORS("OVERLAY_G", rWorld);
376 				FIT_OVERLAY_GRID_COLORS("OVERLAY_B", rWorld);
377 			}
378 			break;
379 		}
380 
381 		return( true );
382 	}
383 
384 	return( false );
385 }
386 
387 
388 ///////////////////////////////////////////////////////////
389 //														 //
390 ///////////////////////////////////////////////////////////
391 
392 //---------------------------------------------------------
is_Projecting(void)393 bool CWKSP_Map_Layer::is_Projecting(void)
394 {
395 	CSG_Projection	prj_Layer, prj_Map;
396 
397 	return( m_bProject && _Projected_Get_Projections(prj_Layer, prj_Map) );
398 }
399 
400 //---------------------------------------------------------
_Projected_Get_Projections(CSG_Projection & prj_Layer,CSG_Projection & prj_Map)401 bool CWKSP_Map_Layer::_Projected_Get_Projections(CSG_Projection &prj_Layer, CSG_Projection &prj_Map)
402 {
403 	prj_Layer	= m_pLayer    ->Get_Object () ->Get_Projection();
404 	prj_Map		= ((CWKSP_Map *)Get_Manager())->Get_Projection();
405 
406 	return( prj_Layer.is_Okay() && prj_Map.is_Okay() && prj_Layer.is_Equal(prj_Map) == false );
407 }
408 
409 //---------------------------------------------------------
_Set_Extent_Points(const CSG_Rect & Extent,CSG_Shapes & Points,int Resolution)410 bool CWKSP_Map_Layer::_Set_Extent_Points(const CSG_Rect &Extent, CSG_Shapes &Points, int Resolution)
411 {
412 	double	d	= (Extent.Get_XRange() + Extent.Get_YRange()) / (double)Resolution;
413 
414 	for(double y=Extent.Get_YMin(); y<Extent.Get_YMax(); y+=d)
415 	{
416 		for(double x=Extent.Get_XMin(); x<Extent.Get_XMax(); x+=d)
417 		{
418 			Points.Add_Shape()->Add_Point(x, y);
419 		}
420 
421 		Points.Add_Shape()->Add_Point(Extent.Get_XMax(), y);
422 	}
423 
424 	for(double x=Extent.Get_XMin(); x<Extent.Get_XMax(); x+=d)
425 	{
426 		Points.Add_Shape()->Add_Point(x, Extent.Get_YMax());
427 	}
428 
429 	Points.Add_Shape()->Add_Point(Extent.Get_XMax(), Extent.Get_YMax());
430 
431 	return( true );
432 }
433 
434 //---------------------------------------------------------
Get_Extent(void)435 CSG_Rect CWKSP_Map_Layer::Get_Extent(void)
436 {
437 	CSG_Projection	prj_Layer, prj_Map;
438 
439 	if( !m_bProject || !_Projected_Get_Projections(prj_Layer, prj_Map) )
440 	{
441 		return( m_pLayer->Get_Extent() );
442 	}
443 
444 	//-----------------------------------------------------
445 	CSG_Rect	rLayer	= m_pLayer->Get_Extent();
446 
447 	if( rLayer.Get_XRange() == 0. || rLayer.Get_YRange() == 0. )
448 	{
449 		if( m_pLayer->Get_Object()->asShapes()
450 		&&  m_pLayer->Get_Object()->asShapes()->Get_Count() == 1
451 		&&  m_pLayer->Get_Object()->asShapes()->Get_Type() == SHAPE_TYPE_Point )
452 		{
453 			rLayer.Inflate(1., false);
454 		}
455 
456 		if( m_pLayer->Get_Object()->asPointCloud()
457 		&&  m_pLayer->Get_Object()->asPointCloud()->Get_Count() == 1 )
458 		{
459 			rLayer.Inflate(1., false);
460 		}
461 	}
462 
463 	//-----------------------------------------------------
464 	CSG_Shapes	Extent(SHAPE_TYPE_Point);
465 
466 	Extent.Get_Projection().Create(prj_Layer);
467 
468 	_Set_Extent_Points(rLayer, Extent);
469 
470 	SG_Get_Projected(&Extent, NULL, prj_Map);
471 
472 	return( Extent.Get_Extent() );
473 
474 	//-----------------------------------------------------
475 	//CSG_Rect	rLayer(m_pLayer->Get_Extent());
476 
477 	//SG_Get_Projected(prj_Layer, prj_Map, rLayer.m_rect);
478 
479 	//return( rLayer );
480 }
481 
482 //---------------------------------------------------------
_Projected_Get_Layer_Extent(const CSG_Rect & rMap)483 CSG_Rect CWKSP_Map_Layer::_Projected_Get_Layer_Extent(const CSG_Rect &rMap)
484 {
485 	CSG_Projection	prj_Layer, prj_Map;
486 
487 	if( !m_bProject || !_Projected_Get_Projections(prj_Layer, prj_Map) )
488 	{
489 		return( m_pLayer->Get_Extent() );
490 	}
491 
492 	//-----------------------------------------------------
493 	CSG_Shapes	Extent(SHAPE_TYPE_Point);
494 
495 	Extent.Get_Projection().Create(prj_Map);
496 
497 	_Set_Extent_Points(rMap, Extent);
498 
499 	SG_Get_Projected(&Extent, NULL, prj_Layer);
500 
501 	TSG_Rect	rLayer	= Extent.Get_Extent();
502 
503 	if( std::isinf(rLayer.xMin) ) rLayer.xMin = m_pLayer->Get_Extent().Get_XMin();
504 	if( std::isinf(rLayer.xMax) ) rLayer.xMax = m_pLayer->Get_Extent().Get_XMax();
505 	if( std::isinf(rLayer.yMin) ) rLayer.yMin = m_pLayer->Get_Extent().Get_YMin();
506 	if( std::isinf(rLayer.yMax) ) rLayer.yMax = m_pLayer->Get_Extent().Get_YMax();
507 
508 	return( rLayer );
509 }
510 
511 //---------------------------------------------------------
_Projected_Shapes_Clipped(const CSG_Rect & rMap,CSG_Shapes * pShapes,CSG_Shapes & Clipped)512 bool CWKSP_Map_Layer::_Projected_Shapes_Clipped(const CSG_Rect &rMap, CSG_Shapes *pShapes, CSG_Shapes &Clipped)
513 {
514 	CSG_Projection	prj_Layer, prj_Map;
515 
516 	if( !m_bProject || !_Projected_Get_Projections(prj_Layer, prj_Map) )
517 	{
518 		return( false );
519 	}
520 
521 	//-----------------------------------------------------
522 	CSG_Shapes	Shapes, Extent(SHAPE_TYPE_Point);	Extent.Get_Projection().Create(prj_Map);
523 
524 	_Set_Extent_Points(rMap, Extent);
525 
526 	if( !SG_Get_Projected(&Extent, NULL, prj_Layer) || Extent.Get_Extent().Get_Area() == 0 )
527 	{
528 		return( false );
529 	}
530 
531 	if( std::isfinite(Extent.Get_Extent().Get_XMin()) && std::isfinite(Extent.Get_Extent().Get_XMax())
532 	&&  std::isfinite(Extent.Get_Extent().Get_YMin()) && std::isfinite(Extent.Get_Extent().Get_YMax()) )
533 	{
534 		CSG_Shapes	Clip(SHAPE_TYPE_Polygon);	CSG_Shape	*pClip	= Clip.Add_Shape();
535 
536 		for(int i=0; i<Extent.Get_Count(); i++)
537 		{
538 			TSG_Point	p	= Extent.Get_Shape(i)->Get_Point(0);
539 
540 			if( std::isinf(p.x) ) p.x = std::signbit(p.x) ? Extent.Get_Extent().Get_XMin() : Extent.Get_Extent().Get_XMax();
541 			if( std::isinf(p.y) ) p.y = std::signbit(p.y) ? Extent.Get_Extent().Get_YMin() : Extent.Get_Extent().Get_YMax();
542 
543 			if( std::isfinite(p.x) && std::isfinite(p.y) )
544 			{
545 				pClip->Add_Point(p);
546 			}
547 		}
548 
549 		//-------------------------------------------------
550 		if( ((CSG_Shape_Polygon *)pClip)->Get_Area() > 0.0 )
551 		{
552 			Shapes.Create(pShapes->Get_Type(), SG_T(""), pShapes);	Shapes.Get_Projection().Create(prj_Layer);
553 
554 			for(int i=0; i<pShapes->Get_Count(); i++)
555 			{
556 				if( pClip->Intersects(pShapes->Get_Shape(i)) )
557 				{
558 					Shapes.Add_Shape(pShapes->Get_Shape(i));
559 				}
560 			}
561 
562 			pShapes	= &Shapes;	//	SG_UI_DataObject_Add(SG_Create_Shapes(Clip), 0);
563 		}
564 	}
565 
566 	//-----------------------------------------------------
567 	SG_Get_Projected(pShapes, pShapes == &Shapes ? NULL : &Shapes, prj_Map);
568 
569 	//-----------------------------------------------------
570 	CSG_Shapes	Clip(SHAPE_TYPE_Polygon);	Clip.Get_Projection().Create(prj_Map);
571 	CSG_Shape	*pClip	= Clip.Add_Shape();
572 	pClip->Add_Point(rMap.Get_XMin(), rMap.Get_YMin());
573 	pClip->Add_Point(rMap.Get_XMin(), rMap.Get_YMax());
574 	pClip->Add_Point(rMap.Get_XMax(), rMap.Get_YMax());
575 	pClip->Add_Point(rMap.Get_XMax(), rMap.Get_YMin());
576 
577 	SG_UI_ProgressAndMsg_Lock(true);
578 
579 	CSG_Tool	*pTool	= SG_Get_Tool_Library_Manager().Create_Tool("shapes_polygons", 11);	// Polygon Clipping
580 
581 	bool	bResult	= pTool && pTool->Set_Manager(NULL)
582 	&&  pTool->Set_Parameter("CLIP"    , &Clip)
583 	&&  pTool->Set_Parameter("S_INPUT" , &Shapes)
584 	&&  pTool->Set_Parameter("S_OUTPUT", &Clipped)
585 	&&  pTool->Set_Parameter("DISSOLVE", false)
586 	&&  pTool->Set_Parameter("MULTIPLE", false)
587 	&&  pTool->Execute();
588 
589 	SG_Get_Tool_Library_Manager().Delete_Tool(pTool);
590 
591 	SG_UI_ProgressAndMsg_Lock(false);
592 
593 	return( bResult );
594 }
595 
596 //---------------------------------------------------------
_Projected_Shapes_Draw(CWKSP_Map_DC & dc_Map,int Flags,CSG_Shapes * pShapes,bool bSelection)597 bool CWKSP_Map_Layer::_Projected_Shapes_Draw(CWKSP_Map_DC &dc_Map, int Flags, CSG_Shapes *pShapes, bool bSelection)
598 {
599 	CSG_Shapes	Shapes;
600 
601 	if( pShapes->Get_Type() != SHAPE_TYPE_Polygon )
602 	{
603 		SG_Get_Projected(pShapes, &Shapes, ((CWKSP_Map *)Get_Manager())->Get_Projection());
604 	}
605 	else
606 	{
607 		CSG_Rect	rMap(dc_Map.m_rWorld);	rMap.Inflate(dc_Map.m_DC2World, false);	// inflate by one pixel
608 
609 		_Projected_Shapes_Clipped(rMap, pShapes, Shapes);
610 	}
611 
612 	if( bSelection )
613 	{
614 		Shapes.Inv_Selection();
615 	}
616 
617 	return( m_pLayer->Draw(dc_Map, Flags, &Shapes) );
618 }
619 
620 
621 ///////////////////////////////////////////////////////////
622 //														 //
623 ///////////////////////////////////////////////////////////
624 
625 //---------------------------------------------------------
Draw(CWKSP_Map_DC & dc_Map,int Flags)626 bool CWKSP_Map_Layer::Draw(CWKSP_Map_DC &dc_Map, int Flags)
627 {
628 	CSG_Projection	prj_Layer, prj_Map;
629 
630 	if( !m_bProject || !_Projected_Get_Projections(prj_Layer, prj_Map) )
631 	{
632 		if( m_pLayer->do_Show(dc_Map.m_rWorld) )
633 		{
634 			m_pLayer->Draw(dc_Map, Flags);
635 		}
636 
637 		return( true );
638 	}
639 
640 	//-----------------------------------------------------
641 	CSG_Rect	rLayer(_Projected_Get_Layer_Extent(dc_Map.m_rWorld));
642 
643 	if( !m_pLayer->do_Show(rLayer, false) )
644 	{
645 		return( true );
646 	}
647 
648 	switch( m_pLayer->Get_Object()->Get_ObjectType() )
649 	{
650 	//-----------------------------------------------------
651 	case SG_DATAOBJECT_TYPE_Shapes: {
652 		CSG_Shapes	*pShapes	= m_pLayer->Get_Object()->asShapes();
653 
654 		_Projected_Shapes_Draw(dc_Map, Flags, pShapes);
655 
656 		if( pShapes->Get_Selection_Count() > 0 )
657 		{
658 			CSG_Shapes	Selection(pShapes->Get_Type(), pShapes->Get_Name(), pShapes);
659 
660 			for(int i=0; i<pShapes->Get_Selection_Count(); i++)
661 			{
662 				Selection.Add_Shape(pShapes->Get_Selection(i));
663 			}
664 
665 			_Projected_Shapes_Draw(dc_Map, Flags, &Selection, true);
666 		}
667 
668 	}	break;
669 
670 	//-----------------------------------------------------
671 	case SG_DATAOBJECT_TYPE_PointCloud: {
672 		CSG_PointCloud	Points, *pPoints	= m_pLayer->Get_Object()->asPointCloud();
673 
674 		Points.Create(pPoints);
675 		Points.Get_Projection().Create(pPoints->Get_Projection());
676 
677 		for(int i=0; i<pPoints->Get_Count(); i++)
678 		{
679 			if( rLayer.Contains(pPoints->Get_X(i), pPoints->Get_Y(i)) )
680 			{
681 				Points.Add_Shape(pPoints->Get_Record(i));
682 			}
683 		}
684 
685 		SG_Get_Projected(&Points, NULL, prj_Map);
686 
687 		m_pLayer->Draw(dc_Map, Flags, &Points);
688 
689 	}	break;
690 
691 	//-----------------------------------------------------
692 	case SG_DATAOBJECT_TYPE_Grid: {
693 		CSG_Grid	Grid, *pGrid = m_pLayer->Get_Object()->asGrid();
694 
695 		if( !pGrid->Get_Extent().Intersects(rLayer) )	// == INTERSECTION_None
696 		{
697 			return( true );
698 		}
699 
700 		CSG_Grid_System	System(dc_Map.m_DC2World, dc_Map.m_rWorld.Get_XMin(), dc_Map.m_rWorld.Get_YMin(), dc_Map.dc.GetSize().GetWidth(), dc_Map.dc.GetSize().GetHeight());
701 
702 		if( !System.is_Valid() || !Grid.Create(System, pGrid->Get_Type()) )
703 		{
704 			return( false );
705 		}
706 
707 		//-------------------------------------------------
708 		SG_UI_ProgressAndMsg_Lock(true);
709 
710 		CSG_Tool	*pTool	= SG_Get_Tool_Library_Manager().Create_Tool("pj_proj4", 4);	// Coordinate Transformation (Grid)
711 
712 		if( pTool && pTool->Set_Manager(NULL)
713 			&&  pTool->Set_Parameter("CRS_PROJ4"        , prj_Map.Get_Proj4())
714 			&&  pTool->Set_Parameter("RESAMPLING"       , m_pLayer->Get_Parameter("DISPLAY_RESAMPLING")->asInt() ? 3 : 0)
715 			&&  pTool->Set_Parameter("KEEP_TYPE"        , true)
716 			&&  pTool->Set_Parameter("TARGET_AREA"      , m_bProject_Area)
717 			&&  pTool->Set_Parameter("TARGET_DEFINITION", 1)
718 			&&  pTool->Set_Parameter("SOURCE"           , pGrid)
719 			&&  pTool->Set_Parameter("GRID"             , &Grid)
720 			&&  pTool->Execute() )
721 		{
722 			m_pLayer->Draw(dc_Map, Flags, &Grid);
723 		}
724 
725 		SG_Get_Tool_Library_Manager().Delete_Tool(pTool);
726 
727 		SG_UI_ProgressAndMsg_Lock(false);
728 
729 	}	break;
730 
731 	//-----------------------------------------------------
732 	case SG_DATAOBJECT_TYPE_Grids: {
733 		CSG_Grids	*pGrids = m_pLayer->Get_Object()->asGrids();
734 
735 		if( !pGrids->Get_Extent().Intersects(rLayer) )	// == INTERSECTION_None
736 		{
737 			return( true );
738 		}
739 
740 		CSG_Grid_System	System(dc_Map.m_DC2World, dc_Map.m_rWorld.Get_XMin(), dc_Map.m_rWorld.Get_YMin(), dc_Map.dc.GetSize().GetWidth(), dc_Map.dc.GetSize().GetHeight());
741 
742 		if( !System.is_Valid() )
743 		{
744 			return( false );
745 		}
746 
747 		//-------------------------------------------------
748 		SG_UI_ProgressAndMsg_Lock(true);
749 
750 		CSG_Tool	*pTool	= SG_Get_Tool_Library_Manager().Create_Tool("pj_proj4", 3);	// Coordinate Transformation (Grid List)
751 
752 		if( pTool && pTool->Set_Manager(NULL)
753 			&&  pTool->Set_Parameter("CRS_PROJ4"        , prj_Map.Get_Proj4())
754 			&&  pTool->Set_Parameter("RESAMPLING"       , m_pLayer->Get_Parameter("DISPLAY_RESAMPLING")->asInt() ? 3 : 0)
755 			&&  pTool->Set_Parameter("KEEP_TYPE"        , true)
756 			&&  pTool->Set_Parameter("TARGET_AREA"      , true)
757 			&&  pTool->Set_Parameter("TARGET_DEFINITION", 0)
758 			&&  pTool->Set_Parameter("TARGET_USER_SIZE" , System.Get_Cellsize())
759 			&&  pTool->Set_Parameter("TARGET_USER_XMIN" , System.Get_XMin())
760 			&&  pTool->Set_Parameter("TARGET_USER_YMIN" , System.Get_YMin())
761 			&&  pTool->Set_Parameter("TARGET_USER_XMAX" , System.Get_XMax())
762 			&&  pTool->Set_Parameter("TARGET_USER_YMAX" , System.Get_YMax())
763 			&&  pTool->Get_Parameter("SOURCE")->asList()->Add_Item(pGrids)
764 			&&  pTool->Execute() && (pGrids = (CSG_Grids *)pTool->Get_Parameter("GRIDS")->asList()->Get_Item(0)) != NULL )
765 		{
766 			m_pLayer->Draw(dc_Map, Flags, pGrids);
767 
768 			delete(pGrids);
769 		}
770 
771 		SG_Get_Tool_Library_Manager().Delete_Tool(pTool);
772 
773 		SG_UI_ProgressAndMsg_Lock(false);
774 
775 	}	break;
776 
777 	//-----------------------------------------------------
778 	default:
779 		return( false );
780 	}
781 
782 	//-----------------------------------------------------
783 	return( true );
784 }
785 
786 
787 ///////////////////////////////////////////////////////////
788 //														 //
789 //														 //
790 //														 //
791 ///////////////////////////////////////////////////////////
792 
793 //---------------------------------------------------------
794