Reading raster value based on the point feature location

516
4
11-24-2023 07:58 AM
mykola
by
New Contributor III

Good day everyone,

I am trying to find solution to a particular problem with reading raster values. I have a point feature class in local coordinate system (3057) and I also have a raster in .gtx format that is in WGS 1984 (in Spatial Reference tab in "Properties -> Raster Information"). I need to extract a raster value based on the location of a point feature using arcpy. From the search on the web, I ended up using RasterToNumPyArray() to convert the raster and then read the value from it based on a point location. However, for some reason the coordinate of the point does not align to the same location in the raster and usually returns the raster value of None.  I think the problem that I am facing has something to do with projecting the data because for some reason the script stops when comes to arcpy.projectAs(). I appreciate if someone knows how to tackle this issue. 

My code includes the following:

point = ['SHAPE@'] that I am reading with UpdateCursor()

raster_desc = arcpy.Describe(gtx_raster)
raster_sr = raster_desc.spatialReference

point_x_local = point.centroid.X

point_y_local = point.centroid.Y
point_centroid = arcpy.Point(point_x_local, point_y_local)
point_projected = point_centroid.projectAs(raster_sr)

raster_array = arcpy.RasterToNumPyArray(gtx_raster, (point_projected.X, point_projected.Y),1, 1, nodata_to_value=None)
raster_value = float(raster_array[0, 0])

 

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

Extract Values to Points (Spatial Analyst)—ArcGIS Pro | Documentation

should be used

RasterToNumpy array is not designed to extract a single value from a raster, in fact the "point" values you are providing are supposed to be the lower-left corner of the array

RasterToNumPyArray—ArcGIS Pro | Documentation

If you wish to continue in numpy, convert your points to a raster in the same coordinate system as your other day and use fancy indexing to extract the raster values.


... sort of retired...
mykola
by
New Contributor III

Thanx for a prompt reply. The reason I went the numpy way is that I need to schedule the script to run on daily basis but we do not have a spatial analyst permission. Some people suggest the way I described above which locates the point in raster matrix and extracts (1,1) value in raster. But it seems to fail in my case 😁.  

0 Kudos
DanPatterson
MVP Esteemed Contributor

it would be 0,0 since arrays are 0 based


... sort of retired...
mykola
by
New Contributor III

As always, once I gave up and looked for a solution here in the community, I managed to figure out what was wrong. As you suggested above about (0,0), the thing is that the RasterToNumPyArray takes ncols and nrows arguments, so (1,1) is basically the number of row,col. This is not about indexing the array. Anyway, the main issue that caused the error was the definition of the {lower_left_corner} which supposed to be a Point object and not just point coordinates (which I saw in some of the answers on another forums). 

I need to thoroughly test the solution before I can confirm that it is indeed working as it should. Thanks for answers, it led me to the solution by focusing on the point itself and indexing.

0 Kudos