Hi all,
I am trying to understand the behavior of ExtractMultiValuesToPoints when incorporated into an Arcgis tool.
I have used the function in an Arcpy standalone script, and it works just fine. However, after incorporating into a python tool, the stars have to be aligned just right for it to work. Included is a tool that I have created to extract values from a multiband raster into a new shapefile.
The issues are (1) I have to rename the bands because the tool generates new names not related to the raster bands, (2) if the shapefile is linked to the tool from a GIS workspace, the names aren't carried over, (3) rasters cannot be linked to from a GIS workspace and (4) ExtractMultiValuesToPoints has no option to preserve the original file (as far as I can tell). These are really just minor annoyances, so am wondering if I am somehow doing it wrong. Thanks for any information.
class Combine_labels_features(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "07 Combine labels and features"
self.description = "This tool combines the labels point shapefile (generated and QAQC'd) with the appropriate features file"
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
labels = arcpy.Parameter(
displayName="labels file",
name="labels",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
labels.filter.list = ["Point"]
features = arcpy.Parameter(
displayName="features (raster)",
name="features",
datatype= u'GPRasterLayer',
parameterType="Required",
direction="Input")
outfile = arcpy.Parameter(
displayName="outfile",
name="outFile",
datatype="DEType",
parameterType="Required",
direction="Output")
params = [labels,features,outfile]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
labels = parameters[0].valueAsText
infile = parameters[1].valueAsText
outfile = parameters[2].valueAsText
features = Raster(infile)
rasters = [] ### holder for raster predictors
nams = features.bandNames
names = ["B_" + str(i+1) for i in range(0,len(nams))]
for i in range(0,len(nams)):
print(nams[i])
tmplist = [infile + "\\" + nams[i],names[i]]
rasters.append(tmplist)
origNames = [field.name for field in arcpy.ListFields(labels)]
arcpy.sa.ExtractMultiValuesToPoints(labels,infile,"BILINEAR")
tmpfile = os.path.basename(tempfile.TemporaryFile().name)
arcpy.management.CopyFeatures(labels,tmpfile,'', None, None, None)
badNames = [field.name for field in arcpy.ListFields(tmpfile)]
badNames = [x for x in badNames if x not in origNames]
badNames.remove("OBJECTID")
BandKey = {badNames[i]:nams[i] for i in range(len(badNames))}
for key, value in BandKey.items():
print(key)
print(value)
arcpy.AlterField_management(tmpfile, key,value)
## add directly to initial labels. Should make copy if you want to preserve.
## ExtractMultiValuesToPoints is not behaving as it should. In Arcpy, the names from
## the original rasters carries through. Does not happen in the tool. Not sure why,
arcpy.management.CopyFeatures(tmpfile,outfile,'', None, None, None)
arcpy.Delete_management(tmpfile)
return
Extract Multi Values to Points (Spatial Analyst)—ArcGIS Pro | Documentation
regarding (4)
OBJECTID is mentioned as well as shapefile, did you mean to include FID and OID for removal as well?
For the purposes of the tool, ObjectID is not important, but I can see that in many cases it would be important.