1

As I am not very experienced with Python I need your help. I managed to modify a Script (that has been posted here Get boundary of raster image as polygon in ArcGIS Desktop? by https://gis.stackexchange.com/users/2043/jeb)

import arcpy,os

InFolder = arcpy.GetParameterAsText(0)
Dest=arcpy.GetParameterAsText(1)

arcpy.env.workspace=InFolder
#The raster datasets in the input workspace
in_raster_datasets = arcpy.ListRasters()

arcpy.CreateFeatureclass_management(os.path.dirname(Dest),os.path.basename(Dest),"POLYGON")
arcpy.AddField_management(Dest,"RasterName", "String","","",250)
arcpy.AddField_management(Dest,"RasterPath", "String","","",250)

cursor = arcpy.InsertCursor(Dest)
point = arcpy.Point()
array = arcpy.Array()
corners = ["lowerLeft", "lowerRight", "upperRight", "upperLeft"]
for Ras in in_raster_datasets:
    feat = cursor.newRow()  
    r = arcpy.Raster(Ras)
    for corner in corners:    
        point.X = getattr(r.extent, "%s" % corner).X
        point.Y = getattr(r.extent, "%s" % corner).Y
        array.add(point)
    array.add(array.getObject(0))
    polygon = arcpy.Polygon(array)
    feat.shape = polygon
    feat.setValue("RasterName", Ras)
    feat.setValue("RasterPath", InFolder + "\\" + Ras)
    cursor.insertRow(feat)
    array.removeAll()
del feat
del cursor

that writes the Extents of a List of rasters that are in one folder to a Shapefile and adds the name of the raster to the attribute table. I would need the same function, but instead of the Extent of each Raster with the Raster Domain - to have the exact frame of each extracted georeferenced video still.

Because I do not completely understand the single steps... I do not know where to incorporate the arcpy.RasterDomain_3d...

Frauke
  • 59
  • 9

1 Answers1

1

This worked for me, except the last part, but that can be done separately:

"""****************************************************************************  
Name: RasterDomain
Description: This script uses theRaster Domain tool to generate polygon footprints for all
*.jpg rasters in a given workspace. In a second step two new Fields are created and populated 
with the Filename and the Framenumber (part of the filename), then it appends all shapefiles 
into a new created shapefile

****************************************************************************"""

#02.02.2015
#by F.Scharf

# Import system modules
import arcpy, os
from arcpy import env
from arcpy.sa import *

# Obtain a license for the ArcGIS 3D Analyst extension
arcpy.CheckOutExtension("3D")

arcpy.env.overwriteOutput = True

###Set local variables
InFolder = r"D:\Videoforgeoref\dolo3_neu3\trim"
# Dest = "D:/VideoTest_WS/C4dolo1_3/dest"

# Set environment settings
env.workspace = InFolder


try:
    # Create the list of JPG rasters
    rasterList = arcpy.ListRasters("*", "JPG")
    print rasterList
    # Verify there are rasters in the list
    if rasterList:
        # Loop the process for each raster
        for raster in rasterList:
            # Set Local Variables
            outGeom = "POLYGON" # output geometry type
            outPoly = "domain_" + raster[:-4] + ".shp" # names all output with the prefix "domain_", [:-4] strips the .jpg from the raster name
            print "Creating footprint polygon for " + raster + "."
            #Execute RasterDomain
            arcpy.RasterDomain_3d(raster, outPoly, outGeom)
        print "Finished Loop RasterDomain."
    else:
        "There are no JPG files in the " + env.workspace + " directory."
except:
    # If an error occurred while running a tool print the messages
    print arcpy.GetMessages()   


"""*****************************************************************************
Here two fields are added, the Filename and - extracted from that -  the frame 
number (FrameNo) are calculated
******************************************************************************"""

#arcpy.env.workspace = r"D:\VideoTest_WS\C4dolo1_3\dest"

fcList = arcpy.ListFeatureClasses("domain*") # select all features starts with "domain"
fcs= []
pattern ="domain" # Select all features that have word "domain" in the name
#output = "D:\VideoTest_WS\C4dolo1_3\dest\merged"
for fcs in fcList:
    print "processing " + fcs
    # Define field name and expression
    field1 = "FILENAME"
    field2 = "FrameNo"
    fcs_name = fcs[0:-4] ### populates field with the filename, the [0:-4] strips the .shp from the fcs name
    fcs_no = fcs[31:-4] ### populates field with the frame number - here the last 8 to 5 places of the string ### fcs[:] NEEDS TO BE MODIFIED FOR EVERY NAMING FORMAT
    # Create two new fields with a new name
    arcpy.AddField_management(fcs, field1 ,"TEXT","","","35")
    arcpy.AddField_management(fcs, field2 ,"LONG","","",10)
    # Calculate fields here
    arcpy.CalculateField_management(fcs, field1 , "\"" + fcs_name + "\"", "PYTHON")
    arcpy.CalculateField_management(fcs, field2 , long(fcs_no) , "PYTHON")#"\"" + long(fcs_no) + "\"", "PYTHON")
    print "field added and calculated"    

'''*********************************************************************************
Here the before created RasterDomains-Polygon Shapefiles are Merged together (Appended - not working because 
the Fields of the new created feature class do not fit the ones i the list - fields are lost -- here the 
outLocation has to be different then the first workspace! because there is no selection of the shapefiles
to be appended and the "ERROR 000572: The output cannot be the same as input." appears
********************************************************************************'''

outLocation = r"D:\Videoforgeoref\dolo3_neu3\out_box" ### HERE THE OUTPUT LOCATION OF THE MERGED SHAPEFILE HAS TO BE DEFINED - SHOULD BE DIFFERENT THEN THE INFOLDER LOCATION
emptyFC = "MergedFootprints.shp" ### name of the new shapefile

try:
    # Process:  Create a new empty feature class to append shapefiles into
    arcpy.CreateFeatureclass_management(outLocation, emptyFC, "POLYGON")

    # All polygon FCs in the workspace are footprint shapefiles, we want to merge these to a new shapefile in another folder
    fcList = arcpy.ListFeatureClasses("domain*","POLYGON") ### list will resemble [the shapes in the list] features classes starting with "domain"
    print fcList

    #Merge the Feature classes form the list
    arcpy.Merge_management(fcList, outLocation + os.sep + emptyFC)
    print "done with merging"
except:
    # If an error occurred while running a tool print the messages
    print arcpy.GetMessages()  
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Frauke
  • 59
  • 9