CPLErr ELASDataset::SetGeoTransform( double * padfTransform ) { /* -------------------------------------------------------------------- */ /* I don't think it supports rotation, but perhaps it is possible */ /* for us to use the 2x2 transform matrix to accomplish some */ /* sort of rotation. */ /* -------------------------------------------------------------------- */ if( padfTransform[2] != 0.0 || padfTransform[4] != 0.0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to set rotated geotransform on ELAS file.\n" "ELAS does not support rotation.\n" ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* Remember the new transform, and update the header. */ /* -------------------------------------------------------------------- */ int nXOff, nYOff; memcpy( adfGeoTransform, padfTransform, sizeof(double)*6 ); bHeaderModified = TRUE; nXOff = (int) (adfGeoTransform[0] + adfGeoTransform[1]*0.5); nYOff = (int) (adfGeoTransform[3] + adfGeoTransform[5]*0.5); sHeader.XOffset = CPL_MSBWORD32(nXOff); sHeader.YOffset = CPL_MSBWORD32(nYOff); sHeader.XPixSize = (float) ABS(adfGeoTransform[1]); sHeader.YPixSize = (float) ABS(adfGeoTransform[5]); CPL_MSBPTR32(&(sHeader.XPixSize)); CPL_MSBPTR32(&(sHeader.YPixSize)); strncpy( sHeader.YLabel, "NOR ", 4 ); strncpy( sHeader.XLabel, "EAS ", 4 ); sHeader.Matrix[0] = 1.0; sHeader.Matrix[1] = 0.0; sHeader.Matrix[2] = 0.0; sHeader.Matrix[3] = -1.0; CPL_MSBPTR32(&(sHeader.Matrix[0])); CPL_MSBPTR32(&(sHeader.Matrix[1])); CPL_MSBPTR32(&(sHeader.Matrix[2])); CPL_MSBPTR32(&(sHeader.Matrix[3])); return( CE_None ); }
GDALJP2Box *GDALJP2Box::CreateAsocBox( int nCount, GDALJP2Box **papoBoxes ) { int nDataSize=0, iBox; GByte *pabyCompositeData, *pabyNext; /* -------------------------------------------------------------------- */ /* Compute size of data area of asoc box. */ /* -------------------------------------------------------------------- */ for( iBox = 0; iBox < nCount; iBox++ ) nDataSize += 8 + (int) papoBoxes[iBox]->GetDataLength(); pabyNext = pabyCompositeData = (GByte *) CPLMalloc(nDataSize); /* -------------------------------------------------------------------- */ /* Copy subboxes headers and data into buffer. */ /* -------------------------------------------------------------------- */ for( iBox = 0; iBox < nCount; iBox++ ) { GUInt32 nLBox, nTBox; nLBox = CPL_MSBWORD32(papoBoxes[iBox]->nBoxLength); memcpy( pabyNext, &nLBox, 4 ); pabyNext += 4; memcpy( &nTBox, papoBoxes[iBox]->szBoxType, 4 ); nTBox = CPL_MSBWORD32( nTBox ); memcpy( pabyNext, &nTBox, 4 ); pabyNext += 4; memcpy( pabyNext, papoBoxes[iBox]->pabyData, (int) papoBoxes[iBox]->GetDataLength() ); pabyNext += papoBoxes[iBox]->GetDataLength(); } /* -------------------------------------------------------------------- */ /* Create asoc box. */ /* -------------------------------------------------------------------- */ GDALJP2Box *poAsoc = new GDALJP2Box(); poAsoc->SetType( "asoc" ); poAsoc->SetWritableData( nDataSize, pabyCompositeData ); CPLFree( pabyCompositeData ); return poAsoc; }
OGRErr OGRPoint::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData, OGRwkbVariant eWkbVariant ) const { /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if ( eWkbVariant == wkbVariantIso ) nGType = getIsoGeometryType(); if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ if ( IsEmpty() && eWkbVariant == wkbVariantIso ) { double dNan = std::numeric_limits<double>::quiet_NaN(); memcpy( pabyData+5, &dNan, 8 ); memcpy( pabyData+5+8, &dNan, 8 ); if( getCoordinateDimension() == 3 ) memcpy( pabyData+5+16, &dNan, 8 ); } else { memcpy( pabyData+5, &x, 16 ); if( getCoordinateDimension() == 3 ) { memcpy( pabyData + 5 + 16, &z, 8 ); } } /* -------------------------------------------------------------------- */ /* Swap if needed. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { CPL_SWAPDOUBLE( pabyData + 5 ); CPL_SWAPDOUBLE( pabyData + 5 + 8 ); if( getCoordinateDimension() == 3 ) CPL_SWAPDOUBLE( pabyData + 5 + 16 ); } return OGRERR_NONE; }
static CPLErr AIGProcessRaw32BitBlock( GByte *pabyCur, int nDataSize, int nMin, int nBlockXSize, int nBlockYSize, GInt32 * panData ) { int i; if( nDataSize < nBlockXSize*nBlockYSize*4 ) { CPLError(CE_Failure, CPLE_AppDefined, "Block too small"); return CE_Failure; } /* -------------------------------------------------------------------- */ /* Collect raw data. */ /* -------------------------------------------------------------------- */ for( i = 0; i < nBlockXSize * nBlockYSize; i++ ) { memcpy(panData + i, pabyCur, 4); panData[i] = CPL_MSBWORD32(panData[i]); panData[i] = AIGSaturatedAdd(panData[i], nMin); pabyCur += 4; } return( CE_None ); }
OGRErr OGRLineString::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData ) const { /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the data count. */ /* -------------------------------------------------------------------- */ memcpy( pabyData+5, &nPointCount, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ int i; if( getCoordinateDimension() == 3 ) { for( i = 0; i < nPointCount; i++ ) { memcpy( pabyData + 9 + 24*i, paoPoints+i, 16 ); memcpy( pabyData + 9 + 16 + 24*i, padfZ+i, 8 ); } } else memcpy( pabyData+9, paoPoints, 16 * nPointCount ); /* -------------------------------------------------------------------- */ /* Swap if needed. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { int nCount; nCount = CPL_SWAP32( nPointCount ); memcpy( pabyData+5, &nCount, 4 ); for( i = getCoordinateDimension() * nPointCount - 1; i >= 0; i-- ) { CPL_SWAP64PTR( pabyData + 9 + 8 * i ); } } return OGRERR_NONE; }
CPLErr SIGDEMRasterBand::IReadBlock( int /*nBlockXOff*/, int nBlockYOff, void *pImage) { const int nBlockIndex = nRasterYSize - nBlockYOff - 1; if (nLoadedBlockIndex == nBlockIndex) { return CE_None; } const vsi_l_offset nReadStart = HEADER_LENGTH + static_cast<vsi_l_offset>(nBlockSizeBytes) * nBlockIndex; // Seek to the correct line. if (VSIFSeekL(fpRawL, nReadStart, SEEK_SET) == -1) { if (poDS != nullptr && poDS->GetAccess() == GA_ReadOnly) { CPLError(CE_Failure, CPLE_FileIO, "Failed to seek to block %d @ " CPL_FRMT_GUIB ".", nBlockIndex, nReadStart); return CE_Failure; } else { std::fill(pBlockBuffer, pBlockBuffer + nRasterXSize, 0); nLoadedBlockIndex = nBlockIndex; return CE_None; } } const size_t nCellReadCount = VSIFReadL(pBlockBuffer, CELL_SIZE_FILE, nRasterXSize, fpRawL); if (nCellReadCount < static_cast<size_t>(nRasterXSize)) { if (poDS != nullptr && poDS->GetAccess() == GA_ReadOnly) { CPLError(CE_Failure, CPLE_FileIO, "Failed to read block %d.", nBlockIndex); return CE_Failure; } std::fill(pBlockBuffer + nCellReadCount, pBlockBuffer + nRasterXSize, NO_DATA); } nLoadedBlockIndex = nBlockIndex; const int32_t* pnSourceValues = pBlockBuffer; double* padfDestValues = static_cast<double*>(pImage); double dfOffset = this->dfOffsetZ; const double dfInvScaleFactor = dfScaleFactorZ != 0.0 ? 1.0 / dfScaleFactorZ : 0.0; int nCellCount = this->nRasterXSize; for (int i = 0; i < nCellCount; i++) { int32_t nValue = CPL_MSBWORD32(*pnSourceValues); if (nValue == NO_DATA) { *padfDestValues = -9999; } else { *padfDestValues = dfOffset + nValue * dfInvScaleFactor; } pnSourceValues++; padfDestValues++; } return CE_None; }
int GDALJP2Box::ReadBox() { GUInt32 nLBox; GUInt32 nTBox; nBoxOffset = VSIFTellL( fpVSIL ); if( VSIFReadL( &nLBox, 4, 1, fpVSIL ) != 1 || VSIFReadL( &nTBox, 4, 1, fpVSIL ) != 1 ) { return FALSE; } memcpy( szBoxType, &nTBox, 4 ); szBoxType[4] = '\0'; nLBox = CPL_MSBWORD32( nLBox ); if( nLBox != 1 ) { nBoxLength = nLBox; nDataOffset = nBoxOffset + 8; } else { GByte abyXLBox[8]; if( VSIFReadL( abyXLBox, 8, 1, fpVSIL ) != 1 ) return FALSE; if( sizeof(nBoxLength) == 8 ) { CPL_MSBPTR64( abyXLBox ); memcpy( &nBoxLength, abyXLBox, 8 ); } else { CPL_MSBPTR32( abyXLBox+4 ); memcpy( &nBoxLength, abyXLBox+4, 4 ); } nDataOffset = nBoxOffset + 16; } if( nBoxLength == 0 ) { VSIFSeekL( fpVSIL, 0, SEEK_END ); nBoxLength = VSIFTellL( fpVSIL ) - nBoxOffset; } if( EQUAL(szBoxType,"uuid") ) { VSIFReadL( abyUUID, 16, 1, fpVSIL ); nDataOffset += 16; } return TRUE; }
int ELASDataset::Identify( GDALOpenInfo * poOpenInfo ) { /* -------------------------------------------------------------------- */ /* First we check to see if the file has the expected header */ /* bytes. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->nHeaderBytes < 256 ) return FALSE; if( CPL_MSBWORD32(*((GInt32 *) (poOpenInfo->pabyHeader+0))) != 1024 || CPL_MSBWORD32(*((GInt32 *) (poOpenInfo->pabyHeader+28))) != 4321 ) { return FALSE; } return TRUE; }
OGRErr OGRPolygon::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData ) const { int nOffset; int b3D = getCoordinateDimension() == 3; /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { int nCount; nCount = CPL_SWAP32( nRingCount ); memcpy( pabyData+5, &nCount, 4 ); } else { memcpy( pabyData+5, &nRingCount, 4 ); } nOffset = 9; /* ==================================================================== */ /* Serialize each of the rings. */ /* ==================================================================== */ for( int iRing = 0; iRing < nRingCount; iRing++ ) { papoRings[iRing]->_exportToWkb( eByteOrder, b3D, pabyData + nOffset ); nOffset += papoRings[iRing]->_WkbSize(b3D); } return OGRERR_NONE; }
OGRErr OGRGeometryCollection::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData ) const { int nOffset; /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type, ensuring that 3D flag is */ /* preserved. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { int nCount; nCount = CPL_SWAP32( nGeomCount ); memcpy( pabyData+5, &nCount, 4 ); } else { memcpy( pabyData+5, &nGeomCount, 4 ); } nOffset = 9; /* ==================================================================== */ /* Serialize each of the Geoms. */ /* ==================================================================== */ for( int iGeom = 0; iGeom < nGeomCount; iGeom++ ) { papoGeoms[iGeom]->exportToWkb( eByteOrder, pabyData + nOffset ); nOffset += papoGeoms[iGeom]->WkbSize(); } return OGRERR_NONE; }
OGRErr OGRPoint::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData ) const { /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ memcpy( pabyData+5, &nQual, 4 ); memcpy( pabyData+9, &x, 16 ); if( z != 0 ) { memcpy( pabyData + 9 + 16, &z, 8 ); } /* -------------------------------------------------------------------- */ /* Swap if needed. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { CPL_SWAP32PTR( pabyData + 5 ); CPL_SWAPDOUBLE( pabyData + 9 ); CPL_SWAPDOUBLE( pabyData + 9 + 8 ); if( z != 0 ) CPL_SWAPDOUBLE( pabyData + 9 + 16 ); } return OGRERR_NONE; }
CPLErr SIGDEMRasterBand::IWriteBlock( int /*nBlockXOff*/, int nBlockYOff, void *pImage) { const int nBlockIndex = nRasterYSize - nBlockYOff - 1; const double* padfSourceValues = static_cast<double*>(pImage); int32_t* pnDestValues = pBlockBuffer; double dfOffset = this->dfOffsetZ; double dfScaleFactor = this->dfScaleFactorZ; int nCellCount = this->nRasterXSize; for (int i = 0; i < nCellCount; i++) { double dfValue = *padfSourceValues; int32_t nValue; if (dfValue == -9999) { nValue = NO_DATA; } else { nValue = static_cast<int32_t>( std::round((dfValue - dfOffset) * dfScaleFactor)); } *pnDestValues = CPL_MSBWORD32(nValue); padfSourceValues++; pnDestValues++; } const vsi_l_offset nWriteStart = HEADER_LENGTH + static_cast<vsi_l_offset>(nBlockSizeBytes) * nBlockIndex; if (VSIFSeekL(fpRawL, nWriteStart, SEEK_SET) == -1 || VSIFWriteL(pBlockBuffer, CELL_SIZE_FILE, nRasterXSize, fpRawL) < static_cast<size_t>(nRasterXSize)) { CPLError(CE_Failure, CPLE_FileIO, "Failed to write block %d to file.", nBlockIndex); return CE_Failure; } return CE_None; }
GDALDataset *ELASDataset::Open( GDALOpenInfo * poOpenInfo ) { /* -------------------------------------------------------------------- */ /* First we check to see if the file has the expected header */ /* bytes. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->nHeaderBytes < 256 ) return NULL; if( CPL_MSBWORD32(*((GInt32 *) (poOpenInfo->pabyHeader+0))) != 1024 || CPL_MSBWORD32(*((GInt32 *) (poOpenInfo->pabyHeader+28))) != 4321 ) { return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ ELASDataset *poDS; const char *pszAccess; if( poOpenInfo->eAccess == GA_Update ) pszAccess = "r+b"; else pszAccess = "rb"; poDS = new ELASDataset(); poDS->fp = VSIFOpen( poOpenInfo->pszFilename, pszAccess ); if( poDS->fp == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Attempt to open `%s' with acces `%s' failed.\n", poOpenInfo->pszFilename, pszAccess ); delete poDS; return NULL; } poDS->eAccess = poOpenInfo->eAccess; /* -------------------------------------------------------------------- */ /* Read the header information. */ /* -------------------------------------------------------------------- */ poDS->bHeaderModified = FALSE; if( VSIFRead( &(poDS->sHeader), 1024, 1, poDS->fp ) != 1 ) { CPLError( CE_Failure, CPLE_FileIO, "Attempt to read 1024 byte header filed on file %s\n", poOpenInfo->pszFilename ); return NULL; } /* -------------------------------------------------------------------- */ /* Extract information of interest from the header. */ /* -------------------------------------------------------------------- */ int nStart, nEnd, nELASDataType, nBytesPerSample; poDS->nLineOffset = CPL_MSBWORD32( poDS->sHeader.NBPR ); nStart = CPL_MSBWORD32( poDS->sHeader.IL ); nEnd = CPL_MSBWORD32( poDS->sHeader.LL ); poDS->nRasterYSize = nEnd - nStart + 1; nStart = CPL_MSBWORD32( poDS->sHeader.IE ); nEnd = CPL_MSBWORD32( poDS->sHeader.LE ); poDS->nRasterXSize = nEnd - nStart + 1; poDS->nBands = CPL_MSBWORD32( poDS->sHeader.NC ); if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize) || !GDALCheckBandCount(poDS->nBands, FALSE)) { delete poDS; return NULL; } nELASDataType = (poDS->sHeader.IH19[2] & 0x7e) >> 2; nBytesPerSample = poDS->sHeader.IH19[3]; if( nELASDataType == 0 && nBytesPerSample == 1 ) poDS->eRasterDataType = GDT_Byte; else if( nELASDataType == 1 && nBytesPerSample == 1 ) poDS->eRasterDataType = GDT_Byte; else if( nELASDataType == 16 && nBytesPerSample == 4 ) poDS->eRasterDataType = GDT_Float32; else if( nELASDataType == 17 && nBytesPerSample == 8 ) poDS->eRasterDataType = GDT_Float64; else { delete poDS; CPLError( CE_Failure, CPLE_AppDefined, "Unrecognised image data type %d, with BytesPerSample=%d.\n", nELASDataType, nBytesPerSample ); return NULL; } /* -------------------------------------------------------------------- */ /* Band offsets are always multiples of 256 within a multi-band */ /* scanline of data. */ /* -------------------------------------------------------------------- */ poDS->nBandOffset = (poDS->nRasterXSize * GDALGetDataTypeSize(poDS->eRasterDataType)/8); if( poDS->nBandOffset % 256 != 0 ) { poDS->nBandOffset = poDS->nBandOffset - (poDS->nBandOffset % 256) + 256; } /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ int iBand; for( iBand = 0; iBand < poDS->nBands; iBand++ ) { poDS->SetBand( iBand+1, new ELASRasterBand( poDS, iBand+1 ) ); } /* -------------------------------------------------------------------- */ /* Extract the projection coordinates, if present. */ /* -------------------------------------------------------------------- */ if( poDS->sHeader.XOffset != 0 ) { CPL_MSBPTR32(&(poDS->sHeader.XPixSize)); CPL_MSBPTR32(&(poDS->sHeader.YPixSize)); poDS->adfGeoTransform[0] = (GInt32) CPL_MSBWORD32(poDS->sHeader.XOffset); poDS->adfGeoTransform[1] = poDS->sHeader.XPixSize; poDS->adfGeoTransform[2] = 0.0; poDS->adfGeoTransform[3] = (GInt32) CPL_MSBWORD32(poDS->sHeader.YOffset); poDS->adfGeoTransform[4] = 0.0; poDS->adfGeoTransform[5] = -1.0 * ABS(poDS->sHeader.YPixSize); CPL_MSBPTR32(&(poDS->sHeader.XPixSize)); CPL_MSBPTR32(&(poDS->sHeader.YPixSize)); poDS->adfGeoTransform[0] -= poDS->adfGeoTransform[1] * 0.5; poDS->adfGeoTransform[3] -= poDS->adfGeoTransform[5] * 0.5; } else { poDS->adfGeoTransform[0] = 0.0; poDS->adfGeoTransform[1] = 1.0; poDS->adfGeoTransform[2] = 0.0; poDS->adfGeoTransform[3] = 0.0; poDS->adfGeoTransform[4] = 0.0; poDS->adfGeoTransform[5] = 1.0; } /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Check for external overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename, poOpenInfo->papszSiblingFiles ); return( poDS ); }
OGRErr OGRCurveCollection::exportToWkb( const OGRGeometry* poGeom, OGRwkbByteOrder eByteOrder, unsigned char * pabyData, OGRwkbVariant eWkbVariant ) const { int nOffset; /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type, ensuring that 3D flag is */ /* preserved. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = poGeom->getIsoGeometryType(); if( eWkbVariant == wkbVariantPostGIS1 ) { int bIs3D = wkbHasZ((OGRwkbGeometryType)nGType); nGType = wkbFlatten(nGType); if( nGType == wkbCurvePolygon ) nGType = POSTGIS15_CURVEPOLYGON; if( bIs3D ) nGType = (OGRwkbGeometryType)(nGType | wkb25DBitInternalUse); /* yes we explicitly set wkb25DBit */ } if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { int nCount; nCount = CPL_SWAP32( nCurveCount ); memcpy( pabyData+5, &nCount, 4 ); } else { memcpy( pabyData+5, &nCurveCount, 4 ); } nOffset = 9; /* ==================================================================== */ /* Serialize each of the Geoms. */ /* ==================================================================== */ for( int iGeom = 0; iGeom < nCurveCount; iGeom++ ) { papoCurves[iGeom]->exportToWkb( eByteOrder, pabyData + nOffset, eWkbVariant ); nOffset += papoCurves[iGeom]->WkbSize(); } return OGRERR_NONE; }
void EnvisatDataset::ScanForGCPs_ASAR() { int nDatasetIndex, nNumDSR, nDSRSize, iRecord; /* -------------------------------------------------------------------- */ /* Do we have a meaningful geolocation grid? */ /* -------------------------------------------------------------------- */ nDatasetIndex = EnvisatFile_GetDatasetIndex( hEnvisatFile, "GEOLOCATION GRID ADS" ); if( nDatasetIndex == -1 ) return; if( EnvisatFile_GetDatasetInfo( hEnvisatFile, nDatasetIndex, NULL, NULL, NULL, NULL, NULL, &nNumDSR, &nDSRSize ) != SUCCESS ) return; if( nNumDSR == 0 || nDSRSize != 521 ) return; /* -------------------------------------------------------------------- */ /* Collect the first GCP set from each record. */ /* -------------------------------------------------------------------- */ GByte abyRecord[521]; int nRange=0, nSample, iGCP, nRangeOffset=0; GUInt32 unValue; nGCPCount = 0; pasGCPList = (GDAL_GCP *) CPLCalloc(sizeof(GDAL_GCP),(nNumDSR+1) * 11); for( iRecord = 0; iRecord < nNumDSR; iRecord++ ) { if( EnvisatFile_ReadDatasetRecord( hEnvisatFile, nDatasetIndex, iRecord, abyRecord ) != SUCCESS ) continue; memcpy( &unValue, abyRecord + 13, 4 ); nRange = CPL_MSBWORD32( unValue ) + nRangeOffset; if((iRecord>1) && (int(pasGCPList[nGCPCount-1].dfGCPLine+0.5) > nRange)) { int delta = (int) (pasGCPList[nGCPCount-1].dfGCPLine - pasGCPList[nGCPCount-12].dfGCPLine); nRange = int(pasGCPList[nGCPCount-1].dfGCPLine+0.5) + delta; nRangeOffset = nRange-1; } for( iGCP = 0; iGCP < 11; iGCP++ ) { char szId[128]; GDALInitGCPs( 1, pasGCPList + nGCPCount ); CPLFree( pasGCPList[nGCPCount].pszId ); sprintf( szId, "%d", nGCPCount+1 ); pasGCPList[nGCPCount].pszId = CPLStrdup( szId ); memcpy( &unValue, abyRecord + 25 + iGCP*4, 4 ); nSample = CPL_MSBWORD32(unValue); memcpy( &unValue, abyRecord + 25 + 176 + iGCP*4, 4 ); pasGCPList[nGCPCount].dfGCPX = ((int)CPL_MSBWORD32(unValue))*0.000001; memcpy( &unValue, abyRecord + 25 + 132 + iGCP*4, 4 ); pasGCPList[nGCPCount].dfGCPY = ((int)CPL_MSBWORD32(unValue))*0.000001; pasGCPList[nGCPCount].dfGCPZ = 0.0; pasGCPList[nGCPCount].dfGCPLine = nRange - 0.5; pasGCPList[nGCPCount].dfGCPPixel = nSample - 0.5; nGCPCount++; } } /* -------------------------------------------------------------------- */ /* We also collect the bottom GCPs from the last granule. */ /* -------------------------------------------------------------------- */ memcpy( &unValue, abyRecord + 17, 4 ); nRange = nRange + CPL_MSBWORD32( unValue ) - 1; for( iGCP = 0; iGCP < 11; iGCP++ ) { char szId[128]; GDALInitGCPs( 1, pasGCPList + nGCPCount ); CPLFree( pasGCPList[nGCPCount].pszId ); sprintf( szId, "%d", nGCPCount+1 ); pasGCPList[nGCPCount].pszId = CPLStrdup( szId ); memcpy( &unValue, abyRecord + 279 + iGCP*4, 4 ); nSample = CPL_MSBWORD32(unValue); memcpy( &unValue, abyRecord + 279 + 176 + iGCP*4, 4 ); pasGCPList[nGCPCount].dfGCPX = ((int)CPL_MSBWORD32(unValue))*0.000001; memcpy( &unValue, abyRecord + 279 + 132 + iGCP*4, 4 ); pasGCPList[nGCPCount].dfGCPY = ((int)CPL_MSBWORD32(unValue))*0.000001; pasGCPList[nGCPCount].dfGCPZ = 0.0; pasGCPList[nGCPCount].dfGCPLine = nRange - 0.5; pasGCPList[nGCPCount].dfGCPPixel = nSample - 0.5; nGCPCount++; } }
GDALDataset* SIGDEMDataset::CreateCopy( const char * pszFilename, GDALDataset * poSrcDS, int /*bStrict*/, char ** /*papszOptions*/, GDALProgressFunc pfnProgress, void * pProgressData) { const int nBands = poSrcDS->GetRasterCount(); double adfGeoTransform[6] = { }; if (poSrcDS->GetGeoTransform(adfGeoTransform) != CE_None) { CPLError(CE_Failure, CPLE_NotSupported, "SIGDEM driver requires a valid GeoTransform."); return nullptr; } if (nBands != 1) { CPLError(CE_Failure, CPLE_NotSupported, "SIGDEM driver doesn't support %d bands. Must be 1 band.", nBands); return nullptr; } VSILFILE *fp = VSIFOpenL(pszFilename, "wb"); if (fp == nullptr) { CPLError(CE_Failure, CPLE_OpenFailed, "Attempt to create file `%s' failed.", pszFilename); return nullptr; } GDALRasterBand* band = poSrcDS->GetRasterBand(1); const char* pszProjection = poSrcDS->GetProjectionRef(); int32_t nCols = poSrcDS->GetRasterXSize(); int32_t nRows = poSrcDS->GetRasterYSize(); int32_t nCoordinateSystemId = GetCoordinateSystemId(pszProjection); SIGDEMHeader sHeader; sHeader.nCoordinateSystemId = nCoordinateSystemId; sHeader.dfMinX = adfGeoTransform[0]; const char* pszMin = band->GetMetadataItem("STATISTICS_MINIMUM"); if (pszMin == nullptr) { sHeader.dfMinZ = -10000; } else { sHeader.dfMinZ = CPLAtof(pszMin); } sHeader.dfMaxY = adfGeoTransform[3]; const char* pszMax = band->GetMetadataItem("STATISTICS_MAXIMUM"); if (pszMax == nullptr) { sHeader.dfMaxZ = 10000; } else { sHeader.dfMaxZ = CPLAtof(pszMax); } sHeader.nCols = poSrcDS->GetRasterXSize(); sHeader.nRows = poSrcDS->GetRasterYSize(); sHeader.dfXDim = adfGeoTransform[1]; sHeader.dfYDim = -adfGeoTransform[5]; sHeader.dfMaxX = sHeader.dfMinX + sHeader.nCols * sHeader.dfXDim; sHeader.dfMinY = sHeader.dfMaxY - sHeader.nRows * sHeader.dfYDim; sHeader.dfOffsetX = sHeader.dfMinX; sHeader.dfOffsetY = sHeader.dfMinY; if( !sHeader.Write(fp) ) { VSIUnlink(pszFilename); VSIFCloseL(fp); return nullptr; } // Write fill with all NO_DATA values int32_t* row = static_cast<int32_t*>(VSI_MALLOC2_VERBOSE(nCols, sizeof(int32_t))); if( !row ) { VSIUnlink(pszFilename); VSIFCloseL(fp); return nullptr; } std::fill(row, row + nCols, CPL_MSBWORD32(NO_DATA)); for (int i = 0; i < nRows; i++) { if( VSIFWriteL(row, CELL_SIZE_FILE, nCols, fp) != static_cast<size_t>(nCols) ) { VSIFree(row); VSIUnlink(pszFilename); VSIFCloseL(fp); return nullptr; } } VSIFree(row); if (VSIFCloseL(fp) != 0) { return nullptr; } if (nCoordinateSystemId <= 0) { if (!EQUAL(pszProjection, "")) { CPLString osPrjFilename = CPLResetExtension(pszFilename, "prj"); VSILFILE *fpProj = VSIFOpenL(osPrjFilename, "wt"); if (fpProj != nullptr) { OGRSpatialReference oSRS; oSRS.importFromWkt(pszProjection); oSRS.morphToESRI(); char *pszESRIProjection = nullptr; oSRS.exportToWkt(&pszESRIProjection); CPL_IGNORE_RET_VAL( VSIFWriteL(pszESRIProjection, 1, strlen(pszESRIProjection), fpProj)); CPL_IGNORE_RET_VAL(VSIFCloseL(fpProj)); CPLFree(pszESRIProjection); } else { CPLError(CE_Failure, CPLE_FileIO, "Unable to create file %s.", osPrjFilename.c_str()); } } } GDALOpenInfo oOpenInfo(pszFilename, GA_Update); GDALDataset * poDstDS = Open(&oOpenInfo); if (poDstDS != nullptr && GDALDatasetCopyWholeRaster(poSrcDS, poDstDS, nullptr, pfnProgress, pProgressData) == OGRERR_NONE) { return poDstDS; } else { VSIUnlink(pszFilename); return nullptr; } }
OGRErr OGRPolygon::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData, OGRwkbVariant eWkbVariant ) const { /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if( eWkbVariant == wkbVariantPostGIS1 ) { nGType = wkbFlatten(nGType); if( Is3D() ) nGType = (OGRwkbGeometryType)(nGType | wkb25DBitInternalUse); /* yes we explicitly set wkb25DBit */ if( IsMeasured() ) nGType = (OGRwkbGeometryType)(nGType | 0x40000000); } else if ( eWkbVariant == wkbVariantIso ) nGType = getIsoGeometryType(); if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { int nCount; nCount = CPL_SWAP32( oCC.nCurveCount ); memcpy( pabyData+5, &nCount, 4 ); } else { memcpy( pabyData+5, &oCC.nCurveCount, 4 ); } /* ==================================================================== */ /* Serialize each of the rings. */ /* ==================================================================== */ int nOffset = 9; for( int iRing = 0; iRing < oCC.nCurveCount; iRing++ ) { OGRLinearRing* poLR = (OGRLinearRing*) oCC.papoCurves[iRing]; poLR->_exportToWkb( eByteOrder, flags, pabyData + nOffset ); nOffset += poLR->_WkbSize( flags ); } return OGRERR_NONE; }
GDALDataset *ELASDataset::Create( const char * pszFilename, int nXSize, int nYSize, int nBands, GDALDataType eType, char ** /* notdef: papszParmList */ ) { int nBandOffset; /* -------------------------------------------------------------------- */ /* Verify input options. */ /* -------------------------------------------------------------------- */ if (nBands <= 0) { CPLError( CE_Failure, CPLE_NotSupported, "ELAS driver does not support %d bands.\n", nBands); return NULL; } if( eType != GDT_Byte && eType != GDT_Float32 && eType != GDT_Float64 ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to create an ELAS dataset with an illegal\n" "data type (%d).\n", eType ); return NULL; } /* -------------------------------------------------------------------- */ /* Try to create the file. */ /* -------------------------------------------------------------------- */ FILE *fp; fp = VSIFOpen( pszFilename, "w" ); if( fp == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Attempt to create file `%s' failed.\n", pszFilename ); return NULL; } /* -------------------------------------------------------------------- */ /* How long will each band of a scanline be? */ /* -------------------------------------------------------------------- */ nBandOffset = nXSize * GDALGetDataTypeSize(eType)/8; if( nBandOffset % 256 != 0 ) { nBandOffset = nBandOffset - (nBandOffset % 256) + 256; } /* -------------------------------------------------------------------- */ /* Setup header data block. */ /* */ /* Note that CPL_MSBWORD32() will swap little endian words to */ /* big endian on little endian platforms. */ /* -------------------------------------------------------------------- */ ELASHeader sHeader; memset( &sHeader, 0, 1024 ); sHeader.NBIH = CPL_MSBWORD32(1024); sHeader.NBPR = CPL_MSBWORD32(nBands * nBandOffset); sHeader.IL = CPL_MSBWORD32(1); sHeader.LL = CPL_MSBWORD32(nYSize); sHeader.IE = CPL_MSBWORD32(1); sHeader.LE = CPL_MSBWORD32(nXSize); sHeader.NC = CPL_MSBWORD32(nBands); sHeader.H4321 = CPL_MSBWORD32(4321); sHeader.IH19[0] = 0x04; sHeader.IH19[1] = 0xd2; sHeader.IH19[3] = (GByte) (GDALGetDataTypeSize(eType) / 8); if( eType == GDT_Byte ) sHeader.IH19[2] = 1 << 2; else if( eType == GDT_Float32 ) sHeader.IH19[2] = 16 << 2; else if( eType == GDT_Float64 ) sHeader.IH19[2] = 17 << 2; /* -------------------------------------------------------------------- */ /* Write the header data. */ /* -------------------------------------------------------------------- */ VSIFWrite( &sHeader, 1024, 1, fp ); /* -------------------------------------------------------------------- */ /* Now write out zero data for all the imagery. This is */ /* inefficient, but simplies the IReadBlock() / IWriteBlock() logic.*/ /* -------------------------------------------------------------------- */ GByte *pabyLine; pabyLine = (GByte *) CPLCalloc(nBandOffset,nBands); for( int iLine = 0; iLine < nYSize; iLine++ ) { if( VSIFWrite( pabyLine, 1, nBandOffset, fp ) != (size_t) nBandOffset ) { CPLError( CE_Failure, CPLE_FileIO, "Error writing ELAS image data ... likely insufficient" " disk space.\n" ); VSIFClose( fp ); CPLFree( pabyLine ); return NULL; } } CPLFree( pabyLine ); VSIFClose( fp ); /* -------------------------------------------------------------------- */ /* Try to return a regular handle on the file. */ /* -------------------------------------------------------------------- */ return (GDALDataset *) GDALOpen( pszFilename, GA_Update ); }
OGRErr OGRPoint::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData, OGRwkbVariant eWkbVariant ) const { /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER(static_cast<unsigned char>(eByteOrder)); pabyData += 1; /* -------------------------------------------------------------------- */ /* Set the geometry feature type. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if( eWkbVariant == wkbVariantPostGIS1 ) { nGType = wkbFlatten(nGType); if( Is3D() ) // Explicitly set wkb25DBit. nGType = static_cast<OGRwkbGeometryType>(nGType | wkb25DBitInternalUse); if( IsMeasured() ) nGType = static_cast<OGRwkbGeometryType>(nGType | 0x40000000); } else if( eWkbVariant == wkbVariantIso ) { nGType = getIsoGeometryType(); } if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData, &nGType, 4 ); pabyData += 4; /* -------------------------------------------------------------------- */ /* Copy in the raw data. Swap if needed. */ /* -------------------------------------------------------------------- */ if( IsEmpty() && eWkbVariant == wkbVariantIso ) { const double dNan = std::numeric_limits<double>::quiet_NaN(); memcpy( pabyData, &dNan, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); pabyData += 8; memcpy( pabyData, &dNan, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); pabyData += 8; if( flags & OGR_G_3D ) { memcpy( pabyData, &dNan, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); pabyData += 8; } if( flags & OGR_G_MEASURED ) { memcpy( pabyData, &dNan, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); } } else { memcpy( pabyData, &x, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); pabyData += 8; memcpy( pabyData, &y, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); pabyData += 8; if( flags & OGR_G_3D ) { memcpy( pabyData, &z, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); pabyData += 8; } if( flags & OGR_G_MEASURED ) { memcpy( pabyData, &m, 8 ); if( OGR_SWAP( eByteOrder ) ) CPL_SWAPDOUBLE( pabyData ); } } return OGRERR_NONE; }
OGRErr OGRGeometryCollection::exportToWkb( OGRwkbByteOrder eByteOrder, unsigned char * pabyData, OGRwkbVariant eWkbVariant ) const { if( eWkbVariant == wkbVariantOldOgc && (wkbFlatten(getGeometryType()) == wkbMultiCurve || wkbFlatten(getGeometryType()) == wkbMultiSurface) ) /* does not make sense for new geometries, so patch it */ { eWkbVariant = wkbVariantIso; } /* -------------------------------------------------------------------- */ /* Set the byte order. */ /* -------------------------------------------------------------------- */ pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER((unsigned char) eByteOrder); /* -------------------------------------------------------------------- */ /* Set the geometry feature type, ensuring that 3D flag is */ /* preserved. */ /* -------------------------------------------------------------------- */ GUInt32 nGType = getGeometryType(); if ( eWkbVariant == wkbVariantIso ) nGType = getIsoGeometryType(); else if( eWkbVariant == wkbVariantPostGIS1 ) { int bIs3D = wkbHasZ((OGRwkbGeometryType)nGType); nGType = wkbFlatten(nGType); if( nGType == wkbMultiCurve ) nGType = POSTGIS15_MULTICURVE; else if( nGType == wkbMultiSurface ) nGType = POSTGIS15_MULTISURFACE; if( bIs3D ) nGType = (OGRwkbGeometryType)(nGType | wkb25DBitInternalUse); /* yes we explicitly set wkb25DBit */ } if( eByteOrder == wkbNDR ) nGType = CPL_LSBWORD32( nGType ); else nGType = CPL_MSBWORD32( nGType ); memcpy( pabyData + 1, &nGType, 4 ); /* -------------------------------------------------------------------- */ /* Copy in the raw data. */ /* -------------------------------------------------------------------- */ if( OGR_SWAP( eByteOrder ) ) { int nCount = CPL_SWAP32( nGeomCount ); memcpy( pabyData+5, &nCount, 4 ); } else { memcpy( pabyData+5, &nGeomCount, 4 ); } int nOffset = 9; /* ==================================================================== */ /* Serialize each of the Geoms. */ /* ==================================================================== */ for( int iGeom = 0; iGeom < nGeomCount; iGeom++ ) { papoGeoms[iGeom]->exportToWkb( eByteOrder, pabyData + nOffset, eWkbVariant ); // Should normally not happen if everyone else does its job // but has happened sometimes (#6332) if( papoGeoms[iGeom]->getCoordinateDimension() != getCoordinateDimension() ) { CPLError( CE_Warning, CPLE_AppDefined, "Sub-geometry %d has coordinate dimension %d, but container has %d", iGeom, papoGeoms[iGeom]->getCoordinateDimension(), getCoordinateDimension() ); } nOffset += papoGeoms[iGeom]->WkbSize(); } return OGRERR_NONE; }
void EnvisatDataset::ScanForGCPs_MERIS() { int nDatasetIndex, nNumDSR, nDSRSize, iRecord; /* -------------------------------------------------------------------- */ /* Do we have a meaningful geolocation grid? Seach for a */ /* DS_TYPE=A and a name containing "geolocation" or "tie */ /* points". */ /* -------------------------------------------------------------------- */ nDatasetIndex = EnvisatFile_GetDatasetIndex( hEnvisatFile, "Tie points ADS" ); if( nDatasetIndex == -1 ) return; if( EnvisatFile_GetDatasetInfo( hEnvisatFile, nDatasetIndex, NULL, NULL, NULL, NULL, NULL, &nNumDSR, &nDSRSize ) != SUCCESS ) return; if( nNumDSR == 0 ) return; /* -------------------------------------------------------------------- */ /* Figure out the tiepoint space, and how many we have. */ /* -------------------------------------------------------------------- */ int nLinesPerTiePoint, nSamplesPerTiePoint; int nTPPerLine, nTPPerColumn = nNumDSR; if( nNumDSR == 0 ) return; nLinesPerTiePoint = EnvisatFile_GetKeyValueAsInt( hEnvisatFile, SPH, "LINES_PER_TIE_PT", 0 ); nSamplesPerTiePoint = EnvisatFile_GetKeyValueAsInt( hEnvisatFile, SPH, "SAMPLES_PER_TIE_PT", 0 ); if( nLinesPerTiePoint == 0 || nSamplesPerTiePoint == 0 ) return; nTPPerLine = (GetRasterXSize() + nSamplesPerTiePoint - 1) / nSamplesPerTiePoint; if( (GetRasterYSize() + nLinesPerTiePoint - 1) / nLinesPerTiePoint != nTPPerColumn ) { CPLDebug( "EnvisatDataset", "Got %d instead of %d nTPPerColumn.", (GetRasterYSize()+nLinesPerTiePoint-1)/nLinesPerTiePoint, nTPPerColumn ); return; } if( 50*nTPPerLine + 13 != nDSRSize ) { CPLDebug( "EnvisatDataset", "DSRSize=%d instead of expected %d for tiepoints ADS.", nDSRSize, 50*nTPPerLine + 13 ); return; } /* -------------------------------------------------------------------- */ /* Collect the first GCP set from each record. */ /* -------------------------------------------------------------------- */ GByte *pabyRecord = (GByte *) CPLMalloc(nDSRSize); int iGCP; GUInt32 unValue; nGCPCount = 0; pasGCPList = (GDAL_GCP *) CPLCalloc(sizeof(GDAL_GCP),nNumDSR * nTPPerLine); for( iRecord = 0; iRecord < nNumDSR; iRecord++ ) { if( EnvisatFile_ReadDatasetRecord( hEnvisatFile, nDatasetIndex, iRecord, pabyRecord ) != SUCCESS ) continue; memcpy( &unValue, pabyRecord + 13, 4 ); for( iGCP = 0; iGCP < nTPPerLine; iGCP++ ) { char szId[128]; GDALInitGCPs( 1, pasGCPList + nGCPCount ); CPLFree( pasGCPList[nGCPCount].pszId ); sprintf( szId, "%d", nGCPCount+1 ); pasGCPList[nGCPCount].pszId = CPLStrdup( szId ); memcpy( &unValue, pabyRecord + 13 + nTPPerLine*4 + iGCP*4, 4 ); pasGCPList[nGCPCount].dfGCPX = ((int)CPL_MSBWORD32(unValue))*0.000001; memcpy( &unValue, pabyRecord + 13 + iGCP*4, 4 ); pasGCPList[nGCPCount].dfGCPY = ((int)CPL_MSBWORD32(unValue))*0.000001; pasGCPList[nGCPCount].dfGCPZ = 0.0; pasGCPList[nGCPCount].dfGCPLine = iRecord*nLinesPerTiePoint + 0.5; pasGCPList[nGCPCount].dfGCPPixel = iGCP*nSamplesPerTiePoint + 0.5; nGCPCount++; } } CPLFree( pabyRecord ); }
GDALDataset *SGIDataset::Create( const char * pszFilename, int nXSize, int nYSize, int nBands, GDALDataType eType, CPL_UNUSED char **papszOptions ) { if( eType != GDT_Byte ) { CPLError( CE_Failure, CPLE_AppDefined, "Attempt to create SGI dataset with an illegal\n" "data type (%s), only Byte supported by the format.\n", GDALGetDataTypeName(eType) ); return NULL; } /* -------------------------------------------------------------------- */ /* Open the file for output. */ /* -------------------------------------------------------------------- */ VSILFILE *fp = VSIFOpenL( pszFilename, "w" ); if( fp == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to create file '%s': %s", pszFilename, VSIStrerror( errno ) ); return NULL; } /* -------------------------------------------------------------------- */ /* Prepare and write 512 byte header. */ /* -------------------------------------------------------------------- */ GByte abyHeader[512]; GInt16 nShortValue; GInt32 nIntValue; memset( abyHeader, 0, 512 ); abyHeader[0] = 1; abyHeader[1] = 218; abyHeader[2] = 1; // RLE abyHeader[3] = 1; // 8bit if( nBands == 1 ) nShortValue = CPL_MSBWORD16(2); else nShortValue = CPL_MSBWORD16(3); memcpy( abyHeader + 4, &nShortValue, 2 ); nShortValue = CPL_MSBWORD16(nXSize); memcpy( abyHeader + 6, &nShortValue, 2 ); nShortValue = CPL_MSBWORD16(nYSize); memcpy( abyHeader + 8, &nShortValue, 2 ); nShortValue = CPL_MSBWORD16(nBands); memcpy( abyHeader + 10, &nShortValue, 2 ); nIntValue = CPL_MSBWORD32(0); memcpy( abyHeader + 12, &nIntValue, 4 ); nIntValue = CPL_MSBWORD32(255); memcpy( abyHeader + 16, &nIntValue, 4 ); VSIFWriteL( abyHeader, 1, 512, fp ); /* -------------------------------------------------------------------- */ /* Create our RLE compressed zero-ed dummy line. */ /* -------------------------------------------------------------------- */ GByte *pabyRLELine; GInt32 nRLEBytes = 0; int nPixelsRemaining = nXSize; pabyRLELine = (GByte *) CPLMalloc((nXSize/127) * 2 + 4); while( nPixelsRemaining > 0 ) { pabyRLELine[nRLEBytes] = (GByte) MIN(127,nPixelsRemaining); pabyRLELine[nRLEBytes+1] = 0; nPixelsRemaining -= pabyRLELine[nRLEBytes]; nRLEBytes += 2; } /* -------------------------------------------------------------------- */ /* Prepare and write RLE offset/size tables with everything */ /* zeroed indicating dummy lines. */ /* -------------------------------------------------------------------- */ int i; int nTableLen = nYSize * nBands; GInt32 nDummyRLEOffset = 512 + 4 * nTableLen * 2; CPL_MSBPTR32( &nRLEBytes ); CPL_MSBPTR32( &nDummyRLEOffset ); for( i = 0; i < nTableLen; i++ ) VSIFWriteL( &nDummyRLEOffset, 1, 4, fp ); for( i = 0; i < nTableLen; i++ ) VSIFWriteL( &nRLEBytes, 1, 4, fp ); /* -------------------------------------------------------------------- */ /* write the dummy RLE blank line. */ /* -------------------------------------------------------------------- */ CPL_MSBPTR32( &nRLEBytes ); if( (GInt32) VSIFWriteL( pabyRLELine, 1, nRLEBytes, fp ) != nRLEBytes ) { CPLError( CE_Failure, CPLE_FileIO, "Failure writing SGI file '%s'.\n%s", pszFilename, VSIStrerror( errno ) ); return NULL; } VSIFCloseL( fp ); CPLFree( pabyRLELine ); return (GDALDataset*) GDALOpen( pszFilename, GA_Update ); }