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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityI 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?
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.
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.