📄 shapes2grid.cpp
字号:
///////////////////////////////////////////////////////////
// //
// SAGA //
// //
// System for Automated Geoscientific Analyses //
// //
// Module Library: //
// Grid_Gridding //
// //
//-------------------------------------------------------//
// //
// Shapes2Grid.cpp //
// //
// Copyright (C) 2003 by //
// Olaf Conrad //
// //
//-------------------------------------------------------//
// //
// This file is part of 'SAGA - System for Automated //
// Geoscientific Analyses'. SAGA is free software; you //
// can redistribute it and/or modify it under the terms //
// of the GNU General Public License as published by the //
// Free Software Foundation; version 2 of the License. //
// //
// SAGA is distributed in the hope that it will be //
// useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A //
// PARTICULAR PURPOSE. See the GNU General Public //
// License for more details. //
// //
// You should have received a copy of the GNU General //
// Public License along with this program; if not, //
// write to the Free Software Foundation, Inc., //
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, //
// USA. //
// //
//-------------------------------------------------------//
// //
// e-mail: oconrad@saga-gis.org //
// //
// contact: Olaf Conrad //
// Institute of Geography //
// University of Goettingen //
// Goldschmidtstr. 5 //
// 37077 Goettingen //
// Germany //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#include <string.h>
#include "Shapes2Grid.h"
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#define X_WORLD_TO_GRID(X) (((X) - m_pGrid->Get_XMin()) / m_pGrid->Get_Cellsize())
#define Y_WORLD_TO_GRID(Y) (((Y) - m_pGrid->Get_YMin()) / m_pGrid->Get_Cellsize())
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
CShapes2Grid::CShapes2Grid(void)
{
CSG_Parameter *pNode_0, *pNode_1;
CSG_Parameters *pParameters;
//-----------------------------------------------------
Set_Name (_TL("Shapes to Grid"));
Set_Author (_TL("Copyrights (c) 2003 by Olaf Conrad"));
Set_Description (_TW(
"Gridding of a shapes layer."
));
//-----------------------------------------------------
Parameters.Add_Grid_Output(
NULL , "GRID" , _TL("Grid"),
_TL("")
);
pNode_0 = Parameters.Add_Shapes(
NULL , "INPUT" , _TL("Shapes"),
_TL(""),
PARAMETER_INPUT
);
pNode_1 = Parameters.Add_Table_Field(
pNode_0 , "FIELD" , _TL("Attribute"),
_TL("")
);
pNode_0 = Parameters.Add_Choice(
NULL , "TARGET_TYPE" , _TL("Target Dimensions"),
_TL(""),
CSG_String::Format(SG_T("%s|%s|%s|"),
_TL("User defined"),
_TL("Grid Project"),
_TL("Grid")
), 0
);
pNode_0 = Parameters.Add_Choice(
NULL , "LINE_TYPE" , _TL("Method for Lines"),
_TL(""),
CSG_String::Format(SG_T("%s|%s|"),
_TL("thin"),
_TL("thick")
), 1
);
//-----------------------------------------------------
pParameters = Add_Parameters("USER", _TL("User defined grid"), _TL(""));
pNode_0 = pParameters->Add_Value(
NULL , "CELL_SIZE" , _TL("Grid Size"),
_TL(""),
PARAMETER_TYPE_Double, 100.0, 0.0, true
);
pNode_0 = pParameters->Add_Value(
NULL , "FIT_EXTENT" , _TL("Fit Extent"),
_TL("Automatically fits the grid to the shapes layers extent."),
PARAMETER_TYPE_Bool , true
);
pNode_1 = pParameters->Add_Range(
pNode_0 , "X_EXTENT" , _TL("X-Extent"),
_TL("")
);
pNode_1 = pParameters->Add_Range(
pNode_0 , "Y_EXTENT" , _TL("Y-Extent"),
_TL("")
);
pNode_0 = pParameters->Add_Choice(
NULL , "GRID_TYPE" , _TL("Target Grid Type"),
_TL(""),
CSG_String::Format(SG_T("%s|%s|%s|%s|%s|"),
_TL("Integer (1 byte)"),
_TL("Integer (2 byte)"),
_TL("Integer (4 byte)"),
_TL("Floating Point (4 byte)"),
_TL("Floating Point (8 byte)")
), 3
);
//-----------------------------------------------------
pParameters = Add_Parameters("GET_SYSTEM" , _TL("Choose Grid Project"), _TL(""));
pNode_0 = pParameters->Add_Grid_System(
NULL, "SYSTEM" , _TL("System") , _TL("")
);
pNode_0 = pParameters->Add_Choice(
NULL , "GRID_TYPE" , _TL("Target Grid Type"),
_TL(""),
CSG_String::Format(SG_T("%s|%s|%s|%s|%s|"),
_TL("Integer (1 byte)"),
_TL("Integer (2 byte)"),
_TL("Integer (4 byte)"),
_TL("Floating Point (4 byte)"),
_TL("Floating Point (8 byte)")
), 3
);
//-----------------------------------------------------
pParameters = Add_Parameters("GRID" , _TL("Choose Grid") , _TL(""));
pNode_0 = pParameters->Add_Grid(
NULL , "GRID" , _TL("Grid"),
_TL(""),
PARAMETER_INPUT , false
);
}
//---------------------------------------------------------
CShapes2Grid::~CShapes2Grid(void)
{}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
TSG_Grid_Type CShapes2Grid::Get_Grid_Type(int iType)
{
switch( iType )
{
case 0:
return( GRID_TYPE_Byte );
case 1:
return( GRID_TYPE_Short );
case 2:
return( GRID_TYPE_Int );
case 3: default:
return( GRID_TYPE_Float );
case 4:
return( GRID_TYPE_Double );
}
}
//---------------------------------------------------------
CSG_Grid * CShapes2Grid::Get_Target_Grid(CSG_Parameters *pParameters, CSG_Shapes *m_pShapes)
{
int nx, ny;
double Cell_Size, xMin, yMin, xMax, yMax;
if( pParameters->Get_Parameter("FIT_EXTENT")->asBool() )
{
xMin = m_pShapes->Get_Extent().m_rect.xMin;
yMin = m_pShapes->Get_Extent().m_rect.yMin;
xMax = m_pShapes->Get_Extent().m_rect.xMax;
yMax = m_pShapes->Get_Extent().m_rect.yMax;
}
else
{
xMin = pParameters->Get_Parameter("X_EXTENT")->asRange()->Get_LoVal();
yMin = pParameters->Get_Parameter("Y_EXTENT")->asRange()->Get_LoVal();
xMax = pParameters->Get_Parameter("X_EXTENT")->asRange()->Get_HiVal();
yMax = pParameters->Get_Parameter("Y_EXTENT")->asRange()->Get_HiVal();
}
Cell_Size = pParameters->Get_Parameter("CELL_SIZE")->asDouble();
nx = 1 + (int)((xMax - xMin) / Cell_Size);
ny = 1 + (int)((yMax - yMin) / Cell_Size);
return( SG_Create_Grid(Get_Grid_Type(pParameters->Get_Parameter("GRID_TYPE")->asInt()), nx, ny, Cell_Size, xMin, yMin) );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CShapes2Grid::On_Execute(void)
{
m_pGrid = NULL;
m_pShapes = Parameters("INPUT")->asShapes();
//-----------------------------------------------------
if( m_pShapes->Get_Table().Get_Field_Count() > 0 )
{
switch( Parameters("TARGET_TYPE")->asInt() )
{
case 0: // User defined...
if( Dlg_Parameters("USER") )
{
m_pGrid = Get_Target_Grid(Get_Parameters("USER"), m_pShapes);
}
break;
case 1: // Grid Project...
if( Dlg_Parameters("GET_SYSTEM") )
{
m_pGrid = SG_Create_Grid(
*Get_Parameters("GET_SYSTEM")->Get_Parameter("SYSTEM")->asGrid_System(),
Get_Grid_Type(Get_Parameters("GET_SYSTEM")->Get_Parameter("GRID_TYPE")->asInt())
);
}
break;
case 2: // Grid...
if( Dlg_Parameters("GRID") )
{
m_pGrid = Get_Parameters("GRID")->Get_Parameter("GRID")->asGrid();
}
break;
}
//-------------------------------------------------
if( m_pGrid )
{
int i, iField;
CSG_Shape *pShape;
//---------------------------------------------
m_Method_Lines = Parameters("LINE_TYPE") ->asInt();
iField = Parameters("FIELD") ->asInt();
if( iField >= 0 && iField < m_pShapes->Get_Table().Get_Field_Count()
&& m_pShapes->Get_Table().Get_Field_Type(iField) != TABLE_FIELDTYPE_String )
{
m_pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pShapes->Get_Name(), m_pShapes->Get_Table().Get_Field_Name(iField)));
}
else
{
m_pGrid->Set_Name(m_pShapes->Get_Name());
iField = -1;
}
//---------------------------------------------
m_pGrid->Assign_NoData();
Parameters("GRID")->Set_Value(m_pGrid);
m_pLock = m_pShapes->Get_Type() == SHAPE_TYPE_Point ? NULL
: SG_Create_Grid(m_pGrid, GRID_TYPE_Byte);
//---------------------------------------------
for(i=0, m_Lock_ID=1; i<m_pShapes->Get_Count() && Set_Progress(i, m_pShapes->Get_Count()); i++, m_Lock_ID++)
{
pShape = m_pShapes->Get_Shape(i);
m_Value = iField < 0 ? i + 1 : pShape->Get_Record()->asDouble(iField);
if( pShape->Intersects(m_pGrid->Get_Extent().m_rect) )
{
switch( m_pShapes->Get_Type() )
{
case SHAPE_TYPE_Point:
case SHAPE_TYPE_Points: Set_Points (pShape); break;
case SHAPE_TYPE_Line: Set_Line (pShape); break;
case SHAPE_TYPE_Polygon: Set_Polygon (pShape); break;
}
}
}
//---------------------------------------------
if( m_pLock )
{
delete(m_pLock);
}
}
return( true );
}
return( false );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
inline void CShapes2Grid::Set_Value(int x, int y)
{
if( m_pGrid->is_InGrid(x, y, false) )
{
if( m_pLock )
{
if( m_Lock_ID >= 255 )
{
m_Lock_ID = 1;
m_pLock->Assign(0.0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -