Create input raster list for Extract Multi Values to Points using Python

5654
8
Jump to solution
04-14-2015 03:22 PM
StacyMcNeil
New Contributor II

I have a python script to run the tool Extract Mulit Values to Points.  In that script I have created a list of raster files from a directory that I would like to use as input for this tool.  I would also like to be able to specify the field name.  The problem I am having is that the list doesn't "look" the way the tool wants it.  Here is a snippet.

#Import system modules
import arcpy
import os

from arcpy import env
from arcpy.sa import *

#Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial") 

#Set environment settings - Location of Rasters
arcpy.env.workspace = r"N:\Raster\PRISM\DailyData\Rasters"

#Input Point Feature
inPnt = r"D:\workspace\r_prism2.shp"

#Create list of rasters for input to tool
listRa = arcpy.ListRasters()
inRasterList = []
for i in listRa:
    inRasterList.append(i +  " " + "P" + i[27:31])

#Execute Tool
arcpy.sa.ExtractMultiValuesToPoints(inPnt,inRasterList,"BILINEAR")
print "finished"

So, the inRasterList looks like this- the raster file name followed by the field name which was extraced from the file name.

Raster_20010116.bil P0116

Raster_20010117.bil P0117

Raster_20010118.bil P0118

I think I am very close but I don't have it in the format the tool wants:

[["Raster_20010116.bil","P0116"],["Raster_20010117.bil","P0117"],"Raster_20010118.bil", "P0118"]]

I  could create a file like this and read it in but would like to do it all within Python.  Any help would be appreciated!

1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Interesting case... I see that you area close to the same syntax as in the help. Since it is a nested list, you should create a list with two items (raster name and output field name) and append that to your list:

listRa = arcpy.ListRasters()
inRasterList = []
for i in listRa:
    ras_fld = [i, "P" + i[27:31]]
    inRasterList.append(ras_fld)

arcpy.sa.ExtractMultiValuesToPoints(inPnt, inRasterList, "BILINEAR")

What I also notice is that you are using BIL rasters. These are band interleaved by line rasters usually have multiple bands. At first I though one would need to access each band in the extract operation, but once tested that seems not to be the case. You can use BIL rasters and for each band a field will be added like b#_colname (where # represents the band number).

To be sure it is normally wise to do this step one time with two rasters manually and copy the python snippet in the Results window to learn the syntax used.

So when I manually do the extract multi value to points and checked the syntax, it seems a different tool is being used:

arcpy.gp.ExtractMultiValuesToPoints_sa("mypoints","mandala1.bil manda_1;mandala2.bil manda_2","NONE")

There is no usage of nested lists, but a single string where each raster and field is separated by a space and each input raster field by a semi colon.

You can achieve this by doing this:

inRasterList = []
for i in listRa:
    inRasterList.append(i +  " " + "P" + i[27:31])
string_list = ";".join(inRasterList)

arcpy.gp.ExtractMultiValuesToPoints_sa(inPnt, string_list, "BILINEAR")

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

sort of confused...why don't you use listRa which is the actual raster list, the inRasterList seems to be for output

0 Kudos
StacyMcNeil
New Contributor II

inRasterList is just a variable name.  It could be anything.  With the field names specified, the list doesn't work in the tool since it is not formatted correct.  That is what I am having trouble with.  Sorry for the confusion.

0 Kudos
DanPatterson_Retired
MVP Emeritus

>>> inRasterList = []

>>> i = 'testtesttesttesttest'

>>> inRasterList.append( [ i ,  ( "P" + i[:-4] ) ] )  #took some liberties on the slicing

>>> inRasterList

[['testtesttesttesttest', 'Ptesttesttesttest']]

XanderBakker
Esri Esteemed Contributor

Interesting case... I see that you area close to the same syntax as in the help. Since it is a nested list, you should create a list with two items (raster name and output field name) and append that to your list:

listRa = arcpy.ListRasters()
inRasterList = []
for i in listRa:
    ras_fld = [i, "P" + i[27:31]]
    inRasterList.append(ras_fld)

arcpy.sa.ExtractMultiValuesToPoints(inPnt, inRasterList, "BILINEAR")

What I also notice is that you are using BIL rasters. These are band interleaved by line rasters usually have multiple bands. At first I though one would need to access each band in the extract operation, but once tested that seems not to be the case. You can use BIL rasters and for each band a field will be added like b#_colname (where # represents the band number).

To be sure it is normally wise to do this step one time with two rasters manually and copy the python snippet in the Results window to learn the syntax used.

So when I manually do the extract multi value to points and checked the syntax, it seems a different tool is being used:

arcpy.gp.ExtractMultiValuesToPoints_sa("mypoints","mandala1.bil manda_1;mandala2.bil manda_2","NONE")

There is no usage of nested lists, but a single string where each raster and field is separated by a space and each input raster field by a semi colon.

You can achieve this by doing this:

inRasterList = []
for i in listRa:
    inRasterList.append(i +  " " + "P" + i[27:31])
string_list = ";".join(inRasterList)

arcpy.gp.ExtractMultiValuesToPoints_sa(inPnt, string_list, "BILINEAR")
DanPatterson_Retired
MVP Emeritus

​nice catch

0 Kudos
XanderBakker
Esri Esteemed Contributor

Thanx Dan, sorry to jump in, but I have had some similar experiences with tool that can be called in different ways from different modules... Very confusing...

0 Kudos
StacyMcNeil
New Contributor II

Thank you so much Xander.  That worked perfectly!  I did as you suggested and looked at the geoprocessing results and just couln't figure out how to get my raster list in that format using Python.  I think I really wanted it to be a list since I am used to working with those and am new to Python.  I can now sleep tonight without Python code running through my head

0 Kudos
XanderBakker
Esri Esteemed Contributor

I normally sleep better when I have some Python code running through my head...