[PROJ] How to map pj_latlong_from_proj to PROJ ver 7 API

Even Rouault even.rouault at spatialys.com
Mon Nov 30 12:32:42 PST 2020


On lundi 30 novembre 2020 21:00:32 CET Even Rouault wrote:
> Pierluigi,
> 
> > I've successfully compiled PROJ ver 7.2.0 on Windows 10 (build 19041.630)
> > and I am trying to port my code from PROJ ver 4 API to PROJ ver 7 API but
> > I
> > can't find a clear matching between the old version 4 API
> > *pj_latlong_from_proj* routine and version 7 API code.
> > 
> > I tried the suggested function mapping as specified here
> > https://proj.org/development/migration.html#function-mapping-from-old-to-n
> > ew -api, but I am consistently getting a NULL pointer returned by the
> > *proj_crs_get_horizontal_datum()*. Here is my sample code:
> > 
> > //------------------------------------------------
> > #include <iostream>
> > #include <proj.h>
> > 
> > int main()
> > {
> > 
> >    PJ *proj_test = proj_create(PJ_DEFAULT_CTX, "+proj=ortho +datum=WGS84
> > 
> > +ellps=WGS84 +lat_0=60 +lon_0=90");
> 
> The above is instanciated as a coordinate operation, not a CRS, so you can't
> use CRS related API on it. You likely need to add " +type=crs" to the PROJ
> string
> 
> >    PJ *datum = proj_crs_get_horizontal_datum(PJ_DEFAULT_CTX, proj_test);
> 
> You'll rather want to use proj_get_source_crs() to extract the geographic
> CRS from the projected CRS ( proj_crs_get_geodetic_crs() would also do the
> job here ).
> 
> The resulting PJ* object will be a CRS too, so as you likely want to do a
> coordinate operation from your projected CRS to the base geographic CRS,
> you'll need to create a coordinate operation object with:
> 
> 	proj_create_crs_to_crs_from_pj(PJ_DEFAULT_CTX, proj_test, geog_crs, NULL,
> NULL)
> 
> and then you can use proj_trans() on the resulting object.
> 
> 
> Another option is to use proj_crs_get_coordoperation(PJ_DEFAULT_CTX,
> proj_test) (with the PROJ string of proj_test having " +type=crs"), which
> will give you the coordinate operation to convert from the base geographic
> CRS of the projected CRS to the projected CRS.
> You can use proj_trans() on it (in the reverse direction, if you want to go
> from projected to geographic)

Actually that can be even be simpler, if your purpose is to transform between 
this projection and the underlying geographic CRS, then you can use directly 
proj_test (without +type=crs) with proj_trans(). In that case, the geographic 
coordinates will be in radians, whereas with the approaches I mentionned 
above, they will be in degrees (since this is the unit for geographic CRS)

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the PROJ mailing list