Get raster cell XY and VALUE?

751
3
Jump to solution
06-15-2012 07:08 AM
KevinBell
Occasional Contributor III
Anyone out there know how to access a raster cell's center XY and VALUE directly?  I could arcpy.RasterToPoint and then get it from there, but that seems like an extra step.

Thanks!:D
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BruceNielsen
Occasional Contributor III
I don't think you can re-assign a variable while you're using it as an index. You will probably have to break it into 2 lines
for i in xrange(xwidth):     xmin += i     d[xmin, ymax] = ar[0]   for j in xrange(yheight):     ymax -= j     d[xmin, ymax] = ar[0]


Here's what I tried in the interactive window
>>> a=[1,2,3] >>> b=1 >>> a[b+=1] Traceback (  File "<interactive input>", line 1     a[b+=1]         ^ SyntaxError: invalid syntax

View solution in original post

0 Kudos
3 Replies
KevinBell
Occasional Contributor III
Ok, am I going nuts, or is it just Friday?

I'm trying to build a dict of raster values to later build into a point feature class, and I'm getting a syntax error in the += down where I'm filling my dictionary.  I don't see the problem.  :confused:

The dict looks like this:
(x,y): value
I'll append other values to the xy later


import arcpy
import pprint

theRaster = r'C:\gis\solarTESTING\default.gdb\d1'
ras = arcpy.Raster(theRaster)
ar = arcpy.RasterToNumPyArray(theRaster)

xmin = ras.extent.XMin 
ymax = ras.extent.YMax

yheight, xwidth = ar.shape

d={}

for i in xrange(xwidth):
 d[xmin += i, ymax] = ar[0]
 
for j in xrange(yheight):
 d[xmin, ymax -= j] = ar[0]

pprint.pprint(d) 
0 Kudos
BruceNielsen
Occasional Contributor III
I don't think you can re-assign a variable while you're using it as an index. You will probably have to break it into 2 lines
for i in xrange(xwidth):     xmin += i     d[xmin, ymax] = ar[0]   for j in xrange(yheight):     ymax -= j     d[xmin, ymax] = ar[0]


Here's what I tried in the interactive window
>>> a=[1,2,3] >>> b=1 >>> a[b+=1] Traceback (  File "<interactive input>", line 1     a[b+=1]         ^ SyntaxError: invalid syntax
0 Kudos
KevinBell
Occasional Contributor III
That did it!  Thanks!  I was going batty!

NOTE that code doesn't do what i want just yet, but it is ok as far as i've gotten on it.
0 Kudos