How to load a directory of rasters into a geodatabase using Python?

3040
12
Jump to solution
11-15-2016 04:50 PM
DaveBranson
New Contributor II

I am trying to use Python in ArcGIS 10.4 to load a directory of JP2 images into a raster catalog.

I can load individual files but can figure out how to load all of the file at the same time.

this will load individual files ....

>>> import arcpy
>>> from arcpy import env
>>> env.workspace = "d:/data"

>>> arcpy.RasterToGeodatabase_conversion("Image1.jp2;Image2.jp2","d:/data/Database.gdb/Orthos")

I tried creating a list of the rasters with arcpy.ListRasters and replace the Input_Rasters with the list name and this works if there is only one item in the list.

Txs.

1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

Dave, I still have the issue that is doesn't like me to have a feature dataset name after the .gdb for the output, but otherwise, I got this to read all the .jpg files in a folder, and then add them to my fgdb.  I'm not sure if all the formatting and/or the full path to the .jpgs was necessary or not, but other than the output file going to the root of the fgdb, this works for me (maybe a bug for me?  ArcCatalog 10.3.1)

import arcpy
import os
from arcpy import env
sourceDir = env.workspace = r"d:\data"
myList = arcpy.ListRasters("", "jpg")
print(myList)

#OutGDB = r"C:/_scratch/test.gdb/blah"
OutGDB = r"C:/_scratch/test.gdb"

newList = []
for x in myList:
     z = os.path.join(sourceDir, x)
     newList.append(z)
     
inList = (";".join([i for i in newList]))
print(inList)

arcpy.RasterToGeodatabase_conversion(inList, OutGDB)

View solution in original post

12 Replies
DarrenWiens2
MVP Honored Contributor

May not be the issue, but your example has an error: Image2,jp2 should have a period, not comma.

RebeccaStrauch__GISP
MVP Emeritus

Also, you may want to try it without the "/Orthos"         I was able to do multiple into the "root" of a .fgdb, but not into a feature dataset.

0 Kudos
DaveBranson
New Contributor II

I should clarify that the above does work for loading individual files.  How do you specify a wildcard for Raster_Input to read the entire directory?  

I have unsuccessfully tried variations on:

 arcpy.RasterToGeodatabase_conversion("d:/data/*.jp2","d:/data/Database.gdb/Orthos")

 

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Are you setting the results of the ListRasters to a variable, and if so, have you printed it to make sure it is in the correct format?  Maybe the list is blank and/or formatted incorrectly for input into the RasterToGeodatabase command.  I don't have a good folder of .jpg rasters to test against right now (looking though), but that would be the next thing I checked.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Run the tool from arctoolbox, once using some representative data, go to the Geoprocessing Results window, expand the tool output Using the Results window—Help | ArcGIS for Desktop 

have a look and you can even export to a python snippet.  I get worried, when it says a 'list' is required, sometimes that is a semi-colon delimited string and not python list... both are ok, but they are not the same type of object.  Once you know what is expected, check to see what ListRasters with an appropriate wildcard selected returns,

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Dan, this is something you could probably format in a couple seconds.  I've tested this on a folder with a bunch of .jpg and the output is a list in the incorrect format...you are much faster at conversion than I am.

import arcpy
from arcpy import env
env.workspace = r"d:\data"
myList = arcpy.ListRasters("", "jpg")
print(myList)

OutGDB = "C:/_scratch/test.gdb/blah"

#arcpy.RasterToGeodatabase_conversion(myList, OutGDB)

as a sample with my data...

print(myList)  # gives me

[u'bluefin.JPG', u'char.JPG', u'clams.JPG', u'cod.JPG', u'dungenes.JPG', u'fish.JPG', u'grayling.JPG', u'halibut.JPG', u'herring.JPG', u'octopus1.JPG', u'salmon-chum.JPG', u'salmon-sockeye.JPG', u'skipjack.JPG', u'sole.JPG', u'squid2.JPG', u'trout-lake.JPG', u'trout-rainbow.JPG']

Jay would need"

"bluefin.jpg; char.jpg; clams.jpg; ....."

no "u" , [] or single quotes....separated by semi-colon...and enclused with double quotes.   I'm not sure if full path to the data is also needed.

0 Kudos
DanPatterson_Retired
MVP Emeritus

python 2.7 and 3.5 on the iThing

>>> a
['bluefin.JPG', 'char.JPG', 'clams.JPG', 'cod.JPG', 'dungenes.JPG', 'fish.JPG', 'grayling.JPG', 'halibut.JPG', 'herring.JPG', 'octopus1.JPG', 'salmon-chum.JPG', 'salmon-sockeye.JPG', 'skipjack.JPG', 'sole.JPG', 'squid2.JPG', 'trout-lake.JPG', 'trout-rainbow.JPG']
>>> ";".join([ i for i in a])
'bluefin.JPG;char.JPG;clams.JPG;cod.JPG;dungenes.JPG;fish.JPG;grayling.JPG;halibut.JPG;herring.JPG;octopus1.JPG;salmon-chum.JPG;salmon-sockeye.JPG;skipjack.JPG;sole.JPG;squid2.JPG;trout-lake.JPG;trout-rainbow.JPG'
‍‍‍‍

The single or double quotes don't matter nor does the unicode prefix

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Dave, I still have the issue that is doesn't like me to have a feature dataset name after the .gdb for the output, but otherwise, I got this to read all the .jpg files in a folder, and then add them to my fgdb.  I'm not sure if all the formatting and/or the full path to the .jpgs was necessary or not, but other than the output file going to the root of the fgdb, this works for me (maybe a bug for me?  ArcCatalog 10.3.1)

import arcpy
import os
from arcpy import env
sourceDir = env.workspace = r"d:\data"
myList = arcpy.ListRasters("", "jpg")
print(myList)

#OutGDB = r"C:/_scratch/test.gdb/blah"
OutGDB = r"C:/_scratch/test.gdb"

newList = []
for x in myList:
     z = os.path.join(sourceDir, x)
     newList.append(z)
     
inList = (";".join([i for i in newList]))
print(inList)

arcpy.RasterToGeodatabase_conversion(inList, OutGDB)
DaveBranson
New Contributor II

Thanks Rebecca!  That solved the problem.  

The ListRasters output did not include the path.

0 Kudos