1

I'm having two specific problems with this python script I'm developing.

Background: I am trying to generate near_tables from fc's stored in fgdb, join the near table back to target feats, and save the output to a new fc.

Setup: I'm working with data stored in two separate directories (transects and datums). Each directory contains one fgdb containing the same # of feature classes as the other fgdb. The fgdb in one directory is (point) transect data. The other fgdb is (point) datum near features.

Workflow: I read each fgdb into a list. I then sort both lists by filename (important, else results are completely wrong). I want to simultaneously iterate through feature classes in both lists and feed them into the near tool (ie. two loops.

Software: ArcGIS 10.2 (Advanced/ArcInfo), Python 2.7.3. I also have ArcGIS 10.1 (Adv/ArcInfo) in a VM I can run this on.

Directory Structure: enter image description here

My Problem: 1) Only the first fc from each list is processed in the "for x in xrange loop". 2) I cannot for the life of me successfully create a feature layer and save the layer using arcpy.MakeFeatureLayer and arcpy.SaveToLayerFile_management.

What I've tried 1) I tried using the zip function to combine both lists into an iterator, but started spinning wheels about how to separate the list objects as different inputs into the Near and Join tools which take two inputs. Also, tried a nested for loop (which i don't think does what I need it to do). And finally using this for loop with xrange (len(list)), however, i don't know how to increment it to process the next fc in both lists.

2) ArcGIS arcpy tool file input parameters always gives me a major headache. I tried both: feeding the tool the path+filename of the fc and the filename of the fc only. The script runs and ends as it should, but without any error messages and my print message after the MakeFeatureLayer tool executes does not print any feedback to let me know it ran successfully. For MakeFeatureLayer, ArcGIS Resource Forums and blogs code the output layer as a "string_lyr" so I tried type casting the transect list variable as string using the str() function.

Note: I have a ton of debug code for tracking the input and output filenames and variable contents before and after almost every step in this script that I have omitted.

My goals 1) To successfully simultaneously iterate two feature layers (one from each fgdb). 2) Get the MakeFeatureLayer tool to give me some kind of feedback of whether it worked or not, and if not, why.

import arcpy, os
from arcpy import env

# enable overwrite outputs
env.overwriteOutput = True

# Set workspace directory
dir = r"W:\\Working\\abcdefg_working\\Test\\sourcedata\\point_distance_operation\\method1"
arcpy.env.workspace = dir

# input feature directory: transects
dir_transects = r"W:\\Working\\abcdefg_working\\Test\\sourcedata\\point_distance_operation\\method1\\transects\\transects.gdb"

# near feature directory: SLE datum points
dir_datums = r"W:\\Working\\abcdefg_working\\Test\\sourcedata\\point_distance_operation\\method1\\datums\\datums.gdb"

# output directory
outWorkspace = r"W:\\Working\\abcdefg_working\\Test\\sourcedata\\point_distance_operation\\method1\\output"

arcpy.env.workspace = dir_transects
# Create list of Transect features
fcTransects_list = arcpy.ListFeatureClasses()

# sort transects list
fcTransects_list.sort()

arcpy.env.workspace = dir_datums
# Create list of Near Features
fcDatums_list = arcpy.ListFeatureClasses()

# sort datums list
fcDatums_list.sort()

try:
    for x in xrange (len(fcTransects_list)):
        transect = fcTransects_list[x]
        datum = fcDatums_list[x]

        # filename of output table
        out_NearTable = os.path.join(outWorkspace, transect) 

        # create temporary feature layers
        transect_lyr = transect
        datum_lyr = datum

        arcpy.MakeFeatureLayer(transect, transect_lyr)

        print "transect feature layer created!"

        out_transect_lyr = transect_lyr

        print out_transect_lyr

        # Execute SaveToLayerFile
        arcpy.SaveToLayerFile_management(transect_lyr, out_transect_lyr, "RELATIVE")        

        print "transect layer saved!"

        arcpy.MakeFeatureLayer(datum, datum_lyr)

        print "datum feature layer created!"

        out_datum_lyr = datum_lyr

        # Execute SaveToLayerFile
        arcpy.SaveToLayerFile_management(transect_lyr, out_datum_lyr, "RELATIVE")

except:
    print arcpy.GetMessages()

UPDATE 8/15/2013 @ 1:30pm: Paul's re-coding the file input construct to use the 'zip' function worked and got the iterative flow going which solves #1. And because order has been restored to the land, #2 is receiving the correct file inputs. CASE CLOSED.

2

It seems like you are defining a lot of redundant variables, so I cleaned it up some and changed the workflow slightly. This is completely untested, but it might point you in the right direction:

import arcpy
from os.path import join

# enable overwrite outputs
arcpy.env.overwriteOutput = True

# input feature directory: transects
dir_transects = r"W:\Working\abcdefg_working\Test\sourcedata\point_distance_operation\method1\transects\transects.gdb"

# near feature directory: SLE datum points
dir_datums = r"W:\Working\abcdefg_working\Test\sourcedata\point_distance_operation\method1\datums\datums.gdb"

# output directory
outWorkspace = r"W:\Working\abcdefg_working\Test\sourcedata\point_distance_operation\method1\output"

# Create list of Transect features
arcpy.env.workspace = dir_transects
fcTransects_list = sorted(arcpy.ListFeatureClasses())

# Create list of Near Features
arcpy.env.workspace = dir_datums
fcDatums_list = sorted(arcpy.ListFeatureClasses())


def clearmem(layers):
    [arcpy.Delete_management(layer) for layer in layers if arcpy.Exists(layer)]

for transect, datum in zip(fcTransects_list, fcDatums_list):
    try:
        #Cast to string because ListFC() returns unicode.
        transect = str(transect)
        datum = str(datum)

        transect_lyr = arcpy.MakeFeatureLayer_management(join(dir_transects, transect), transect)
        datum_lyr = arcpy.MakeFeatureLayer_management(join(dir_datums, datum), datum)  

        arcpy.SaveToLayerFile_management(transect_lyr, join(outworkspace, transect_lyr), "RELATIVE")        
        arcpy.SaveToLayerFile_management(datum_lyr, join(outworkspace, datum_lyr), "RELATIVE")        

        #Delete layers.
        clearmem([transect_lyr, datum_lyr])

    except:
        print arcpy.GetMessages()
        clearmem([transect_lyr, datum_lyr]) #Always clear memory.
  • I really appreciate the help. You've solved #1 and set me up to solve #2 which im working now. And I now know how to use the zip command. Thank you! – Brach Aug 15 '13 at 18:53
  • @Brachabre regarding 2), you could use Exists() to check if it was created. – Paul Aug 15 '13 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.