How do I update a Raster Field using a cursor? I'm attempting to write a script that uses a cursor to update this field with rasters from a directory. It's a stand-alone script using arcpy for ArcGIS Pro 2.3.1.
arcpy.da.InsertCursor, arcpy.da.SearchCursor and arcpy.da.UpdateCursor state that as a field_name, "Raster fields are not supported."
Thanks to GeoNet, here's the work around I eventually came up with seeing that using Python to update a raster field isn't supported.
In the end I didn't use a raster field at all. I had a text field with a unique ID that matched the file name of JPGs in a directory. I used a script that updated another text field named 'Photo' with the full name of the JPG. Then I used arcpy.EnableAttachments_management(), which created a related table that can store rasters. Finally, I used arcpy.AddAttachments_management(), which populated the related table. Working with attachments was also helpful.
<SPAN class="string token">'''the purpose of this script is to update the 'Photo' field in the fc with the file path of the same name. '''</SPAN> <SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os ws <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'path\to\Projects\DOT_SignInventory\pics'</SPAN> jpgs <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># empty dictionary</SPAN> <SPAN class="keyword token">for</SPAN> dirName<SPAN class="punctuation token">,</SPAN> subdirList<SPAN class="punctuation token">,</SPAN> fileList <SPAN class="keyword token">in</SPAN> os<SPAN class="punctuation token">.</SPAN>walk<SPAN class="punctuation token">(</SPAN>ws<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> fname <SPAN class="keyword token">in</SPAN> fileList<SPAN class="punctuation token">:</SPAN> jpgs<SPAN class="punctuation token">[</SPAN>fname<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>dirName<SPAN class="punctuation token">,</SPAN>fname<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> jpg <SPAN class="keyword token">in</SPAN> jpgs<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>jpg<SPAN class="punctuation token">)</SPAN> fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'path\to\Projects\DOT_SignInventory\Data2.gdb\WCDOT_Signs'</SPAN> fields <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'OID@'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Post_Number'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Photo'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> fields<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> jpgs<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">except</SPAN> KeyError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"No photo for: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">del</SPAN> cursor<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
For what it's worth, it looks like I can convert the rasters to a FGDB.
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'\pathTo\jpgs'</SPAN> <SPAN class="comment token">#Convert Multiple Raster Dataset to FGDB</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RasterToGeodatabase_conversion<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"0100030.jpg;0100040.jpg"</SPAN><SPAN class="punctuation token">,</SPAN> r<SPAN class="string token">"\pathTo\Data.gdb"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Of course, this is just a sample. I'll have to set the script up to iterate through the directory and convert thousands of jpgs to the FGDB. But, after I do that I'm still stuck wondering how to get the raster into the Raster Field via Python.
Hello Jared,
Maybe the following thread can get you closer to what you are looking for: https://community.esri.com/thread/92354
I hope it helps!
Dan,
Adding raster datasets as attributes is exactly what I need. But is there a way to automate this? I'm dealing with 1000s of rasters. I started out on this page and because there was no mention of Arcpy I moved on to looking into cursors.
I was referring more to hyperlinking to the data or similar.
Adding raster datasets as attributes in a feature class—ArcGIS Pro | ArcGIS Desktop
is the remaining option, which would probably only be useful for a few
So creating a separate table (using an insertcursor) for joining isn't a possibility?
How to use an insertcursor on a Raster Field is pretty much my question. There's a Raster Field in a FC that I want populated with rasters. But, as I was reading how to access that type of field with a cursor it says it's not possible:
Join Field—Data Management toolbox | ArcGIS Desktop
This join is permanent if memory serves
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.