GDALDataset *SDTSDataset::Open( GDALOpenInfo * poOpenInfo ) { int i; /* -------------------------------------------------------------------- */ /* Before trying SDTSOpen() we first verify that the first */ /* record is in fact a SDTS file descriptor record. */ /* -------------------------------------------------------------------- */ char *pachLeader = (char *) poOpenInfo->pabyHeader; if( poOpenInfo->nHeaderBytes < 24 ) return NULL; if( pachLeader[5] != '1' && pachLeader[5] != '2' && pachLeader[5] != '3' ) return NULL; if( pachLeader[6] != 'L' ) return NULL; if( pachLeader[8] != '1' && pachLeader[8] != ' ' ) return NULL; /* -------------------------------------------------------------------- */ /* Try opening the dataset. */ /* -------------------------------------------------------------------- */ SDTSTransfer *poTransfer = new SDTSTransfer; if( !poTransfer->Open( poOpenInfo->pszFilename ) ) { delete poTransfer; return NULL; } /* -------------------------------------------------------------------- */ /* Confirm the requested access is supported. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->eAccess == GA_Update ) { delete poTransfer; CPLError( CE_Failure, CPLE_NotSupported, "The SDTS driver does not support update access to existing" " datasets.\n" ); return NULL; } /* -------------------------------------------------------------------- */ /* Find the first raster layer. If there are none, abort */ /* returning an error. */ /* -------------------------------------------------------------------- */ SDTSRasterReader *poRL = NULL; for( i = 0; i < poTransfer->GetLayerCount(); i++ ) { if( poTransfer->GetLayerType( i ) == SLTRaster ) { poRL = poTransfer->GetLayerRasterReader( i ); break; } } if( poRL == NULL ) { delete poTransfer; CPLError( CE_Warning, CPLE_AppDefined, "%s is an SDTS transfer, but has no raster cell layers.\n" "Perhaps it is a vector transfer?\n", poOpenInfo->pszFilename ); return NULL; } /* -------------------------------------------------------------------- */ /* Initialize a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ SDTSDataset *poDS = new SDTSDataset(); poDS->poTransfer = poTransfer; poDS->poRL = poRL; /* -------------------------------------------------------------------- */ /* Capture some information from the file that is of interest. */ /* -------------------------------------------------------------------- */ poDS->nRasterXSize = poRL->GetXSize(); poDS->nRasterYSize = poRL->GetYSize(); /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ poDS->nBands = 1; poDS->papoBands = (GDALRasterBand **) VSICalloc(sizeof(GDALRasterBand *),poDS->nBands); for( i = 0; i < poDS->nBands; i++ ) poDS->SetBand( i+1, new SDTSRasterBand( poDS, i+1, poRL ) ); /* -------------------------------------------------------------------- */ /* Try to establish the projection string. For now we only */ /* support UTM and GEO. */ /* -------------------------------------------------------------------- */ OGRSpatialReference oSRS; SDTS_XREF *poXREF = poTransfer->GetXREF(); if( EQUAL(poXREF->pszSystemName,"UTM") ) { oSRS.SetUTM( poXREF->nZone ); } else if( EQUAL(poXREF->pszSystemName,"GEO") ) { /* we set datum later */ } else oSRS.SetLocalCS( poXREF->pszSystemName ); if( oSRS.IsLocal() ) /* don't try to set datum. */; else if( EQUAL(poXREF->pszDatum,"NAS") ) oSRS.SetWellKnownGeogCS( "NAD27" ); else if( EQUAL(poXREF->pszDatum, "NAX") ) oSRS.SetWellKnownGeogCS( "NAD83" ); else if( EQUAL(poXREF->pszDatum, "WGC") ) oSRS.SetWellKnownGeogCS( "WGS72" ); else if( EQUAL(poXREF->pszDatum, "WGE") ) oSRS.SetWellKnownGeogCS( "WGS84" ); else oSRS.SetWellKnownGeogCS( "WGS84" ); oSRS.Fixup(); poDS->pszProjection = NULL; if( oSRS.exportToWkt( &poDS->pszProjection ) != OGRERR_NONE ) poDS->pszProjection = CPLStrdup(""); /* -------------------------------------------------------------------- */ /* Get metadata from the IDEN file. */ /* -------------------------------------------------------------------- */ const char* pszIDENFilePath = poTransfer->GetCATD()->GetModuleFilePath("IDEN"); if (pszIDENFilePath) { DDFModule oIDENFile; if( oIDENFile.Open( pszIDENFilePath ) ) { DDFRecord* poRecord; while( (poRecord = oIDENFile.ReadRecord()) != NULL ) { if( poRecord->GetStringSubfield( "IDEN", 0, "MODN", 0 ) == NULL ) continue; static const char* fields[][2] = { { "TITL", "TITLE" }, { "DAID", "DATASET_ID" }, { "DAST", "DATA_STRUCTURE" }, { "MPDT", "MAP_DATE" }, { "DCDT", "DATASET_CREATION_DATE" } }; for (i = 0; i < (int)sizeof(fields) / (int)sizeof(fields[0]) ; i++) { const char* pszFieldValue = poRecord->GetStringSubfield( "IDEN", 0, fields[i][0], 0 ); if ( pszFieldValue ) poDS->SetMetadataItem(fields[i][1], pszFieldValue); } break; } } } /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); return( poDS ); }
int main( int nArgc, char ** papszArgv ) { { int i; const char *pszCATDFilename = NULL; const char *pszMODN = "LE01"; char *pszShapefile = "sdts_out.shp"; SDTSTransfer oTransfer; /* -------------------------------------------------------------------- */ /* Interpret commandline switches. */ /* -------------------------------------------------------------------- */ if( nArgc < 2 ) Usage(); pszCATDFilename = papszArgv[1]; for( i = 2; i < nArgc; i++ ) { if( EQUAL(papszArgv[i],"-m") && i+1 < nArgc ) pszMODN = papszArgv[++i]; else if( EQUAL(papszArgv[i],"-o") && i+1 < nArgc ) pszShapefile = papszArgv[++i]; else if( EQUAL(papszArgv[i],"-v") ) bVerbose = TRUE; else { printf( "Incomplete, or unsupported option `%s'\n\n", papszArgv[i] ); Usage(); } } /* -------------------------------------------------------------------- */ /* Massage shapefile name to have no extension. */ /* -------------------------------------------------------------------- */ pszShapefile = CPLStrdup(pszShapefile); for( i = strlen(pszShapefile)-1; i >= 0; i-- ) { if( pszShapefile[i] == '.' ) { pszShapefile[i] = '\0'; break; } else if( pszShapefile[i] == '/' || pszShapefile[i] == '\\' ) break; } /* -------------------------------------------------------------------- */ /* Open the transfer. */ /* -------------------------------------------------------------------- */ if( !oTransfer.Open( pszCATDFilename ) ) { fprintf( stderr, "Failed to read CATD file `%s'\n", pszCATDFilename ); exit( 100 ); } /* -------------------------------------------------------------------- */ /* Dump available layer in verbose mode. */ /* -------------------------------------------------------------------- */ if( bVerbose ) { printf( "Layers:\n" ); for( i = 0; i < oTransfer.GetLayerCount(); i++ ) { int iCATDEntry = oTransfer.GetLayerCATDEntry(i); printf( " %s: `%s'\n", oTransfer.GetCATD()->GetEntryModule(iCATDEntry), oTransfer.GetCATD()->GetEntryTypeDesc(iCATDEntry) ); } printf( "\n" ); } /* -------------------------------------------------------------------- */ /* Check that module exists. */ /* -------------------------------------------------------------------- */ if( oTransfer.FindLayer( pszMODN ) == -1 ) { fprintf( stderr, "Unable to identify module: %s\n", pszMODN ); exit( 1 ); } /* -------------------------------------------------------------------- */ /* If the module is an LE module, write it to an Arc file. */ /* -------------------------------------------------------------------- */ if( pszMODN[0] == 'L' || pszMODN[0] == 'l' ) { WriteLineShapefile( pszShapefile, &oTransfer, pszMODN ); } /* -------------------------------------------------------------------- */ /* If the module is an attribute primary one, dump to DBF. */ /* -------------------------------------------------------------------- */ else if( pszMODN[0] == 'A' || pszMODN[0] == 'a' || pszMODN[0] == 'B' || pszMODN[0] == 'b' ) { WriteAttributeDBF( pszShapefile, &oTransfer, pszMODN ); } /* -------------------------------------------------------------------- */ /* If the module is a point one, dump to Shapefile. */ /* -------------------------------------------------------------------- */ else if( pszMODN[0] == 'N' || pszMODN[0] == 'N' ) { WritePointShapefile( pszShapefile, &oTransfer, pszMODN ); } /* -------------------------------------------------------------------- */ /* If the module is a polygon one, dump to Shapefile. */ /* -------------------------------------------------------------------- */ else if( pszMODN[0] == 'P' || pszMODN[0] == 'p' ) { WritePolygonShapefile( pszShapefile, &oTransfer, pszMODN ); } else { fprintf( stderr, "Unrecognised module name: %s\n", pszMODN ); } CPLFree( pszShapefile ); } #ifdef DBMALLOC malloc_dump(1); #endif }