Select to view content in your preferred language

How to load image to Raster attribute field with python

5463
4
04-28-2014 08:55 AM
AndrésSantos_Lozano
Deactivated User
I'm working with ArcGis 10.0 and I try to load dinamically images to Raster attribute field using python, anyone knows any method to load an image in this field.

This is my code

arcpy.env.workspace = r'D:\LABSIG\CAU\trabajos_diarios\Pruebas Raster\mbd_proyecto_arbolado.mdb'

entityDataset = arcpy.ListDatasets("arbolado")
featureClass = arcpy.ListFeatureClasses("Arbol_censo", "", entityDataset[0])
image = r'D:/Proyecto Arbolado/C12/C12_657.JPG'
cursor = arcpy.UpdateCursor(featureClass[0])
for row in cursor:
 row.setValue("raster_field", image) 
 cursor.updateRow(row)


When I try to do this, nothing happened. Should exist an special method or function to load an image in a Raster field?
Please help me to find a solution. Thanks
Tags (2)
0 Kudos
4 Replies
XanderBakker
Esri Esteemed Contributor
Hi Andrés,

The thing that goes wrong is that you are not writing the binary representation of the image, but a string containing the path to the image.

Changing the code to something like this might help:

arcpy.env.workspace = r'D:\LABSIG\CAU\trabajos_diarios\Pruebas Raster\mbd_proyecto_arbolado.mdb'

entityDataset = arcpy.ListDatasets("arbolado")
featureClass = arcpy.ListFeatureClasses("Arbol_censo", "", entityDataset[0])
image = r'D:/Proyecto Arbolado/C12/C12_657.JPG'
image_blob = open(image,'rb').read()

cursor = arcpy.UpdateCursor(featureClass[0])
for row in cursor:
    row.setValue("raster_field", image_blob)
    cursor.updateRow(row)


Now I notice you are using a field of type raster. The code above might not work with a raster field. It does work using a BLOB field. What I don't know is if BLOB fields are supported in a personal geodatabase (MDB). I would recommend switching to a file geodatabase.

You can read more about reading and writing images to a BLOB field in the following blog:
http://anothergisblog.blogspot.nl/2012/06/working-with-blob-data-at-101-arcpyda.html

Kind regards,

Xander
0 Kudos
AndrésSantos_Lozano
Deactivated User
Hi Xander, thanks for your help.

Right now, I'm using ArcGIS 10.2.1, in this release there are many improves to the arcpy library.

Then doesn't exist any method to load an image in a RASTER field with python, may be something similiar how read the image, compressed, and save it in the Raster field?, because I need to do it for that way. It doesn't matter if I need to switch to a file geodatabase.

Best regards,

Andrés Santos
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Andrés,

The only way I found to load images into raster fields (not doing it manually as described in Help topic "Adding raster datasets as attributes in a feature class") is using a raster catalog. You can create a new raster catalog (set Managed By GDB to Yes) in a file geodatabase and load all the images that you may have stored in a folder with the tool "Workspace To Raster Catalog".

This will load all the images into a raster catalog, which is kinda like a table. It also contains a raster field holding the raster. If in the table where you want to load the rasters as attribute, you have a field with the name of the raster, this field can be used to join the raster catalog into the table. Please note the a table or featureclass cannot have more than 1 field of type raster!

After joining the raster catalog the result can be exported to make the join permanent. The output table or featureclass will contain a field called Raster with the actual raster inside.

Kind regards,

Xander

Any intent I did with code to load the raster resulted in an error. Since I could not find any documentation on doing this in Python I assume it is not supported.
0 Kudos
JaredPilbeam2
MVP Regular Contributor
Any intent I did with code to load the raster resulted in an error. Since I could not find any documentation on doing this in Python I assume it is not supported.

Xander,

Is this supported in arcpy for ArcGIS Pro? I'm currently looking for a way to update raster fields in FC table with Python.

I have a script that uses the updatecursor to populate a text field with a path to the directory where the rasters are stored. But, what I really need is to update the raster field with the image itself. And apparently you can't use cursors for this. I started a thread the other day: 

https://community.esri.com/thread/231019-raster-fields-not-supported-using-cursors

0 Kudos