filipkral

Solution to: Dissolve tool does not fully dissolve medium-size dataset

Discussion created by filipkral on Jan 27, 2014
Latest reply on May 7, 2016 by curtvprice
Hi, this is a solution to a problem with the Dissolve (management) tool in Python.

Problem description:
I needed to dissolve singlepart polygon File Geodatabase feature class with a little over 200 000 features.

(Details:
feature areas: 0.00156 to 725.1 km sq
vertex counts: 6 to 2983 per feature, which I didn�??t consider to be Godzillas, expected output should not result in massive features either. Besides I didn�??t want to Dice the features because I didn�??t want any extra vertices in the output
Extent width and height: 521550.0 m and 967850.0 m.
Check Geometry and Repair Geometry didn�??t find or repair any errors.)

The operation I needed was:
arcpy.Dissolve_management(in_fc, out_fc, myField, "#", "MULTI_PART", "DISSOLVE_LINES")


This command was part of a larger script which used the dissolve tool several times.
Well, the result of this command was a feature class of polygons that were dissolved only within contiguous groups, as if the MULTI_PART parameter was set to SINGLE_PART!
The strange thing was that running the tool from ArcMap with exactly the same settings worked fine. When I copied the geoprocessing result as a snippet and ran it from script, it produced incorrect result as described above. No error or warning message was raised.

Several forum topics and blog posts describe various difficulties with the Dissolve tool, but nothing I found quite matched my situation. The closest was perhaps this blog post suggesting that it might be related to bug NIM079373: Running a large number of features through the Dissolve or Buffer with dissolve option, hangs during process.

What I tried and did not work

  • Playing with all parameters of the tool and with arcpy.env.XYTolerance and arcpy.env.XYResolution.

  • I built a model with the Dissolve tool, saved the model i a toolbox, added the toolbox to my script and ran the model as a geoprocessing tool from Python.

  • I wrote a function that looped through distinct combinations of values in dissolve fields and dissolved features in one category at a time (i.e. features with values in one combination at a time) and then merged the results into a final feature class. I had some success with this approach but it did not work when used as part of my larger script.

  • I wrote a function that splitted the input into chunks using another polygon feature class, dissolved the chunks and merged them into a final feature class.


What seems to work, so far:
I wrote a script with the Dissolve tool and �??exposed�?� its parameters using sys.argv. Then I called this script from my larger script using subprocess.popen.
My lines in the larger script look like this:

def dissolveInSubprocess(in_fc, out_fc, dissolve_fields, stat_fields = '#', multi_part = 'MULTI_PART', unsplit_lines = 'DISSOLVE_LINES', xytol = '#'):
    """Execute Dissolve tool in a subprocess"""
    opts = (in_fc, out_fc, dissolve_fields, stat_fields, multi_part, unsplit_lines, xytol)
    cmd = r"C:\Python27\ArcGIS10.1\python.exe C:\Scripts\dissolver.py " + " ".join(map(str, opts))
    fn.msg("Executing subprocess with " + str(cmd))
    chld = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    r = chld.communicate()
    print r
    if chld.returncode != 0:
        raise Exception("Error while dissolving in subprocess:" + str(chld.returncode))
    else:
        fn.msg("Finished subprocess with " + str(cmd))
    return chld.returncode

# �?� then I call the function

dissolveInSubprocess(in_fc, out_fc, ";".join(list_of_cols), stat_fields ='#', multi_part = 'MULTI_PART', unsplit_lines = "DISSOLVE_LINES", xytol = '#')


And the script that is called (dissolver.py) looks like this:
"""Script to execute dissolve in a separate subprocess."""
def main(in_fc, out_fc, dissolve_fields, stat_fields, multi_part, unsplit_lines, xytol):
    import arcpy
    if xytol[0].isdigit():
        arcpy.env.XYTolerance = xytol
    try:
        estuarycatchmentsRaw = arcpy.Dissolve_management(in_fc, out_fc, dissolve_fields, stat_fields , multi_part, unsplit_lines).getOutput(0)
    except Exception as ex:
        print ex.message
    return 0

import sys
args = sys.argv
print args
script, in_fc, out_fc, dissolve_fields, stat_fields, multi_part, unsplit_lines, xytol = args

main(in_fc, out_fc, dissolve_fields, stat_fields, multi_part, unsplit_lines, xytol)


I cannot fully explain why this works and all of the previous approaches didn�??t, but it seems that arcpy.Dissolve_management is more reliable when it runs in a separate process. Perhaps running tools in a separate process might help also when the Analysis Toolbox tools (Intersect etc.) exhibit inappropriate behaviour.

My system is ArcGIS 10.1 SP1 build 3143 on Windows 7 SP1 64 bit, Dell Precision T3600, 3.6 GHz 8 core Intel Xeon CPU and 16 GB RAM. After some experiments with ArcGIS 10.1 SP 1 for Desktop Background Geoprocessing (64-bit) I stick to the 32 bit Python that came with ArcGIS originally.

Unfortunately I cannot provide the dataset due to licensing restrictions.

I would appreciate your experiences and comments.
Filip.

Outcomes