[gdal-dev] exportToProj4

Even Rouault even.rouault at mines-paris.org
Sat Jan 25 15:01:55 PST 2014


Le samedi 25 janvier 2014 23:50:10, Paul Meems a écrit :
> Hi all,
> 
> Thanks to Tamas Szekeres we are now using his binaries from
> http://www.gisinternals.com/sdk/ with MapWinGIS, our mapping ActiveX.
> This has drastically simplified our build process and we are very grateful
> for Tamas' help.
> 
> We're now struggling with some minor issues regarding our 'old' C++ code
> and the new binaries for GDAL, GEOS, Proj4, etc.
> Several of them I managed to solve, but I'm struggling with this one.
> We have code to read a prj file and get the Proj4 string:
> 
>     void ProjectionTools::GetProj4FromPRJFile(char * prjfileName, char **
> prj4)
>     {
>         FILE * pFile;
>         pFile = fopen (prjfileName,"r");
>         if (pFile == NULL)
>         {
>             // Doesn't exist
>             return;
>         }
>         fclose(pFile);
>         pFile = NULL;
>         OGRSpatialReference* oSRS = new OGRSpatialReference();
> 
>         char **papszPrj = CSLLoad(prjfileName);
>         if (papszPrj == NULL)
>         {
>             return;
>         }
> 
>         OGRErr eErr = oSRS->importFromESRI(papszPrj);
>         if (eErr != OGRERR_NONE)
>         {
>             OGRErr eErr = oSRS->importFromProj4(*papszPrj);
>         }
> 
>         CSLDestroy( papszPrj );
>         char * pszProj4 = NULL;
>         eErr = oSRS->exportToProj4( &pszProj4 );
>         delete oSRS; <-- going wrong now
> 
>         *prj4 = new char[_tcslen(pszProj4)+1];
>         strcpy(*prj4, pszProj4);
>         CPLFree(pszProj4);
>     }
> 
> This code was working for years, but now it crashes on delete oSRS;.
> I've been searching for examples of how to use exportToProj4() in C++ but
> can only find Python examples.
> And I'm not a C++ developer, just a C# developer :)
> How should I now free oSRS?

The code looks OK and there's no fundamental reason why it would not work any 
longer. I highly suspect that you are running into a classical issue of a 
mismatch between the compile flags used in your application and the ones used 
by the GDAL built itself.

You could perhaps avoid that by letting GDAL do the allocation and 
deallocation with 

oSRS = (OGRSpatialReference*)OSRNewSpatialReference(NULL);

and

OGRSpatialReference::DestroySpatialReference(oSRS)

> 
> Thanks for any advice.
> 
> Paul
> 
> *Paul Meems *
> Release manager, configuration manager
> and forum moderator of MapWindow GIS.
> www.mapwindow.org
> 
> Owner of MapWindow.nl - Support for
> Dutch speaking users.
> www.mapwindow.nl

-- 
Geospatial professional services
http://even.rouault.free.fr/services.html


More information about the gdal-dev mailing list