VTK-PROJ simple example not working

VTK source comes with self-contained PROJ source, in directory ThirdParty/libproj, and library libvtklibproj.so is built from that. Compiling application C++ code with this flag enables VTK PROJ headers and the aforementioned library:
-isystem /usr/local/include/vtk-9.2/vtklibproj/src
I’m currently using VTK-9.2.0, which incorporates PROJ-8.1.0, with ubuntu 20.04. My system also has PROJ-7.1.0 installed “standalone”.

The following simple program works if VTK PROJ is not enabled, but fails with “libproj_proj_create: Cannot find proj.db” if VTK PROJ is enabled:

#include <iostream>
// Using compile flag
// -isystem /usr/local/include/vtk-9.2/vtklibproj/src
// enables vtk's "built-in" proj version
#include <proj.h>

int main(int argc, char **argv) {

  PJ_INFO projInfo = proj_info();
  std::cerr << "proj release: " << projInfo.release << std::endl;
  
  double xMin = 0.;
  
  // Get UTM zone of grid's W edge
  int utmZone = ((xMin + 180)/6 + 0.5);

  std::cerr << "UTM zone: " << utmZone << std::endl;
  
  PJ_CONTEXT *projContext = proj_context_create();
  if (projContext) {
    std::cerr << "Created projContext OK" << std::endl;
  } else {
    return -1;
  }

  const char *srcCRS = "EPSG:4326";
  char targCRS[64];
  sprintf(targCRS, "+proj=utm +zone=%d +datum=WGS84", utmZone); 
  std::cout << "targCRS: " << targCRS << std::endl;
  PJ *proj = proj_create_crs_to_crs (projContext,
                                     srcCRS,
                                     targCRS,
                                     nullptr);
  if (!proj) {
    std::cerr << "failed to create proj" << std::endl;
  } else {
    std::cerr << "created proj OK" << std::endl;    
  }

  return 0;
}

With VTK PROJ not enabled, the program works, gives this output:

proj release: Rel. 7.1.0, August 1st, 2020
UTM zone: 30
Created projContext OK
targCRS: +proj=utm +zone=30 +datum=WGS84
created proj OK

When VTK PROJ is enabled, the proj_create_crs_to_crs() call fails:

proj release: Rel. 8.1.0, July 1st, 2021
UTM zone: 30
Created projContext OK
targCRS: +proj=utm +zone=30 +datum=WGS84
libproj_proj_create: Cannot find proj.db
failed to create proj

The VTK PROJ proj.db is at /usr/local/share/vtk-9.2/proj/proj.db, so I tried setting environment variable PROJ_LIB to /usr/local/share/vtk-9.2/proj/ - but get the same “Cannot find proj.db” error. Does anyone know why this is happening?
Thanks!

I set PROJ_DEBUG=3, and get a bit more detail on this error:

proj release: Rel. 8.1.0, July 1st, 2021
UTM zone: 30
Created projContext OK
pj_open_lib(proj.db): call fopen(proj.db) - failed
libproj_proj_create: Cannot find proj.db
pj_open_lib(proj.db): call fopen(proj.db) - failed
libproj_proj_create: no database context specified
Cannot instantiate source_crs

Based on those debug messages it appears PROJ is looking for proj.db in the current working directory. I place a copy of proj.db in the current working directory, and now the program works. But of course it’s not practical to put a copy of proj.db in every directory the program is run from. Why is it looking in the current directory by default? And why doesn’t setting PROJ_LIB make any difference?

Are you trying to use proj in a program using VTK? If that is the case, you can investigate further by turning on VTK_MODULE_ENABLE_VTK_IOCesium3DTiles. This module has a writer that uses proj. You can try to run the test for this writer to see if it works and also see how proj is used in this writer.

I think we test running the tests against installed VTK so that should pick up proj.db correctly as well.

1 Like

Yes, my VTK application calls PROG functions. I submitted a description of this odd behavior to the PROJ email list, and a response claims that PROJ-8.1.0 does in fact add $PROJ_LIB to the searchpath, contrary to what I see with the PROJ-8.1.0 shipped with VTK-9.2.0.
Is there some way I can easily disable VTK’s use of its “internal” PROJ code, i.e. so that my VTK application invokes the “external” PROG headers/libraries already installed on my system?
Thanks!

VTK_MODULE_USE_EXTERNAL_VTK_libproj ON.

Dan

1 Like

Sorry Dan, how do I use that flag? When compiling VTK, or when compiling my app?
Thanks!

In cmake-gui when you compile VTK.

1 Like