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

557
2
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 Frequent 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

2 Replies
DuncanHornby
MVP Frequent 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

by Anonymous User
Not applicable

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

0 Kudos