I was successful in getting centroid coordinates for pixels in Sentinel 2A image (10m by 10m). But Landsat (30m by 30 m) posed a problem.

Sentinel image and its coordinates

Landsat small clipped image zoomed to show my problem

from osgeo import gdal
# Open tif file
ds = gdal.Open('C:\\Users\\ADMIN\\Desktop\\Data\\viveka.img')
# GDAL affine transform parameters, According to gdal documentation xoff/yoff 
#are image left corner, a/e are pixel wight/height and b/d is rotation and is 
# zero if image is north up. 
xoff, a, b, yoff, d, e = ds.GetGeoTransform()

def pixel2coord(x, y):
"""Returns global coordinates from pixel x, y coords"""
 xp = a * x + b * y + xoff

 yp = d * x + e * y + yoff
return(xp, yp)

# get columns and rows of your image from gdalinfo
cols = ds.RasterXSize + 1
rows = ds.RasterYSize + 1
# Output and export
if __name__ == "__main__":
file=open("landsatfile2.csv","w") ## open file in write mode
for row in  range(0,rows):
    for col in  range(0,cols): 
        x,y = pixel2coord(col,row)
        print "({} {})".format(x+15,y-15)
        file.write("{} {}\n".format(x+15,y-15)) ## write each coordinate separated

Why can't the code do it for Landsat?

The first image is from Sentinel and the second zoomed is from Landsat. I want coordinates for centroid of each Landsat pixels.

enter image description here

  • Why do you have the magic number 15 in your code? I think that may well be what causes the issues for you. Jun 29 '18 at 11:27
  • cause landsat pixel is 30m?
    – ChrisL
    Jun 29 '18 at 12:37
  • yes, thats because landsat pixel is 30m by 30m
    – user123098
    Jun 29 '18 at 17:31
  • See the image. I edited the question. The cols and rows output is 11 and 11. but i see 5 pixels in each row and column.Any idea?
    – user123098
    Jun 29 '18 at 17:49
  • You had a tag for ArcMap but make no mention of using that software.
    – PolyGeo
    Jul 1 '18 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.