I have a table("ecw") with a field ("raster_path") which contains the paths of many rasters (ecw type) Selecting several rows from the table, I Would like to add the rasters into my dataframe.
How do I do it?
(Sorry if my english is not good; It isn't my native languaje)
2 Answers 2
Thank's for your answer. I applied your code to my work. but it doesn't works good.The first raster is added correctly,but it is no one of the selected raster in the table. I think the code tries to add all the files. And then, the code show this message: Runtime error Traceback (most recent call last): File "", line 20, in NameError: name 'result' is not defined
Mi code is this:
#get the mxd you're in
mxd = arcpy.mapping.MapDocument("CURRENT")
#get the dataframe
df = mxd.activeDataFrame
#select the records via your query
arcpy.SelectLayerByAttribute_management("CATALUNA_2013", "NEW_SELECTION")
#create a cursor to go through those records
rows = arcpy.SearchCursor("CATALUNA_2013")
#go through the records
for row in rows:
#get the path
tablePath = row.getValue("FILENAME")
#set up the new raster layer
rasterLayer = arcpy.MakeRasterLayer_management(tablePath, row.NAME)
#get the output from the in memory layer
addLayer = result.getOutput(0)
#insert into the data frame
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
del rows
-
You should post these as comments instead of answers or edit your question. I corrected my code. The issue was I never set up the
result
variable (carry over from the other forum post). Change it torasterLayer.getOutput(0)
and it should work better. I corrected my answer above.Branco– Branco2015年05月07日 12:35:07 +00:00Commented May 7, 2015 at 12:35
My solution is more arcpy based. I would run this from the interactive window. I left some spots in there for you to add your own information/query/etc. I borrowed some of the raster code from this other post.
#get the mxd you're in
mxd = arcpy.mapping.MapDocument("CURRENT")
#get the dataframe
df = mxd.activeDataFrame
#select the records via your query
arcpy.SelectLayerByAttribute_management("TABLENAME", "NEW_SELECTION", "WHERE QUERY OF SORTS")
#create a cursor to go through those records
rows = arcpy.SearchCursor("TABLENAME")
#go through the records
for row in rows:
#get the path
tablePath = row.getValue("raster_path")
#set up the new raster layer
rasterLayer = arcpy.MakeRasterLayer_management(tablePath, "NAME FOR THE LAYER")
#get the output from the in memory layer
addLayer = rasterLayer.getOutput(0)
#insert into the data frame
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
del rows