예제 #1
0
//---------------------------------------------------------
bool DLG_Get_FILE_Filter_GDAL_Read(int Type, wxString &Filter)
{
	bool		bResult;
	CSG_Table	Formats;

	SG_UI_ProgressAndMsg_Lock(true);

	SG_RUN_TOOL(bResult, "io_gdal", 10,	// GDAL Formats
		SG_TOOL_PARAMETER_SET("FORMATS"   , &Formats)
	&&	SG_TOOL_PARAMETER_SET("TYPE"      , Type    )	// all (rasters and vectors)
	&&	SG_TOOL_PARAMETER_SET("ACCESS"    , 0       )	// read
	&&	SG_TOOL_PARAMETER_SET("RECOGNIZED", true    )	// add an entry for all recognized files
	);

	SG_UI_ProgressAndMsg_Lock(false);

	if( bResult && Formats.Get_Count() > 0 )
	{
		Filter	+= Formats[Formats.Get_Count() - 1].asString(2);

		Filter.Replace("*.sdat;", "");	// we go for *.sgrd
		Filter.Replace("*.xml;" , "");	// too much noise
	}

	return( bResult );
}
//---------------------------------------------------------
bool CTable_Running_Average::On_Execute(void)
{
	int			iValue, nValues;
	CSG_Table	*pTable;

	//-----------------------------------------------------
	pTable	= Parameters("INPUT")	->asTable();
	iValue	= Parameters("FIELD")	->asInt();
	nValues	= Parameters("COUNT")	->asInt();

	if( Parameters("OUTPUT")->asTable() && Parameters("OUTPUT")->asTable() != pTable )
	{
		pTable	= Parameters("OUTPUT")	->asTable();

		pTable->Create(*Parameters("INPUT")->asTable());
	}

	//-----------------------------------------------------
	if( pTable->is_Valid() )
	{
		int		i, iLo, iHi, nRange, iAverage;
		double	sValues;

		iAverage	= pTable->Get_Field_Count();
		pTable->Add_Field(CSG_String::Format(SG_T("%s [%s]"), pTable->Get_Field_Name(iValue), _TL("Average")), SG_DATATYPE_Double);

		nRange	= nValues / 2;
		sValues	= 0.0;

		for(iLo=-nValues, i=-nRange, iHi=0; i<pTable->Get_Count() && Set_Progress(i, pTable->Get_Count() + nRange); iLo++, i++, iHi++)
		{
			sValues	+= pTable->Get_Record(iHi < pTable->Get_Count() ? iHi : pTable->Get_Count() - 1)->asDouble(iValue);

			if( i < 0 )
			{
				sValues	+= pTable->Get_Record( 0 )->asDouble(iValue);
			}
			else
			{
				if( iLo < 0 )
				{
					sValues	-= pTable->Get_Record( 0 )->asDouble(iValue);
				}
				else if( iLo >= 0 )
				{
					sValues	-= pTable->Get_Record(iLo)->asDouble(iValue);
				}

				pTable->Get_Record(i)->Set_Value(iAverage, sValues / (double)nValues);
			}
		}

		return( true );
	}

	//-----------------------------------------------------
	return( false );
}
예제 #3
0
//---------------------------------------------------------
bool CFilter_Rank::Get_Value(int x, int y, double Rank, double &Value)
{
	if( m_pInput->is_InGrid(x, y) )
	{
		CSG_Table	Values;

		Values.Add_Field("Z", SG_DATATYPE_Double);

		for(int i=0; i<m_Kernel.Get_Count(); i++)
		{
			int	ix	= m_Kernel.Get_X(i, x);
			int	iy	= m_Kernel.Get_Y(i, y);

			if( m_pInput->is_InGrid(ix, iy) )
			{
				Values.Add_Record()->Set_Value(0, m_pInput->asDouble(ix, iy));
			}
		}

		switch( Values.Get_Count() )
		{
		case 0:
			return( false );

		case 1:
			Value	= Values[0].asDouble(0);
			return( true );

		case 2:
			Value	= (Values[0].asDouble(0) + Values[1].asDouble(0)) / 2.0;
			return( true );

		default:
			{
				Values.Set_Index(0, TABLE_INDEX_Ascending);

				Rank	= Rank * (Values.Get_Count() - 1.0);

				int	i	= (int)Rank;

				Value	= Values.Get_Record_byIndex(i)->asDouble(0);

				if( Rank - i > 0.0 && i < Values.Get_Count() - 1 )
				{
					Value	= (Value + Values.Get_Record_byIndex(i + 1)->asDouble(0)) / 2.0;
				}
			}

			return( true );
		}
	}

	return( false );
}
//---------------------------------------------------------
void CData_Source_PgSQL::Update_Sources(const wxTreeItemId &Root)
{
	Freeze();

	//-----------------------------------------------------
	wxTreeItemIdValue Cookie; wxTreeItemId Item = GetFirstChild(Root, Cookie);

	while( Item.IsOk() )
	{
		Update_Source(Item);

		Item	= GetNextChild(Item, Cookie);
	}

	//-----------------------------------------------------
	CSG_Table	Connections;

	RUN_MODULE(DB_PGSQL_Get_Connections, false, SET_PARAMETER("CONNECTIONS", &Connections));	// CGet_Connections

	for(int i=0; i<Connections.Get_Count(); i++)
	{
		if( !Find_Source(Connections[i].asString(0)) )
		{
			Update_Source(Connections[i].asString(0));
		}
	}

	//-----------------------------------------------------
	SortChildren(Root);
	Expand      (Root);

	Thaw();
}
예제 #5
0
//---------------------------------------------------------
bool CShapes_SRID_Update::On_Execute(void)
{
	if( !Get_Connection()->has_PostGIS() )	{	Error_Set(_TL("no PostGIS layer"));	return( false );	}

	//-----------------------------------------------------
	CSG_String	Select;
	CSG_Table	Table;

	Select.Printf(SG_T("f_table_name='%s'"), Parameters("TABLES")->asString());

	if( !Get_Connection()->Table_Load(Table, "geometry_columns", "*", Select) || Table.Get_Count() != 1 )
	{
		return( false );
	}

	Select.Printf(SG_T("SELECT UpdateGeometrySRID('%s', '%s', %d)"),
		Parameters("TABLES")->asString(),
		Table[0].asString("f_geometry_column"),
		Get_SRID()
	);

	//-----------------------------------------------------
	if( !Get_Connection()->Execute(Select) )
	{
		return( false );
	}

	return( true );
}
//---------------------------------------------------------
void CData_Source_PgSQL::Append_Table(const wxTreeItemId &Parent, const SG_Char *Name, int Type, int Image)
{
	CData_Source_PgSQL_Data	*pData	= Parent.IsOk() ? (CData_Source_PgSQL_Data *)GetItemData(Parent) : NULL; if( pData == NULL )	return;

	wxTreeItemId	Item	= AppendItem(Parent, Name, Image, Image, new CData_Source_PgSQL_Data(Type, Name, pData->Get_Server()));

	if( Type == TYPE_GRIDS )
	{
		CSG_Table	Grids;

		RUN_MODULE(DB_PGSQL_Table_Query, false,	// CTable_Query
				SET_PARAMETER("CONNECTION", pData->Get_Server())
			&&	SET_PARAMETER("TABLES"    , Name)
			&&	SET_PARAMETER("TABLE"     , &Grids)
			&&  SET_PARAMETER("FIELDS"    , SG_T("rid, name"))
		);

		if( bResult )
		{
			for(int i=0; i<Grids.Get_Count(); i++)
			{
				AppendItem(Item, Grids[i].asString(1), IMG_GRID, IMG_GRID,
					new CData_Source_PgSQL_Data(TYPE_GRID, CSG_String::Format("%s:rid=%d", Name, Grids[i].asInt(0)), pData->Get_Server())
				);
			}
		}
	}
}
//---------------------------------------------------------
void CData_Source_PgSQL::Update_Source(const wxTreeItemId &Item)
{
	CData_Source_PgSQL_Data	*pData	= Item.IsOk() ? (CData_Source_PgSQL_Data *)GetItemData(Item) : NULL; if( pData == NULL )	return;

	if( pData->Get_Type() != TYPE_SOURCE )
	{
		return;
	}

	Freeze();

	DeleteChildren(Item);

	//-----------------------------------------------------
	if( !pData->is_Connected() )
	{
		SetItemImage(Item, IMG_SRC_CLOSED, wxTreeItemIcon_Normal);
		SetItemImage(Item, IMG_SRC_CLOSED, wxTreeItemIcon_Selected);
	}
	else
	{
		SetItemImage(Item, IMG_SRC_OPENED, wxTreeItemIcon_Normal);
		SetItemImage(Item, IMG_SRC_OPENED, wxTreeItemIcon_Selected);

		CSG_Table	Tables;

		RUN_MODULE(DB_PGSQL_Table_List, false,	// CTable_List
				SET_PARAMETER("CONNECTION", pData->Get_Value())
			&&	SET_PARAMETER("TABLES"    , &Tables)
		);

		Tables.Set_Index(1, TABLE_INDEX_Ascending, 0, TABLE_INDEX_Ascending);

		for(int i=0; i<Tables.Get_Count(); i++)
		{
			CSG_String	s(Tables[i].asString(1));

			TSG_Shape_Type	Shape;
			TSG_Vertex_Type	Vertex;

			if( CSG_Shapes_OGIS_Converter::to_ShapeType(s, Shape, Vertex) )
			{
				switch( Shape )
				{
				case SHAPE_TYPE_Point:   Append_Table(Item, Tables[i].asString(0), TYPE_SHAPES, IMG_POINT  ); break;
				case SHAPE_TYPE_Points:  Append_Table(Item, Tables[i].asString(0), TYPE_SHAPES, IMG_POINTS ); break;
				case SHAPE_TYPE_Line:    Append_Table(Item, Tables[i].asString(0), TYPE_SHAPES, IMG_LINE   ); break;
				case SHAPE_TYPE_Polygon: Append_Table(Item, Tables[i].asString(0), TYPE_SHAPES, IMG_POLYGON); break;
				}
			}
			else if( !s.Cmp("RASTER" ) ) Append_Table(Item, Tables[i].asString(0), TYPE_GRIDS , IMG_GRIDS  );
			else if( !s.Cmp("TABLE"  ) ) Append_Table(Item, Tables[i].asString(0), TYPE_TABLE , IMG_TABLE  );
		}

		Expand(Item);
	}

	Thaw();
}
예제 #8
0
//---------------------------------------------------------
bool CSG_mRMR::Set_Data(CSG_Table &Data, int ClassField, double Threshold)
{
    if( !Get_Memory(Data.Get_Field_Count(), Data.Get_Count()) )
    {
        return( false );
    }

    //-----------------------------------------------------
    if( ClassField < 0 || ClassField >= m_nVars )
    {
        ClassField	= 0;
    }

    Data.Set_Index(ClassField, TABLE_INDEX_Ascending);

    CSG_String	s;

    for(int iSample=0, Class=0; iSample<m_nSamples; iSample++)
    {
        double	*pData	= m_Samples[iSample] = m_Samples[0] + iSample * m_nVars;

        if( s.Cmp(Data[iSample].asString(ClassField)) )
        {
            s	= Data[iSample].asString(ClassField);

            Class++;
        }

        *pData++	= Class;

        for(int iVar=0; iVar<m_nVars; iVar++)
        {
            if( iVar != ClassField )
            {
                *pData++	= Data[iSample].asDouble(iVar);
            }
        }
    }

    Data.Del_Index();

    m_VarNames	+= Data.Get_Field_Name(ClassField);

    for(int iVar=0; iVar<m_nVars; iVar++)
    {
        if( iVar != ClassField )
        {
            m_VarNames	+= Data.Get_Field_Name(iVar);
        }
    }

    //-----------------------------------------------------
    if( Threshold >= 0.0 )	// discretization
    {
        Discretize(Threshold);
    }

    return( true );
}
//---------------------------------------------------------
bool CShapes_Split_by_Attribute::On_Execute(void)
{
	int			iField;
	CSG_Table	*pTable;

	//-----------------------------------------------------
	pTable	= Parameters("TABLE")	->asTable();
	iField	= Parameters("FIELD")	->asInt();

	Parameters("CUTS")->asTableList()->Del_Items();

	//-----------------------------------------------------
	if( pTable->is_Valid() && pTable->Set_Index(iField, TABLE_INDEX_Ascending) )
	{
		CSG_String	sValue;
		CSG_Table	*pCut	= NULL;

		for(int iRecord=0; iRecord<pTable->Get_Count() && Set_Progress(iRecord, pTable->Get_Count()); iRecord++)
		{
			CSG_Table_Record	*pRecord	= pTable->Get_Record_byIndex(iRecord);

			if( !pCut || sValue.Cmp(pRecord->asString(iField)) )
			{
				pCut	= pTable->Get_ObjectType() == DATAOBJECT_TYPE_Shapes
						? SG_Create_Shapes(((CSG_Shapes *)pTable)->Get_Type(), SG_T(""), pTable)
						: SG_Create_Table(pTable);

				pCut->Set_Name(CSG_String::Format(SG_T("%s [%s = %s]"),
					pTable->Get_Name(),
					pTable->Get_Field_Name(iField),
					pRecord->asString(iField)
				));

				Parameters("CUTS")->asTableList()->Add_Item(pCut);

				sValue	= pRecord->asString(iField);
			}

			pCut->Add_Record(pRecord);
		}

		return( pCut != NULL );
	}

	return( false );
}
예제 #10
0
//---------------------------------------------------------
int CChange_Detection::Get_Class(CSG_Table &Classes, double Value)
{
	int		a, b, i, c;

	if( Classes.Get_Count() > 0 )
	{
		if( Classes.Get_Index_Field(0) != CLASS_MIN || Classes.Get_Index_Order(0) != TABLE_INDEX_Ascending )
		{
			Classes.Set_Index(CLASS_MIN, TABLE_INDEX_Ascending);
		}

		for(a=0, b=Classes.Get_Record_Count()-1; a < b; )
		{
			i	= a + (b - a) / 2;
			c	= Cmp_Class(Classes, Value, i);

			if( c > 0 )
			{
				b	= b > i ? i : b - 1;
			}
			else if( c < 0 )
			{
				a	= a < i ? i : a + 1;
			}
			else
			{
				return( Classes.Get_Record_byIndex(i)->Get_Index() );
			}
		}

		if( Cmp_Class(Classes, Value, a) == 0 )
		{
			return( Classes.Get_Record_byIndex(a)->Get_Index() );
		}

		if( a != b && Cmp_Class(Classes, Value, b) == 0 )
		{
			return( Classes.Get_Record_byIndex(b)->Get_Index() );
		}
	}

	return( Classes.Get_Count() );	// := unclassified
}
예제 #11
0
//---------------------------------------------------------
int CGrid_Class_Statistics_For_Polygons::Get_Class(double Value, const CSG_Table &LUT, int fMin, int fMax)
{
	for(int i=0; i<LUT.Get_Count(); i++)
	{
		if( LUT[i].asDouble(fMin) <= Value && Value <= LUT[i].asDouble(fMax) )
		{
			return( i );
		}
	}

	return( -1 );
}
예제 #12
0
//---------------------------------------------------------
CSG_String CSG_ODBC_Connection::Get_Field_Names(const CSG_String &Table_Name) const
{
	CSG_Table	Fields	= Get_Field_Desc(Table_Name);

	CSG_String	Names;

	for(int i=0; i<Fields.Get_Count(); i++)
	{
		Names	+= Fields[i].asString(3);
		Names	+= SG_T("|");
	}

	return( Names );
}
예제 #13
0
//---------------------------------------------------------
void CShapes_Load::On_Connection_Changed(CSG_Parameters *pParameters)
{
	CSG_String	s;
	CSG_Table	t;

	if( Get_Connection()->Table_Load(t, SG_T("geometry_columns")) )
	{
		for(int i=0; i<t.Get_Count(); i++)
		{
			s	+= t[i].asString(SG_T("f_table_name")) + CSG_String("|");
		}
	}

	Parameters("TABLES")->asChoice()->Set_Items(s);
}
//---------------------------------------------------------
bool	PGSQL_is_Connected		(const CSG_String &Server)
{
	CSG_Table	Connections;

	RUN_MODULE(DB_PGSQL_Get_Connections, false, SET_PARAMETER("CONNECTIONS", &Connections));	// CGet_Connections

	for(int i=0; bResult && i<Connections.Get_Count(); i++)
	{
		if( !Server.Cmp(Connections[i].asString(0)) )
		{
			return( true );
		}
	}

	return( false );
}
//---------------------------------------------------------
bool	PGSQL_Get_Connections	(CSG_Strings &Servers, double vPostGIS)
{
	Servers.Clear();

	CSG_Table	Connections;

	RUN_MODULE(DB_PGSQL_Get_Connections, false, SET_PARAMETER("CONNECTIONS", &Connections));	// CGet_Connections

	for(int i=0; bResult && i<Connections.Get_Count(); i++)
	{
		if( vPostGIS <= 0.0 || vPostGIS <= Connections[i].asDouble("PostGIS") )
		{
			Servers	+= Connections[i].asString(0);
		}
	}

	return( Servers.Get_Count() > 0 );
}
예제 #16
0
//---------------------------------------------------------
bool CChange_Detection::Get_Changes(CSG_Table &Initial, CSG_Table &Final, CSG_Table *pChanges, CSG_Matrix &Identity)
{
	int		iInitial, iFinal;

	//-----------------------------------------------------
	Identity.Create(Final.Get_Count() + 1, Initial.Get_Count() + 1);

	for(iInitial=0; iInitial<Initial.Get_Count(); iInitial++)
	{
		CSG_String	s	= Initial[iInitial].asString(CLASS_NAM);

		for(iFinal=0; iFinal<Final.Get_Count(); iFinal++)
		{
			Identity[iInitial][iFinal]	= s.Cmp(Final[iFinal].asString(CLASS_NAM)) ? 0 : 1;
		}
	}

	Identity[Initial.Get_Count()][Final.Get_Count()]	= 1;	// unclassified

	//-----------------------------------------------------
	pChanges->Destroy();

	pChanges->Add_Field(_TL("Name"), SG_DATATYPE_String);

	for(iFinal=0; iFinal<Final.Get_Count(); iFinal++)
	{
		pChanges->Add_Field(Final[iFinal].asString(CLASS_NAM), SG_DATATYPE_Double);
	}

	pChanges->Add_Field(_TL("Unclassified"), SG_DATATYPE_Double);

	//-----------------------------------------------------
	for(iInitial=0; iInitial<Initial.Get_Count(); iInitial++)
	{
		pChanges->Add_Record()->Set_Value(0, Initial[iInitial].asString(CLASS_NAM));
	}

	pChanges->Add_Record()->Set_Value(0, _TL("Unclassified"));

	return( true );
}
예제 #17
0
//---------------------------------------------------------
bool CTable_List::On_Execute(void)
{
	CSG_Table	*pTables	= Parameters("TABLES")->asTable();

	pTables->Destroy();
	pTables->Set_Name(Get_Connection()->Get_Connection() + " [" + _TL("Tables") + "]");

	pTables->Add_Field(_TL("Table"), SG_DATATYPE_String);
	pTables->Add_Field(_TL("Type" ), SG_DATATYPE_String);

	CSG_Strings	Tables;

	if( Get_Connection()->Get_Tables(Tables) )
	{
		CSG_Table	t;

		for(int i=0; i<Tables.Get_Count(); i++)
		{
			CSG_Table_Record	*pTable	= pTables->Add_Record();

			pTable->Set_Value(0, Tables[i]);

			if( Get_Connection()->Table_Load(t, "geometry_columns", "type", CSG_String::Format("f_table_name='%s'", Tables[i].c_str())) && t.Get_Count() == 1 )
			{
				pTable->Set_Value(1, t[0][0].asString());
			}
			else if( Get_Connection()->Table_Load(t, "raster_columns", "*", CSG_String::Format("r_table_name='%s'", Tables[i].c_str())) && t.Get_Count() == 1 )
			{
				pTable->Set_Value(1, "RASTER");
			}
			else
			{
				pTable->Set_Value(1, "TABLE");
			}
		}
	}

	return( pTables->Get_Count() > 0 );
}
예제 #18
0
	bool					Del_Edge			(int ID)
	{
		int	n	= 0;

		for(int i=m_Edges.Get_Count()-1; i>=0; i--)
		{
			if( m_Edges[i].asInt(0) == ID )
			{
				m_Edges.Del_Record(i);

				n++;
			}
		}

		if( n > 0 )
		{
			m_Edges.Set_Index(1, TABLE_INDEX_Ascending);

			return( true );
		}

		return( false );
	}
