I have a folder tree with many other folders in it containing many images. I have managed to make a script that will parse through the entire folder and find every image in it. It then uses raster domain to create a polygon shapefile of every footprint for each image.
The issue I am having is I need to have those footprints exported to a single folder named "index". The current script always exports the footprints to the same folder the image is located in. How can I change my script to export the raster domain footprints to the "index" folder instead of them being exported to the folder containing the imagery?
Here is my script:
workspace = r”D:\Folder\Test”
outFile = r”D:\Folder\Index”
rasters = 
for dirpath, dirnames, filenames, in arcpy.da.Walk(workspace, topdown=True, datatype=”RasterDataset”):
for filename in filenames:
for raster in rasters:
outGeom = “POLYGON”
outPoly = raster[:-4]+”.shp”
arcpy.RasterDomain_3d(raster, outPoly, outGeom)
I'm pretty sure the answer lies with changing the "outPoly" parameter but anything I have tried resulted in an error. Any help would be much appreciated.
Thank you for your time.
Solved! Go to Solution.
Try the following:
for raster in rasters: outGeom = "POLYGON" outPoly = os.path.basename(raster)[:-4]+".shp" arcpy.RasterDomain_3d(raster, os.path.join(outFile, outPoly), outGeom)
Gave that a shot, it still exported the shapefiles to the workspace, there are no shapefiles in the outfile folder. The script did still run without any errors though.
So adding the command line as so:
outPoly = raster[:-4]+"shp"
print (os.path.join(outFile, outPoly))
returns paths for each file as follows:
I'm guessing this means it is still reading the outFile as the workspace?
Tried simply using
Without outFile, it returned the same thing as if I had (outFile, outPoly)
What are the raster types? You are slicing off the last 4 characters of the raster name which is useful if it is a *.tif, but can result in other errors if they aren't. for example
would result in a null name to which you append .shp.
What is the error message?
There is a large variety of types, .TIF is the most common but we also have .TIL .IMD .NTF .JPG, etc.
I did this because I started trying this out with model builder, but the model would end up saving the file as a "Raster.tif.shp" which gave errors with following tools. When I switched over to a script instead of a model I kept that code to slice off the .tif or which ever extension to make sure I get a .shp and not a .tif.shp shapefile like I use to get in the model builder.
And there is no error message the script runs as it should and it outputs a shapefile with the same name as the raster with the footprint. It just does not save it where I want it to be saved.
have you check for shapefiles in the folders where the rasters were initially located?
The print statement suggested by Jake should clarify that
in the interim check one folder first... use the code snippet at the bottom of this help incase it is the 'walk' thing that is fouling up
Yes I have already looked at that code sample and that is actually where I got the [:-4]. The script did work for individual folders, so it is possible that the way I have my walk setup or the raster list appended might be causing issues. But I do need this to work with a walk as it is the best way of parsing through a folder tree while ignoring images in geodatabases and in mosaics.
The statement Jake provided as shown in the responses below did give an unexpected result.