int SDTS_XREF::Read( const char * pszFilename ) { DDFModule oXREFFile; DDFRecord *poRecord; /* -------------------------------------------------------------------- */ /* Open the file, and read the header. */ /* -------------------------------------------------------------------- */ if( !oXREFFile.Open( pszFilename ) ) return FALSE; /* -------------------------------------------------------------------- */ /* Read the first record, and verify that this is an XREF record. */ /* -------------------------------------------------------------------- */ poRecord = oXREFFile.ReadRecord(); if( poRecord == NULL ) return FALSE; if( poRecord->GetStringSubfield( "XREF", 0, "MODN", 0 ) == NULL ) return FALSE; /* -------------------------------------------------------------------- */ /* Read fields of interest. */ /* -------------------------------------------------------------------- */ CPLFree( pszSystemName ); pszSystemName = CPLStrdup( poRecord->GetStringSubfield( "XREF", 0, "RSNM", 0 ) ); CPLFree( pszDatum ); pszDatum = CPLStrdup( poRecord->GetStringSubfield( "XREF", 0, "HDAT", 0 ) ); nZone = poRecord->GetIntSubfield( "XREF", 0, "ZONE", 0 ); return TRUE; }
int SDTSRasterReader::Open( SDTS_CATD * poCATD, SDTS_IREF * poIREF, const char * pszModule ) { strncpy( szModule, pszModule, sizeof(szModule) ); szModule[sizeof(szModule) - 1] = '\0'; /* ==================================================================== */ /* Search the LDEF module for the requested cell module. */ /* ==================================================================== */ DDFModule oLDEF; DDFRecord *poRecord; /* -------------------------------------------------------------------- */ /* Open the LDEF module, and report failure if it is missing. */ /* -------------------------------------------------------------------- */ if( poCATD->GetModuleFilePath("LDEF") == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find LDEF entry in CATD module ... " "can't treat as raster.\n" ); return FALSE; } if( !oLDEF.Open( poCATD->GetModuleFilePath("LDEF") ) ) return FALSE; /* -------------------------------------------------------------------- */ /* Read each record, till we find what we want. */ /* -------------------------------------------------------------------- */ while( (poRecord = oLDEF.ReadRecord() ) != NULL ) { const char* pszCandidateModule = poRecord->GetStringSubfield("LDEF",0,"CMNM",0); if( pszCandidateModule == NULL ) { poRecord = NULL; break; } if( EQUAL(pszCandidateModule, pszModule) ) break; } if( poRecord == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find module `%s' in LDEF file.\n", pszModule ); return FALSE; } /* -------------------------------------------------------------------- */ /* Extract raster dimensions, and origin offset (0/1). */ /* -------------------------------------------------------------------- */ nXSize = poRecord->GetIntSubfield( "LDEF", 0, "NCOL", 0 ); nYSize = poRecord->GetIntSubfield( "LDEF", 0, "NROW", 0 ); nXStart = poRecord->GetIntSubfield( "LDEF", 0, "SOCI", 0 ); nYStart = poRecord->GetIntSubfield( "LDEF", 0, "SORI", 0 ); /* -------------------------------------------------------------------- */ /* Get the point in the pixel that the origin defines. We only */ /* support top left and center. */ /* -------------------------------------------------------------------- */ const char* pszINTR = poRecord->GetStringSubfield( "LDEF", 0, "INTR", 0 ); if( pszINTR == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find INTR subfield of LDEF field" ); return FALSE; } strcpy( szINTR, pszINTR ); if( EQUAL(szINTR,"") ) strcpy( szINTR, "CE" ); if( !EQUAL(szINTR,"CE") && !EQUAL(szINTR,"TL") ) { CPLError( CE_Warning, CPLE_AppDefined, "Unsupported INTR value of `%s', assume CE.\n" "Positions may be off by one pixel.\n", szINTR ); strcpy( szINTR, "CE" ); } /* -------------------------------------------------------------------- */ /* Record the LDEF record number we used so we can find the */ /* corresponding RSDF record. */ /* -------------------------------------------------------------------- */ int nLDEF_RCID; nLDEF_RCID = poRecord->GetIntSubfield( "LDEF", 0, "RCID", 0 ); oLDEF.Close(); /* ==================================================================== */ /* Search the RSDF module for the requested cell module. */ /* ==================================================================== */ DDFModule oRSDF; /* -------------------------------------------------------------------- */ /* Open the RSDF module, and report failure if it is missing. */ /* -------------------------------------------------------------------- */ if( poCATD->GetModuleFilePath("RSDF") == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find RSDF entry in CATD module ... " "can't treat as raster.\n" ); return FALSE; } if( !oRSDF.Open( poCATD->GetModuleFilePath("RSDF") ) ) return FALSE; /* -------------------------------------------------------------------- */ /* Read each record, till we find what we want. */ /* -------------------------------------------------------------------- */ while( (poRecord = oRSDF.ReadRecord() ) != NULL ) { if( poRecord->GetIntSubfield("LYID",0,"RCID",0) == nLDEF_RCID ) break; } if( poRecord == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find LDEF:%d record in RSDF file.\n", nLDEF_RCID ); return FALSE; } /* -------------------------------------------------------------------- */ /* Establish the raster pixel/line to georef transformation. */ /* -------------------------------------------------------------------- */ double dfZ; if( poRecord->FindField( "SADR" ) == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find SADR field in RSDF record.\n" ); return FALSE; } poIREF->GetSADR( poRecord->FindField( "SADR" ), 1, adfTransform + 0, adfTransform + 3, &dfZ ); adfTransform[1] = poIREF->dfXRes; adfTransform[2] = 0.0; adfTransform[4] = 0.0; adfTransform[5] = -1 * poIREF->dfYRes; /* -------------------------------------------------------------------- */ /* If the origin is the center of the pixel, then shift it back */ /* half a pixel to the top left of the top left. */ /* -------------------------------------------------------------------- */ if( EQUAL(szINTR,"CE") ) { adfTransform[0] -= adfTransform[1] * 0.5; adfTransform[3] -= adfTransform[5] * 0.5; } /* -------------------------------------------------------------------- */ /* Verify some other assumptions. */ /* -------------------------------------------------------------------- */ const char *pszString; pszString = poRecord->GetStringSubfield( "RSDF", 0, "OBRP", 0); if( pszString == NULL ) pszString = ""; if( !EQUAL(pszString,"G2") ) { CPLError( CE_Failure, CPLE_AppDefined, "OBRP value of `%s' not expected 2D raster code (G2).\n", pszString ); return FALSE; } pszString = poRecord->GetStringSubfield( "RSDF", 0, "SCOR", 0); if( pszString == NULL ) pszString = ""; if( !EQUAL(pszString,"TL") ) { CPLError( CE_Warning, CPLE_AppDefined, "SCOR (origin) is `%s' instead of expected top left.\n" "Georef coordinates will likely be incorrect.\n", pszString ); } oRSDF.Close(); /* -------------------------------------------------------------------- */ /* For now we will assume that the block size is one scanline. */ /* We will blow a gasket later while reading the cell file if */ /* this isn't the case. */ /* */ /* This isn't a very flexible raster implementation! */ /* -------------------------------------------------------------------- */ nXBlockSize = nXSize; nYBlockSize = 1; /* ==================================================================== */ /* Fetch the data type used for the raster, and the units from */ /* the data dictionary/schema record (DDSH). */ /* ==================================================================== */ DDFModule oDDSH; /* -------------------------------------------------------------------- */ /* Open the DDSH module, and report failure if it is missing. */ /* -------------------------------------------------------------------- */ if( poCATD->GetModuleFilePath("DDSH") == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find DDSH entry in CATD module ... " "can't treat as raster.\n" ); return FALSE; } if( !oDDSH.Open( poCATD->GetModuleFilePath("DDSH") ) ) return FALSE; /* -------------------------------------------------------------------- */ /* Read each record, till we find what we want. */ /* -------------------------------------------------------------------- */ while( (poRecord = oDDSH.ReadRecord() ) != NULL ) { const char* pszName = poRecord->GetStringSubfield("DDSH",0,"NAME",0); if( pszName == NULL ) { poRecord = NULL; break; } if( EQUAL(pszName,pszModule) ) break; } if( poRecord == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Can't find DDSH record for %s.\n", pszModule ); return FALSE; } /* -------------------------------------------------------------------- */ /* Get some values we are interested in. */ /* -------------------------------------------------------------------- */ if( poRecord->GetStringSubfield("DDSH",0,"FMT",0) != NULL ) strcpy( szFMT, poRecord->GetStringSubfield("DDSH",0,"FMT",0) ); else strcpy( szFMT, "BUI16" ); if( poRecord->GetStringSubfield("DDSH",0,"UNIT",0) != NULL ) strcpy( szUNITS, poRecord->GetStringSubfield("DDSH",0,"UNIT",0) ); else strcpy( szUNITS, "METERS" ); if( poRecord->GetStringSubfield("DDSH",0,"ATLB",0) != NULL ) strcpy( szLabel, poRecord->GetStringSubfield("DDSH",0,"ATLB",0) ); else strcpy( szLabel, "" ); /* -------------------------------------------------------------------- */ /* Open the cell file. */ /* -------------------------------------------------------------------- */ return( oDDFModule.Open( poCATD->GetModuleFilePath(pszModule) ) ); }
int SDTSRasterReader::GetBlock( int nXOffset, int nYOffset, void * pData ) { DDFRecord *poRecord = NULL; int nBytesPerValue; int iTry; CPLAssert( nXOffset == 0 ); /* -------------------------------------------------------------------- */ /* Analyse the datatype. */ /* -------------------------------------------------------------------- */ CPLAssert( EQUAL(szFMT,"BI16") || EQUAL(szFMT,"BFP32") ); if( EQUAL(szFMT,"BI16") ) nBytesPerValue = 2; else nBytesPerValue = 4; for(iTry=0;iTry<2;iTry++) { /* -------------------------------------------------------------------- */ /* Read through till we find the desired record. */ /* -------------------------------------------------------------------- */ CPLErrorReset(); while( (poRecord = oDDFModule.ReadRecord()) != NULL ) { if( poRecord->GetIntSubfield( "CELL", 0, "ROWI", 0 ) == nYOffset + nYStart ) { break; } } if( CPLGetLastErrorType() == CE_Failure ) return FALSE; /* -------------------------------------------------------------------- */ /* If we didn't get what we needed just start over. */ /* -------------------------------------------------------------------- */ if( poRecord == NULL ) { if (iTry == 0) oDDFModule.Rewind(); else { CPLError( CE_Failure, CPLE_AppDefined, "Cannot read scanline %d. Raster access failed.\n", nYOffset ); return FALSE; } } else { break; } } /* -------------------------------------------------------------------- */ /* Validate the records size. Does it represent exactly one */ /* scanline? */ /* -------------------------------------------------------------------- */ DDFField *poCVLS; poCVLS = poRecord->FindField( "CVLS" ); if( poCVLS == NULL ) return FALSE; if( poCVLS->GetRepeatCount() != nXSize ) { CPLError( CE_Failure, CPLE_AppDefined, "Cell record is %d long, but we expected %d, the number\n" "of pixels in a scanline. Raster access failed.\n", poCVLS->GetRepeatCount(), nXSize ); return FALSE; } /* -------------------------------------------------------------------- */ /* Does the CVLS field consist of exactly 1 B(16) field? */ /* -------------------------------------------------------------------- */ if( poCVLS->GetDataSize() < nBytesPerValue * nXSize || poCVLS->GetDataSize() > nBytesPerValue * nXSize + 1 ) { CPLError( CE_Failure, CPLE_AppDefined, "Cell record is not of expected format. Raster access " "failed.\n" ); return FALSE; } /* -------------------------------------------------------------------- */ /* Copy the data to the application buffer, and byte swap if */ /* required. */ /* -------------------------------------------------------------------- */ memcpy( pData, poCVLS->GetData(), nXSize * nBytesPerValue ); #ifdef CPL_LSB if( nBytesPerValue == 2 ) { for( int i = 0; i < nXSize; i++ ) { ((GInt16 *) pData)[i] = CPL_MSBWORD16(((GInt16 *) pData)[i]); } } else { for( int i = 0; i < nXSize; i++ ) { CPL_MSBPTR32( ((GByte *)pData) + i*4 ); } } #endif return TRUE; }