예제 #19
0
//---------------------------------------------------------
int CTable_Query_GUI::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
{
	if( pParameter->Cmp_Identifier("TABLES") )
	{
		CSG_Parameters	&Tables	= *pParameters->Get_Parameter("TABLES")->asParameters();
		CSG_Parameters	&Fields	= *pParameters->Get_Parameter("FIELDS")->asParameters();
		CSG_Parameters	&Group	= *pParameters->Get_Parameter("GROUP" )->asParameters();

		for(int i=0; i<Tables.Get_Count(); i++)
		{
			CSG_String	Table	= Tables[i].Get_Identifier();

			if( Tables[i].asBool() && !Fields(Table) )
			{
				CSG_Table	Desc	= Get_Connection()->Get_Field_Desc(Table);

				CSG_Parameter	*pFields	= Fields.Add_Node("", Table, Table, "");
				CSG_Parameter	*pGroup 	= Group .Add_Node("", Table, Table, "");

				for(int j=0; j<Desc.Get_Count(); j++)
				{
					CSG_String	ID	= Table + "." + Desc[j].asString(0);

					Fields.Add_Bool(pFields, ID, Desc[j].asString(0), "");
					Group .Add_Bool(pGroup , ID, Desc[j].asString(0), "");
				}
			}
			else if( !Tables[i].asBool() && Fields(Table) )
			{
				Fields.Del_Parameter(Table);
				Group .Del_Parameter(Table);
			}
		}
	}

	return( CSG_PG_Tool::On_Parameter_Changed(pParameters, pParameter) );
}
예제 #20
0
//---------------------------------------------------------
bool CSG_Table::_Load_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator)
{
	int			i, iField, fLength;
	CSG_String	sLine, sField;
	CSG_File	Stream;
	CSG_Table	Table;

	//-----------------------------------------------------
	if( Stream.Open(File_Name, SG_FILE_R, false) == false )
	{
		return( false );
	}

	if( !Stream.Read_Line(sLine) )
	{
		return( false );
	}

	//-----------------------------------------------------
	sLine	+= Separator;

	while( (i = sLine.Find(Separator)) >= 0 )
	{
		sField	= bHeadline ? sLine.Left(i) : CSG_String::Format(SG_T("FIELD_%02d"), Table.Get_Field_Count() + 1);

		if( sField[0] == SG_T('\"') && sField[(int)(sField.Length() - 1)] == SG_T('\"') )	// remove quota
		{
			sField	= sField.AfterFirst('\"').BeforeLast('\"');
		}

		Table.Add_Field(sField, SG_DATATYPE_String);

		sLine.Remove(0, i + 1);
	}

	//-----------------------------------------------------
	TSG_Data_Type	*Type	= new TSG_Data_Type[Table.Get_Field_Count()];

	for(iField=0; iField<Table.Get_Field_Count(); iField++)
	{
		Type[iField]	= SG_DATATYPE_Int;
	}

	if( !bHeadline )
	{
		Stream.Seek_Start();
	}

	fLength	= Stream.Length();

	while( Stream.Read_Line(sLine) && sLine.Length() > 0 && SG_UI_Process_Set_Progress(Stream.Tell(), fLength) )
	{
		CSG_Table_Record	*pRecord	= Table._Add_Record();

		sLine	+= Separator;

		for(iField=0; iField<Table.Get_Field_Count(); iField++)
		{
			if( (i = sLine.Find(Separator)) >= 0 )
			{
				sField	= sLine.Left(i);

				if( sField[0] == SG_T('\"') && sField[(int)(sField.Length() - 1)] == SG_T('\"') )	// remove quota
				{
					sField	= sField.AfterFirst('\"').BeforeLast('\"');
				}

				if( Type[iField] != SG_DATATYPE_String )
				{
					double	Value;

					if( SG_SSCANF(sField, SG_T("%lf"), &Value) != 1 )
					{
						Type[iField]	= SG_DATATYPE_String;
					}
					else if( Type[iField] != SG_DATATYPE_Double && Value - (int)Value != 0.0 )
					{
						Type[iField]	= SG_DATATYPE_Double;
					}
				}

				pRecord->Set_Value(iField, sField);

				sLine.Remove(0, i + 1);
			}
			else
			{
				break;
			}
		}
	}

	//-----------------------------------------------------
	if( Table.Get_Count() > 0 )
	{
		for(iField=0; iField<Table.Get_Field_Count(); iField++)
		{
			Add_Field(Table.Get_Field_Name(iField), Type[iField]);
		}

		for(int iRecord=0; iRecord<Table.Get_Count() && SG_UI_Process_Set_Progress(iRecord, Table.Get_Count()); iRecord++)
		{
			CSG_Table_Record	*pRecord	= _Add_Record();

			for(iField=0; iField<Get_Field_Count(); iField++)
			{
				switch( Get_Field_Type(iField) )
				{
				default:						pRecord->Set_Value(iField, Table[iRecord].asString(iField));	break;
				case SG_DATATYPE_Int:		pRecord->Set_Value(iField, Table[iRecord].asInt   (iField));	break;
				case SG_DATATYPE_Double:	pRecord->Set_Value(iField, Table[iRecord].asDouble(iField));	break;
				}
			}
		}
	}

	delete[](Type);

	SG_UI_Process_Set_Ready();

	return( Get_Field_Count() > 0 );
}
예제 #21
0
//---------------------------------------------------------
bool CSG_Shapes::Create(const CSG_String &File_Name)
{
	Destroy();

	SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Load shapes"), File_Name.c_str()), true);

	//-----------------------------------------------------
	bool	bResult	= File_Name.BeforeFirst(':').Cmp("PGSQL") && SG_File_Exists(File_Name) && _Load_ESRI(File_Name);

	if( bResult )
	{
		Set_File_Name(File_Name, true);
	}

	//-----------------------------------------------------
	else if( File_Name.BeforeFirst(':').Cmp("PGSQL") == 0 )	// database source
	{
		CSG_String	s(File_Name);

		s	= s.AfterFirst(':');	CSG_String	Host  (s.BeforeFirst(':'));
		s	= s.AfterFirst(':');	CSG_String	Port  (s.BeforeFirst(':'));
		s	= s.AfterFirst(':');	CSG_String	DBName(s.BeforeFirst(':'));
		s	= s.AfterFirst(':');	CSG_String	Table (s.BeforeFirst(':'));

		CSG_Tool	*pTool	= SG_Get_Tool_Library_Manager().Get_Tool("db_pgsql", 0);	// CGet_Connections

		if(	pTool != NULL )
		{
			SG_UI_ProgressAndMsg_Lock(true);

			//---------------------------------------------
			CSG_Table	Connections;
			CSG_String	Connection	= DBName + " [" + Host + ":" + Port + "]";

			pTool->Settings_Push();

			if( pTool->On_Before_Execution() && SG_TOOL_PARAMETER_SET("CONNECTIONS", &Connections) && pTool->Execute() )	// CGet_Connections
			{
				for(int i=0; !bResult && i<Connections.Get_Count(); i++)
				{
					if( !Connection.Cmp(Connections[i].asString(0)) )
					{
						bResult	= true;
					}
				}
			}

			pTool->Settings_Pop();

			//---------------------------------------------
			if( bResult && (bResult = (pTool = SG_Get_Tool_Library_Manager().Get_Tool("db_pgsql", 20)) != NULL) == true )	// CPGIS_Shapes_Load
			{
				pTool->Settings_Push();

				bResult	= pTool->On_Before_Execution()
					&& SG_TOOL_PARAMETER_SET("CONNECTION", Connection)
					&& SG_TOOL_PARAMETER_SET("TABLES"    , Table)
					&& SG_TOOL_PARAMETER_SET("SHAPES"    , this)
					&& pTool->Execute();

				pTool->Settings_Pop();
			}

			SG_UI_ProgressAndMsg_Lock(false);
		}
	}

	//-----------------------------------------------------
	if( bResult )
	{
		Set_Modified(false);
		Set_Update_Flag();

		SG_UI_Process_Set_Ready();
		SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS);

		return( true );
	}

	for(int iShape=Get_Count()-1; iShape>=0; iShape--)	// be kind, keep at least those shapes that have been loaded successfully
	{
		if( !Get_Shape(iShape)->is_Valid() )
		{
			Del_Shape(iShape);
		}
	}

	if( Get_Count() <= 0 )
	{
		Destroy();
	}

	SG_UI_Process_Set_Ready();
	SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);

	return( false );
}
//---------------------------------------------------------
bool CTable_Fill_Record_Gaps::On_Execute(void)
{
	int			Method;
	CSG_Table	*pTable;

	//-----------------------------------------------------
	pTable		= Parameters("TABLE")	->asTable();
	m_pNoGaps	= Parameters("NOGAPS")	->asTable();
	m_fOrder	= Parameters("ORDER")	->asInt();
	Method		= Parameters("METHOD")	->asInt();

	if( pTable->Get_Count() == 0 || !pTable->Set_Index(m_fOrder, TABLE_INDEX_Ascending) )
	{
		return( false );
	}

	//-----------------------------------------------------
	CSG_Table_Record	*pA, *pB;

	m_pNoGaps->Create(pTable);
	m_pNoGaps->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pTable->Get_Name(), _TL("no gaps")));
	m_pNoGaps->Add_Record(pB = pTable->Get_Record(0));

	//-----------------------------------------------------
	for(int iRecord=1; iRecord<pTable->Get_Count() && Set_Progress(iRecord, pTable->Get_Count()-1); iRecord++)
	{
		pA		= pB;
		pB		= pTable->Get_Record(iRecord);

		int	iA	= pA->asInt(m_fOrder);
		int	iB	= pB->asInt(m_fOrder);

		if( iB - iA > 1 )
		{
			int		iStart	= m_pNoGaps->Get_Count();

			for(int i=iA+1; i<iB; i++)
			{
				m_pNoGaps->Add_Record()->Set_Value(m_fOrder, i);
			}

			for(int iField=0; iField<pTable->Get_Field_Count(); iField++)
			{
				if( iField != m_fOrder && ::SG_Data_Type_is_Numeric(pTable->Get_Field_Type(iField)) )
				{
					switch( Method )
					{
					case 0:
						Set_Nearest (iStart, iField, pA, pB);
						break;

					case 1:
						Set_Linear	(iStart, iField, pA, pB);
						break;

					case 2:
						Set_Spline	(iStart, iField, pTable->Get_Record(iRecord - 2), pA, pB, pTable->Get_Record(iRecord + 1));
						break;
					}
				}
			}
		}

		m_pNoGaps->Add_Record(pB);
	}

	//-----------------------------------------------------
	return( true );
}
//---------------------------------------------------------
bool CGrid_Value_Replace::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Grid	*pGrid	= Parameters("OUTPUT")->asGrid();

	if( !pGrid || pGrid == Parameters("INPUT")->asGrid() )
	{
		pGrid	= Parameters("INPUT")->asGrid();
	}
	else
	{
		pGrid->Assign(Parameters("INPUT")->asGrid());

		DataObject_Set_Parameters(pGrid, Parameters("INPUT")->asGrid());

		pGrid->Fmt_Name("%s [%s]", Parameters("INPUT")->asGrid()->Get_Name(), _TL("Changed"));
	}

	//-----------------------------------------------------
	int		Method	= Parameters("METHOD")->asInt();

	CSG_Table	LUT;

	switch( Method )
	{
	default:	LUT.Create(*Parameters("IDENTITY")->asTable());	break;
	case  1:	LUT.Create(*Parameters("RANGE"   )->asTable());	break;
	case  2:	LUT.Create( Parameters("RANGE"   )->asTable());
		if( SG_UI_Get_Window_Main()	// gui only
		&&  DataObject_Get_Parameter(Parameters("GRID" )->asGrid(), "LUT")
		&&  DataObject_Get_Parameter(Parameters("INPUT")->asGrid(), "LUT") )
		{
			CSG_Table	LUTs[2];

			LUTs[0].Create(*DataObject_Get_Parameter(Parameters("GRID" )->asGrid(), "LUT")->asTable());
			LUTs[1].Create(*DataObject_Get_Parameter(Parameters("INPUT")->asGrid(), "LUT")->asTable());

			for(int i=0; i<LUTs[0].Get_Count(); i++)
			{
				CSG_String	Name	= LUTs[0][i].asString(1);

				for(int j=LUTs[1].Get_Count()-1; j>=0; j--)
				{
					if( !Name.Cmp(LUTs[1][j].asString(1)) )
					{
						CSG_Table_Record	*pReplace	= LUT.Add_Record();

						pReplace->Set_Value(0, LUTs[0][i].asDouble(3));
						pReplace->Set_Value(1, LUTs[1][j].asDouble(3));
						pReplace->Set_Value(2, LUTs[1][j].asDouble(4));

						LUTs[1].Del_Record(j);

						break;
					}
				}
			}

			for(int j=0; j<LUTs[1].Get_Count(); j++)
			{
				LUTs[0].Add_Record(LUTs[1].Get_Record(j));
			}

			DataObject_Add(pGrid);
			CSG_Parameter	*pLUT	= DataObject_Get_Parameter(pGrid, "LUT");
			pLUT->asTable()->Assign_Values(&LUTs[0]);
			DataObject_Set_Parameter(pGrid, pLUT);
		}
		break;
	}

	//-----------------------------------------------------
	if( LUT.Get_Count() == 0 )
	{
		Error_Set(_TL("empty look-up table, nothing to replace"));

		return( false );
	}

	//-----------------------------------------------------
	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#ifndef _DEBUG
		#pragma omp parallel for
		#endif
		for(int x=0; x<Get_NX(); x++)
		{
			double	Value	= pGrid->asDouble(x, y);

			for(int i=0; i<LUT.Get_Count(); i++)
			{
				if( Method == 0 )
				{
					if( LUT[i].asDouble(1) == Value )
					{
						pGrid->Set_Value(x, y, LUT[i].asDouble(0));

						break;
					}
				}
				else
				{
					if( LUT[i].asDouble(1) <= Value && Value <= LUT[i].asDouble(2) )
					{
						pGrid->Set_Value(x, y, LUT[i].asDouble(0));

						break;
					}
				}
			}
		}
	}

	//-----------------------------------------------------
	if( pGrid == Parameters("INPUT")->asGrid() )
	{
		DataObject_Update(pGrid);
	}

	return( true );
}
예제 #24
0
//---------------------------------------------------------
bool CSG_ODBC_Connection::Table_Insert(const CSG_String &Table_Name, const CSG_Table &Table, bool bCommit)
{
	//-----------------------------------------------------
	if( !is_Connected() )
	{
		_Error_Message(_TL("no database connection"));

		return( false );
	}

	if( !Table_Exists(Table_Name) )
	{
		return( false );
	}

	CSG_Table	Fields	= Get_Field_Desc(Table_Name);

	if( Fields.Get_Count() != Table.Get_Field_Count() )
	{
		return( false );
	}

	//-----------------------------------------------------
	try
	{
		bool	bLOB	= false;

		int				iField, iRecord;
		CSG_String		Insert;
		otl_stream		Stream;

		//-------------------------------------------------
		Insert.Printf(SG_T("INSERT INTO %s VALUES("), Table_Name.c_str());

		for(iField=0; iField<Table.Get_Field_Count(); iField++)
		{
			if( iField > 0 )
			{
				Insert	+= SG_T(",");
			}

			Insert	+= CSG_String::Format(SG_T(":f%d"), 1 + iField);

			switch( Table.Get_Field_Type(iField) )
			{
			default:
			case SG_DATATYPE_String:	Insert	+= SG_T("<varchar>");	break;
			case SG_DATATYPE_Date:		Insert	+= SG_T("<char[12]>");	break;
			case SG_DATATYPE_Char:		Insert	+= SG_T("<char>");		break;
			case SG_DATATYPE_Short:		Insert	+= SG_T("<short>");		break;
			case SG_DATATYPE_Int:		Insert	+= SG_T("<int>");		break;
			case SG_DATATYPE_Color:		Insert	+= SG_T("<long>");		break;
			case SG_DATATYPE_Long:		Insert	+= SG_T("<long>");		break;
			case SG_DATATYPE_Float:		Insert	+= SG_T("<float>");		break;
			case SG_DATATYPE_Double:	Insert	+= SG_T("<double>");	break;
			}
		}

		Insert	+= SG_T(")");

		Stream.set_all_column_types(otl_all_date2str);
		Stream.set_lob_stream_mode(bLOB);
		Stream.open(bLOB ? 1 : m_Size_Buffer, Insert, m_Connection);

		std_string	valString;

		//-------------------------------------------------
		for(iRecord=0; iRecord<Table.Get_Count() && SG_UI_Process_Set_Progress(iRecord, Table.Get_Count()); iRecord++)
		{
			CSG_Table_Record	*pRecord	= Table.Get_Record(iRecord);

			for(iField=0; iField<Table.Get_Field_Count(); iField++)
			{
				if( pRecord->is_NoData(iField) )
				{
					Stream << otl_null();
				}
				else switch( Table.Get_Field_Type(iField) )
				{
				default:
				case SG_DATATYPE_String:
				case SG_DATATYPE_Date:
					valString	= CSG_String(pRecord->asString(iField));
					Stream << valString;
					break;

				case SG_DATATYPE_Char:		Stream << (char)pRecord->asChar  (iField);	break;
				case SG_DATATYPE_Short:		Stream <<       pRecord->asShort (iField);	break;
				case SG_DATATYPE_Int:		Stream <<       pRecord->asInt   (iField);	break;
				case SG_DATATYPE_Color:
				case SG_DATATYPE_Long:		Stream << (long)pRecord->asInt   (iField);	break;
				case SG_DATATYPE_Float:		Stream <<       pRecord->asFloat (iField);	break;
				case SG_DATATYPE_Double:	Stream <<       pRecord->asDouble(iField);	break;
				}
			}
		}
	}
	//-----------------------------------------------------
	catch( otl_exception &e )
	{
		_Error_Message(e);

		return( false );
	}

	return( true );
}
예제 #25
0
//---------------------------------------------------------
bool CFilter_3x3::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Table	*pFilter	= Parameters("FILTER")->asTable()
		? Parameters("FILTER"    )->asTable()
		: Parameters("FILTER_3X3")->asTable();

	if( pFilter->Get_Count() < 1 || pFilter->Get_Field_Count() < 1 )
	{
		Error_Set(_TL("invalid filter matrix"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Matrix	Filter(pFilter->Get_Field_Count(), pFilter->Get_Count());

	{
		for(int iy=0; iy<Filter.Get_NY(); iy++)
		{
			CSG_Table_Record	*pRecord	= pFilter->Get_Record(iy);

			for(int ix=0; ix<Filter.Get_NX(); ix++)
			{
				Filter[iy][ix]	= pRecord->asDouble(ix);
			}
		}
	}

	int	nx	= (Filter.Get_NX() - 1) / 2;
	int	ny	= (Filter.Get_NY() - 1) / 2;

	//-----------------------------------------------------
	CSG_Grid	*pInput 	= Parameters("INPUT" )->asGrid();
	CSG_Grid	*pResult	= Parameters("RESULT")->asGrid();

	if( !pResult || pResult == pInput )
	{
		pResult	= SG_Create_Grid(pInput);
	}
	else
	{
		pResult->Fmt_Name("%s [%s]", pInput->Get_Name(), _TL("Filter"));

		pResult->Set_NoData_Value(pInput->Get_NoData_Value());
	}

	//-----------------------------------------------------
	bool	bAbsolute	= Parameters("ABSOLUTE")->asBool();

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		#pragma omp parallel for
		for(int x=0; x<Get_NX(); x++)
		{
			double	s	= 0.0;
			double	n	= 0.0;

			if( pInput->is_InGrid(x, y) )
			{
				for(int iy=0, jy=y-ny; iy<Filter.Get_NY(); iy++, jy++)
				{
					for(int ix=0, jx=x-nx; ix<Filter.Get_NX(); ix++, jx++)
					{
						if( pInput->is_InGrid(jx, jy) )
						{
							s	+=      Filter[iy][ix] * pInput->asDouble(jx, jy);
							n	+= fabs(Filter[iy][ix]);
						}
					}
				}
			}

			if( n > 0.0 )
			{
				pResult->Set_Value(x, y, bAbsolute ? s : s / n);
			}
			else
			{
				pResult->Set_NoData(x, y);
			}
		}
	}

	//-----------------------------------------------------
	if( !Parameters("RESULT")->asGrid() || Parameters("RESULT")->asGrid() == pInput )
	{
		pInput->Assign(pResult);

		delete(pResult);

		DataObject_Update(pInput);
	}

	return( true );
}
예제 #26
0
//---------------------------------------------------------
bool CLine_Dissolve::On_Execute(void)
{
	CSG_Shapes	*pLines	= Parameters("LINES")->asShapes();

	if(	!pLines->is_Valid() || pLines->Get_Count() < 2 )
	{
		Error_Set(_TL("invalid or empty lines layer"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Shapes	*pDissolved	= Parameters("DISSOLVED")->asShapes();

	pDissolved->Create(SHAPE_TYPE_Line);

	CSG_Parameter_Table_Fields	&Fields	= *Parameters("FIELDS")->asTableFields();

	CSG_Table	Dissolve;

	//-----------------------------------------------------
	if( Fields.Get_Count() == 0 )
	{
		pDissolved->Fmt_Name("%s [%s]", pLines->Get_Name(), _TL("Dissolved"));
	}
	else
	{
		Dissolve.Add_Field("INDEX", SG_DATATYPE_Int   );
		Dissolve.Add_Field("VALUE", SG_DATATYPE_String);

		Dissolve.Set_Record_Count(pLines->Get_Count());

		for(int i=0; i<pLines->Get_Count() && Set_Progress(i, pLines->Get_Count()); i++)
		{
			CSG_Shape	*pLine	= pLines->Get_Shape(i);

			CSG_String	Value;

			for(int iField=0; iField<Fields.Get_Count(); iField++)
			{
				Value	+= pLine->asString(Fields.Get_Index(iField));
			}

			Dissolve[i].Set_Value(0, i);
			Dissolve[i].Set_Value(1, Value);
		}

		Dissolve.Set_Index(1, TABLE_INDEX_Ascending);

		//-------------------------------------------------
		CSG_String	Name;

		for(int iField=0; iField<Fields.Get_Count(); iField++)
		{
			if( iField > 0 )
			{
				Name	+= "; ";
			}

			Name	+= pLines->Get_Field_Name(Fields.Get_Index(iField));

			pDissolved->Add_Field(
				pLines->Get_Field_Name(Fields.Get_Index(iField)),
				pLines->Get_Field_Type(Fields.Get_Index(iField))
			);
		}

		pDissolved->Fmt_Name("%s [%s: %s]", pLines->Get_Name(), _TL("Dissolved"), Name.c_str());
	}

	//-----------------------------------------------------
	Statistics_Initialize(pDissolved, pLines);

	//-----------------------------------------------------
	CSG_String	Value;	CSG_Shape	*pDissolve	= NULL;

	for(int i=0; i<pLines->Get_Count() && Set_Progress(i, pLines->Get_Count()); i++)
	{
		CSG_Shape	*pLine	= pLines->Get_Shape(!Dissolve.Get_Count() ? i : Dissolve[i].asInt(0));

		if( !pDissolve || (Dissolve.Get_Count() && Value.Cmp(Dissolve[i].asString(1))) )
		{
			if( Dissolve.Get_Count() )
			{
				Value	= Dissolve[i].asString(1);
			}

			pDissolve	= pDissolved->Add_Shape(pLine, SHAPE_COPY_GEOM);

			for(int iField=0; iField<Fields.Get_Count(); iField++)
			{
				*pDissolve->Get_Value(iField)	= *pLine->Get_Value(Fields.Get_Index(iField));
			}

			Statistics_Add(pDissolve, pLine, true);
		}
		else
		{
			Add_Line(pDissolve, pLine);

			Statistics_Add(pDissolve, pLine, false);
		}
	}

	//-----------------------------------------------------
	return( pDissolved->is_Valid() );
}
//---------------------------------------------------------
bool CTable_Record_Statistics_Base::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Table	*pTable	= Parameters("TABLE")->asTable();

	if( !pTable->is_Valid() || pTable->Get_Field_Count() <= 0 || pTable->Get_Record_Count() <= 0 )
	{
		Error_Set(_TL("invalid table"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Array_Int	_Fields;

	int	*Fields	= (int *)Parameters("FIELDS")->asPointer();
	int	nFields	=        Parameters("FIELDS")->asInt    ();

	if( nFields == 0 )
	{
		for(int i=0; i<pTable->Get_Field_Count(); i++)
		{
			if( SG_Data_Type_is_Numeric(pTable->Get_Field_Type(i)) )
			{
				_Fields.Inc_Array(); _Fields[nFields++]	= i;
			}
		}

		if( nFields == 0 )
		{
			Error_Set(_TL("could not find any numeric attribute field"));

			return( false );
		}

		Fields	= _Fields.Get_Array();
	}

	//-----------------------------------------------------
	if( Parameters("RESULT")->asTable() && Parameters("RESULT")->asTable() != pTable )
	{
		pTable	= Parameters("RESULT")->asTable();
		pTable->Create( *Parameters("TABLE")->asTable());
		pTable->Set_Name(Parameters("TABLE")->asTable()->Get_Name());
	}

	//-----------------------------------------------------
	double	Quantile	= Parameters("PCTL_VAL")->asDouble();

	int	offResult	= pTable->Get_Field_Count();

	bool	bStats[STATS_COUNT];

	{
		for(int i=0; i<STATS_COUNT; i++)
		{
			if( (bStats[i] = Parameters(STATS[i][0])->asBool()) == true )
			{
				pTable->Add_Field(STATS[i][1], SG_DATATYPE_Double);
			}
		}

		if( pTable->Get_Field_Count() == offResult )
		{
			Error_Set(_TL("no statistical measure has been selected"));

			return( false );
		}
	}

	//-----------------------------------------------------
	for(int iRecord=0; iRecord<pTable->Get_Count() && Set_Progress(iRecord, pTable->Get_Count()); iRecord++)
	{
		CSG_Table_Record	*pRecord	= pTable->Get_Record(iRecord);

		CSG_Simple_Statistics	s(bStats[STATS_PCTL]);

		for(int iField=0; iField<nFields; iField++)
		{
			if( !pRecord->is_NoData(Fields[iField]) )
			{
				s	+= pRecord->asDouble(Fields[iField]);
			}
		}

		//-------------------------------------------------
		int	iField	= offResult;

		if( s.Get_Count() > 0 )
		{
			if( bStats[STATS_MEAN ]	)	pRecord->Set_Value(iField++, s.Get_Mean    ());
			if( bStats[STATS_MIN  ]	)	pRecord->Set_Value(iField++, s.Get_Minimum ());
			if( bStats[STATS_MAX  ]	)	pRecord->Set_Value(iField++, s.Get_Maximum ());
			if( bStats[STATS_RANGE]	)	pRecord->Set_Value(iField++, s.Get_Range   ());
			if( bStats[STATS_SUM  ]	)	pRecord->Set_Value(iField++, s.Get_Sum     ());
			if( bStats[STATS_NUM  ]	)	pRecord->Set_Value(iField++, s.Get_Count   ());
			if( bStats[STATS_VAR  ]	)	pRecord->Set_Value(iField++, s.Get_Variance());
			if( bStats[STATS_STDV ]	)	pRecord->Set_Value(iField++, s.Get_StdDev  ());
			if( bStats[STATS_PCTL ]	)	pRecord->Set_Value(iField++, s.Get_Quantile(Quantile));
		}
		else
		{
			for(int i=0; i<STATS_COUNT; i++)
			{
				if( bStats[i] )
				{
					pRecord->Set_NoData(iField++);
				}
			}
		}
	}

	//-----------------------------------------------------
	if( pTable == Parameters("TABLE")->asTable() )
	{
		DataObject_Update(pTable);
	}

	return( true );
}
예제 #28
0
//---------------------------------------------------------
bool CChange_Detection::Get_Classes(CSG_Table &Classes, CSG_Grid *pGrid, bool bInitial)
{
	CSG_Table	*pClasses;

	Classes.Destroy();

	Classes.Add_Field(_TL("NAME")	, SG_DATATYPE_String);
	Classes.Add_Field(_TL("MIN")	, SG_DATATYPE_Double);
	Classes.Add_Field(_TL("MAX")	, SG_DATATYPE_Double);

	//-----------------------------------------------------
	if( (pClasses = Parameters(bInitial ? "INI_LUT" : "FIN_LUT")->asTable()) != NULL )
	{
		int	fNam	= Parameters(bInitial ? "INI_LUT_NAM" : "FIN_LUT_NAM")->asInt();
		int	fMin	= Parameters(bInitial ? "INI_LUT_MIN" : "FIN_LUT_MIN")->asInt();
		int	fMax	= Parameters(bInitial ? "INI_LUT_MAX" : "FIN_LUT_MAX")->asInt();

		if( fNam < 0 || fNam >= pClasses->Get_Field_Count() )	{	fNam	= fMin;	}
		if( fMax < 0 || fMax >= pClasses->Get_Field_Count() )	{	fMax	= fMin;	}

		for(int iClass=0; iClass<pClasses->Get_Count(); iClass++)
		{
			CSG_Table_Record	*pClass	= Classes.Add_Record();

			pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(fNam));
			pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(fMin));
			pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(fMax));
		}
	}

	//-----------------------------------------------------
	else if( DataObject_Get_Parameter(pGrid, "LUT") )
	{
		pClasses	= DataObject_Get_Parameter(pGrid, "LUT")->asTable();

		for(int iClass=0; iClass<pClasses->Get_Count(); iClass++)
		{
			CSG_Table_Record	*pClass	= Classes.Add_Record();

			pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(1));
			pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(3));
			pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(4));
		}
	}

	//-----------------------------------------------------
	else
	{
		double	z;

		for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++)
		{
			double iz	= pGrid->asDouble(pGrid->Get_Sorted(i, false, false));

			if( i == 0 || iz != z )
			{
				CSG_Table_Record	*pClass	= Classes.Add_Record();

				pClass->Set_Value(CLASS_NAM, z = iz);
				pClass->Set_Value(CLASS_MIN, z);
				pClass->Set_Value(CLASS_MAX, z);
			}
		}
	}

	//-----------------------------------------------------
	return( Classes.Get_Count() > 0 );
}
예제 #29
0
//---------------------------------------------------------
bool CJoin_Tables_Base::On_Execute(void)
{
	//-----------------------------------------------------
	int			id_A, id_B;
	CSG_Table	*pT_A, *pT_B;

	pT_A	= Parameters("TABLE_A")->asTable();
	id_A	= Parameters("ID_A"   )->asInt();

	pT_B	= Parameters("TABLE_B")->asTable();
	id_B	= Parameters("ID_B"   )->asInt();

	if(	id_A < 0 || id_A >= pT_A->Get_Field_Count() || pT_A->Get_Count() <= 0
	||	id_B < 0 || id_B >= pT_B->Get_Field_Count() || pT_B->Get_Count() <= 0 )
	{
		return( false );
	}

	//-----------------------------------------------------
	if( Parameters("RESULT")->asTable() && Parameters("RESULT")->asTable() != pT_A )
	{
		pT_A	= Parameters("RESULT")->asTable();

		if( Parameters("RESULT")->asTable()->Get_ObjectType() == DATAOBJECT_TYPE_Shapes )
		{
			((CSG_Shapes *)pT_A)->Create(*Parameters("TABLE_A")->asShapes());
		}
		else
		{
			pT_A->Create(*Parameters("TABLE_A")->asTable());
		}
	}

	//-----------------------------------------------------
	int		nJoins, *Join, Offset	= pT_A->Get_Field_Count();

	if( Parameters("FIELDS_ALL")->asBool() )
	{
		if( (nJoins = pT_B->Get_Field_Count() - 1) <= 0 )
		{
			Error_Set(_TL("no fields to add"));

			return( false );
		}

		Join	= new int[nJoins];

		for(int i=0, j=0; i<pT_B->Get_Field_Count(); i++)
		{
			if( i != id_B )
			{
				pT_A->Add_Field(pT_B->Get_Field_Name(i), pT_B->Get_Field_Type(i));

				Join[j++]	= i;
			}
		}
	}
	else
	{
		CSG_Parameter_Table_Fields	*pFields	= Parameters("FIELDS")->asTableFields();

		if( (nJoins = pFields->Get_Count()) <= 0 )
		{
			Error_Set(_TL("no fields to add"));

			return( false );
		}

		Join	= new int[nJoins];

		for(int j=0; j<pFields->Get_Count(); j++)
		{
			int	i	= pFields->Get_Index(j);

			pT_A->Add_Field(pT_B->Get_Field_Name(i), pT_B->Get_Field_Type(i));

			Join[j]	= i;
		}
	}

	pT_A->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pT_A->Get_Name(), pT_B->Get_Name()));

	//-----------------------------------------------------
	bool	bCmpNumeric	=  SG_Data_Type_is_Numeric(pT_A->Get_Field_Type(id_A))
						|| SG_Data_Type_is_Numeric(pT_B->Get_Field_Type(id_B));

	CSG_Table	Delete;	if( !Parameters("KEEP_ALL")->asBool() )	Delete.Add_Field("ID", SG_DATATYPE_Int);

	pT_A->Set_Index(id_A, TABLE_INDEX_Ascending);
	pT_B->Set_Index(id_B, TABLE_INDEX_Ascending);

	CSG_Table_Record	*pRecord_B	= pT_B->Get_Record_byIndex(0);

	for(int a=0, b=0, Cmp; pRecord_B && a<pT_A->Get_Count() && Set_Progress(a, pT_A->Get_Count()); a++)
	{
		CSG_Table_Record	*pRecord_A	= pT_A->Get_Record_byIndex(a);

		while( pRecord_B && (Cmp = Cmp_Keys(pRecord_A->Get_Value(id_A), pRecord_B->Get_Value(id_B), bCmpNumeric)) < 0 )
		{
			pRecord_B	= pT_B->Get_Record_byIndex(++b);
		}

		if( pRecord_B && Cmp == 0 )
		{
			for(int i=0; i<nJoins; i++)
			{
				*pRecord_A->Get_Value(Offset + i)	= *pRecord_B->Get_Value(Join[i]);
			}
		}
		else if( Delete.Get_Field_Count() == 0 )
		{
			for(int i=0; i<nJoins; i++)
			{
				pRecord_A->Set_NoData(Offset + i);
			}
		}
		else
		{
			Delete.Add_Record()->Set_Value(0, pRecord_A->Get_Index());
		}
	}

	//-----------------------------------------------------
	delete[](Join);

	pT_A->Set_Index(id_A, TABLE_INDEX_None);
	pT_B->Set_Index(id_B, TABLE_INDEX_None);

	if( Delete.Get_Count() > 0 )
	{
		Delete.Set_Index(0, TABLE_INDEX_Descending);

		for(int i=0; i<Delete.Get_Count(); i++)
		{
		//	((CSG_Shapes *)pT_A)->Del_Shape(Delete[i].asInt(0));

			pT_A->Del_Record(Delete[i].asInt(0));
		}

		Message_Add(CSG_String::Format(SG_T("%d %s"), pT_A->Get_Selection_Count(), _TL("unjoined records have been removed")));
	}

	if( pT_A == Parameters("TABLE_A")->asTable() )
	{
		DataObject_Update(pT_A);
	}

	return( pT_A->Get_Count() > 0 );
}
//---------------------------------------------------------
bool CGridsFromTableAndGrid::On_Execute(void)
{
	int						iField, iRecord, iAttribute, nAttributes, *Attribute;
	sLong					iCell, jCell;
	CSG_Parameter_Grid_List	*pGrids;
	CSG_Grid				*pClasses;
	CSG_Table				*pTable;

	//-----------------------------------------------------
	pClasses	= Parameters("CLASSES" )->asGrid();
	pGrids		= Parameters("GRIDS"   )->asGridList();
	pTable		= Parameters("TABLE"   )->asTable();
	iField		= Parameters("ID_FIELD")->asInt();

	pGrids->Del_Items();

	if( !pClasses->Set_Index() )
	{
		Error_Set(_TL("index creation failed"));

		return( false );
	}

	//-----------------------------------------------------
	if( pTable->Get_Field_Count() == 0 || pTable->Get_Count() == 0 )
	{
		Message_Add(_TL("selected table contains no valid records"));

		return( false );
	}

	//-----------------------------------------------------
	if( !pTable->Set_Index(iField, TABLE_INDEX_Ascending) )
	{
		Message_Add(_TL("failed to create index for table"));

		return( false );
	}

	//-----------------------------------------------------
	Attribute	= new int[pTable->Get_Field_Count()];

	for(iAttribute=0, nAttributes=0; iAttribute<pTable->Get_Field_Count(); iAttribute++)
	{
		if( iAttribute != iField && pTable->Get_Field_Type(iAttribute) != SG_DATATYPE_String )
		{
			Attribute[nAttributes++]	= iAttribute;

			CSG_Grid	*pGrid	= SG_Create_Grid(*Get_System());

			pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pClasses->Get_Name(), pTable->Get_Field_Name(iAttribute)));

			pGrids->Add_Item(pGrid);
		}
	}

	if( nAttributes == 0 )
	{
		delete[](Attribute);

		Message_Add(_TL("selected table does not have numeric attributes"));

		return( false );
	}

	//-----------------------------------------------------
	CSG_Table_Record	*pRecord	= pTable->Get_Record_byIndex(0);

	for(iCell=0, iRecord=0; iCell<Get_NCells() && pRecord && Set_Progress_NCells(iCell); iCell++)
	{
		if( pClasses->Get_Sorted(iCell, jCell, false, true) )
		{
			double	valClass	= pClasses->asDouble(jCell);

			while( pRecord && pRecord->asDouble(iField) < valClass )
			{
				pRecord	= pTable->Get_Record_byIndex(++iRecord);
			}

			if( !pRecord || pRecord->asDouble(iField) > valClass )
			{
				for(iAttribute=0; iAttribute<nAttributes; iAttribute++)
				{
					pGrids->asGrid(iAttribute)->Set_NoData(jCell);
				}
			}
			else
			{
				for(iAttribute=0; iAttribute<nAttributes; iAttribute++)
				{
					pGrids->asGrid(iAttribute)->Set_Value(jCell, pRecord->asDouble(Attribute[iAttribute]));
				}
			}
		}
	}

	//-----------------------------------------------------
	delete[](Attribute);

	return true;
}