Select to view content in your preferred language

Problems in "summarize within" using Python notebook

833
5
Jump to solution
11-16-2022 11:52 PM
JinseoYoon
New Contributor III

HI!

I am trying to do "summarize within" with a multiple years of points data with a polygon. I need to summarize some columns in the table of the points within boundaries of the polygon. Since I have many point data files, I would like to use the "for" loop in Python notebook.

The problem is ... I can successfully do the process when I defined the "outFeatureClass" as one file. But when I try to make the "outFeatureClass" as the combination of path and the name (for multiple file), the results become quite strange.

The first code works, and the second code does not work.

 

1) the first code 

arcpy.env.workspace = 'D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD_2.gdb'
polys = 'D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD.gdb\TBGIS_TRDAR_RELM'
fcs = arcpy.ListFeatureClasses("seo_biz_mstr_open*", "Point")

for fc in fcs:
    outFeatureClass = 'D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD.gdb\seo_biz_mstr_open_201903_test_out_out'
    keepAll = 'KEEP_ALL'
    sumFields = [["STR_TOT","SUM"]]
    addShapeSum = 'ADD_SHAPE_SUM'
    addMinMaj = 'NO_MIN_MAJ'
    addPercents = 'NO_PERCENT'
    arcpy.analysis.SummarizeWithin(polys, fc, outFeatureClass, keepAll, 
                               sumFields, addShapeSum, '', '', 
                               addMinMaj, addPercents)

 

JinseoYoon_1-1668671302651.png

 

 

2) the second code 

arcpy.env.overwriteOutput=True

arcpy.env.workspace = 'D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD_2.gdb'
polys = 'D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD.gdb\TBGIS_TRDAR_RELM'
fcs = arcpy.ListFeatureClasses("seo_biz_mstr_open*", "Point")

for fc in fcs:
    outFeatureClass = os.path.join(r"D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD.gdb", fc)
    keepAll = 'KEEP_ALL'
    sumFields = [["STR_TOT","SUM"]]
    addShapeSum = 'ADD_SHAPE_SUM'
    addMinMaj = 'NO_MIN_MAJ'
    addPercents = 'NO_PERCENT'
    arcpy.analysis.SummarizeWithin(polys, fc, outFeatureClass, keepAll, 
                               sumFields, addShapeSum, '', '', 
                               addMinMaj, addPercents)

JinseoYoon_2-1668671342748.png

 

I cannot see any difference between the two code, rather than the way of defining the outfeatureclass, but the outcome seems quite different (the first one is correct).

Your advice would be very much appreciated. 

The data is quite large, so I cannot upload, but I can provide via email if requested. 

1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Do all your featureclasses have the same structure?

According to the error and the code examples, you require a STR_TOT field to exist in all the featureclasses in order to perform the sum operation them.

To assess which code is actually working, compare the results to doing it manually.  If the first code produces the 'correct' result for one featureclass test against more.

In fact, you can batch the tool to produce results for multiple featureclasses... 

summarizewithin.png


... sort of retired...

View solution in original post

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

In the working case, the output fc and its name doesn't change and the results are accumulated to it.  In the non-working version, its output and name change with every loop and I suspect only the last input to the loop is kept


... sort of retired...
JinseoYoon
New Contributor III

Thank you very much for your help.

Indeed, I have an error (copied below) when I increased the number of featureclasses in the input folder, and I do not know how to address the issue. 

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000728: Field STR_TOT does not exist within table
Failed to execute (SummarizeWithin).

But besides the problem, I posted the question because the outcomes of the two codes (1 and 2) differ even there was only one featureclass in the input folder.

I attach the files after substantially reduced the size by modifying features. 

It would be very much appreciated if you can help further.  - I just uploaded the files again due to some error... 

0 Kudos
JinseoYoon
New Contributor III

I just tried and the problem you raised was not indeed a problem. 

The problem is the differing results from the code 1 and 2. 

the first image is from the code 1 and the second one is one of the results with the code 2. 

JinseoYoon_0-1668689276968.png

JinseoYoon_1-1668689350969.png

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Do all your featureclasses have the same structure?

According to the error and the code examples, you require a STR_TOT field to exist in all the featureclasses in order to perform the sum operation them.

To assess which code is actually working, compare the results to doing it manually.  If the first code produces the 'correct' result for one featureclass test against more.

In fact, you can batch the tool to produce results for multiple featureclasses... 

summarizewithin.png


... sort of retired...
0 Kudos
JinseoYoon
New Contributor III

Yes I do have the STR_TOT field in every featureclass, and I confirmed that the first code works well.  The second code, for the batch of the input file, does not work and I am struggling to figure out why for a couple of days. 

The second code is different from the first code only in one line below, copied here, the subject line from first and second code respectively. 

outFeatureClass = 'D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD.gdb\seo_biz_mstr_open_201903_test_out_out'
outFeatureClass = os.path.join(r"D:\WORK\PROJECT\COVID_19_4\GIS\CVD_STORE_INDVD.gdb", fc)

 

BTW, your suggestion to go with the batch option in in the tool box panel works very well - thank you!! 

but I have too many files to select one by one. Indeed I can do the manual work since it would take less time than I spent for the code 🙂 BUT,,, I really want to make the code work. Your help would be very much appreciated!! 

0 Kudos