//--------------------------------------------------------- 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 ); }
//--------------------------------------------------------- bool CTable_List::On_Execute(void) { CSG_Table *pTables = Parameters("TABLES")->asTable(); pTables->Destroy(); pTables->Set_Name(_TL("Tables")); pTables->Add_Field(_TL("Table"), SG_DATATYPE_String); if( Get_Connection() ) { CSG_Strings Tables; Get_Connection()->Get_Tables(Tables); for(int i=0; i<Tables.Get_Count(); i++) { CSG_Table_Record *pTable = pTables->Add_Record(); pTable->Set_Value(0, Tables[i]); } pTables->Set_Name(Get_Connection()->Get_Server() + " [" + _TL("Tables") + "]"); return( true ); } return( false ); }
//--------------------------------------------------------- bool CTable_Drop::On_Execute(void) { if( Get_Connection()->Table_Drop(Parameters("TABLES")->asChoice()->asString()) ) { SG_UI_ODBC_Update(Get_Connection()->Get_Server()); return( true ); } return( false ); }
//--------------------------------------------------------- void CTable_Drop::On_Connection_Changed(CSG_Parameters *pParameters) { CSG_Parameter *pParameter = pParameters->Get_Parameter("TABLES"); pParameter->asChoice()->Set_Items(Get_Connection()->Get_Tables()); pParameter->Set_Value(pParameter->asString()); }
//--------------------------------------------------------- bool CPGIS_Shapes_Save::On_Before_Execution(void) { if( !CSG_ODBC_Module::On_Before_Execution() ) { return( false ); } if( !Get_Connection()->is_PostgreSQL() ) { SG_UI_Dlg_Message(_TL("Not a PostgreSQL database!"), _TL("Database Connection Error")); return( false ); } if( !Get_Connection()->Table_Exists(SG_T("spatial_ref_sys")) || !Get_Connection()->Table_Exists(SG_T("geometry_columns")) ) { SG_UI_Dlg_Message(_TL("Not a valid PostGIS database!"), _TL("Database Connection Error")); return( false ); } Parameters("SRID")->asChoice()->Set_Items(SG_Get_Projections().Get_Names()); /* if( Parameters("SRID")->asChoice()->Get_Count() > 1 ) return( true ); CSG_Table SRIDs; if( !Get_Connection()->Table_Load(SRIDs, SG_T("spatial_ref_sys")) || SRIDs.Get_Count() == 0 ) { return( false ); } CSG_String s, sID; for(int i=0; i<SRIDs.Get_Count(); i++) { sID = SRIDs[i].asString(3); s += sID.AfterFirst('\"').BeforeFirst('\"') + SG_T("|"); } Parameters("SRID")->asChoice()->Set_Items(s); /**/ return( true ); }
//--------------------------------------------------------- bool CTable_Info::On_Execute(void) { CSG_String Table = Parameters("TABLES")->asString(); CSG_Table *pTable = Parameters("TABLE" )->asTable(); pTable->Create(Get_Connection()->Get_Field_Desc(Table, Parameters("VERBOSE")->asBool())); pTable->Set_Name(Table + " [" + _TL("Field Description") + "]"); return( true ); }
//--------------------------------------------------------- bool CTable_Info::On_Execute(void) { CSG_String Table = Parameters("TABLES")->asString(); CSG_Table *pTable = Parameters("TABLE" )->asTable(); CSG_Table tab = Get_Connection()->Get_Field_Desc(Table); pTable->Assign(&tab); pTable->Set_Name(Table + " [" + _TL("Field Description") + "]"); return( true ); }
//--------------------------------------------------------- bool CTable_Query::On_Execute(void) { CSG_Table *pTable = Parameters("TABLE" )->asTable (); CSG_String Tables = Parameters("TABLES" )->asString(); CSG_String Fields = Parameters("FIELDS" )->asString(); CSG_String Where = Parameters("WHERE" )->asString(); CSG_String Group = Parameters("GROUP" )->asString(); CSG_String Having = Parameters("HAVING" )->asString(); CSG_String Order = Parameters("ORDER" )->asString(); bool bDistinct = Parameters("DISTINCT")->asBool (); return( Get_Connection()->Table_Load(*pTable, Tables, Fields, Where, Group, Having, Order, bDistinct) ); }
//--------------------------------------------------------- bool CPGIS_Shapes_Load::On_Before_Execution(void) { if( !CSG_ODBC_Module::On_Before_Execution() ) { return( false ); } if( !Get_Connection()->is_PostgreSQL() ) { SG_UI_Dlg_Message(_TL("Not a PostgreSQL database!"), _TL("Database Connection Error")); return( false ); } if( !Get_Connection()->Table_Exists(SG_T("spatial_ref_sys")) || !Get_Connection()->Table_Exists(SG_T("geometry_columns")) ) { SG_UI_Dlg_Message(_TL("Not a valid PostGIS database!"), _TL("Database Connection Error")); return( false ); } CSG_Table Geo_Tables; if( !Get_Connection()->Table_Load(Geo_Tables, SG_T("geometry_columns")) ) { return( false ); } CSG_String s; for(int i=0; i<Geo_Tables.Get_Count(); i++) { s += Geo_Tables[i].asString(SG_T("f_table_name")) + CSG_String("|"); } Parameters("TABLES")->asChoice()->Set_Items(s); return( true ); }
//--------------------------------------------------------- bool CTable_Query_GUI::On_Execute(void) { return( Get_Connection()->Table_Load( *Parameters ("TABLE" )->asTable(), Get_Selection("TABLES" ), Get_Selection("FIELDS" ), Parameters ("WHERE" )->asString(), Get_Selection("GROUP" ), Parameters ("HAVING" )->asString(), Parameters ("ORDER" )->asString(), Parameters ("DISTINCT")->asBool () )); }
//--------------------------------------------------------- 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 ); }
//--------------------------------------------------------- bool CShapes_Load::On_Execute(void) { CSG_Shapes *pShapes = Parameters("SHAPES")->asShapes(); CSG_String Name = Parameters("TABLES")->asString(); if( !Get_Connection()->Shapes_Load(pShapes, Name) ) { Error_Set(_TL("unable to load vector data from PostGIS database") + CSG_String(":\n") + Name); return( false ); } return( true ); }
//--------------------------------------------------------- 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 CTable_Save::On_Execute(void) { bool bResult = false; CSG_Table *pTable = Parameters("TABLE")->asTable(); CSG_String Name = Parameters("NAME" )->asString(); if( Name.Length() == 0 ) Name = pTable->Get_Name(); //----------------------------------------------------- if( Get_Connection()->Table_Exists(Name) ) { Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("table already exists"), Name.c_str())); switch( Parameters("EXISTS")->asInt() ) { case 0: // abort export break; case 1: // replace existing table Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("dropping table"), Name.c_str())); if( !Get_Connection()->Table_Drop(Name, false) ) { Message_Add(CSG_String::Format(SG_T(" ...%s!"), _TL("failed"))); } else { bResult = Get_Connection()->Table_Save(Name, *pTable, Get_Constraints(Parameters("FLAGS")->asParameters(), pTable)); } break; case 2: // append records, if table structure allows Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("appending to existing table"), Name.c_str())); if( !(bResult = Get_Connection()->Table_Insert(Name, *pTable)) ) { Message_Add(CSG_String::Format(SG_T(" ...%s!"), _TL("failed"))); } break; } } else { bResult = Get_Connection()->Table_Save(Name, *pTable, Get_Constraints(Parameters("FLAGS")->asParameters(), pTable)); } //----------------------------------------------------- if( bResult ) { SG_UI_ODBC_Update(Get_Connection()->Get_Server()); } return( bResult ); }
//--------------------------------------------------------- void CTable_Query_GUI::On_Connection_Changed(CSG_Parameters *pParameters) { CSG_Parameters *pTables = pParameters->Get_Parameter("TABLES")->asParameters(); pTables->Del_Parameters(); CSG_Strings Tables; if( Get_Connection()->Get_Tables(Tables) > 0 ) { for(int i=0; i<Tables.Get_Count(); i++) { pTables->Add_Bool("", Tables[i], Tables[i], ""); } } pParameters->Get_Parameter("FIELDS")->asParameters()->Del_Parameters(); pParameters->Get_Parameter("GROUP" )->asParameters()->Del_Parameters(); On_Parameter_Changed(pParameters, pParameters->Get_Parameter("TABLES")); }
//--------------------------------------------------------- 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) ); }
//--------------------------------------------------------- bool CPGIS_Shapes_Load::On_Execute(void) { TSG_Vertex_Type tVertex; TSG_Shape_Type tShape; CSG_String Select, Geo_Table, Geo_Type, Geo_Field; CSG_Table Geo_Tables; CSG_Shapes *pShapes; pShapes = Parameters("SHAPES") ->asShapes(); Geo_Table = Parameters("TABLES") ->asString(); Select.Printf(SG_T("SELECT * FROM geometry_columns WHERE f_table_name = '%s'"), Geo_Table.c_str()); if( !Get_Connection()->Table_Load(Geo_Tables, SG_T("geometry_columns"), SG_T("*"), CSG_String::Format(SG_T("f_table_name = '%s'"), Geo_Table.c_str()), SG_T("")) ) { return( false ); } if( Geo_Tables.Get_Count() != 1 ) { return( false ); } Geo_Type = Geo_Tables[0].asString(SG_T("type")); Geo_Field = Geo_Tables[0].asString(SG_T("f_geometry_column")); //----------------------------------------------------- if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Point ) ) { tShape = SHAPE_TYPE_Point; tVertex = SG_VERTEX_TYPE_XY; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPoint ) ) { tShape = SHAPE_TYPE_Points; tVertex = SG_VERTEX_TYPE_XY; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Line ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XY; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiLine ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XY; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Polygon ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XY; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPolygon ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XY; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Point_Z ) ) { tShape = SHAPE_TYPE_Point; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPoint_Z ) ) { tShape = SHAPE_TYPE_Points; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Line_Z ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiLine_Z ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Polygon_Z ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPolygon_Z ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Point_M ) ) { tShape = SHAPE_TYPE_Point; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPoint_M ) ) { tShape = SHAPE_TYPE_Points; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Line_M ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiLine_M ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Polygon_M ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPolygon_M ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XYZ; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Point_ZM ) ) { tShape = SHAPE_TYPE_Point; tVertex = SG_VERTEX_TYPE_XYZM; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPoint_ZM ) ) { tShape = SHAPE_TYPE_Points; tVertex = SG_VERTEX_TYPE_XYZM; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Line_ZM ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XYZM; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiLine_ZM ) ) { tShape = SHAPE_TYPE_Line; tVertex = SG_VERTEX_TYPE_XYZM; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_Polygon_ZM ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XYZM; } else if( !Geo_Type.CmpNoCase(SG_OGIS_TYPE_STR_MultiPolygon_ZM ) ) { tShape = SHAPE_TYPE_Polygon; tVertex = SG_VERTEX_TYPE_XYZM; } else { return( false ); } if( pShapes->Get_Type() != SHAPE_TYPE_Undefined && pShapes->Get_Type() != tShape ) { pShapes = SG_Create_Shapes(tShape, Geo_Table, NULL, tVertex); Parameters("SHAPES")->Set_Value(pShapes); } else { pShapes->Create(tShape, Geo_Table, NULL, tVertex); } //----------------------------------------------------- if( !Get_Connection()->Table_Load(*pShapes, Geo_Table) ) { return( false ); } //----------------------------------------------------- if( 1 ) // WKBinary { CSG_Bytes_Array BLOBs; Select.Printf(SG_T("AsBinary(%s) AS geom"), Geo_Field.c_str()); if( !Get_Connection()->Table_Load_BLOBs(BLOBs, Geo_Table, Select, SG_T(""), SG_T("")) ) { return( false ); } if( BLOBs.Get_Count() != pShapes->Get_Count() ) { return( false ); } for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shapes_OGIS_Converter::from_WKBinary(BLOBs[iShape], pShapes->Get_Shape(iShape)); } } //----------------------------------------------------- else // WKText { CSG_Table Shapes; Select.Printf(SG_T("AsText(%s) AS geom"), Geo_Field.c_str()); if( !Get_Connection()->Table_Load(Shapes, Geo_Table, Select, SG_T(""), SG_T("")) ) { return( false ); } if( Shapes.Get_Count() != pShapes->Get_Count() ) { return( false ); } for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shapes_OGIS_Converter::from_WKText(Shapes[iShape].asString(0), pShapes->Get_Shape(iShape)); } } return( true ); }
//--------------------------------------------------------- bool CTable_Load::On_Execute(void) { CSG_Table *pTable = Parameters("TABLE")->asTable(); return( Get_Connection()->Table_Load(*pTable, Parameters("TABLES")->asString()) ); }
//--------------------------------------------------------- bool CPGIS_Shapes_Save::On_Execute(void) { int SRID; CSG_Shapes *pShapes; CSG_String SQL, Geo_Table, Geo_Type, Geo_Field, sSRID; pShapes = Parameters("SHAPES") ->asShapes(); Geo_Table = Parameters("NAME") ->asString(); if( Geo_Table.Length() == 0 ) Geo_Table = pShapes->Get_Name(); SRID = Parameters("SRID") ->asInt(); SRID = SG_Get_Projections().Get_SRID_byNamesIndex(SRID); sSRID.Printf(SG_T("%d"), SRID); //----------------------------------------------------- switch( pShapes->Get_Vertex_Type() ) { case SG_VERTEX_TYPE_XY: switch( pShapes->Get_Type() ) { default: return( false ); case SHAPE_TYPE_Point: Geo_Type = SG_OGIS_TYPE_STR_Point; Geo_Field = SG_T("geo_point"); break; case SHAPE_TYPE_Points: Geo_Type = SG_OGIS_TYPE_STR_MultiPoint; Geo_Field = SG_T("geo_points"); break; case SHAPE_TYPE_Line: Geo_Type = SG_OGIS_TYPE_STR_MultiLine; Geo_Field = SG_T("geo_line"); break; case SHAPE_TYPE_Polygon: Geo_Type = SG_OGIS_TYPE_STR_MultiPolygon; Geo_Field = SG_T("geo_polygon"); break; } break; case SG_VERTEX_TYPE_XYZ: switch( pShapes->Get_Type() ) { default: return( false ); case SHAPE_TYPE_Point: Geo_Type = SG_OGIS_TYPE_STR_Point_Z; Geo_Field = SG_T("geo_point_z"); break; case SHAPE_TYPE_Points: Geo_Type = SG_OGIS_TYPE_STR_MultiPoint_Z; Geo_Field = SG_T("geo_points_z"); break; case SHAPE_TYPE_Line: Geo_Type = SG_OGIS_TYPE_STR_MultiLine_Z; Geo_Field = SG_T("geo_line_z"); break; case SHAPE_TYPE_Polygon: Geo_Type = SG_OGIS_TYPE_STR_MultiPolygon_Z; Geo_Field = SG_T("geo_polygon_z"); break; } break; case SG_VERTEX_TYPE_XYZM: switch( pShapes->Get_Type() ) { default: return( false ); case SHAPE_TYPE_Point: Geo_Type = SG_OGIS_TYPE_STR_Point_ZM; Geo_Field = SG_T("geo_point_zm"); break; case SHAPE_TYPE_Points: Geo_Type = SG_OGIS_TYPE_STR_MultiPoint_ZM; Geo_Field = SG_T("geo_points_zm"); break; case SHAPE_TYPE_Line: Geo_Type = SG_OGIS_TYPE_STR_MultiLine_ZM; Geo_Field = SG_T("geo_line_zm"); break; case SHAPE_TYPE_Polygon: Geo_Type = SG_OGIS_TYPE_STR_MultiPolygon_ZM; Geo_Field = SG_T("geo_polygon_zm"); break; } break; } //----------------------------------------------------- if( Get_Connection()->Table_Exists(Geo_Table) ) { Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("table already exists"), Geo_Table.c_str())); switch( Parameters("EXISTS")->asInt() ) { case 0: // abort export return( false ); case 1: // replace existing table Message_Add(CSG_String::Format(SG_T("%s: %s"), _TL("trying to drop table"), Geo_Table.c_str())); if( !Get_Connection()->Table_Drop(Geo_Table, false) ) { Message_Add(CSG_String::Format(SG_T(" ...%s!"), _TL("failed"))); return( false ); } break; case 2: // append records, if table structure allows break; } } //----------------------------------------------------- if( !Get_Connection()->Table_Exists(Geo_Table) && !Get_Connection()->Table_Create(Geo_Table, *pShapes, Get_Constraints(Parameters("FLAGS")->asParameters(), pShapes), false) ) { Get_Connection()->Rollback(); return( false ); } //----------------------------------------------------- SQL.Printf(SG_T("SELECT AddGeometryColumn('%s', '%s', %d, '%s', %d)"), Geo_Table.c_str(), // <table_name> Geo_Field.c_str(), // <column_name> SRID, // <srid> Geo_Type.Make_Upper().c_str(), // <type> 2 // <dimension> ); if( !Get_Connection()->Execute(SQL) ) { Get_Connection()->Rollback(); Message_Add(_TL("could not create geometry field")); return( false ); } //----------------------------------------------------- int iShape, iField, nAdded; CSG_String Insert, Fields, sWKT; Fields = Geo_Field; for(iField=0; iField<pShapes->Get_Field_Count(); iField++) { Fields += CSG_String(", ") + pShapes->Get_Field_Name(iField); } Insert.Printf(SG_T("INSERT INTO %s (%s) VALUES ("), Geo_Table.c_str(), Fields.c_str()); for(iShape=0, nAdded=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( pShape->is_Valid() ) { SQL = Insert; CSG_Shapes_OGIS_Converter::to_WKText(pShape, sWKT); SQL += SG_T("GeomFromText('") + sWKT + SG_T("', ") + sSRID + SG_T(")"); for(iField=0; iField<pShapes->Get_Field_Count(); iField++) { CSG_String s = pShape->asString(iField); if( pShapes->Get_Field_Type(iField) == SG_DATATYPE_String ) { s.Replace(SG_T("'"), SG_T("\"")); s = SG_T("'") + s + SG_T("'"); } SQL += SG_T(", ") + s; } SQL += SG_T(")"); if( Get_Connection()->Execute(SQL) ) { nAdded++; } else { Message_Add(CSG_String::Format(SG_T("dropped %d. shape"), iShape)); } } } //----------------------------------------------------- if( nAdded == 0 ) { Get_Connection()->Rollback(); Get_Connection()->Table_Drop(Geo_Table); return( false ); } return( Get_Connection()->Commit() ); }
//--------------------------------------------------------- bool CTable_Save::On_Execute(void) { bool bResult = false; CSG_Table *pTable = Parameters("TABLE")->asTable(); CSG_String Name = Parameters("NAME" )->asString(); if( Name.Length() == 0 ) Name = pTable->Get_Name(); //----------------------------------------------------- CSG_String SavePoint; Get_Connection()->Begin(SavePoint = Get_Connection()->is_Transaction() ? "SHAPES_SAVE" : ""); //----------------------------------------------------- if( Get_Connection()->Table_Exists(Name) ) { Message_Fmt("\n%s: %s", _TL("table already exists"), Name.c_str()); switch( Parameters("EXISTS")->asInt() ) { case 0: // abort export break; case 1: // replace existing table Message_Fmt("\n%s: %s", _TL("dropping table"), Name.c_str()); if( !Get_Connection()->Table_Drop(Name, false) ) { Message_Fmt("...%s!", _TL("failed")); } else { bResult = Get_Connection()->Table_Save(Name, *pTable, Get_Constraints(&Parameters, "TABLE")); } break; case 2: // append records, if table structure allows Message_Fmt("\n%s: %s", _TL("appending to existing table"), Name.c_str()); if( !(bResult = Get_Connection()->Table_Insert(Name, *pTable)) ) { Message_Fmt("...%s!", _TL("failed")); } break; } } else { bResult = Get_Connection()->Table_Save(Name, *pTable, Get_Constraints(&Parameters, "TABLE")); } //----------------------------------------------------- if( !bResult ) { Get_Connection()->Rollback(SavePoint); } else { Get_Connection()->Commit(SavePoint); Get_Connection()->GUI_Update(); } 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 ); }