[Gdal-dev] importFromWkt() problem

Petteri Packalen packalen at cs.joensuu.fi
Sat Sep 2 12:53:44 EDT 2006


Mateusz, Frank, list,

I prepared the code below that demonstrates an essential part of the 
function in question. There was actually a bug in my first mail in example 
1. Here is the more comprehensive illustration. Thus, although in both 
examples the address of the COPY of the pointer is passed to 
createFromWkt() or importFromWkt() only latter one works properly.

// Some variables needed in the above code.
OGRFeature   *poSrcFeature;
char         *geo_wkt = new char[ WKT_BUF ];
char         *geo_trans = new char[ WKT_BUF ];

// This loop which goes through all OGR features. Partly taken
// from the TranslateLayer() function in ogr2ogr.cpp.
while( TRUE )
{
    OGRFeature *poDstFeature = NULL;

    // Fetch input feature.
    poSrcFeature = poSrcLayer->GetNextFeature();
    if ( poSrcFeature == NULL )
       break;

    if ( ++nFeaturesInTransaction == nGroupTransactions )
    {
       poDstLayer->CommitTransaction();
       poDstLayer->StartTransaction();
       nFeaturesInTransaction = 0;
    }

    // Create output feature based on input feature.
    CPLErrorReset();
    poDstFeature = OGRFeature::CreateFeature( poDstLayer->GetLayerDefn() );
    poDstFeature->SetFrom( poSrcFeature, TRUE );

    // Fetch output geometry ref.
    OGRGeometry *poGeom = poDstFeature->GetGeometryRef();

    // Export geometry to WKT to be transformed.
    poGeom->exportToWkt( &geo_wkt );

    // Transform WKT (it is verified that proper WKT is returned)
    if ( bToEurefJhs154 == true )
       geo_trans = ConvertWktByJhs154ToEuref( geo_wkt );
    else
       geo_trans = ConvertWktByJhs154ToYkj( geo_wkt );

    // Notice that geo_trans should always point to the beginnig
    // of its memory allocation. Therefore, pass a copy of it to
    // the createFromWkt() and importFromWkt().
    char* pszWkt = (char*) geo_trans;

//--> EXAMPLE 1: This does not work (e.g. in the case of point)...
    if ( poGeom->getGeometryType() == wkbPoint )
    {
       OGRPoint *poPoint = new OGRPoint();
       poPoint->importFromWkt( &pszWkt );
       poDstFeature->SetGeometryDirectly( poPoint );
     }
     else
     ...

//--> EXAMPLE 2: but this works.
    OGRSpatialReference *ref = new OGRSpatialReference( NULL );
    OGRGeometry *new_geom;
    OGRGeometryFactory::createFromWkt( &pszWkt, ref, &new_geom );
    poDstFeature->SetGeometryDirectly( new_geom );

    // Destroy source feature.
    OGRFeature::DestroyFeature( poSrcFeature );

    // Write new feature to layer.
    poDstLayer->CreateFeature( poDstFeature );

    // Destroy destination feature.
    OGRFeature::DestroyFeature( poDstFeature );
}

Best regards,
Petteri

On Sat, 2 Sep 2006, Petteri Packalen wrote:

> Frank, list,
>
> Once again, I struggled with the weird problem until I understood to look at 
> the documentation... The problem was that, within importFromWkt(), the 
> pointer was updated to pointer after the consumed text. However, I don't 
> still understand why the first *corrected* solution does not work properly. I 
> want to reset the geometry of an existing feature based on WKT 
> representation. Below are two examples in which only the latter one works 
> properly. An example 1 crashes due to illegal memory access. geo_trans is a 
> buffer which stores WKT.
>
> EXAMPLE 1
> =========
> OGRPolygon * poPoly = new OGRPolygon();
> poPoly->importFromWkt( &geo_trans );
> poDstFeature->SetGeometryDirectly( poPoly );
> ...
> OGRFeature::DestroyFeature( poFeature );
>
> EXAMPLE 2
> =========
> char* pszWkt = (char*) geo_trans;
> OGRSpatialReference *ref = new OGRSpatialReference( NULL );
> OGRGeometry *new_geom;
> OGRErr err = OGRGeometryFactory::createFromWkt(&pszWkt, ref, &new_geom);
> poDstFeature->SetGeometryDirectly( new_geom );
> ...
> OGRFeature::DestroyFeature( poFeature );
>
> In the comment within the ogrgeometryfactory.cpp it states that 
> "createFromWkt() now updates pointer to text to indicate how much text was 
> consumed". I understand that this can be useful in many occasions although I 
> don't understand why it is implemented this way. Anyway, why the first 
> solution does not work? In addition, in an example 1 the problem occurres 
> only after very many (>million) calls to importFromWkt(). Something is 
> happening in the background (related to reference counting etc.?) I don't 
> understand.
>
> Best regards,
> Petteri
>
> =========================================================================
> Petteri Packalén University of Joensuu Faculty of Forestry P.O. Box 111 80101 
> Joensuu
> Finland e-mail: packalen at cs.joensuu.fi 
> =========================================================================
>


More information about the Gdal-dev mailing list