[PROJ] Extracting WKT string

Roger Oberholtzer roger.oberholtzer at gmail.com
Fri Jan 31 05:01:29 PST 2025


Here is a snippet of code I use in C. Hope that helps tinfo->CRS_TO is
just a string like "EPSG:12345". The opts/lopts bit is the trick.

int GetWktFromProj(WGS84Info *tinfo) {

       char *prj;
       PJ *tmp = proj_create(0, tinfo->CRS_TO);

       const char *opts[2] = {"MULTILINE=NO", 0};
       const char *lopts[3] = {"MULTILINE=NO",
"ALLOW_ELLIPSOIDAL_HEIGHT_AS_VERTICAL_CRS=YES", 0};

       // printf("%s: %s\n", __FUNCTION__, tinfo->CRS_TO);

       prj = (char *) proj_as_wkt(0, tmp, PJ_WKT2_2015, opts);
       if (!prj) prj = (char *) proj_context_errno_string(0,
proj_context_errno(0));
       tinfo->WKT[WKT2_2015]   = strdup(prj);
       // printf("WKT2_2015: %s\n", tinfo->WKT[WKT2_2015]);

       prj = (char *) proj_as_wkt(0, tmp, PJ_WKT2_2019, opts);
       if (!prj) prj = (char *) proj_context_errno_string(0,
proj_context_errno(0));
       tinfo->WKT[WKT2_2019]   = strdup(prj);
       // printf("WKT2_2019: %s\n", tinfo->WKT[WKT2_2019]);

       prj = (char *) proj_as_wkt(0, tmp, PJ_WKT1_GDAL, opts);
       if (!prj) prj = (char *) proj_context_errno_string(0,
proj_context_errno(0));
       tinfo->WKT[WKT1_GDAL]   = strdup(prj);
       // printf("WKT1_GDAL: %s\n", tinfo->WKT[WKT1_GDAL]);

       prj = (char *) proj_as_wkt(0, tmp, PJ_WKT1_GDAL, lopts);
       if (!prj) prj = (char *) proj_context_errno_string(0,
proj_context_errno(0));
       tinfo->WKT[WKT1_LAS]    = strdup(prj);
       // printf("WKT1_LAS: %s\n", tinfo->WKT[WKT1_LAS]);

       prj = (char *) proj_as_wkt(0, tmp, PJ_WKT1_ESRI, opts);
       if (!prj) prj = (char *) proj_context_errno_string(0,
proj_context_errno(0));
       tinfo->WKT[WKT1_ESRI]   = strdup(prj);
       // printf("WKT1_ESRI: %s\n", tinfo->WKT[WKT1_ESRI]);

       prj = (char *) proj_as_proj_string(0, tmp, PJ_PROJ_5, opts);
       if (!prj) prj = (char *) proj_context_errno_string(0,
proj_context_errno(0));
       tinfo->WKT[PROJ5]       = strdup(prj);
       // printf("PROJ5: %s\n", tinfo->WKT[PROJ5]);

       prj = (char *) proj_as_projjson(0, tmp, opts);
       if (!prj) prj = (char *) proj_context_errno_string(0,
proj_context_errno(0));
       tinfo->WKT[PROJJSON]    = strdup(prj);
       // printf("PROJJSON: %s\n", tinfo->WKT[PROJJSON]);

       proj_destroy(tmp);

       return(ISOK);
}

On Fri, Jan 31, 2025 at 1:56 PM Tony Beckett via PROJ
<proj at lists.osgeo.org> wrote:
>
> Hi there,
>
>
>
> I’m trying to convert a Python coordinate conversion calculator (that I wrote a few years back) into C++.
>
>
>
> I have the basic code working, but I am struggling with how to extract a string containing the WKT for the source and target CRS.
>
>
>
> Can anyone provide me with a simple example, using the example app in the docs “Quick start for C++ API usage”. I’m assuming that the function ‘proj_as_wkt(…)’ is what is needed.
>
>
>
> Regards,
>
> Tony Beckett
>
> _______________________________________________
> PROJ mailing list
> PROJ at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/proj



-- 
Roger Oberholtzer


More information about the PROJ mailing list