0

I set up an ubuntu 18.04 EC2 through AWS that "On Start" will run a python script in which will download a .tif from an s3 bucket and project it from EPSG:4326 to EPSG:3857 using gdal.Warp(). The method to execute the script on start was used from this question/solution. I am also using an anaconda3 env to host the script and desired packages. GDAL was installed with conda install -c conda-forge gdal=3.0.2.

onStartUp.service

[Unit]
Description=Some kind of description

[Service]
Type=simple
ExecStart=/home/ubuntu/anaconda3/envs/myEnv/bin/python3.8 /home/ubuntu/run.py

[Install]
WantedBy=multi-user.target

I can successfully project with the code below both through my local machine and with the EC2 if executed with python myScript.py, however when used in conjunction with the "On Start" method, I receive an error.

I found that this is due to the GDAL_DATA environment variable being not properly set for a server use case. I am not entirely sure how to set this variable, weather I need to set it in the main script or in the command terminal. I found this solution, and this solution, but they were unsuccessful.

from osgeo import gdal

def reproject(output_raster, filename):
    input_raster = gdal.Open(filename)
    warp_options = gdal.WarpOptions(creationOptions = ['COMPRESS=DEFLATE', 'PREDICTOR=2', 'ZLEVEL=9'], srcSRS='EPSG:4326', dstSRS='EPSG:3857', resampleAlg='near', format='GTiff')
    warp = gdal.Warp(output_raster, input_raster, options=warp_options)
    warp = None

warp = gdal.Warp(output_raster, input_raster, options=warp_options)
File "/home/ubuntu/anaconda3/envs/myEnv/lib/python3.8/site-packages/osgeo/gdal.py", line 625, in Warp
return wrapper_GDALWarpDestName(destNameOrDestDS, srcDSTab, opts, callback, callback_data)
File "/home/ubuntu/anaconda3/envs/myEnv/lib/python3.8/site-packages/osgeo/gdal.py", line 3410, in wrapper_GDALWarpDestName
return _gdal.wrapper_GDALWarpDestName(*args)
TypeError: in method 'wrapper_GDALWarpDestName', argument 4 of type 'GDALWarpAppOptions *'

Updates I was able to find some more information running journalctl -xe

ERROR 1: PROJ: proj_create_from_database: Open of /home/ubuntu/anaconda3/envs/myEnv/share/proj failed
ERROR 1: Translating source or target SRS failed:
EPSG:4326
5
  • 1
    try adding this environment variable PROJ_LIB=/home/ubuntu/anaconda3/envs/myEnv/share/proj. Also try upgrading your gdal to a different (later) version Commented Jun 1, 2022 at 15:37
  • @AmanBagrecha thanks! I actually just found a similar response from a 'stackoverflow' post. Unforturantly I cannot Flag the question as a duplicate since they are two different sites.
    – Binx
    Commented Jun 1, 2022 at 15:42
  • If it solves the error, the mods can take the call. But did that solve your issue? Commented Jun 1, 2022 at 15:47
  • 1
    Yes is did solve the issue. I just flagged it for the mods.
    – Binx
    Commented Jun 1, 2022 at 15:48
  • 1
    @AmanBagrecha Flag was declined. If you want to post, I'll accept!
    – Binx
    Commented Jun 1, 2022 at 18:54

1 Answer 1

1

You need to add the following environment variable

PROJ_LIB=/home/ubuntu/anaconda3/envs/myEnv/share/proj

Where the path is pointing to proj directory inside your conda environment

1
  • I'd like to add that if you activate the environment, I do not need to set the PROJ_LIB var. But if I run the script via onStartUp.service then the PROJ_LIB var does need to be set.
    – Binx
    Commented Jun 2, 2022 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.