7

I am trying to get the X, Y coordinates of all the cells of a raster file, using Python 2.6 and ArcGIS 10.0.

I would like to get the X, Y coordinates in the center of the cell.

Is there any way to do so?

3
  • the raster is equally spaced, so you just need to know the max X, min X, max Y, max Y and the raster resolution value.
    – Gago-Silva
    Nov 27, 2012 at 8:51
  • Hasn't ESRI retained analogs of $$XMap and $$YMap in Python yet? See this forums post for a discussion of the issue and an expeditious workaround.
    – whuber
    Nov 27, 2012 at 17:47
  • Finally I tried the code of Marcin. It was really useful...
    – irini
    Nov 29, 2012 at 10:26

2 Answers 2

10

You can use the arcpy module's GetRasterProperties command to get the xmin, xmax, ymin and ymax coordinates for the raster. By converting these values, which are returned using getOutput as unicode, to floats simple arithmetic can be performed to give the center coordinates of the raster file.

import arcpy

path = r'C:\dem\profile-dtm_57583\grids\ns78ne1'

xmin = arcpy.GetRasterProperties_management(path,'LEFT')
xmax = arcpy.GetRasterProperties_management(path,'RIGHT')
ymin = arcpy.GetRasterProperties_management(path,'BOTTOM')
ymax = arcpy.GetRasterProperties_management(path,'TOP')

centerX = (float(xmax.getOutput(0)) + float(xmin.getOutput(0))) / 2
centerY = (float(ymax.getOutput(0)) + float(ymin.getOutput(0))) / 2

print centerX, centerY

EDIT:

To get the coordinates of every cell in the raster, you can take the raster cellsize, and use it as a step to iterate through all the values from xmin to xmax and ymin to ymax to get pass through the coordinates for each cell.

#assuming square cells
cellsize = arcpy.GetRasterProperties_management(path,'CELLSIZEX')
cellsize = int(cellsize.getOutput(0))

for x in xrange(int(xmin.getOutput(0)), int(xmax.getOutput(0)), cellsize):
    for y in xrange(int(ymin.getOutput(0)), int(ymax.getOutput(0)), cellsize):
        print x,y

Note this code will take a long time to run with this print statement as it must print a new line for every cell in your raster.

Another method for this would be to read the convert the raster to an ASCII raster and read it into a 2D numpy array. The array indexes would then correspond to the coordinates of the cells.

6
  • I have a 5x5 grid of cells. With the help of the above code we can get the X, Y coordinates location of the whole cell, but what if we need to get the X, Y coordinates of every cell?
    – irini
    Nov 27, 2012 at 9:41
  • I have updated the answer to give coordinated for each cell.
    – sgrieve
    Nov 27, 2012 at 9:53
  • Would you please explain me how the 2D numpy array indexes correspond to the coordinates of the cells? I have created this numpy array for my raster file... and I have already read every cell value! Is there any extra command to read at the same time the coordinates of every cell? But I am also wondering whether I can obtain the coordinates in the center of every grid cell. I really thank you very much for your time!!!
    – irini
    Nov 27, 2012 at 10:31
  • To find the center of a cell simply add half the cell size to each x and y coordinate. The numpy array index will count from 0,0 but you can translate that index to your coordinate system by adding xmin, ymin to each index value.
    – sgrieve
    Nov 27, 2012 at 10:56
  • But how can I get the numpy array index? and how can I obtain the X, Y coordinates from the index? i am a really new user in Python scripting... please excuse me for my really stupid queries...
    – irini
    Nov 27, 2012 at 12:22
2

If you convert the raster to points add the X,Y fields and then use the calculate geometry function to add values to the X and Y fields.

2
  • I am actually programming in Python language and I would like to do the former procedure in Python.
    – irini
    Nov 27, 2012 at 7:55
  • 1
    so maybe you could have a look at arcpy.RasterToPoint_conversion(), arcpy.AddFeild() and arcpy.UpdateCursor()
    – dango
    Nov 27, 2012 at 9:42

Your Answer

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

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