OGRLayer *GNMGenericNetwork::CopyLayer(OGRLayer *poSrcLayer, const char *pszNewName, char **papszOptions) { if(CSLFindName(papszOptions, "DST_SRSWKT") == -1) { papszOptions = CSLAddNameValue(papszOptions, "DST_SRSWKT", GetProjectionRef()); } else { papszOptions = CSLSetNameValue(papszOptions, "DST_SRSWKT", GetProjectionRef()); } return GDALDataset::CopyLayer(poSrcLayer, pszNewName, papszOptions); }
OGRLayer *GNMGenericNetwork::GetPath(GNMGFID nStartFID, GNMGFID nEndFID, GNMGraphAlgorithmType eAlgorithm, char **papszOptions) { if(!m_bIsGraphLoaded && LoadGraph() != CE_None) { return NULL; } GDALDriver* poMEMDrv = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("Memory"); if (poMEMDrv == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot load 'Memory' driver"); return NULL; } GDALDataset* poMEMDS = poMEMDrv->Create("dummy_name", 0, 0, 0, GDT_Unknown, NULL); OGRSpatialReference oDstSpaRef(GetProjectionRef()); OGRLayer* poMEMLayer = poMEMDS->CreateLayer(GetAlgorithmName(eAlgorithm, true), &oDstSpaRef, wkbGeometryCollection, NULL); OGRGNMWrappedResultLayer* poResLayer = new OGRGNMWrappedResultLayer(poMEMDS, poMEMLayer); const bool bReturnEdges = CPLFetchBool(papszOptions, GNM_MD_FETCHEDGES, true); const bool bReturnVertices = CPLFetchBool(papszOptions, GNM_MD_FETCHVERTEX, true); switch (eAlgorithm) { case GATDijkstraShortestPath: { GNMPATH path = m_oGraph.DijkstraShortestPath(nStartFID, nEndFID); // fill features in result layer FillResultLayer(poResLayer, path, 1, bReturnVertices, bReturnEdges); } break; case GATKShortestPath: { int nK = atoi(CSLFetchNameValueDef(papszOptions, GNM_MD_NUM_PATHS, "1")); CPLDebug("GNM", "Search %d path(s)", nK); std::vector<GNMPATH> paths = m_oGraph.KShortestPaths(nStartFID, nEndFID, nK); // fill features in result layer for(size_t i = 0; i < paths.size(); ++i) { FillResultLayer(poResLayer, paths[i], static_cast<int>(i + 1), bReturnVertices, bReturnEdges); } } break; case GATConnectedComponents: { GNMVECTOR anEmitters; if(NULL != papszOptions) { char** papszEmitter = CSLFetchNameValueMultiple(papszOptions, GNM_MD_EMITTER); for(int i = 0; papszEmitter[i] != NULL; ++i) { GNMGFID nEmitter = atol(papszEmitter[i]); anEmitters.push_back(nEmitter); } CSLDestroy(papszEmitter); } if(nStartFID != -1) { anEmitters.push_back(nStartFID); } if(nStartFID != -1) { anEmitters.push_back(nEndFID); } GNMPATH path = m_oGraph.ConnectedComponents(anEmitters); // fill features in result layer FillResultLayer(poResLayer, path, 1, bReturnVertices, bReturnEdges); } break; } return poResLayer; }
int Raster::Copy(const char * pOutputRaster, double * dNewCellSize, double fLeft, double fTop, int nRows, int nCols) { if (fLeft <=0) return LEFT_ERROR; if (fTop <=0) return TOP_ERROR; if (nRows <=0) return ROWS_ERROR; if (nCols <=0) return COLS_ERROR; // Open the original dataset GDALDataset * pDSOld = (GDALDataset*) GDALOpen(m_sFilePath, GA_ReadOnly); if (pDSOld == NULL) return INPUT_FILE_ERROR; GDALRasterBand * pRBInput = pDSOld->GetRasterBand(1); /* Create the new dataset. Determine the driver from the output file extension. * Enforce LZW compression for TIFs. The predictor 3 is used for floating point prediction. * Not using this value defaults the LZW to prediction to 1 which causes striping. */ char **papszOptions = NULL; GDALDriver * pDR = NULL; std::string psDR = ""; const char * pSuffix = ExtractFileExt(pOutputRaster); if (pSuffix == NULL) return OUTPUT_FILE_EXT_ERROR; else { if (strcmp(pSuffix, ".tif") == 0) { psDR = "GTiff"; pDR = GetGDALDriverManager()->GetDriverByName(psDR.c_str()); papszOptions = CSLSetNameValue(papszOptions, "COMPRESS", "LZW"); //papszOptions = CSLSetNameValue(papszOptions, "PREDICTOR", "3"); } else if (strcmp(pSuffix, ".img") == 0){ psDR = "HFA"; pDR = GetGDALDriverManager()->GetDriverByName(psDR.c_str()); } else return OUTPUT_UNHANDLED_DRIVER; } double dNewCellHeight = (*dNewCellSize) * -1; RasterMeta OutputMeta(fTop, fLeft, nRows, nCols, &dNewCellHeight, dNewCellSize, GetNoDataValuePtr(), psDR.c_str(), GetGDALDataType(), GetProjectionRef() ); //const char * pC = pDR->GetDescription(); GDALDataset * pDSOutput = pDR->Create(pOutputRaster, nCols, nRows, 1, *GetGDALDataType(), papszOptions); CSLDestroy( papszOptions ); if (pDSOutput == NULL) return OUTPUT_FILE_ERROR; if (HasNoDataValue()) { CPLErr er = pDSOutput->GetRasterBand(1)->SetNoDataValue(GetNoDataValue()); if (er == CE_Failure || er == CE_Fatal) return OUTPUT_NO_DATA_ERROR; } pDSOutput->SetGeoTransform(OutputMeta.GetGeoTransform()); pDSOutput->SetProjection(pDSOld->GetProjectionRef()); int nInputCols = pRBInput->GetXSize(); int nInputRows = pRBInput->GetYSize(); double * pInputLine = (double *) CPLMalloc(sizeof(double)*nInputCols); double * pOutputLine = (double *) CPLMalloc(sizeof(double)*pDSOutput->GetRasterBand(1)->GetXSize()); int nRowTrans = GetRowTranslation(&OutputMeta); int nColTrans = GetColTranslation(&OutputMeta); /* * Loop over the raster rows. Note that geographic coordinate origin is bottom left. But * the GDAL image anchor is top left. The cell height is negative. * * The loop is over the centres of the output raster cells. Two rows are read from the * input raster. The line just above the output cell and the line just below. The line * just above is called the "anchor" row. */ int nOldRow, nOldCol; int i, j; for (i = 0; i < nRows; i++) { nOldRow = i - nRowTrans; if (nOldRow >= 0 && nOldRow < nInputRows) { pRBInput->RasterIO(GF_Read, 0, nOldRow, nInputCols, 1, pInputLine, nInputCols, 1, GDT_Float64, 0, 0); for (j = 0; j < nCols; j++) { nOldCol = j + nColTrans; if (nOldCol >=0 && nOldCol < nInputCols) { pOutputLine[j] = pInputLine[nOldCol]; } else { if (HasNoDataValue()) { pOutputLine[j] = GetNoDataValue(); } else { pOutputLine[j] = 0; } } } } else { // Outside the bounds of the input image. Loop over all cells in current output row and set to NoData. for (j = 0; j < nCols; j++) { pOutputLine[j] = GetNoDataValue(); } } pDSOutput->GetRasterBand(1)->RasterIO(GF_Write, 0, i, pDSOutput->GetRasterBand(1)->GetXSize(), 1, pOutputLine, pDSOutput->GetRasterBand(1)->GetXSize(), 1, GDT_Float64, 0, 0); } CPLFree(pInputLine); CPLFree(pOutputLine); CalculateStats(pDSOutput->GetRasterBand(1)); GDALClose(pDSOld); GDALClose(pDSOutput); GDALDumpOpenDatasets(stderr); return PROCESS_OK; }
/* * Algorithm taken from: http://www.quantdec.com/SYSEN597/GTKAV/section9/map_algebra.htm * */ int Raster::ReSample(const char * pOutputRaster, double fNewCellSize, double fNewLeft, double fNewTop, int nNewRows, int nNewCols) { if (fNewCellSize <= 0) return CELL_SIZE_ERROR; if (fNewLeft <=0) return LEFT_ERROR; if (fNewTop <=0) return TOP_ERROR; if (nNewRows <=0) return ROWS_ERROR; if (nNewCols <=0) return COLS_ERROR; /************************************************************************************************* * Open the original dataset and retrieve its basic properties */ GDALDataset * pDSOld = (GDALDataset*) GDALOpen(m_sFilePath, GA_ReadOnly); if (pDSOld == NULL) return INPUT_FILE_ERROR; GDALRasterBand * pRBInput = pDSOld->GetRasterBand(1); /************************************************************************************************* * Create the new dataset. Determine the driver from the output file extension. * Enforce LZW compression for TIFs. The predictor 3 is used for floating point prediction. * Not using this value defaults the LZW to prediction to 1 which causes striping. */ char **papszOptions = NULL; GDALDriver * pDR = NULL; const char * pSuffix = ExtractFileExt(pOutputRaster); if (pSuffix == NULL) return OUTPUT_FILE_EXT_ERROR; else { if (strcmp(pSuffix, ".tif") == 0) { pDR = GetGDALDriverManager()->GetDriverByName("GTiff"); papszOptions = CSLSetNameValue(papszOptions, "COMPRESS", "LZW"); //papszOptions = CSLSetNameValue(papszOptions, "PREDICTOR", "3"); } else if (strcmp(pSuffix, ".img") == 0) pDR = GetGDALDriverManager()->GetDriverByName("HFA"); else return OUTPUT_UNHANDLED_DRIVER; } GDALDataset * pDSOutput = pDR->Create(pOutputRaster, nNewCols, nNewRows, 1, *GetGDALDataType(), papszOptions); CSLDestroy( papszOptions ); if (pDSOutput == NULL) return OUTPUT_FILE_ERROR; GDALRasterBand * pRBOutput = pDSOutput->GetRasterBand(1); if (HasNoDataValue()) { CPLErr er = pRBOutput->SetNoDataValue(this->GetNoDataValue()); if (er == CE_Failure || er == CE_Fatal) return OUTPUT_NO_DATA_ERROR; } double newTransform[6]; newTransform[0] = fNewLeft; newTransform[1] = fNewCellSize; newTransform[2] = 0; newTransform[3] = fNewTop; newTransform[4] = 0; newTransform[5] = -1 * fNewCellSize; pDSOutput->SetGeoTransform(newTransform); pDSOutput->SetProjection(GetProjectionRef()); ReSampleRaster(pRBInput, pRBOutput, fNewCellSize, fNewLeft, fNewTop, nNewRows, nNewCols); CalculateStats(pDSOutput->GetRasterBand(1)); GDALClose(pDSOld); GDALClose(pDSOutput); return PROCESS_OK; }
CPLErr GDALPamDataset::CloneInfo( GDALDataset *poSrcDS, int nCloneFlags ) { int bOnlyIfMissing = nCloneFlags & GCIF_ONLY_IF_MISSING; int nSavedMOFlags = GetMOFlags(); PamInitialize(); /* -------------------------------------------------------------------- */ /* Supress NotImplemented error messages - mainly needed if PAM */ /* disabled. */ /* -------------------------------------------------------------------- */ SetMOFlags( nSavedMOFlags | GMO_IGNORE_UNIMPLEMENTED ); /* -------------------------------------------------------------------- */ /* GeoTransform */ /* -------------------------------------------------------------------- */ if( nCloneFlags & GCIF_GEOTRANSFORM ) { double adfGeoTransform[6]; if( poSrcDS->GetGeoTransform( adfGeoTransform ) == CE_None ) { double adfOldGT[6]; if( !bOnlyIfMissing || GetGeoTransform( adfOldGT ) != CE_None ) SetGeoTransform( adfGeoTransform ); } } /* -------------------------------------------------------------------- */ /* Projection */ /* -------------------------------------------------------------------- */ if( nCloneFlags & GCIF_PROJECTION ) { const char *pszWKT = poSrcDS->GetProjectionRef(); if( pszWKT != NULL && strlen(pszWKT) > 0 ) { if( !bOnlyIfMissing || GetProjectionRef() == NULL || strlen(GetProjectionRef()) == 0 ) SetProjection( pszWKT ); } } /* -------------------------------------------------------------------- */ /* GCPs */ /* -------------------------------------------------------------------- */ if( nCloneFlags & GCIF_GCPS ) { if( poSrcDS->GetGCPCount() > 0 ) { if( !bOnlyIfMissing || GetGCPCount() == 0 ) { SetGCPs( poSrcDS->GetGCPCount(), poSrcDS->GetGCPs(), poSrcDS->GetGCPProjection() ); } } } /* -------------------------------------------------------------------- */ /* Metadata */ /* -------------------------------------------------------------------- */ if( nCloneFlags & GCIF_METADATA ) { if( poSrcDS->GetMetadata() != NULL ) { if( !bOnlyIfMissing || CSLCount(GetMetadata()) != CSLCount(poSrcDS->GetMetadata()) ) { SetMetadata( poSrcDS->GetMetadata() ); } } if( poSrcDS->GetMetadata("RPC") != NULL ) { if( !bOnlyIfMissing || CSLCount(GetMetadata("RPC")) != CSLCount(poSrcDS->GetMetadata("RPC")) ) { SetMetadata( poSrcDS->GetMetadata("RPC"), "RPC" ); } } } /* -------------------------------------------------------------------- */ /* Process bands. */ /* -------------------------------------------------------------------- */ if( nCloneFlags & GCIF_PROCESS_BANDS ) { int iBand; for( iBand = 0; iBand < GetRasterCount(); iBand++ ) { GDALPamRasterBand *poBand = (GDALPamRasterBand *) GetRasterBand(iBand+1); if( poBand == NULL || !(poBand->GetMOFlags() & GMO_PAM_CLASS) ) continue; if( poSrcDS->GetRasterCount() >= iBand+1 ) poBand->CloneInfo( poSrcDS->GetRasterBand(iBand+1), nCloneFlags ); else CPLDebug( "GDALPamDataset", "Skipping CloneInfo for band not in source, this is a bit unusual!" ); } } /* -------------------------------------------------------------------- */ /* Copy masks. These are really copied at a lower level using */ /* GDALDefaultOverviews, for formats with no native mask */ /* support but this is a convenient central point to put this */ /* for most drivers. */ /* -------------------------------------------------------------------- */ if( nCloneFlags & GCIF_MASK ) { GDALDriver::DefaultCopyMasks( poSrcDS, this, FALSE ); } /* -------------------------------------------------------------------- */ /* Restore MO flags. */ /* -------------------------------------------------------------------- */ SetMOFlags( nSavedMOFlags ); return CE_None; }