How to export your raster datasets referenced in a “raster” attribute field (file geodatabase) using Python.


Although I can’t call myself a Python expert, from time to time I come across some interesting workflows worth sharing through this blog. This short story will be my first post on using Python to solve some non-standard tasks in ArcGIS for Desktop 10.2.1

This blog was actually created from a support incident where one of my clients asked me how to export the raster datasets that are stored within a file geodatabase, and referenced in a “Raster” attribute field, into a set of external files stored in one of the common files “. TIF” or “.JPEG.”

Ok, so here are a few pointers:

1. Imagine that we have a geodatabase with a point feature class and it contains two important attribute fields [image] and [name] (raster and text field types accordingly).

01

2. We need to export rasters from that geodatabase and produce a single JPEG or TIFF file per row. To make things more challenging we need to name the output rasters using the value stored in the corresponding row in the attribute field called [NAME], as above.

A small dataset can be processed manually: we can click on each raster (the “>” symbol within the raster field) and use the Save As option.

02

But what if the dataset contained thousands of records?? Well, we would have to play around with a Python code.

The following simple code can help you to resolve this monumental task: (click on the image to view it in high resolution):  

04

The subsequent output will be a set of TIFF files created in a folder specified in the script.

03

To run the script, use Notepad or any other text editor app, save the code as a *.py file, replace the paths and field names and just load it to the Python Window in ArcMap. But please note that the above script is shared as a recommendation only and Esri Australia is not responsible for anything related to the deployment of this script. Just a friendly disclaimer!

A request for this functionality to be included in the core software had also been posted as a software enhancement idea on ideas.arcgis.com. So if you feel that this tool could be useful, you can just jump online and vote for the idea here: >>

Enjoy geoprocessing with Python!

PS
Special thanks to my colleagues Tania and Rhys for their participation.

Ivan E

13 thoughts on “How to export your raster datasets referenced in a “raster” attribute field (file geodatabase) using Python.

  1. Physgeo

    Hello! Thank you for posting this!

    Could you please take a look? Where there may be an error?

    When I try to run a script I get the following error:

    >>> output_path = r’D:\Temp’

    … import os
    … os.chdir(output_path)

    … fc = r’D:\Temp\Borodin\Borodin.gdb\Collector\SampleWater’

    … with arcpy.da.SearchCursor(fc, (“Photo”, “Sample”)) as cursor:
    … for row in cursor:
    … filename = “{0}.tiff”.format(row[1])
    … row[0].save(filename)

    … print ‘finished’

    Runtime error
    Traceback (most recent call last):
    File “”, line 9, in
    RuntimeError: unsupported field type Photo #9
    >>>

    Thanks!

    Reply
    1. Kev

      I am having the same error as Physgeo , what is the solution you email? would be really handy if you could post it

      Reply
  2. Ivan E. Post author

    Thank you for your question. I have just replied to you via email.
    Let me know if this helps.

    Ivan

    Reply
  3. Physgeo

    Work good on 10.2 with python 2.7.2! Thank you!

    For features with some empty raster-rows we get an error message saying:
    Runtime error
    Traceback (most recent call last):
    File “”, line 10, in
    AttributeError: ‘NoneType’ object has no attribute ‘save’

    One more interesting thing. We find all output files in the ‘C:\Users\username\AppData\Local\ESRI\Desktop10.2\SpatialAnalyst’

    Oleg

    Reply
  4. Ivan E. Post author

    No worries, Oleg. I’m glad it worked.

    As for the results being saved to “C:/users…./”, you can either change the environment variable in the model (properties/environments/workspace/) or change the default home folder in ArcMap (in the Catalog window hit the Options button > switch to the Home Folder tab > change the default folder).

    Hope it works for you.

    Reply
  5. Travis

    I can save images as .tif just fine with your example. However, I’m trying to save as .jpg and I’m getting the following runtime error:

    ERROR: 010093: Output raster format UNKNOWN is unsupported

    All I’m doing is replacing .tif with .jpg. Any ideas?

    Reply
  6. Kev

    I am having the same error as Physgeo , what is the solution you email? would be really handy if you could post it

    Reply
    1. Travis

      I figured it out Kev. You need to add “skip_nulls=True” as shown below:

      import arcpy, os
      output_path = r”C:\…ENTER THE PATH”
      os.chdir(output_path)

      fc = r”C:\…ENTER THE LOCATION OF THE FEATURE CLASS”

      with arcpy.da.SearchCursor (fc, (‘RASTER FIELD NAME’, ‘NAME FIELD’), skip_nulls=True) as cursor:
      for row in cursor:
      filename = “{0}.tif”.format(row[1])
      row[0].save(filename)

      print “finished”

      Reply
  7. Ivan E. Post author

    @Travis and Kev

    I’m sorry for not responding sooner; unfortunately I was quite busy and didn’t have a chance to research this.

    The resolution that I sent to Physgeo was simply to run the script in ArcGIS 10.2 (he had been testing it in 10.1) and it worked for him.

    Thank you Travis for finding and publishing the solution.

    Reply
    1. Kev

      Thanks for the responses. I can confirm that my issue was using 10.1, but being able to skip null is really handy good find Ivan

      Reply
  8. Marie

    Would the script work in 10.1, if Python 2.7 was installed, or is there some issue with it not working in 10.1 regardless of python version?

    Reply
  9. Gaston

    Hello, thank you very much for this so useful post!
    Maybe someone can help me with my problem, I am not very skilled in Python coding… I’m using ArcGIS 10.0 (Python 2.6.5), which does not have the Data Access Module ‘da’. Is it possible to backport the script to make it compatible with this older version of arcpy?
    Thank you in advance.

    Reply

Got something to say?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s