ModelBuilder union process won't work in Python

467
4
Jump to solution
12-09-2019 07:08 AM
StephenRhone
New Contributor III

I have a series of ModelBuilder models which I am in the process of converting into Python scripts, and in one script I am running into trouble with the union of two large polygon feature classes.  The model version has always been able to successfully perform the union; however, when I attempt the same process in Python, I always run into topoengine errors.  

I even used a cursor to break down the union process and use smaller subsets of one of the large FCs, and this initially worked through a few iterations before it threw another topoengine error.  And even had it run successfully, it would have taken far longer than the MB process (several hours as opposed to less than one hour).

Is there a way for me to edit my script so that the union process will work (and work as efficiently) as it does in MB?  Thanks in advance for your help.

0 Kudos
1 Solution

Accepted Solutions
StephenRhone
New Contributor III

I was able to fix the issue by simply using the Dice tool on both of the feature classes before intersecting them.  Afterwards, I dissolved the result based on the ID fields of the original FCs and then used those to rejoin the attribute fields.

View solution in original post

0 Kudos
4 Replies
JoeBorgione
MVP Esteemed Contributor

With out seeing your script, it's a little tough to comment on where to make adjustments....

That should just about do it....
0 Kudos
StephenRhone
New Contributor III

Below is the relevant portion of my code - I'm using an intersect here rather than a union.  It takes the territory feature class and a zip code feature class, makes layers of each, then runs through a cursor of each territory, selects the zips that intersect it, runs an intersect on all selected features, then appends them into a combined feature class.  

So far this morning, I'm not getting any topoengine errors in Python, though the process is taking far longer than it would using ModelBuilder.  And as I mentioned, intersecting or unioning both of the feature classes in their entirety, all at once, does not throw any errors or otherwise run into memory issues when I use MB (and goes much faster).  

# Set up layer of territories and cursor of territory numbers
# Then iterate through cursor to intersect zips with individual territories
# This is necessary because intersecting them all at once results in errors
print "Intersecting zips with territory boundaries"
arcpy.MakeFeatureLayer_management(Territories, Territories_Lyr)
arcpy.MakeFeatureLayer_management(Zips, Zip_Lyr)
TerrCursor = arcpy.da.SearchCursor(Territories_Lyr, ['TERRITORY_NUM'], \
sql_clause = (None, 'ORDER BY TERRITORY_NUM'))
for Terr in TerrCursor:
    TerrNum = Terr[0]
    TerrStr = str(TerrNum)
    TerrQuery = "TERRITORY_NUM = " + TerrStr
    arcpy.SelectLayerByAttribute_management(Territories_Lyr, "NEW_SELECTION", TerrQuery)
    arcpy.SelectLayerByLocation_management(Zip_Lyr, "INTERSECT", Territories_Lyr)
    TempTerrZipName = "Terr_" + TerrStr
    TempTerrZip = OutputGDB + "\\" + TempTerrZipName
    arcpy.Intersect_analysis([Zip_Lyr, Territories_Lyr], TempTerrZip)
    if not arcpy.Exists(TerrZip):
        arcpy.CopyFeatures_management(TempTerrZip, TerrZip)
    else:
        arcpy.Append_management(TempTerrZip, TerrZip, "NO_TEST")
    arcpy.Delete_management(TempTerrZip)
    arcpy.SelectLayerByAttribute_management(Territories_Lyr, "CLEAR_SELECTION")
    arcpy.SelectLayerByAttribute_management(Zip_Lyr, "CLEAR_SELECTION")
    arcpy.Compact_management(OutputGDB)
del TerrCursor
arcpy.Delete_management(Territories_Lyr)
arcpy.Delete_management(Zip_Lyr)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
StephenRhone
New Contributor III

I was able to fix the issue by simply using the Dice tool on both of the feature classes before intersecting them.  Afterwards, I dissolved the result based on the ID fields of the original FCs and then used those to rejoin the attribute fields.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Dice Tool....  Never heard of it, but need to take a look at it!

That should just about do it....
0 Kudos