//--------------------------------------------------------- bool CSG_Table::_Save_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator) { int iField, iRecord; CSG_File Stream; if( Get_Field_Count() > 0 ) { if( Stream.Open(File_Name, SG_FILE_W, false) ) { for(iField=0; iField<Get_Field_Count(); iField++) { Stream.Printf(SG_T("%s%s"), Get_Field_Name(iField), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++) { for(iField=0; iField<Get_Field_Count(); iField++) { Stream.Printf(SG_T("%s"), Get_Record(iRecord)->asString(iField)); Stream.Printf(SG_T("%s"), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } } SG_UI_Process_Set_Ready(); return( true ); } } return( false ); }
*/ REBINT Do_Dialect(REBSER *dialect, REBSER *block, REBCNT *index, REBSER **out) /* ** Format for dialect is: ** CMD arg1 arg2 arg3 CMD arg1 arg2 ... ** ** Returns: ** cmd value or error as result (or zero for end) ** index is updated ** if *out is zero, then we create a new output block ** ** The arg sequence is terminated by: ** 1. Maximum # of args for command ** 2. An arg that is not of a specified datatype for CMD ** 3. Encountering a new CMD ** 4. End of the dialect block ** ***********************************************************************/ { REBDIA dia; REBINT n; REBINT dsp = DSP; // Save stack position CLEARS(&dia); if (*index >= SERIES_TAIL(block)) return 0; // end of block DISABLE_GC; // Avoid GC during Dialect (prevents unknown crash problem) if (!*out) *out = Make_Block(25); dia.dialect = dialect; dia.args = block; dia.argi = *index; dia.out = *out; SET_FLAG(dia.flags, RDIA_NO_CMD); //Print("DSP: %d Dinp: %r - %m", DSP, BLK_SKIP(block, *index), block); n = Do_Dia(&dia); //Print("DSP: %d Dout: CMD: %d %m", DSP, dia.cmd, *out); DSP = dsp; // pop any temp values used above if (Delect_Debug > 0) { Total_Missed += dia.missed; // !!!! debug if (dia.missed) Debug_Fmt(Dia_Fmt, Get_Field_Name(dia.dialect, dia.cmd), dia.out->tail, dia.missed, Total_Missed); } if (n < 0) return n; //error *index = dia.argi; ENABLE_GC; return dia.cmd; }
//--------------------------------------------------------- bool CSG_Table::_Save_Text(const CSG_String &File_Name, bool bHeadline, const SG_Char *Separator) { int iField, iRecord; CSG_File Stream; if( Get_Field_Count() > 0 ) { if( Stream.Open(File_Name, SG_FILE_W, false) ) { for(iField=0; iField<Get_Field_Count(); iField++) { Stream.Printf(SG_T("%s%s"), Get_Field_Name(iField), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++) { for(iField=0; iField<Get_Field_Count(); iField++) { if( !Get_Record(iRecord)->is_NoData(iField) ) { switch( Get_Field_Type(iField) ) { case SG_DATATYPE_String: case SG_DATATYPE_Date: Stream.Printf(SG_T("\"%s\""), Get_Record(iRecord)->asString(iField)); break; default: Stream.Printf(SG_T("%s") , Get_Record(iRecord)->asString(iField)); break; } } Stream.Printf(SG_T("%s"), iField < Get_Field_Count() - 1 ? Separator : SG_T("\n")); } } SG_UI_Process_Set_Ready(); return( true ); } } return( false ); }
//--------------------------------------------------------- bool CPolygonStatisticsFromPoints::On_Execute(void) { //----------------------------------------------------- bool bSum = Parameters("SUM")->asBool(); bool bAvg = Parameters("AVG")->asBool(); bool bVar = Parameters("VAR")->asBool(); bool bDev = Parameters("DEV")->asBool(); bool bMin = Parameters("MIN")->asBool(); bool bMax = Parameters("MAX")->asBool(); bool bNum = Parameters("NUM")->asBool(); if( !bSum && !bAvg && !bVar && !bDev && !bMin && !bMax && !bNum ) { Error_Set(_TL("no target variable in selection")); return( false ); } //----------------------------------------------------- CSG_Parameter_Table_Fields *pFields = Parameters("FIELDS")->asTableFields(); if( pFields->Get_Count() <= 0 ) { Error_Set(_TL("no attributes in selection")); return( false ); } //----------------------------------------------------- CSG_Shapes *pPoints = Parameters("POINTS" )->asShapes(); CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); if( pPolygons->Get_Count() <= 0 || pPoints->Get_Count() <= 0 ) { Error_Set(_TL("no records in input data")); return( false ); } //----------------------------------------------------- if( Parameters("STATISTICS")->asShapes() == NULL ) { Parameters("STATISTICS")->Set_Value(pPolygons); } else if( pPolygons != Parameters("STATISTICS")->asShapes() ) { Parameters("STATISTICS")->asShapes()->Assign(pPolygons); pPolygons = Parameters("STATISTICS")->asShapes(); } //----------------------------------------------------- int i, n, Offset = pPolygons->Get_Field_Count(); for(i=0; i<pFields->Get_Count(); i++) { CSG_String sName = pPoints->Get_Field_Name(pFields->Get_Index(i)); if( bSum ) { pPolygons->Add_Field(Get_Field_Name("SUM", sName), SG_DATATYPE_Double); } if( bAvg ) { pPolygons->Add_Field(Get_Field_Name("AVG", sName), SG_DATATYPE_Double); } if( bVar ) { pPolygons->Add_Field(Get_Field_Name("VAR", sName), SG_DATATYPE_Double); } if( bDev ) { pPolygons->Add_Field(Get_Field_Name("DEV", sName), SG_DATATYPE_Double); } if( bMin ) { pPolygons->Add_Field(Get_Field_Name("MIN", sName), SG_DATATYPE_Double); } if( bMax ) { pPolygons->Add_Field(Get_Field_Name("MAX", sName), SG_DATATYPE_Double); } if( bNum ) { pPolygons->Add_Field(Get_Field_Name("NUM", sName), SG_DATATYPE_Long ); } } //----------------------------------------------------- CSG_Simple_Statistics *Statistics = new CSG_Simple_Statistics[pFields->Get_Count()]; for(int iPolygon=0; iPolygon<pPolygons->Get_Count() && Set_Progress(iPolygon, pPolygons->Get_Count()); iPolygon++) { CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); //------------------------------------------------- for(i=0; i<pFields->Get_Count(); i++) { Statistics[i].Invalidate(); } //------------------------------------------------- for(int iPoint=0; iPoint<pPoints->Get_Count() && Process_Get_Okay(); iPoint++) { CSG_Shape *pPoint = pPoints->Get_Shape(iPoint); if( pPolygon->Contains(pPoint->Get_Point(0)) ) { for(i=0; i<pFields->Get_Count(); i++) { if( !pPoint->is_NoData(pFields->Get_Index(i))) { Statistics[i].Add_Value(pPoint->asDouble(pFields->Get_Index(i))); } } } } //------------------------------------------------- for(i=0, n=Offset; i<pFields->Get_Count(); i++) { if( Statistics[i].Get_Count() > 0 ) { if( bSum ) { pPolygon->Set_Value (n++, Statistics[i].Get_Sum()); } if( bAvg ) { pPolygon->Set_Value (n++, Statistics[i].Get_Mean()); } if( bVar ) { pPolygon->Set_Value (n++, Statistics[i].Get_Variance()); } if( bDev ) { pPolygon->Set_Value (n++, Statistics[i].Get_StdDev()); } if( bMin ) { pPolygon->Set_Value (n++, Statistics[i].Get_Minimum()); } if( bMax ) { pPolygon->Set_Value (n++, Statistics[i].Get_Maximum()); } if( bNum ) { pPolygon->Set_Value (n++, (double)Statistics[i].Get_Count()); } } else { if( bSum ) { pPolygon->Set_NoData(n++); } if( bAvg ) { pPolygon->Set_NoData(n++); } if( bVar ) { pPolygon->Set_NoData(n++); } if( bDev ) { pPolygon->Set_NoData(n++); } if( bMin ) { pPolygon->Set_NoData(n++); } if( bMax ) { pPolygon->Set_NoData(n++); } if( bNum ) { pPolygon->Set_Value (n++, 0.0); } } } } //----------------------------------------------------- delete[](Statistics); DataObject_Update(pPolygons); return( true ); }
//--------------------------------------------------------- bool CSG_Table::Serialize(CSG_File &Stream, bool bSave) { SG_Char Separator = SG_T('\t'); int iField, nFields, iRecord, nRecords, FieldType; CSG_Table_Record *pRecord; CSG_String s, sLine; //----------------------------------------------------- if( bSave ) { Stream.Printf(SG_T("%d %d\n"), m_nFields, m_nRecords); for(iField=0; iField<m_nFields; iField++) { Stream.Printf(SG_T("%d \"%s\"\n"), Get_Field_Type(iField), Get_Field_Name(iField)); } for(iRecord=0; iRecord<m_nRecords; iRecord++) { for(iField=0; iField<m_nFields; iField++) { Stream.Printf(SG_T("%s%c"), Get_Record(iRecord)->asString(iField), iField < m_nFields - 1 ? Separator : '\n'); } } return( true ); } //----------------------------------------------------- else if( Stream.Read_Line(sLine) && SG_SSCANF(sLine, SG_T("%d %d"), &nFields, &nRecords) == 2 && nFields > 0 ) { _Destroy(); for(iField=0; iField<nFields; iField++) { if( Stream.Read_Line(sLine) && SG_SSCANF(sLine, SG_T("%d"), &FieldType) == 1 ) { Add_Field(sLine.AfterFirst(SG_T('\"')).BeforeFirst(SG_T('\"')), (TSG_Data_Type)FieldType); } } for(iRecord=0; iRecord<nRecords; iRecord++) { if( Stream.Read_Line(sLine) ) { pRecord = _Add_Record(); for(iField=0; iField<m_nFields; iField++) { pRecord->Set_Value(iField, sLine.BeforeFirst(Separator)); sLine = sLine.AfterFirst(Separator); } } } return( true ); } //----------------------------------------------------- return( false ); }
//--------------------------------------------------------- bool CSG_Table::_Save_DBase(const CSG_String &File_Name) { int iField, iRecord, nBytes; CSG_Table_DBase dbf; //----------------------------------------------------- CSG_Table_DBase::TFieldDesc *dbfFields = new CSG_Table_DBase::TFieldDesc[Get_Field_Count()]; for(iField=0; iField<Get_Field_Count(); iField++) { strncpy(dbfFields[iField].Name, SG_STR_SGTOMB(Get_Field_Name(iField)), 11); switch( Get_Field_Type(iField) ) { case SG_DATATYPE_String: default: dbfFields[iField].Type = DBF_FT_CHARACTER; dbfFields[iField].Width = (BYTE)((nBytes = Get_Field_Length(iField)) > 255 ? 255 : nBytes); break; case SG_DATATYPE_Date: dbfFields[iField].Type = DBF_FT_DATE; dbfFields[iField].Width = (BYTE)8; break; case SG_DATATYPE_Char: dbfFields[iField].Type = DBF_FT_CHARACTER; dbfFields[iField].Width = (BYTE)1; break; case SG_DATATYPE_Short: case SG_DATATYPE_Int: case SG_DATATYPE_Long: case SG_DATATYPE_Color: dbfFields[iField].Type = DBF_FT_NUMERIC; dbfFields[iField].Width = (BYTE)16; dbfFields[iField].Decimals = (BYTE)0; break; case SG_DATATYPE_Float: case SG_DATATYPE_Double: dbfFields[iField].Type = DBF_FT_NUMERIC; dbfFields[iField].Width = (BYTE)16; dbfFields[iField].Decimals = (BYTE)8; break; } } if( !dbf.Open(File_Name, Get_Field_Count(), dbfFields) ) { delete[](dbfFields); SG_UI_Msg_Add_Error(LNG("[ERR] dbase file could not be opened")); return( false ); } delete[](dbfFields); //----------------------------------------------------- for(iRecord=0; iRecord<Get_Record_Count() && SG_UI_Process_Set_Progress(iRecord, Get_Record_Count()); iRecord++) { CSG_Table_Record *pRecord = Get_Record(iRecord); dbf.Add_Record(); for(iField=0; iField<Get_Field_Count(); iField++) { switch( dbf.Get_FieldType(iField) ) { case DBF_FT_DATE: case DBF_FT_CHARACTER: dbf.Set_Value(iField, SG_STR_SGTOMB(pRecord->asString(iField))); break; case DBF_FT_NUMERIC: dbf.Set_Value(iField, pRecord->asDouble(iField)); break; } } dbf.Flush_Record(); } SG_UI_Process_Set_Ready(); return( true ); }