//--------------------------------------------------------- bool CTable_PCA::On_Execute(void) { CSG_Vector Eigen_Values; CSG_Matrix Eigen_Vectors, Matrix; //----------------------------------------------------- m_pTable = Parameters("TABLE") ->asTable(); m_Method = Parameters("METHOD") ->asInt(); //----------------------------------------------------- if( !Get_Fields() ) { Error_Set(_TL("invalid field selection")); SG_FREE_SAFE(m_Features); return( false ); } if( !Get_Matrix(Matrix) ) { Error_Set(_TL("matrix initialisation failed")); SG_FREE_SAFE(m_Features); return( false ); } if( !SG_Matrix_Eigen_Reduction(Matrix, Eigen_Vectors, Eigen_Values) ) { Error_Set(_TL("Eigen reduction failed")); SG_FREE_SAFE(m_Features); return( false ); } //----------------------------------------------------- Get_Components(Eigen_Vectors, Eigen_Values); //----------------------------------------------------- SG_FREE_SAFE(m_Features); return( true ); }
//--------------------------------------------------------- bool CGWR_Grid_Downscaling::On_Execute(void) { //----------------------------------------------------- CSG_Parameter_Grid_List *pPredictors = Parameters("PREDICTORS")->asGridList(); if( (m_nPredictors = pPredictors->Get_Count()) <= 0 ) { return( false ); } m_pDependent = Parameters("DEPENDENT")->asGrid(); if( !m_pDependent->Get_Extent().Intersects(Get_System()->Get_Extent()) ) { return( false ); } //----------------------------------------------------- int i; Process_Set_Text(_TL("upscaling of predictors")); m_pPredictors = (CSG_Grid **)SG_Calloc(m_nPredictors , sizeof(CSG_Grid *)); m_pModel = (CSG_Grid **)SG_Calloc(m_nPredictors + 1, sizeof(CSG_Grid *)); for(i=0; i<m_nPredictors; i++) { m_pPredictors[i] = SG_Create_Grid(m_pDependent->Get_System()); m_pPredictors[i] ->Assign(pPredictors->asGrid(i), GRID_INTERPOLATION_NearestNeighbour); // GRID_INTERPOLATION_Mean_Cells m_pModel [i] = SG_Create_Grid(m_pDependent->Get_System()); m_pModel [i] ->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pPredictors->asGrid(i)->Get_Name(), _TL("Factor"))); } m_pModel[m_nPredictors] = SG_Create_Grid(m_pDependent->Get_System()); m_pModel[m_nPredictors] ->Set_Name(_TL("Intercept")); //----------------------------------------------------- Process_Set_Text(_TL("model creation")); bool bResult = Get_Model(); //----------------------------------------------------- for(i=0; i<m_nPredictors; i++) { delete(m_pPredictors[i]); m_pPredictors[i] = pPredictors->asGrid(i); } //----------------------------------------------------- if( bResult ) { Process_Set_Text(_TL("downscaling")); bResult = Set_Model(); } //----------------------------------------------------- if( Parameters("MODEL_OUT")->asBool() ) { CSG_Parameter_Grid_List *pModel = Parameters("MODEL")->asGridList(); pModel->Del_Items(); pModel->Add_Item(m_pModel[m_nPredictors]); for(i=0; i<m_nPredictors; i++) { pModel->Add_Item(m_pModel[i]); } } else { for(i=0; i<=m_nPredictors; i++) { delete(m_pModel[i]); } } SG_FREE_SAFE(m_pModel); SG_FREE_SAFE(m_pPredictors); return( bResult ); }
//--------------------------------------------------------- bool CGW_Multi_Regression_Grid::On_Execute(void) { int i; //----------------------------------------------------- CSG_Parameter_Grid_List *pPredictors = Parameters("PREDICTORS")->asGridList(); if( !Initialize(Parameters("POINTS")->asShapes(), Parameters("DEPENDENT")->asInt(), pPredictors) ) { Finalize(); return( false ); } //----------------------------------------------------- CSG_Grid Quality; m_dimModel = *Get_System(); if( Parameters("RESOLUTION")->asInt() == 1 && Parameters("RESOLUTION_VAL")->asDouble() > Get_Cellsize() ) { CSG_Rect r(Get_System()->Get_Extent()); r.Inflate(0.5 * Parameters("RESOLUTION_VAL")->asDouble(), false); m_dimModel.Assign(Parameters("RESOLUTION_VAL")->asDouble(), r); Quality.Create(m_dimModel); m_pQuality = &Quality; } else { m_pQuality = Parameters("QUALITY")->asGrid(); } //----------------------------------------------------- Process_Set_Text(_TL("upsetting model domain")); m_pPredictors = (CSG_Grid **)SG_Calloc(m_nPredictors , sizeof(CSG_Grid *)); m_pModel = (CSG_Grid **)SG_Calloc(m_nPredictors + 1, sizeof(CSG_Grid *)); for(i=0; i<m_nPredictors; i++) { if( m_dimModel.Get_Cellsize() > Get_Cellsize() ) // scaling { m_pPredictors[i] = SG_Create_Grid(m_dimModel); m_pPredictors[i] ->Assign(pPredictors->asGrid(i), GRID_INTERPOLATION_NearestNeighbour); // GRID_INTERPOLATION_Mean_Cells } else { m_pPredictors[i] = pPredictors->asGrid(i); } m_pModel [i] = SG_Create_Grid(m_dimModel); m_pModel [i] ->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pPredictors->asGrid(i)->Get_Name(), _TL("Factor"))); } m_pModel[m_nPredictors] = SG_Create_Grid(m_dimModel); m_pModel[m_nPredictors] ->Set_Name(_TL("Intercept")); //----------------------------------------------------- Process_Set_Text(_TL("model creation")); bool bResult = Get_Model(); //----------------------------------------------------- if( m_dimModel.Get_Cellsize() > Get_Cellsize() ) // scaling { for(i=0; i<m_nPredictors; i++) { delete(m_pPredictors[i]); m_pPredictors[i] = pPredictors->asGrid(i); } } //----------------------------------------------------- if( bResult ) { Process_Set_Text(_TL("model application")); bResult = Set_Model(); } //----------------------------------------------------- if( Parameters("MODEL_OUT")->asBool() ) { CSG_Parameter_Grid_List *pModel = Parameters("MODEL")->asGridList(); pModel->Del_Items(); pModel->Add_Item(m_pModel[m_nPredictors]); for(i=0; i<m_nPredictors; i++) { pModel->Add_Item(m_pModel[i]); } } else { for(i=0; i<=m_nPredictors; i++) { delete(m_pModel[i]); } } SG_FREE_SAFE(m_pModel); SG_FREE_SAFE(m_pPredictors); Finalize(); return( bResult ); }
//--------------------------------------------------------- bool CShapes_Save::On_Execute(void) { if( !Get_Connection()->has_PostGIS() ) { Error_Set(_TL("not a valid PostGIS database!")); return( false ); } //----------------------------------------------------- CSG_Shapes *pShapes; CSG_String SQL, Name, Type, Field, SavePoint; pShapes = Parameters("SHAPES")->asShapes(); Name = Parameters("NAME" )->asString(); if( Name.Length() == 0 ) Name = pShapes->Get_Name(); Field = "geometry"; int SRID = Get_SRID(); //----------------------------------------------------- if( !CSG_Shapes_OGIS_Converter::from_ShapeType(Type, pShapes->Get_Type(), pShapes->Get_Vertex_Type()) ) { Error_Set(_TL("invalid or unsupported shape or vertex type")); return( false ); } //----------------------------------------------------- Get_Connection()->Begin(SavePoint = Get_Connection()->is_Transaction() ? "SHAPES_SAVE" : ""); //----------------------------------------------------- if( Get_Connection()->Table_Exists(Name) ) { Message_Add(_TL("table already exists") + CSG_String(": ") + Name); switch( Parameters("EXISTS")->asInt() ) { case 0: // abort export return( false ); case 1: // replace existing table Message_Add(_TL("trying to drop table") + CSG_String(": ") + Name); if( !Get_Connection()->Table_Drop(Name, false) ) { Message_Add(CSG_String(" ...") + _TL("failed") + "!"); return( false ); } break; case 2: // append records, if table structure allows break; } } //----------------------------------------------------- if( !Get_Connection()->Table_Exists(Name) ) { if( !Get_Connection()->Table_Create(Name, *pShapes, Get_Constraints(&Parameters, "SHAPES"), false) ) { Error_Set(_TL("could not create table")); Get_Connection()->Rollback(SavePoint); return( false ); } //------------------------------------------------- // SELECT AddGeometryColumn(<table_name>, <column_name>, <srid>, <type>, <dimension>) SQL.Printf(SG_T("SELECT AddGeometryColumn('%s', '%s', %d, '%s', %d)"), Name.c_str(), Field.c_str(), SRID, Type.c_str(), pShapes->Get_Vertex_Type() == SG_VERTEX_TYPE_XY ? 2 : pShapes->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZ ? 3 : 4 ); if( !Get_Connection()->Execute(SQL) ) { Error_Set(_TL("could not create geometry field")); Get_Connection()->Rollback(SavePoint); return( false ); } } //----------------------------------------------------- bool bBinary = Get_Connection()->has_Version(9); int iShape, iField, nAdded; CSG_String Insert = "INSERT INTO \"" + Name + "\" (" + Field; for(iField=0; iField<pShapes->Get_Field_Count(); iField++) { Insert += CSG_String(", ") + pShapes->Get_Field_Name(iField); } Insert += ") VALUES ("; //----------------------------------------------------- for(iShape=0, nAdded=0; iShape==nAdded && iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( pShape->is_Valid() ) { SQL = Insert; if( bBinary ) { CSG_Bytes WKB; CSG_Shapes_OGIS_Converter::to_WKBinary(pShape, WKB); SQL += "ST_GeomFromWKB(E'\\\\x" + WKB.toHexString() + CSG_String::Format("', %d)", SRID); } else { CSG_String WKT; CSG_Shapes_OGIS_Converter::to_WKText(pShape, WKT); SQL += "ST_GeomFromText('" + WKT + CSG_String::Format("', %d)", SRID); } for(iField=0; iField<pShapes->Get_Field_Count(); iField++) { CSG_String s = pShape->asString(iField); if( pShapes->Get_Field_Type(iField) == SG_DATATYPE_String ) { if( 1 ) { char *_s = NULL; if( s.to_ASCII(&_s) ) s = _s; SG_FREE_SAFE(_s); } s.Replace("'", "\""); s = "'" + s + "'"; } SQL += ", " + s; } SQL += ")"; if( Get_Connection()->Execute(SQL) ) { nAdded++; } else { Message_Add(CSG_String::Format("%s [%d/%d]", _TL("could not save shape"), 1 + iShape, pShapes->Get_Count())); } } } //----------------------------------------------------- if( nAdded < pShapes->Get_Count() ) { Message_Add(SQL); Get_Connection()->Rollback(SavePoint); return( false ); } Get_Connection()->Commit(SavePoint); Get_Connection()->GUI_Update(); Get_Connection()->Add_MetaData(*pShapes, Name); pShapes->Set_Modified(false); return( true ); }