Raster Domain python script change output location

1598
14
Jump to solution
11-27-2018 02:12 PM
Jacques-AlexandreGadoury
New Contributor

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:

import arcpy

import os

 

workspace = r”D:\Folder\Test”

outFile = r”D:\Folder\Index”

 

rasters = []

 

arcpy.CheckOutExtension(“3D”)

 

print ‘Processing’

 

for dirpath, dirnames, filenames, in arcpy.da.Walk(workspace, topdown=True, datatype=”RasterDataset”):

                for filename in filenames:

                                rasters.append(os.path.join(dirpath, filename))

 

for raster in rasters:

                outGeom = “POLYGON”

                outPoly = raster[:-4]+”.shp”

                arcpy.RasterDomain_3d(raster, outPoly, outGeom)

 

print rasters

 

print ‘Completed’

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.

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

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)

View solution in original post

14 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Jacques,

Try the following:

arcpy.RasterDomain_3d(raster, os.path.join(outFile, outPoly), outGeom)
0 Kudos
Jacques-AlexandreGadoury
New Contributor

Hey Jake,

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.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Before the arcpy.RasterDomain_3d function, add a print statement:

print(os.path.join(outFile, outPoly))

What is this returning?

0 Kudos
Jacques-AlexandreGadoury
New Contributor

So adding the command line as so:

outPoly = raster[:-4]+"shp"

print (os.path.join(outFile, outPoly))

arcpy.RasterDomain_3d(.....)

returns paths for each file as follows:

D:\Folder\Test\Imagery\Applanix\Cambridge\21671694.shp

I'm guessing this means it is still reading the outFile as the workspace?

0 Kudos
Jacques-AlexandreGadoury
New Contributor

Tried simply using

print (os.path.join(outPoly))

Without outFile, it returned the same thing as if I had (outFile, outPoly)

D:\Folder\Test\Imagery\Applanix\Cambridge\21671694.shp

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

'dem'[:-4]
''

would result in a null name to which you append .shp.

What is the error message?

0 Kudos
Jacques-AlexandreGadoury
New Contributor

Hey Dan,

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.

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

Raster Domain—Help | ArcGIS Desktop 

0 Kudos
Jacques-AlexandreGadoury
New Contributor

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.

0 Kudos