[gdal-dev] Real and "raster" coordinates

Alexander Bruy alexander.bruy at gmail.com
Thu Mar 25 02:20:29 EDT 2010

```Thanks! That what I need

2010/3/25 Pinner, Luke <Luke.Pinner at environment.gov.au>:
> The following module might help, use the mapToPixel() function.
>
> #---Imports
> try:
>    from osgeo import gdal
> except ImportError:
>    import gdal
>
> def mapToPixel(mx,my,gt):
>    ''' Convert map to pixel coordinates
>        @param  mx    Input map x coordinate (double)
>        @param  my    Input map y coordinate (double)
>        @param  gt    Input geotransform (six doubles)
>        @return px,py Output coordinates (two doubles)
>    '''
>    if gt[2]+gt[4]==0: #Simple calc, no inversion required
>        px = (mx - gt[0]) / gt[1]
>        py = (my - gt[3]) / gt[5]
>    else:
>        px,py=ApplyGeoTransform(mx,my,InvGeoTransform(gt))
>    return int(px+0.5),int(py+0.5)
>
> def pixelToMap(px,py,gt):
>    ''' Convert pixel to map coordinates
>        @param  px    Input pixel x coordinate (double)
>        @param  py    Input pixel y coordinate (double)
>        @param  gt    Input geotransform (six doubles)
>        @return mx,my Output coordinates (two doubles)
>    '''
>    mx,my=ApplyGeoTransform(px,py,gt)
>    return mx,my
>
> def ApplyGeoTransform(inx,iny,gt):
>    ''' Apply a geotransform
>        @param  inx       Input x coordinate (double)
>        @param  iny       Input y coordinate (double)
>        @param  gt        Input geotransform (six doubles)
>        @return outx,outy Output coordinates (two doubles)
>    '''
>    outx = gt[0] + inx*gt[1] + iny*gt[2]
>    outy = gt[3] + inx*gt[4] + iny*gt[5]
>    return (outx,outy)
>
>  def InvGeoTransform(gt_in):

--
Alexander Bruy
```