0

pyproj is a language binding for PROJ. The pyproj module provides easy-to-use methods for CRS-to-CRS transformations, For instance, use it to convert global latitude/longitude (degrees) to local coordinates with respect to some coordinate reference system (metres):

>>> from pyproj import Transformer
>>> transformer = Transformer.from_crs(4326, 6677)
>>> transformer.transform(35.9032597, 139.9343755)
(-10728.330840296036, 9120.537451921156)

PROJ provides a C++ API but the documentation is pure crap. There are no sample codes and I couldn't find my way through it. I'd be really appreciative if anyone could give a clue on how to use the C++ API the same way pyproj works for arbitrary transformations.

Amir Int
  • 45
  • 7

1 Answers1

0

You can do something like this

#include <proj.h>
#include <iostream>

PJ_CONTEXT *C;
PJ *P;

C = proj_context_create();

P = proj_create_crs_to_crs(C, "EPSG:4326", "EPSG:6677", NULL);

PJ_COORD input_coords,
         output_coords; // https://proj.org/development/reference/datatypes.html#c.PJ_COORD

input_coords = proj_coord(35.9032597, 139.9343755, 0, 0);

output_coords = proj_trans(P, PJ_FWD, input_coords);

std::cout << output_coords.xy.x << " " << output_coords.xy.y << std::endl;

/* Clean up */
proj_destroy(P);
proj_context_destroy(C); // may be omitted in the single threaded case

Of course, beyond this initial level, you can make whatever function or class wrappers you need. But this reproduces the pyproj example you posted.

Andrew Holmgren
  • 1,225
  • 1
  • 11
  • 18
  • Hey! Thanks for your reply. You meant instead of , eh? Also, I thought I'd get something more CPP-ish, because this one uses the C APIs I guess. – Amir Int Feb 26 '23 at 10:36
  • Sorry, yeah, iostream. I'm not sure what you mean about making it more cpp-ish. As I said, you will probably want to wrap it up in a class or a function depending on what makes sense for your project. You could throw in some autos, make some unique pointers, or whatever else works. – Andrew Holmgren Feb 28 '23 at 11:31
  • Hi @AndrewHolmgren I tried your example and getting this error `undefined symbol: proj_context_create` , Would you please help thanks – Ali Akram Mar 14 '23 at 11:09
  • Hi @AliAkram, you're likely not linking the library when you compile. Since I don't know what system you're on and since there are so many ways to compile across different systems, I can't give you an exact answer. – Andrew Holmgren Mar 14 '23 at 12:00
  • Hi @AliAkram. That is a linker problem. On an Ubuntu 22.04 with the g++ compiler I used `g++ sample.cpp -o sample -lproj`. You need to link against the PROJ library. – Amir Int Mar 26 '23 at 11:05
  • Basically I need to compile proj for android which have 4 architectures like below armeabi-v7a arm64-v8a x86 x86_64 – Ali Akram Mar 26 '23 at 12:45
  • it return me same value what i send to it i.e 35.9032597, 139.9343755 – Ali Akram Mar 29 '23 at 07:43