[gdal-dev] gdalwarp under python

Vadim Shlyakhov vadp.devl at gmail.com
Mon Aug 8 05:36:16 EDT 2011


Hi Balazs,

On Thu, Aug 4, 2011 at 3:56 PM, Balazs Szabó <szabobaa at gmail.com> wrote:
> Dear list!
>
> How can I use this two bash command with gdal-python bindings?
>
> gdal_translate -of GTiff  -a_srs EPSG:xxxx -gcp 775 560 20.58329 48.12650
> -gcp 9518 539 20.8329 48.12650  -gcp 9525 7136 20.83329 48.00150  -gcp 758
> 7134 20.58329 48.00150  o.tif s.tif

for  gdal_translate you could do something like this:

            out_format='VRT'
            ext='.'+out_format.lower()
            dst_file= os.path.basename(base+ext).encode('utf-8') # output file
            img_path=os.path.relpath(self.img_file,dst_dir).encode('utf-8')

            src_ds = gdal.Open(img_path,GA_ReadOnly)
            dst_drv = gdal.GetDriverByName(out_format)
            dst_ds = dst_drv.CreateCopy(dst_file,src_ds,0)
            dst_ds.SetProjection(self.srs)

            #double x = 0.0, double y = 0.0, double z = 0.0, double
pixel = 0.0,
            #double line = 0.0, char info = "", char id = ""
            gcps=[gdal.GCP(c[0],c[1],0,p[0],p[1],'',i) for i,p,c in self.refs]
            dst_ds.SetGCPs(gcps,self.refs.srs())
            dst_geotr=gdal.GCPsToGeoTransform(gcps)
            dst_ds.SetGeoTransform(dst_geotr)

            del dst_ds # close dataset

> gdalwarp -tps s.tif t.tif

For this I was just using a few templates to assemble VRT description
"manually" then feed it into drv.CreateCopy().

Perhaps this is not the most elegant solution, but if you need a
sample you may have a look at my gdal_tiler.py script (function
make_base_raster). You can find it here:
http://tilers-tools.googlecode.com/files/tilers_tools-2.2.tar.gz. Just
to note: for SRSes the script internally uses proj4 notation, but this
wouldn't obligatory in your case.

Ironically this was also evolved from some bash script some time ago.

Hope this helps.
VS


More information about the gdal-dev mailing list