0

I want to use the gdal.Warp method with python (doc). I have seen a cutlineSQL option which would be nice for my case since cutlines are stored in a PostGIS table but I can not find the syntax for this parameter.

How to tell gdal which database connection to use?

Or maybe I misunderstood the meaning of this parameter?

How to provide a cutline to Warp method then?


option not presented in the warp tutorial neither.


What I have so far :

from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly

# input dataset
dataset = gdal.OpenShared(path, GA_ReadOnly)
raster = dataset.GetRasterBand(1)

# warp options
db_conn = """PG:"host=xx.xx.xx.xx port=5432 user='xxxx' password='xxxx' dbname='xxxx'" """
warp_opts = gdal.WarpOptions(
        format="GTiff",
        cutlineDSName=db_conn,
        cutlineSQL="SELECT cutline FROM table WHERE id_1",
        cropToCutline=True,
    )

# warp
res = gdal.Warp("test.tif",
                 raster,
                 options=warp_opts,
             )

Producing the error :

ERROR 1: PQconnectdb failed.
invalid connection option ""host"

ERROR 1: Cannot open PG:"host=xx.xx.xx.xx port=5432 user='xxx' password=XXXXXXXXXX dbname='xxxx'" .

While ogrinfo with the same paramters is working.

If I try to remove some quotes in db_conn like this :

db_conn = "PG:host=xx.xx.xx.xx port=5432 user='xxxx' password='xxxx' dbname='xxxx'"

I end up with a seg fault...

1 Answer 1

4

Ok, after a lot of efforts :-p, I ended up with a working script:

from osgeo import gdal

db_conn = "PG:host=xx.xx.xx.xx port=5432 user='xxxx' password='xxxx' dbname='xxxx'"
warp_opts = gdal.WarpOptions(
    format="GTiff",
    cutlineDSName=db_conn,
    cutlineSQL="SELECT cutline FROM table WHERE id=1",
    cropToCutline=True,
)

res = gdal.Warp("test.tif",
                 path,
                 options=warp_opts,
             )

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.