Select to view content in your preferred language

Generate Table from Select Layer by Location "Count" and "Layer Names" outputs

2112
4
Jump to solution
02-07-2019 11:37 AM
by Anonymous User
Not applicable

Hi,

In ArcGIS Pro 2.3.0 - Model Builder.

I am using the Select Layer By Location geoprocessing tool to make a selection through 9 different feature classes.

The main output is the selection itself, but the tool also generates a table with a list of the layer names for all input feature classes, and also a list for the count of select features within each one of these feature classes.

I would like to create a table output that would contain all the layer names listed in one field, and a second field for the count of selected features against each of these layer names.

Example of table to be generated:

Also, ideally this table could be saved in a gdb workspace.

Please let me know if someone has any suggestions!

1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

These appear to be new output parameters for the SelectByLocation tool. A little research identified them to be lists where the values are separated by semicolons.  You can use the Calculate Value tool to write these out to a CSV file as shown below; the output layer names is a precondition to the Calculate Value tool.

 

 

The tool is set up as:

 

 

The Code block would be:

 

 

'''
Description:
    This code will take the output lists created by the SelectByLocation tool in ArcGIS Pro 2.3 and write out the selection results to a CSV file
Usage:
     1] Assumes you have renamed the outputs of the SelectByLocation tool to Output Layer Names and Count
     2] Valid selections are created
     3] You need to change the variable csvPath to where ever you want the CSV file to be written	 
Output:
     1] A CSV file
     2] The output of this tool is a Boolean, if code executes without error this is set to True otherwise False
Author:
    Duncan Hornby (ddh@geodata.soton.ac.uk)
Created:
    14/2/19
'''
import csv,arcpy,sys
def WriteSelectionCounttoCSV(layers,counts):
    try:
        layerList = layers.split(";")
        countList = counts.split(";")
        res = zip(layerList,countList)
        csvPath = r"C:\Scratch\test.csv" # Change this file path to suit your needs
        with open(csvPath, mode='w', newline='') as csvfile:
            writer = csv.writer(csvfile,delimiter=',',quotechar='"',quoting=csv.QUOTE_MINIMAL, dialect='excel')
            writer.writerow(["rowid","count","layer"])
            for i,tup in enumerate(res):
                writer.writerow([i,tup[0],tup[1]])
        arcpy.AddMessage("Data written to CSV file!")
        return True
    except:
	    # An error occurred so return False for the tool so if it is being used as a preconition
		# it can help control the model
        e = sys.exc_info()[0]
        arcpy.AddError(str(e))
        print (str(e))
        return False‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

 

The CSV file will have the headers rowid, count and layer with the information you want written out to each row.

View solution in original post

4 Replies
DuncanHornby
MVP Notable Contributor

These appear to be new output parameters for the SelectByLocation tool. A little research identified them to be lists where the values are separated by semicolons.  You can use the Calculate Value tool to write these out to a CSV file as shown below; the output layer names is a precondition to the Calculate Value tool.

 

 

The tool is set up as:

 

 

The Code block would be:

 

 

'''
Description:
    This code will take the output lists created by the SelectByLocation tool in ArcGIS Pro 2.3 and write out the selection results to a CSV file
Usage:
     1] Assumes you have renamed the outputs of the SelectByLocation tool to Output Layer Names and Count
     2] Valid selections are created
     3] You need to change the variable csvPath to where ever you want the CSV file to be written	 
Output:
     1] A CSV file
     2] The output of this tool is a Boolean, if code executes without error this is set to True otherwise False
Author:
    Duncan Hornby (ddh@geodata.soton.ac.uk)
Created:
    14/2/19
'''
import csv,arcpy,sys
def WriteSelectionCounttoCSV(layers,counts):
    try:
        layerList = layers.split(";")
        countList = counts.split(";")
        res = zip(layerList,countList)
        csvPath = r"C:\Scratch\test.csv" # Change this file path to suit your needs
        with open(csvPath, mode='w', newline='') as csvfile:
            writer = csv.writer(csvfile,delimiter=',',quotechar='"',quoting=csv.QUOTE_MINIMAL, dialect='excel')
            writer.writerow(["rowid","count","layer"])
            for i,tup in enumerate(res):
                writer.writerow([i,tup[0],tup[1]])
        arcpy.AddMessage("Data written to CSV file!")
        return True
    except:
	    # An error occurred so return False for the tool so if it is being used as a preconition
		# it can help control the model
        e = sys.exc_info()[0]
        arcpy.AddError(str(e))
        print (str(e))
        return False‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

 

The CSV file will have the headers rowid, count and layer with the information you want written out to each row.

by Anonymous User
Not applicable

That's awesome thank you very much for taking the time to provide all this information!!

0 Kudos
FredMitchell
Regular Contributor

Hi @DuncanHornby,

I'm trying to incorporate your code into my model but when it runs it's just showing the code in the dialog box and not actually executing. I'm not sure what I'm doing wrong here. I've attached some screenshots. Any assistance you might have to offer would be greatly appreciated!

0 Kudos
DuncanHornby
MVP Notable Contributor

Have you created a folder called c:\scratch? Of course you could simply change that to a new location.

0 Kudos