2

My attempt is to view raster values in the command window. The follwing code is thought to do this by converting the raster to a point feature file and by successively scanning the values at the points:

import arcpy, os
arcpy.env.workspace = os.path.join('H:\\','myexample')
arcpy.env.outputZFlag = 'Enabled'
arcpy.conversion.RasterToPoint('myraster','mypointfeaturefile.shp')
with arcpy.da.SearchCursor('mypointfeaturefile.shp', 'SHAPE@XYZ') as cursor:
    for row in cursor:
        print(u'{0}, {1}, {2}'.format(row[0], row[1], row[2]))

The arcpy.conversion.RasterToPoint process works fine: a raster value can be retrieved from mypointfeaturefile.shp in ArcGIS Pro by activating Explore and clicking onto a point of mypointfeaturefile.shp. Therefore, the points in mypointfeaturefile.shp seem to hold raster values as Z-values. However, the 6th line for row in cursor returns the error message:

RuntimeError: Cannot find field 'SHAPE@XYZ'. Input feature class is not M- or Z-enabled.

I was following the SearchCursor documentation together with Barbarossa's case and assumed that the point feature file would be Z-enabled by the 3rd line

arcpy.env.outputZFlag = 'Enabled'

However, this does not seem to be the case. What am I missing in this process to make mypointfeaturefile.shp Z-enabled?

System:

  • OS: Windows 11 Pro, 21H2
  • arcpy: version 3.11.1
2
  • 1
    You do not say which software you are using and it's version.
    – Hornbydd
    Mar 19 at 22:09
  • The resulting shapefile is probably 2D. You'd need to pass it through a surface to pick up a Z value.
    – Vince
    Mar 19 at 22:30

1 Answer 1

6

Raster to Point doesn't add Z information, it just creates an attribute in the output 2D feature class's attribute table. From the Raster To Point documentation:

Converts a raster dataset to point features.

Usage

...

The Field parameter allows you to choose which attribute field of the input raster dataset will become an attribute in the output feature class. If a field is not specified, the cell values of the input raster (the VALUE field) will become a column with the heading GRID_CODE in the attribute table of the output feature class.

You need a 3D Analyst license and to run the Feature To 3D By Attribute tool on the output of Raster to Point to create a 3D feature class

import arcpy, os

# if running outside of ArcGIS or inside ArcGIS without 3D already checked out
arcpy.CheckOutExtension("3D")

arcpy.env.workspace = os.path.join('H:\\','myexample')
arcpy.env.outputZFlag = 'Enabled'
arcpy.conversion.RasterToPoint('myraster','point2D.shp')
arcpy.ddd.FeatureTo3DByAttribute('point2D.shp', 'point3D.shp', 'GRID_CODE')

with arcpy.da.SearchCursor('point3D.shp', 'SHAPE@XYZ') as cursor:
    for row in cursor:
        # note row is a tuple of requested fields, and you only asked for one field 
        # so row[0] == SHAPE@XYZ
        # however SHAPE@XYZ also returns a tuple 
        # so you need row[0][x],row[0][y],row[0][z]
        print(u'{0}, {1}, {2}'.format(row[0][0], row[0][1], row[0][2]))

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.