Esempio n. 1
0
void Dust::MakeGrid(WindNinjaInputs &input, AsciiGrid<double> &grid)
{
    /*------------------------------------------*/
    /* Open grid as a GDAL dataset              */
    /*------------------------------------------*/
    int nXSize = grid.get_nCols();
    int nYSize = grid.get_nRows();
    
    GDALDriverH hDriver = GDALGetDriverByName( "MEM" );
        
    GDALDatasetH hMemDS = GDALCreate(hDriver, "", nXSize, nYSize, 1, GDT_Float64, NULL);
    
    double *padfScanline;
    padfScanline = new double[nXSize];
    
    double adfGeoTransform[6];
    adfGeoTransform[0] = grid.get_xllCorner();
    adfGeoTransform[1] = grid.get_cellSize();
    adfGeoTransform[2] = 0;
    adfGeoTransform[3] = grid.get_yllCorner()+(grid.get_nRows()*grid.get_cellSize());
    adfGeoTransform[4] = 0;
    adfGeoTransform[5] = -grid.get_cellSize();
        
    char* pszDstWKT = (char*)grid.prjString.c_str();
    GDALSetProjection(hMemDS, pszDstWKT);
    GDALSetGeoTransform(hMemDS, adfGeoTransform);
        
    GDALRasterBandH hBand = GDALGetRasterBand( hMemDS, 1 );
        
    GDALSetRasterNoDataValue(hBand, -9999.0);        

    for(int i=nYSize-1; i>=0; i--)
    {
        for(int j=0; j<nXSize; j++)
        {  
            padfScanline[j] = grid.get_cellValue(nYSize-1-i, j);
        }
        GDALRasterIO(hBand, GF_Write, 0, i, nXSize, 1, padfScanline, nXSize,
                     1, GDT_Float64, 0, 0);
    }
    
    /*------------------------------------------*/
    /* Get the geometry info                    */
    /*------------------------------------------*/
    
    OGRDataSourceH hOGRDS = 0;
    hOGRDS = OGROpen(input.dustFilename.c_str(), FALSE, 0);
    if(hOGRDS == NULL)
    {
        throw std::runtime_error("Could not open the fire perimeter file '" +
              input.dustFilename + "' for reading.");
    }
    OGRLayer *poLayer;
    OGRFeature *poFeature;
    OGRGeometry *poGeo;
    
    poLayer = (OGRLayer*)OGR_DS_GetLayer(hOGRDS, 0);
    poLayer->ResetReading();
    poFeature = poLayer->GetNextFeature();
    poGeo = poFeature->GetGeometryRef();
    OGRGeometryH hPolygon = (OGRGeometryH) poGeo;


    /* -------------------------------------------------------------------- */
    /*  Check for same CRS in fire perimeter and DEM files                  */
    /* -------------------------------------------------------------------- */

    char *pszSrcWKT;
    OGRSpatialReference *poSrcSRS, oDstSRS;
    poSrcSRS = poLayer->GetSpatialRef(); //shapefile CRS
    poSrcSRS->exportToWkt( &pszSrcWKT );

    //printf("CRS of DEM is:\n %s\n", pszDstWKT);
    //printf("WKT CRS of .shp is:\n %s\n", pszSrcWKT);
    
    oDstSRS.importFromWkt( &pszDstWKT );
    
    char *pszDstProj4, *pszSrcProj4;
    oDstSRS.exportToProj4( &pszDstProj4 );
    poSrcSRS->exportToProj4( &pszSrcProj4 );
    
    //printf("proj4 of .shp is:\n %s\n", pszSrcProj4);
    //printf("proj4 of dem is:\n %s\n", pszDstProj4);
    
    /* -------------------------------------------------------------------- */
    /*  If the CRSs are not equal, convert shapefile CRS to DEM CRS         */
    /* -------------------------------------------------------------------- */

    GDALTransformerFunc pfnTransformer = NULL;
    if( !EQUAL( pszSrcProj4, pszDstProj4 ) ){ //tranform shp CRS to DEM CRS
        poGeo->transformTo(&oDstSRS);
    }
     
    /* -------------------------------------------------------------------- */
    /*  Rasterize the shapefile                                             */
    /* -------------------------------------------------------------------- */
    
    int nTargetBand = 1;
    double BurnValue = 1.0;
    CPLErr eErr;
    
    eErr = GDALRasterizeGeometries(hMemDS, 1, &nTargetBand, 1, &hPolygon, pfnTransformer, NULL, &BurnValue, NULL, NULL, NULL);
    if(eErr != CE_None)
    {
        throw std::runtime_error("Error in GDALRasterizeGeometies in Dust:MakeGrid().");
    }
    
    GDAL2AsciiGrid((GDALDataset*)hMemDS, 1, grid);
    
    /* -------------------------------------------------------------------- */
    /*   clean up                                                           */
    /* -------------------------------------------------------------------- */
    
    if( hMemDS != NULL ){
        GDALClose( hMemDS );
        hMemDS = NULL;
    }
    
    OGR_DS_Destroy(hOGRDS);
}