OGRFeatureDefn* OGRMSSQLSpatialTableLayer::GetLayerDefn() { if (poFeatureDefn) return poFeatureDefn; CPLODBCSession *poSession = poDS->GetSession(); /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetKey( poSession ); if( oGetKey.GetPrimaryKeys( pszTableName, poDS->GetCatalog(), pszSchemaName ) && oGetKey.Fetch() ) { pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 )); if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug( "OGR_MSSQLSpatial", "Table %s has multiple primary key fields, " "ignoring them all.", pszTableName ); } } /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetCol( poSession ); CPLErr eErr; if( !oGetCol.GetColumns( pszTableName, poDS->GetCatalog(), pszSchemaName ) ) return NULL; eErr = BuildFeatureDefn( pszLayerName, &oGetCol ); if( eErr != CE_None ) return NULL; poFeatureDefn->SetGeomType(eGeomType); if( poFeatureDefn->GetFieldCount() == 0 && pszFIDColumn == NULL && pszGeomColumn == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszLayerName ); return NULL; } /* -------------------------------------------------------------------- */ /* If we got a geometry column, does it exist? Is it binary? */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL ) { int iColumn = oGetCol.GetColId( pszGeomColumn ); if( iColumn < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Column %s requested for geometry, but it does not exist.", pszGeomColumn ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } else { if ( nGeomColumnType < 0 ) { /* last attempt to identify the geometry column type */ if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "geometry") ) nGeomColumnType = MSSQLCOLTYPE_GEOMETRY; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "geography") ) nGeomColumnType = MSSQLCOLTYPE_GEOGRAPHY; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "varchar") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "nvarchar") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "text") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "ntext") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "image") ) nGeomColumnType = MSSQLCOLTYPE_BINARY; else { CPLError( CE_Failure, CPLE_AppDefined, "Column type %s is not supported for geometry column.", oGetCol.GetColTypeName( iColumn ) ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } } } } return poFeatureDefn; }
CPLErr OGRMSSQLSpatialTableLayer::Initialize( const char *pszSchema, const char *pszLayerName, const char *pszGeomCol, int nCoordDimension, int nSRId, OGRwkbGeometryType eType ) { CPLODBCSession *poSession = poDS->GetSession(); CPLFree( pszFIDColumn ); pszFIDColumn = NULL; /* -------------------------------------------------------------------- */ /* Parse out schema name if present in layer. We assume a */ /* schema is provided if there is a dot in the name, and that */ /* it is in the form <schema>.<tablename> */ /* -------------------------------------------------------------------- */ const char *pszDot = strstr(pszLayerName,"."); if( pszDot != NULL ) { pszTableName = CPLStrdup(pszDot + 1); pszSchemaName = CPLStrdup(pszLayerName); pszSchemaName[pszDot - pszLayerName] = '\0'; } else { pszTableName = CPLStrdup(pszLayerName); pszSchemaName = CPLStrdup(pszSchema); } /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetKey( poSession ); if( oGetKey.GetPrimaryKeys( pszTableName, poDS->GetCatalog(), pszSchemaName ) && oGetKey.Fetch() ) { pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 )); if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug( "OGR_MSSQLSpatial", "Table %s has multiple primary key fields, " "ignoring them all.", pszTableName ); } } /* -------------------------------------------------------------------- */ /* Have we been provided a geometry column? */ /* -------------------------------------------------------------------- */ CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetCol( poSession ); CPLErr eErr; if( !oGetCol.GetColumns( pszTableName, poDS->GetCatalog(), pszSchemaName ) ) return CE_Failure; eErr = BuildFeatureDefn( pszLayerName, &oGetCol ); if( eErr != CE_None ) return eErr; poFeatureDefn->SetGeomType(eType); if( poFeatureDefn->GetFieldCount() == 0 && pszFIDColumn == NULL && pszGeomColumn == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszLayerName ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* If we got a geometry column, does it exist? Is it binary? */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL ) { int iColumn = oGetCol.GetColId( pszGeomColumn ); if( iColumn < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Column %s requested for geometry, but it does not exist.", pszGeomColumn ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } else { if ( nGeomColumnType < 0 ) { /* last attempt to identify the geometry column type */ if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "geometry") ) nGeomColumnType = MSSQLCOLTYPE_GEOMETRY; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "geography") ) nGeomColumnType = MSSQLCOLTYPE_GEOGRAPHY; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "varchar") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "nvarchar") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "text") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "ntext") ) nGeomColumnType = MSSQLCOLTYPE_TEXT; else if ( EQUAL(oGetCol.GetColTypeName( iColumn ), "image") ) nGeomColumnType = MSSQLCOLTYPE_BINARY; else { CPLError( CE_Failure, CPLE_AppDefined, "Column type %s is not supported for geometry column.", oGetCol.GetColTypeName( iColumn ) ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } } } } /* -------------------------------------------------------------------- */ /* Try to find out the spatial reference */ /* -------------------------------------------------------------------- */ nSRSId = nSRId; if (nSRSId < 0) nSRSId = FetchSRSId(); GetSpatialRef(); return CE_None; }
CPLErr OGRPGeoTableLayer::Initialize( const char *pszTableName, const char *pszGeomCol, int nShapeType, double dfExtentLeft, double dfExtentRight, double dfExtentBottom, double dfExtentTop, int nSRID, int bHasZ ) { CPLODBCSession *poSession = poDS->GetSession(); SetDescription( pszTableName ); CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); CPLFree( pszFIDColumn ); pszFIDColumn = NULL; sExtent.MinX = dfExtentLeft; sExtent.MaxX = dfExtentRight; sExtent.MinY = dfExtentBottom; sExtent.MaxY = dfExtentTop; LookupSRID( nSRID ); /* -------------------------------------------------------------------- */ /* Setup geometry type. */ /* -------------------------------------------------------------------- */ OGRwkbGeometryType eOGRType; switch( nShapeType ) { case ESRI_LAYERGEOMTYPE_NULL: eOGRType = wkbNone; break; case ESRI_LAYERGEOMTYPE_POINT: eOGRType = wkbPoint; break; case ESRI_LAYERGEOMTYPE_MULTIPOINT: eOGRType = wkbMultiPoint; break; case ESRI_LAYERGEOMTYPE_POLYLINE: eOGRType = wkbLineString; break; case ESRI_LAYERGEOMTYPE_POLYGON: case ESRI_LAYERGEOMTYPE_MULTIPATCH: eOGRType = wkbPolygon; break; default: CPLDebug("PGeo", "Unexpected value for shape type : %d", nShapeType); eOGRType = wkbUnknown; break; } if( eOGRType != wkbUnknown && eOGRType != wkbNone && bHasZ ) eOGRType = wkbSetZ(eOGRType); /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetKey( poSession ); if( oGetKey.GetPrimaryKeys( pszTableName ) && oGetKey.Fetch() ) { pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 )); if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug( "PGeo", "%s: Compound primary key, ignoring.", pszTableName ); } else CPLDebug( "PGeo", "%s: Got primary key %s.", pszTableName, pszFIDColumn ); } else CPLDebug( "PGeo", "%s: no primary key", pszTableName ); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetCol( poSession ); CPLErr eErr; if( !oGetCol.GetColumns( pszTableName ) ) { CPLError( CE_Failure, CPLE_AppDefined, "GetColumns() failed on %s.\n%s", pszTableName, poSession->GetLastError() ); return CE_Failure; } eErr = BuildFeatureDefn( pszTableName, &oGetCol ); if( eErr != CE_None ) return eErr; if( poFeatureDefn->GetFieldCount() == 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszTableName ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* Set geometry type. */ /* */ /* NOTE: per reports from Craig Miller, it seems we cannot really */ /* trust the ShapeType value. At the very least "line" tables */ /* sometimes have multilinestrings. So for now we just always */ /* return wkbUnknown. */ /* */ /* TODO - mloskot: Similar issue has been reported in Ticket #1484 */ /* -------------------------------------------------------------------- */ #ifdef notdef poFeatureDefn->SetGeomType( eOGRType ); #endif return CE_None; }
CPLErr OGRGeomediaTableLayer::Initialize( const char *pszTableName, const char *pszGeomCol, OGRSpatialReference* poSRSIn ) { CPLODBCSession *poSession = poDS->GetSession(); CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); CPLFree( pszFIDColumn ); pszFIDColumn = NULL; this->poSRS = poSRSIn; /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ { CPLODBCStatement oGetKey( poSession ); if( oGetKey.GetPrimaryKeys( pszTableName ) && oGetKey.Fetch() ) { pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 )); if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug( "Geomedia", "%s: Compound primary key, ignoring.", pszTableName ); } else CPLDebug( "Geomedia", "%s: Got primary key %s.", pszTableName, pszFIDColumn ); } else CPLDebug( "Geomedia", "%s: no primary key", pszTableName ); } /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetCol( poSession ); CPLErr eErr; if( !oGetCol.GetColumns( pszTableName ) ) { CPLError( CE_Failure, CPLE_AppDefined, "GetColumns() failed on %s.\n%s", pszTableName, poSession->GetLastError() ); return CE_Failure; } eErr = BuildFeatureDefn( pszTableName, &oGetCol ); if( eErr != CE_None ) return eErr; if( poFeatureDefn->GetFieldCount() == 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszTableName ); return CE_Failure; } return CE_None; }
CPLErr OGRIDBTableLayer::Initialize( const char *pszTableName, const char *pszGeomCol, int bUpdate ) { bUpdateAccess = bUpdate; ITConnection *poConn = poDS->GetConnection(); if ( pszFIDColumn ) { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; } /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ if ( pszFIDColumn == NULL ) { ITCursor oGetKey( *poConn ); CPLString osSql = " select sc.colname" " from syscolumns sc, sysindexes si, systables st" " where st.tabid = si.tabid" " and st.tabid = sc.tabid" " and si.idxtype = 'U'" " and sc.colno = si.part1" " and si.part2 = 0" // only one-column keys " and st.tabname='"; osSql += pszTableName; osSql += "'"; if( oGetKey.Prepare( osSql.c_str() ) && oGetKey.Open(ITCursor::ReadOnly) ) { ITValue * poVal = oGetKey.Fetch(); if ( poVal && poVal->IsNull() == false ) { pszFIDColumn = CPLStrdup(poVal->Printable()); poVal->Release(); } if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug("OGR_IDB", "Table %s has multiple primary key fields," " ignoring them all.", pszTableName ); } } } /* -------------------------------------------------------------------- */ /* Have we been provided a geometry column? */ /* -------------------------------------------------------------------- */ CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ ITCursor oGetCol( *poConn ); CPLErr eErr; CPLString sql; sql.Printf( "select * from %s where 1=0", pszTableName ); if( ! oGetCol.Prepare( sql.c_str() ) || ! oGetCol.Open(ITCursor::ReadOnly) ) return CE_Failure; eErr = BuildFeatureDefn( pszTableName, &oGetCol ); if( eErr != CE_None ) return eErr; if( poFeatureDefn->GetFieldCount() == 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszTableName ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* Do we have XMIN, YMIN, XMAX, YMAX extent fields? */ /* -------------------------------------------------------------------- */ if( poFeatureDefn->GetFieldIndex( "XMIN" ) != -1 && poFeatureDefn->GetFieldIndex( "XMAX" ) != -1 && poFeatureDefn->GetFieldIndex( "YMIN" ) != -1 && poFeatureDefn->GetFieldIndex( "YMAX" ) != -1 ) { bHaveSpatialExtents = TRUE; CPLDebug( "OGR_IDB", "Table %s has geometry extent fields.", pszTableName ); } /* -------------------------------------------------------------------- */ /* If we got a geometry column, does it exist? Is it binary? */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL ) { int iColumn = oGetCol.RowType()->ColumnId( pszGeomColumn ); if( iColumn < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Column %s requested for geometry, but it does not exist.", pszGeomColumn ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } bGeomColumnWKB = TRUE; /*else { if( ITCursor::GetTypeMapping( oGetCol.GetColType( iColumn )) == SQL_C_BINARY ) bGeomColumnWKB = TRUE; }*/ } return CE_None; }
CPLErr OGRODBCTableLayer::Initialize( const char *pszLayerName, const char *pszGeomCol ) { CPLODBCSession *poSession = poDS->GetSession(); CPLFree( pszFIDColumn ); pszFIDColumn = NULL; /* -------------------------------------------------------------------- */ /* Parse out schema name if present in layer. We assume a */ /* schema is provided if there is a dot in the name, and that */ /* it is in the form <schema>.<tablename> */ /* -------------------------------------------------------------------- */ const char *pszDot = strstr(pszLayerName,"."); if( pszDot != NULL ) { pszTableName = CPLStrdup(pszDot + 1); pszSchemaName = CPLStrdup(pszLayerName); pszSchemaName[pszDot - pszLayerName] = '\0'; } else { pszTableName = CPLStrdup(pszLayerName); } /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetKey( poSession ); if( oGetKey.GetPrimaryKeys( pszTableName, NULL, pszSchemaName ) && oGetKey.Fetch() ) { pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 )); if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug( "OGR_ODBC", "Table %s has multiple primary key fields, " "ignoring them all.", pszTableName ); } } /* -------------------------------------------------------------------- */ /* Have we been provided a geometry column? */ /* -------------------------------------------------------------------- */ CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetCol( poSession ); CPLErr eErr; if( !oGetCol.GetColumns( pszTableName, NULL, pszSchemaName ) ) return CE_Failure; eErr = BuildFeatureDefn( pszLayerName, &oGetCol ); if( eErr != CE_None ) return eErr; if( poFeatureDefn->GetFieldCount() == 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszLayerName ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* Do we have XMIN, YMIN, XMAX, YMAX extent fields? */ /* -------------------------------------------------------------------- */ if( poFeatureDefn->GetFieldIndex( "XMIN" ) != -1 && poFeatureDefn->GetFieldIndex( "XMAX" ) != -1 && poFeatureDefn->GetFieldIndex( "YMIN" ) != -1 && poFeatureDefn->GetFieldIndex( "YMAX" ) != -1 ) { bHaveSpatialExtents = TRUE; CPLDebug( "OGR_ODBC", "Table %s has geometry extent fields.", pszLayerName ); } /* -------------------------------------------------------------------- */ /* If we got a geometry column, does it exist? Is it binary? */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL ) { int iColumn = oGetCol.GetColId( pszGeomColumn ); if( iColumn < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Column %s requested for geometry, but it does not exist.", pszGeomColumn ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } else { if( CPLODBCStatement::GetTypeMapping( oGetCol.GetColType( iColumn )) == SQL_C_BINARY ) bGeomColumnWKB = TRUE; } } return CE_None; }
CPLErr OGRWalkTableLayer::Initialize( const char *pszLayerName, const char *pszGeomCol, double minE, double maxE, double minN, double maxN, const char *pszMemo) { SetDescription( pszLayerName ); CPLODBCSession *poSession = poDS->GetSession(); CPLFree( pszFIDColumn ); pszFIDColumn = NULL; sExtent.MinX = minE; sExtent.MaxX = maxE; sExtent.MinY = minN; sExtent.MaxY = maxN; /* -------------------------------------------------------------------- */ /* Look up the Spatial Reference */ /* -------------------------------------------------------------------- */ LookupSpatialRef( pszMemo ); /* -------------------------------------------------------------------- */ /* Generate the Feature Tablename from the Layer Name */ /* which is in the form <layername>Features */ /* -------------------------------------------------------------------- */ char* pszFeatureTableName = (char *) CPLMalloc(strlen(pszLayerName)+10); sprintf(pszFeatureTableName, "%sFeatures", pszLayerName); /* -------------------------------------------------------------------- */ /* Do we have a simple primary key? */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetKey( poSession ); if( oGetKey.GetPrimaryKeys( pszFeatureTableName, NULL, NULL ) && oGetKey.Fetch() ) { pszFIDColumn = CPLStrdup(oGetKey.GetColData( 3 )); if( oGetKey.Fetch() ) // more than one field in key! { CPLFree( pszFIDColumn ); pszFIDColumn = NULL; CPLDebug( "Walk", "Table %s has multiple primary key fields, " "ignoring them all.", pszFeatureTableName ); } } /* -------------------------------------------------------------------- */ /* Have we been provided a geometry column? */ /* -------------------------------------------------------------------- */ CPLFree( pszGeomColumn ); if( pszGeomCol == NULL ) pszGeomColumn = NULL; else pszGeomColumn = CPLStrdup( pszGeomCol ); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ CPLODBCStatement oGetCol( poSession ); CPLErr eErr; if( !oGetCol.GetColumns( pszFeatureTableName, NULL, NULL ) ) { CPLFree( pszFeatureTableName ); return CE_Failure; } eErr = BuildFeatureDefn( pszLayerName, &oGetCol ); if( eErr != CE_None ) { CPLFree( pszFeatureTableName ); return eErr; } if( poFeatureDefn->GetFieldCount() == 0 ) { CPLError( CE_Warning, CPLE_AppDefined, "No column definitions found for table '%s', layer not usable.", pszLayerName ); CPLFree( pszFeatureTableName ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* If we got a geometry column, does it exist? Is it binary? */ /* -------------------------------------------------------------------- */ if( pszGeomColumn != NULL ) { int iColumn = oGetCol.GetColId( pszGeomColumn ); if( iColumn < 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Column %s requested for geometry, but it does not exist.", pszGeomColumn ); CPLFree( pszGeomColumn ); pszGeomColumn = NULL; } else { if( CPLODBCStatement::GetTypeMapping( oGetCol.GetColType( iColumn )) == SQL_C_BINARY ) bGeomColumnWKB = TRUE; } } CPLFree( pszFeatureTableName ); return CE_None; }