static int OGRDGNDriverIdentify( GDALOpenInfo* poOpenInfo ) { return poOpenInfo->fpL != NULL && poOpenInfo->nHeaderBytes >= 512 && DGNTestOpen(poOpenInfo->pabyHeader, poOpenInfo->nHeaderBytes); }
int OGRDGNDataSource::Open( const char * pszNewName, int bTestOpen, int bUpdate ) { CPLAssert( nLayers == 0 ); /* -------------------------------------------------------------------- */ /* For now we require files to have the .dgn or .DGN */ /* extension. Eventually we will implement a more */ /* sophisticated test to see if it is a dgn file. */ /* -------------------------------------------------------------------- */ if( bTestOpen ) { FILE *fp; GByte abyHeader[512]; int nHeaderBytes = 0; fp = VSIFOpen( pszNewName, "rb" ); if( fp == NULL ) return FALSE; nHeaderBytes = (int) VSIFRead( abyHeader, 1, sizeof(abyHeader), fp ); VSIFClose( fp ); if( nHeaderBytes < 512 ) return FALSE; if( !DGNTestOpen( abyHeader, nHeaderBytes ) ) return FALSE; } /* -------------------------------------------------------------------- */ /* Try to open the file as a DGN file. */ /* -------------------------------------------------------------------- */ hDGN = DGNOpen( pszNewName, bUpdate ); if( hDGN == NULL ) { if( !bTestOpen ) CPLError( CE_Failure, CPLE_AppDefined, "Unable to open %s as a Microstation .dgn file.\n", pszNewName ); return FALSE; } /* -------------------------------------------------------------------- */ /* Create the layer object. */ /* -------------------------------------------------------------------- */ OGRDGNLayer *poLayer; poLayer = new OGRDGNLayer( "elements", hDGN, bUpdate ); pszName = CPLStrdup( pszNewName ); /* -------------------------------------------------------------------- */ /* Add layer to data source layer list. */ /* -------------------------------------------------------------------- */ papoLayers = (OGRDGNLayer **) CPLRealloc( papoLayers, sizeof(OGRDGNLayer *) * (nLayers+1) ); papoLayers[nLayers++] = poLayer; return TRUE; }
DGNHandle DGNOpen( const char * pszFilename, int bUpdate ) { DGNInfo *psDGN; FILE *fp; /* -------------------------------------------------------------------- */ /* Open the file. */ /* -------------------------------------------------------------------- */ if( bUpdate ) fp = VSIFOpen( pszFilename, "rb+" ); else fp = VSIFOpen( pszFilename, "rb" ); if( fp == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Unable to open `%s' for read access.\n", pszFilename ); return NULL; } /* -------------------------------------------------------------------- */ /* Verify the format ... add later. */ /* -------------------------------------------------------------------- */ GByte abyHeader[512]; VSIFRead( abyHeader, 1, sizeof(abyHeader), fp ); if( !DGNTestOpen( abyHeader, sizeof(abyHeader) ) ) { CPLError( CE_Failure, CPLE_AppDefined, "File `%s' does not have expected DGN header.\n", pszFilename ); VSIFClose( fp ); return NULL; } VSIRewind( fp ); /* -------------------------------------------------------------------- */ /* Create the info structure. */ /* -------------------------------------------------------------------- */ psDGN = (DGNInfo *) CPLCalloc(sizeof(DGNInfo),1); psDGN->fp = fp; psDGN->next_element_id = 0; psDGN->got_tcb = FALSE; psDGN->scale = 1.0; psDGN->origin_x = 0.0; psDGN->origin_y = 0.0; psDGN->origin_z = 0.0; psDGN->index_built = FALSE; psDGN->element_count = 0; psDGN->element_index = NULL; psDGN->got_bounds = FALSE; if( abyHeader[0] == 0xC8 ) psDGN->dimension = 3; else psDGN->dimension = 2; psDGN->has_spatial_filter = FALSE; psDGN->sf_converted_to_uor = FALSE; psDGN->select_complex_group = FALSE; psDGN->in_complex_group = FALSE; return (DGNHandle) psDGN; }