Example #1
0
bool V2vProj::Compute(const data::VectorBarral * barrel)
{
    OGRDataSource * poSourceDs = VectorOpen(barrel->GetSrcDataSource().c_str(),
                                            GA_ReadOnly);
    ON_SCOPE_EXIT([&]() {OGRDataSource::DestroyDataSource(poSourceDs); });
    OGRDataSource * poOutputDs = VectorOpen(barrel->GetDstDataSource().c_str(),
                                            GA_Update);
    ON_SCOPE_EXIT([&]() {OGRDataSource::DestroyDataSource(poOutputDs); });
    OGRLayer * poSrcLayer = poSourceDs->GetLayerByName(
                                barrel->GetSrcLayer().c_str());
    OGRLayer * poDstLayer = poOutputDs->GetLayerByName(
                                barrel->GetDstLayer().c_str());
    OGRSpatialReference * poSourceSRS = poSrcLayer->GetSpatialRef();
    OGRCoordinateTransformation * poCT = poCT = OGRCreateCoordinateTransformation(
            poSourceSRS, m_ogrSr);
    OGRFeatureDefn * poDstFeatureDefn = poDstLayer->GetLayerDefn();
    auto features = barrel->GetFeatures();
    std::for_each(begin(features), end(features)
    , [&](int fid) {
        poSrcLayer->GetFeature(fid);
        OGRFeature * poDstFeature = OGRFeature::CreateFeature(poDstFeatureDefn);
        ON_SCOPE_EXIT([&]() {OGRFeature::DestroyFeature(poDstFeature); });
        poDstFeature->SetFrom(poSrcLayer->GetFeature(fid));
        OGRGeometry * poDstGeometry = poDstFeature->GetGeometryRef();
        OGRGeometry * poReprojectedGeom = OGRGeometryFactory::transformWithOptions(
                                              poDstGeometry, poCT, NULL);
        poDstFeature->SetGeometryDirectly(poReprojectedGeom);
        poDstLayer->CreateFeature(poDstFeature);
    });
    return true;
}
OGRFeature* OGROpenFileGDBSimpleSQLLayer::GetFeature( GIntBig nFeatureId )
{
    OGRFeature* poSrcFeature = poBaseLayer->GetFeature(nFeatureId);
    if( poSrcFeature == NULL )
        return NULL;

    if( poFeatureDefn == poBaseLayer->GetLayerDefn() )
        return poSrcFeature;
    else
    {
        OGRFeature* poFeature = new OGRFeature(poFeatureDefn);
        poFeature->SetFrom(poSrcFeature);
        poFeature->SetFID(poSrcFeature->GetFID());
        delete poSrcFeature;
        return poFeature;
    }
}
Example #3
0
int wxStation::GetFirstStationLine(const char *xFilename)
{
    OGRDataSourceH hDS;
    OGRLayer *poLayer;
    OGRFeature *poFeature;
    OGRLayerH hLayer;
    GIntBig iBig = 1;

    hDS = OGROpen( xFilename, FALSE, NULL );
    if(hDS == NULL)
    {
        return -1; //very bad!
    }
    poLayer = (OGRLayer*)OGR_DS_GetLayer( hDS, 0 );
    hLayer=OGR_DS_GetLayer(hDS,0);
    OGR_L_ResetReading(hLayer);
    poLayer->ResetReading();
    poFeature = poLayer->GetFeature(iBig);
    if (poFeature==NULL)
    {
        return -1; //If there are no stations in the csv!
    }
    std::string start_datetime(poFeature->GetFieldAsString(15));

    if(start_datetime.empty()==true)
    {
        return 1;
    }
    if(start_datetime.empty()==false)
    {
        return 2;
    }






}
Example #4
0
CPLErr GNMGenericNetwork::ChangeBlockState(GNMGFID nFID, bool bIsBlock)
{
    if(!m_bIsGraphLoaded && LoadGraph() != CE_None)
    {
        return CE_Failure;
    }

    // change block state in layer
    OGRLayer* poLayer = GetLayerByName(m_moFeatureFIDMap[nFID]);
    if(NULL == poLayer)
    {
        CPLError( CE_Failure, CPLE_AppDefined, "Failed to get layer '%s'.",
                  m_moFeatureFIDMap[nFID].c_str() );
        return CE_Failure;
    }

    OGRFeature* poFeature = poLayer->GetFeature(nFID);
    if(NULL == poFeature)
    {
        CPLError( CE_Failure, CPLE_AppDefined, "Failed to get feature '"
                  GNMGFIDFormat"'.", nFID );
        return CE_Failure;
    }

    if(bIsBlock)
    {
        poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_ALL );
    }
    else
    {
        poFeature->SetField( GNM_SYSFIELD_BLOCKED, GNM_BLOCK_NONE );
    }

    if( poLayer->SetFeature( poFeature ) != OGRERR_NONE )
    {
        OGRFeature::DestroyFeature( poFeature );
        CPLError( CE_Failure, CPLE_AppDefined, "Failed to update feature." );
        return CE_Failure;
    }

    OGRFeature::DestroyFeature( poFeature );

    GNMGFID nSrcFID, nTgtFID, nConFID;

    // change block state in graph layer
    m_poGraphLayer->ResetReading();
    while ((poFeature = m_poGraphLayer->GetNextFeature()) != NULL)
    {
        nSrcFID = poFeature->GetFieldAsGNMGFID(GNM_SYSFIELD_SOURCE);
        nTgtFID = poFeature->GetFieldAsGNMGFID(GNM_SYSFIELD_TARGET);
        nConFID = poFeature->GetFieldAsGNMGFID(GNM_SYSFIELD_CONNECTOR);
        int nBlockState = poFeature->GetFieldAsInteger(GNM_SYSFIELD_BLOCKED);

        if(bIsBlock)
        {
            if(nSrcFID == nFID)
                nBlockState |= GNM_BLOCK_SRC;
            else if(nTgtFID == nFID)
                nBlockState |= GNM_BLOCK_TGT;
            else if(nConFID == nFID)
                nBlockState |= GNM_BLOCK_CONN;

            poFeature->SetField( GNM_SYSFIELD_BLOCKED, nBlockState );
        }
        else
        {
            if(nSrcFID == nFID)
                nBlockState &= ~GNM_BLOCK_SRC;
            else if(nTgtFID == nFID)
                nBlockState &= ~GNM_BLOCK_TGT;
            else if(nConFID == nFID)
                nBlockState &= ~GNM_BLOCK_CONN;

            poFeature->SetField( GNM_SYSFIELD_BLOCKED, nBlockState );
        }

        if( m_poGraphLayer->SetFeature( poFeature ) != OGRERR_NONE )
        {
            OGRFeature::DestroyFeature( poFeature );
            CPLError( CE_Failure, CPLE_AppDefined, "Failed to update feature." );
            return CE_Failure;
        }

        OGRFeature::DestroyFeature( poFeature );
    }

    // change block state in graph
    m_oGraph.ChangeBlockState(nFID, bIsBlock);

    return CE_None;
}
int Raster::VectortoRaster(const char * sVectorSourcePath,
                   const char * sRasterOutputPath,
                   const char * psFieldName,
                   RasterMeta * p_rastermeta ){

    OGRRegisterAll();

    OGRDataSource * pDSVectorInput;
    pDSVectorInput = OGRSFDriverRegistrar::Open( sVectorSourcePath, FALSE );
    if (pDSVectorInput == NULL)
        return INPUT_FILE_ERROR;

    OGRLayer * poLayer = pDSVectorInput->GetLayer(0);

    // The type of the field.
    OGRFeature * feat1 = poLayer->GetFeature(0);
    int fieldindex = feat1->GetFieldIndex(psFieldName);
    OGRFieldType fieldType = feat1->GetFieldDefnRef(fieldindex)->GetType();
    OGRFeature::DestroyFeature( feat1 );

    // The data type we're going to use for the file
    GDALDataType OutputDataType = GDT_Byte;

    // Handle field types according to their type:
    switch (fieldType) {
    case OFTString:
        CSVWriteVectorValues(poLayer, psFieldName, sRasterOutputPath);
        break;
    case OFTInteger:
        break;
    case OFTReal:
        OutputDataType = GDT_Float64;
    default:
        throw RasterManagerException(VECTOR_FIELD_NOT_VALID, "Type of field not recognized.");
        break;
    }

    // Get our projection and set the rastermeta accordingly.
    // -------------------------------------------------------
    char *pszWKT = NULL;

    OGRSpatialReference* poSRS = poLayer->GetSpatialRef();

    poSRS->exportToWkt(&pszWKT);
    p_rastermeta->SetProjectionRef(pszWKT);
    CPLFree(pszWKT);
    OSRDestroySpatialReference(poSRS);

    // Create the output dataset for writing
    GDALDataset * pDSOutput = CreateOutputDS(sRasterOutputPath, p_rastermeta);

    // Create a list of burn-in values
    // -------------------------------------------------------


    std::vector<OGRGeometryH> ogrBurnGeometries;
    std::vector<double> dBurnValues;

    poLayer->ResetReading();
    OGRFeature * ogrFeat = poLayer->GetNextFeature();   // <------- DRMEM!!! UNADDRESSABLE ACCESS of freed memory

    while( ogrFeat != NULL ){

        OGRGeometry * ogrGeom = ogrFeat->GetGeometryRef();

        // No geometry found. Move along.
        if( ogrGeom == NULL )
        {
            OGRFeature::DestroyFeature( ogrFeat );
            continue;
        }

        OGRGeometry * geoClone = ogrGeom->clone();  // <------- DRMEM!!! UNADDRESSABLE ACCESS of freed memory AND LEAK 20 direct bytes

        // Push a clone of this geometry onto the list of shapes to burn
        ogrBurnGeometries.push_back( (OGRGeometryH) geoClone );

        if (fieldType == OFTString){
            // If it's a string type we burn the FID. The value is then placed in a CSV file
            dBurnValues.push_back( ogrFeat->GetFID() );
        }
        else {
            // If it's a float type or a byte type we write it directly.
            dBurnValues.push_back( ogrFeat->GetFieldAsDouble(psFieldName) );
        }
        // GetNextFeature() creates a clone so we must delete it.
        OGRFeature::DestroyFeature( ogrFeat );      // <------- DRMEM!!! UNADDRESSABLE ACCESS beyond heap bounds:
        ogrFeat = poLayer->GetNextFeature();        // <------- DRMEM!!! UNADDRESSABLE ACCESS of freed memory
    }

    // Do the Actual Burning of Geometries.
    // -------------------------------------------------------

    int band = 1;
    char **papszRasterizeOptions = NULL;
    papszRasterizeOptions =
        CSLSetNameValue( papszRasterizeOptions, "ALL_TOUCHED", "TRUE" );

    CPLErr err = GDALRasterizeGeometries( pDSOutput, 1, &band,
                                          ogrBurnGeometries.size(),
                                          &(ogrBurnGeometries[0]),
                                          NULL, NULL,
                                          &(dBurnValues[0]),
                                          NULL,
                                          NULL, NULL );

    if (err) { }
    ogrBurnGeometries.clear();
    dBurnValues.clear();

    // Done. Calculate stats and close file
    CalculateStats(pDSOutput->GetRasterBand(1));

    CSLDestroy(papszRasterizeOptions);
    GDALClose(pDSOutput);
    pDSVectorInput->Release();  // <------- DRMEM!!! UNADDRESSABLE ACCESS beyond heap bounds:

    //This is where the implementation actually goes
    return PROCESS_OK;

}