Select to view content in your preferred language

Raster Domain python script change output location

2929
14
Jump to solution
11-27-2018 02:12 PM
Jacques-AlexandreGadoury
Emerging 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
14 Replies
DanPatterson_Retired
MVP Emeritus

If you are using ArcGIS pro then be aware...

Shapefiles cannot be added to the project Databases folder but must be stored in the project folder or a folder connection.

Shapefiles in ArcGIS Pro—Shapefiles | ArcGIS Desktop 

0 Kudos
Jacques-AlexandreGadoury
Emerging Contributor

I am on ArcGIS 10.4.  But it is indeed useful to know that about ArcPro for future reference.

0 Kudos
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)
Jacques-AlexandreGadoury
Emerging Contributor

Beautiful Jake,

That seems to have worked. I'm guessing that os.path.basename(raster) stripped the path that was appended in the rasters =[] list? Anyways I think this should do. Thank you for your help. I'll just action this as correct as it did complete what I set out for.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Yes, the os.path.basename simply took the file name rather than the entire path. 

0 Kudos