# VTK-PROJ simple example not working

**URL:** https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532
**Category:** Support
**Created:** [October 5, 2022, 5:12pm UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532 "2022-10-05T17:12:03Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [October 5, 2022, 5:12pm UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532/1 "2022-10-05T17:12:03Z")

</div>

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:

```auto
#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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [October 5, 2022, 7:38pm UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532/2 "2022-10-05T19:38:05Z")

</div>

I set PROJ\_DEBUG=3, and get a bit more detail on this error:

```auto
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?

---

<div class="post-metadata">

### Author: ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)
#### Post date: [October 5, 2022, 8:17pm UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532/3 "2022-10-05T20:17:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [October 5, 2022, 9:25pm UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532/4 "2022-10-05T21:25:18Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)
#### Post date: [October 5, 2022, 9:34pm UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532/5 "2022-10-05T21:34:38Z")

</div>

VTK\_MODULE\_USE\_EXTERNAL\_VTK\_libproj ON.

Dan

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [October 5, 2022, 10:02pm UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532/6 "2022-10-05T22:02:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)
#### Post date: [October 6, 2022, 1:12am UTC](https://discourse.vtk.org/t/vtk-proj-simple-example-not-working/9532/7 "2022-10-06T01:12:03Z")

</div>

In cmake-gui when you compile VTK.
