OGRErr OGRLayerWithTransaction::AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags ) { if( !m_poDecoratedLayer ) return OGRERR_FAILURE; OGRErr eErr = m_poDecoratedLayer->AlterFieldDefn(iField, poNewFieldDefn, nFlags); if( m_poFeatureDefn && eErr == OGRERR_NONE ) { OGRFieldDefn* poSrcFieldDefn = m_poDecoratedLayer->GetLayerDefn()->GetFieldDefn(iField); OGRFieldDefn* poDstFieldDefn = m_poFeatureDefn->GetFieldDefn(iField); poDstFieldDefn->SetName(poSrcFieldDefn->GetNameRef()); poDstFieldDefn->SetType(poSrcFieldDefn->GetType()); poDstFieldDefn->SetSubType(poSrcFieldDefn->GetSubType()); poDstFieldDefn->SetWidth(poSrcFieldDefn->GetWidth()); poDstFieldDefn->SetPrecision(poSrcFieldDefn->GetPrecision()); poDstFieldDefn->SetDefault(poSrcFieldDefn->GetDefault()); poDstFieldDefn->SetNullable(poSrcFieldDefn->IsNullable()); } return eErr; }
static void ReportOnLayer( OGRLayer * poLayer, int bVerbose ) { OGRFeatureDefn *poDefn = poLayer->GetLayerDefn(); /* -------------------------------------------------------------------- */ /* Report various overall information. */ /* -------------------------------------------------------------------- */ printf( "\n" ); printf( "Layer name: %s\n", poLayer->GetName() ); if( bVerbose ) { int nGeomFieldCount = poLayer->GetLayerDefn()->GetGeomFieldCount(); if( nGeomFieldCount > 1 ) { for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); printf( "Geometry (%s): %s\n", poGFldDefn->GetNameRef(), OGRGeometryTypeToName( poGFldDefn->GetType() ) ); } } else { printf( "Geometry: %s\n", OGRGeometryTypeToName( poLayer->GetGeomType() ) ); } printf( "Feature Count: " CPL_FRMT_GIB "\n", poLayer->GetFeatureCount() ); OGREnvelope oExt; if( nGeomFieldCount > 1 ) { for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { if (poLayer->GetExtent(iGeom, &oExt, TRUE) == OGRERR_NONE) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); CPLprintf("Extent (%s): (%f, %f) - (%f, %f)\n", poGFldDefn->GetNameRef(), oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY); } } } else if ( poLayer->GetExtent(&oExt, TRUE) == OGRERR_NONE) { CPLprintf("Extent: (%f, %f) - (%f, %f)\n", oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY); } char *pszWKT; if( nGeomFieldCount > 1 ) { for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); OGRSpatialReference* poSRS = poGFldDefn->GetSpatialRef(); if( poSRS == NULL ) pszWKT = CPLStrdup( "(unknown)" ); else { poSRS->exportToPrettyWkt( &pszWKT ); } printf( "SRS WKT (%s):\n%s\n", poGFldDefn->GetNameRef(), pszWKT ); CPLFree( pszWKT ); } } else { if( poLayer->GetSpatialRef() == NULL ) pszWKT = CPLStrdup( "(unknown)" ); else { poLayer->GetSpatialRef()->exportToPrettyWkt( &pszWKT ); } printf( "Layer SRS WKT:\n%s\n", pszWKT ); CPLFree( pszWKT ); } if( strlen(poLayer->GetFIDColumn()) > 0 ) printf( "FID Column = %s\n", poLayer->GetFIDColumn() ); for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); if( nGeomFieldCount == 1 && EQUAL(poGFldDefn->GetNameRef(), "") && poGFldDefn->IsNullable() ) break; printf( "Geometry Column "); if( nGeomFieldCount > 1 ) printf("%d ", iGeom + 1); if( !poGFldDefn->IsNullable() ) printf("NOT NULL "); printf("= %s\n", poGFldDefn->GetNameRef() ); } for( int iAttr = 0; iAttr < poDefn->GetFieldCount(); iAttr++ ) { OGRFieldDefn *poField = poDefn->GetFieldDefn( iAttr ); const char* pszType = (poField->GetSubType() != OFSTNone) ? CPLSPrintf("%s(%s)", poField->GetFieldTypeName( poField->GetType() ), poField->GetFieldSubTypeName(poField->GetSubType())) : poField->GetFieldTypeName( poField->GetType() ); printf( "%s: %s (%d.%d)", poField->GetNameRef(), pszType, poField->GetWidth(), poField->GetPrecision() ); if( !poField->IsNullable() ) printf(" NOT NULL"); if( poField->GetDefault() != NULL ) printf(" DEFAULT %s", poField->GetDefault() ); printf( "\n" ); } } /* -------------------------------------------------------------------- */ /* Read, and dump features. */ /* -------------------------------------------------------------------- */ OGRFeature *poFeature = NULL; while( (poFeature = poLayer->GetNextFeature()) != NULL ) { poFeature->DumpReadable( NULL ); OGRFeature::DestroyFeature( poFeature ); } }
OGRErr OGRCARTODBTableLayer::RunDeferedCreationIfNecessary() { if( !bDeferedCreation ) return OGRERR_NONE; bDeferedCreation = FALSE; CPLString osSQL; osSQL.Printf("CREATE TABLE %s ( %s SERIAL,", OGRCARTODBEscapeIdentifier(osName).c_str(), osFIDColName.c_str()); int nSRID = 0; OGRwkbGeometryType eGType = GetGeomType(); if( eGType != wkbNone ) { CPLString osGeomType = OGRToOGCGeomType(eGType); if( wkbHasZ(eGType) ) osGeomType += "Z"; OGRCartoDBGeomFieldDefn *poFieldDefn = (OGRCartoDBGeomFieldDefn *)poFeatureDefn->GetGeomFieldDefn(0); nSRID = poFieldDefn->nSRID; osSQL += CPLSPrintf("%s GEOMETRY(%s, %d)%s, %s GEOMETRY(%s, %d),", "the_geom", osGeomType.c_str(), nSRID, (!poFieldDefn->IsNullable()) ? " NOT NULL" : "", "the_geom_webmercator", osGeomType.c_str(), 3857); } for( int i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { OGRFieldDefn* poFieldDefn = poFeatureDefn->GetFieldDefn(i); if( strcmp(poFieldDefn->GetNameRef(), osFIDColName) != 0 ) { osSQL += OGRCARTODBEscapeIdentifier(poFieldDefn->GetNameRef()); osSQL += " "; osSQL += OGRPGCommonLayerGetType(*poFieldDefn, FALSE, TRUE); if( !poFieldDefn->IsNullable() ) osSQL += " NOT NULL"; if( poFieldDefn->GetDefault() != NULL && !poFieldDefn->IsDefaultDriverSpecific() ) { osSQL += " DEFAULT "; osSQL += poFieldDefn->GetDefault(); } osSQL += ","; } } osSQL += CPLSPrintf("PRIMARY KEY (%s) )", osFIDColName.c_str()); CPLString osSeqName(OGRCARTODBEscapeIdentifier(CPLSPrintf("%s_%s_seq", osName.c_str(), osFIDColName.c_str()))); osSQL += ";"; osSQL += CPLSPrintf("DROP SEQUENCE IF EXISTS %s CASCADE", osSeqName.c_str()); osSQL += ";"; osSQL += CPLSPrintf("CREATE SEQUENCE %s START 1", osSeqName.c_str()); osSQL += ";"; osSQL += CPLSPrintf("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT nextval('%s')", OGRCARTODBEscapeIdentifier(osName).c_str(), osFIDColName.c_str(), osSeqName.c_str()); json_object* poObj = poDS->RunSQL(osSQL); if( poObj == NULL ) return OGRERR_FAILURE; json_object_put(poObj); if( bCartoDBify ) { if( nSRID != 4326 ) { if( eGType != wkbNone ) { CPLError(CE_Warning, CPLE_AppDefined, "Cannot register table in dashboard with " "cdb_cartodbfytable() since its SRS is not EPSG:4326"); } } else { if( poDS->GetCurrentSchema() == "public" ) osSQL.Printf("SELECT cdb_cartodbfytable('%s')", OGRCARTODBEscapeLiteral(osName).c_str()); else osSQL.Printf("SELECT cdb_cartodbfytable('%s', '%s')", OGRCARTODBEscapeLiteral(poDS->GetCurrentSchema()).c_str(), OGRCARTODBEscapeLiteral(osName).c_str()); poObj = poDS->RunSQL(osSQL); if( poObj == NULL ) return OGRERR_FAILURE; json_object_put(poObj); } } return OGRERR_NONE; }
static void ReportOnLayer( OGRLayer * poLayer, const char *pszWHERE, const char* pszGeomField, OGRGeometry *poSpatialFilter, bool bListMDD, bool bShowMetadata, char** papszExtraMDDomains, bool bFeatureCount, bool bExtent, const char* pszWKTFormat ) { OGRFeatureDefn *poDefn = poLayer->GetLayerDefn(); /* -------------------------------------------------------------------- */ /* Set filters if provided. */ /* -------------------------------------------------------------------- */ if( pszWHERE != nullptr ) { if( poLayer->SetAttributeFilter(pszWHERE) != OGRERR_NONE ) { printf("FAILURE: SetAttributeFilter(%s) failed.\n", pszWHERE); exit(1); } } if( poSpatialFilter != nullptr ) { if( pszGeomField != nullptr ) { const int iGeomField = poDefn->GetGeomFieldIndex(pszGeomField); if( iGeomField >= 0 ) poLayer->SetSpatialFilter(iGeomField, poSpatialFilter); else printf("WARNING: Cannot find geometry field %s.\n", pszGeomField); } else { poLayer->SetSpatialFilter(poSpatialFilter); } } /* -------------------------------------------------------------------- */ /* Report various overall information. */ /* -------------------------------------------------------------------- */ if( !bSuperQuiet ) { printf("\n"); printf("Layer name: %s\n", poLayer->GetName()); } GDALInfoReportMetadata(static_cast<GDALMajorObjectH>(poLayer), bListMDD, bShowMetadata, papszExtraMDDomains); if( bVerbose ) { const int nGeomFieldCount = poLayer->GetLayerDefn()->GetGeomFieldCount(); if( nGeomFieldCount > 1 ) { for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); printf("Geometry (%s): %s\n", poGFldDefn->GetNameRef(), OGRGeometryTypeToName(poGFldDefn->GetType())); } } else { printf("Geometry: %s\n", OGRGeometryTypeToName(poLayer->GetGeomType())); } if( bFeatureCount ) printf("Feature Count: " CPL_FRMT_GIB "\n", poLayer->GetFeatureCount()); OGREnvelope oExt; if( bExtent && nGeomFieldCount > 1 ) { for( int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { if( poLayer->GetExtent(iGeom, &oExt, TRUE) == OGRERR_NONE ) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); CPLprintf("Extent (%s): (%f, %f) - (%f, %f)\n", poGFldDefn->GetNameRef(), oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY); } } } else if( bExtent && poLayer->GetExtent(&oExt, TRUE) == OGRERR_NONE ) { CPLprintf("Extent: (%f, %f) - (%f, %f)\n", oExt.MinX, oExt.MinY, oExt.MaxX, oExt.MaxY); } const auto displayDataAxisMapping = [](const OGRSpatialReference* poSRS) { const auto mapping = poSRS->GetDataAxisToSRSAxisMapping(); printf("Data axis to CRS axis mapping: "); for( size_t i = 0; i < mapping.size(); i++ ) { if( i > 0 ) { printf(","); } printf("%d", mapping[i]); } printf("\n"); }; if( nGeomFieldCount > 1 ) { for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); OGRSpatialReference* poSRS = poGFldDefn->GetSpatialRef(); char *pszWKT = nullptr; if( poSRS == nullptr ) { pszWKT = CPLStrdup("(unknown)"); } else { CPLString osWKTFormat("FORMAT="); osWKTFormat += pszWKTFormat; const char* const apszWKTOptions[] = { osWKTFormat.c_str(), "MULTILINE=YES", nullptr }; poSRS->exportToWkt(&pszWKT, apszWKTOptions); } printf("SRS WKT (%s):\n%s\n", poGFldDefn->GetNameRef(), pszWKT); CPLFree(pszWKT); if( poSRS ) { displayDataAxisMapping(poSRS); } } } else { char *pszWKT = nullptr; auto poSRS = poLayer->GetSpatialRef(); if( poSRS == nullptr ) { pszWKT = CPLStrdup("(unknown)"); } else { poSRS->exportToPrettyWkt(&pszWKT); } printf("Layer SRS WKT:\n%s\n", pszWKT); CPLFree(pszWKT); if( poSRS ) { displayDataAxisMapping(poSRS); } } if( strlen(poLayer->GetFIDColumn()) > 0 ) printf("FID Column = %s\n", poLayer->GetFIDColumn()); for(int iGeom = 0;iGeom < nGeomFieldCount; iGeom ++ ) { OGRGeomFieldDefn* poGFldDefn = poLayer->GetLayerDefn()->GetGeomFieldDefn(iGeom); if( nGeomFieldCount == 1 && EQUAL(poGFldDefn->GetNameRef(), "") && poGFldDefn->IsNullable() ) break; printf("Geometry Column "); if( nGeomFieldCount > 1 ) printf("%d ", iGeom + 1); if( !poGFldDefn->IsNullable() ) printf("NOT NULL "); printf("= %s\n", poGFldDefn->GetNameRef()); } for( int iAttr = 0; iAttr < poDefn->GetFieldCount(); iAttr++ ) { OGRFieldDefn *poField = poDefn->GetFieldDefn(iAttr); const char* pszType = (poField->GetSubType() != OFSTNone) ? CPLSPrintf( "%s(%s)", poField->GetFieldTypeName(poField->GetType()), poField->GetFieldSubTypeName(poField->GetSubType())) : poField->GetFieldTypeName(poField->GetType()); printf("%s: %s (%d.%d)", poField->GetNameRef(), pszType, poField->GetWidth(), poField->GetPrecision()); if( !poField->IsNullable() ) printf(" NOT NULL"); if( poField->GetDefault() != nullptr ) printf(" DEFAULT %s", poField->GetDefault()); printf("\n"); } } /* -------------------------------------------------------------------- */ /* Read, and dump features. */ /* -------------------------------------------------------------------- */ if( nFetchFID == OGRNullFID && !bSummaryOnly ) { OGRFeature *poFeature = nullptr; while( (poFeature = poLayer->GetNextFeature()) != nullptr ) { if( !bSuperQuiet ) poFeature->DumpReadable(nullptr, papszOptions); OGRFeature::DestroyFeature(poFeature); } } else if( nFetchFID != OGRNullFID ) { OGRFeature *poFeature = poLayer->GetFeature(nFetchFID); if( poFeature == nullptr ) { printf("Unable to locate feature id " CPL_FRMT_GIB " on this layer.\n", nFetchFID); } else { poFeature->DumpReadable(nullptr, papszOptions); OGRFeature::DestroyFeature(poFeature); } } }