Using a file path stored in an attribute field to add data to the data frame

5209
5
Jump to solution
04-10-2015 07:20 AM
NathanPugh
New Contributor III

I am trying to add a raster dataset to my dataframe from a path stored in a field within the attribute table of a feature class.  Ideally, I would like to add the rasters one at a time based upon a selection performed on the feature class.   I have a python script which will add the raster based on the file path in the field but I am having trouble adding the selection process to it.  Has anybody done something similar or have any ideas? 

Thanks in advance

import arcpy

fc = "C:/Users/username/Documents/ArcGIS/Default.gdb/script_input"

fields = ["filepath"]

# For each row print the "path" field

#

with arcpy.da.SearchCursor(fc, fields) as cursor:

    for row in cursor:

     pathfield = ("{0}".format(row[0]))

print(pathfield)

# Local variables:

qqlayer = pathfield

Output_raster_layer_name = "Raster to Look at"

# Process: Make Raster Layer

test = arcpy.MakeRasterLayer_management(qqlayer, Output_raster_layer_name, "", "213322.051897854 4081064.77976305 290804.751897854 4174358.97976305", "")

0 Kudos
1 Solution

Accepted Solutions
DavidChambers1
New Contributor III

Hey Nathan,

Are you doing something similar to this documentation for selecting the records interactively? If you are selecting the records of the feature layer in the map document then you would need to process that same feature layer with the search cursor.  In your script it looks like you are referencing a hardcoded input fc which is not the same layer that is in the map document:

fc = "C:/Users/username/Documents/ArcGIS/Default.gdb/script_input"

That variable would need to reference the in-memory feature layer.  Check out the arcpy.mapping module in order to accomplish this. 

In order to avoid the in-memory processing I believe the Select Layer By Attribute tool could be used in the script to select records from your harcoded FC. This would not allow you to select attributes interactively though.

Let me know if you have any specific questions about that and best of luck!

Thanks,

David

View solution in original post

5 Replies
DavidChambers1
New Contributor III

Hi Nathan,

The cursor will honor a selection and would work similar to other geoprocessing tools when a selection is applied to the data that is being processed (only the selection gets processed).  You could select the attributes manually in the attribute table or use the Select Layer By Attribute tool within your script and build in a SQL expression to select the records you would like to add.  Let me know what you think!

NathanPugh
New Contributor III

Thanks for suggesting the SQL expression.  Your 1st comment about manually selecting a record in the attribute table is the one I would like to go with but for some reason does not work.  The script always returns the 1st record no matter which record is selected.  Any suggestions?

0 Kudos
DavidChambers1
New Contributor III

Hey Nathan,

Are you doing something similar to this documentation for selecting the records interactively? If you are selecting the records of the feature layer in the map document then you would need to process that same feature layer with the search cursor.  In your script it looks like you are referencing a hardcoded input fc which is not the same layer that is in the map document:

fc = "C:/Users/username/Documents/ArcGIS/Default.gdb/script_input"

That variable would need to reference the in-memory feature layer.  Check out the arcpy.mapping module in order to accomplish this. 

In order to avoid the in-memory processing I believe the Select Layer By Attribute tool could be used in the script to select records from your harcoded FC. This would not allow you to select attributes interactively though.

Let me know if you have any specific questions about that and best of luck!

Thanks,

David

NathanPugh
New Contributor III

That was the ticket!  Thanks.  Here is the correct code script below. 

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

lyr = arcpy.mapping.ListLayers(mxd, "qq")[0]

fields = ["path"]

# For each row print the "path" field

#

with arcpy.da.SearchCursor(lyr, fields) as cursor:

    for row in cursor:

     pathfield = ("{0}".format(row[0]))

print(pathfield)

# Local variables:

qqlayer = pathfield

Output_raster_layer_name = "QQ to inspect"

# Process: Make Raster Layer

arcpy.MakeRasterLayer_management(qqlayer, Output_raster_layer_name, "", "", "")

DanPatterson_Retired
MVP Emeritus

resolved?​

0 Kudos