Exemplo n.º 1
0
def check_topology(input_file, workspace):
    """Create Database and check for overlapping features. This function
    is based on one previously created by Christian Kienholz, University
    of Alaska, Fairbanks, 03/2012"""
    # Create Database, add a data set and upload the features
    database = arcpy.CreateFileGDB_management(workspace, 'database.gdb')
    dataset = arcpy.CreateFeatureDataset_management(database, 'validation',
                                                    input_file)
    feature = str(dataset) + '\\feature'
    arcpy.CopyFeatures_management(input_file, feature)

    #Create topology and rules. Add feature to it
    topology = arcpy.CreateTopology_management(dataset, 'topology_rules')
    arcpy.AddFeatureClassToTopology_management(topology, feature, 1, 1)
    arcpy.AddRuleToTopology_management(topology, 'Must Not Overlap (Area)',
                                       feature)
    arcpy.ValidateTopology_management(topology)

    # Export Errors
    arcpy.ExportTopologyErrors_management(topology, database, 'Errors')
    error_count = arcpy.GetCount_management(str(database) + '\\Errors_poly')
    original_count = arcpy.GetCount_management(input_file)

    arcpy.Delete_management(database)  # Delete database

    return [str(error_count), str(original_count)]
Exemplo n.º 2
0
def esriTopology(outFds, caf, mup):
    addMsgAndPrint('Checking topology of ' + os.path.basename(outFds))
    # First delete any existing topology
    ourTop = os.path.basename(outFds) + '_topology'
    testAndDelete(outFds + '/' + ourTop)
    # create topology
    addMsgAndPrint('  creating topology ' + ourTop)
    arcpy.CreateTopology_management(outFds, ourTop)
    ourTop = outFds + '/' + ourTop
    # add feature classes to topology
    arcpy.AddFeatureClassToTopology_management(ourTop, caf, 1, 1)
    if arcpy.Exists(mup):
        arcpy.AddFeatureClassToTopology_management(ourTop, mup, 2, 2)
    # add rules to topology
    addMsgAndPrint('  adding rules to topology:')
    for aRule in ('Must Not Overlap (Line)', 'Must Not Self-Overlap (Line)',
                  'Must Not Self-Intersect (Line)',
                  'Must Be Single Part (Line)'):
        addMsgAndPrint('    ' + aRule)
        arcpy.AddRuleToTopology_management(ourTop, aRule, caf)
    for aRule in ('Must Not Overlap (Area)', 'Must Not Have Gaps (Area)'):
        addMsgAndPrint('    ' + aRule)
        arcpy.AddRuleToTopology_management(ourTop, aRule, mup)
    addMsgAndPrint('    ' + 'Boundary Must Be Covered By (Area-Line)')
    arcpy.AddRuleToTopology_management(
        ourTop, 'Boundary Must Be Covered By (Area-Line)', mup, '', caf)
    # validate topology
    addMsgAndPrint('  validating topology')
    arcpy.ValidateTopology_management(ourTop)
    nameToken = os.path.basename(caf).replace('ContactsAndFaults', '')
    if nameToken == '':
        nameToken = 'GeologicMap'
    nameToken = 'errors_' + nameToken + 'Topology'
    for sfx in ('_point', '_line', '_poly'):
        testAndDelete(outFds + '/' + nameToken + sfx)
    # export topology errors
    addMsgAndPrint('  exporting topology errors')
    arcpy.ExportTopologyErrors_management(ourTop, outFds, nameToken)
    topoStuff = []
    topoStuff.append(
        '<i>Note that the map boundary commonly results in one "Must Not Have Gaps" line error</i>'
    )
    for sfx in ('_point', '_line', '_poly'):
        fc = outFds + '/' + nameToken + sfx
        topoStuff.append(space4 + str(numberOfRows(fc)) + ' rows in <b>' +
                         os.path.basename(fc) + '</b>')
        addMsgAndPrint('    ' + str(numberOfRows(fc)) + ' rows in ' +
                       os.path.basename(fc))
        if numberOfRows(fc) == 0: testAndDelete(fc)
    return topoStuff
Exemplo n.º 3
0
def run_topology(input_gdb):
    """Runs topology on the TFL lines and prints any errors as arcpy messages
    returns true if all tests pass and false otherwise. If errors are found then
    the topology_Error feature classes will be saved in the edit database, otherwise
    they will be deleted"""
    #Check for pre-existing topology featureclasses
    if arcpy.Exists(input_gdb + os.sep + 'Topology_Error_line'):
        arcpy.Delete_management(input_gdb + os.sep + 'Topology_Error_line')
    if arcpy.Exists(input_gdb + os.sep + 'Topology_Error_point'):
        arcpy.Delete_management(input_gdb + os.sep + 'Topology_Error_point')

    try:
        #Run topology that was created in TFL_Prepare_Edits
        arcpy.ValidateTopology_management(input_gdb + os.sep +
                                          'TFL_Line_Topology')
        arcpy.ExportTopologyErrors_management(
            input_gdb + os.sep + 'TFL_Line_Topology', input_gdb,
            'Topology_Error')

        #Check output topology feature classes - if they are empty - delete them, otherwise add the errors to a list
        errors = []
        arcpy.env.workspace = input_gdb
        arcpy.env.XYTolerance = "0.0001 Meters"
        feature_classes = arcpy.ListFeatureClasses()
        for feature_class in feature_classes:
            if 'Topology_Error' in feature_class:
                if int(arcpy.GetCount_management(feature_class).getOutput(
                        0)) > 0:
                    with arcpy.da.SearchCursor(feature_class,
                                               'RuleDescription') as cursor:
                        for row in cursor:
                            errors.append(row[0])
                else:
                    arcpy.Delete_management(feature_class)

        if not errors:
            arcpy.AddMessage('No topology errors found')
            return (True)
        else:
            arcpy.AddWarning(
                'Topology errors found - please review the topology results and correct during editing'
            )
            for error in errors:
                arcpy.AddWarning(error)
            return (False)
    except:
        arcpy.AddWarning(
            'Error validating topology - manual repair of topology errors using map extent may be required'
        )
        return (False)
Exemplo n.º 4
0
def create_topology(fc, topo_output, rule, error_output):
    '''(feature class,topology output location,topology rule,topology error output location)-> error output location
    Creates a topology in topo_output. Adds fc to newly created topology.
    Adds rule to topology. Validates topology. Exports topology errors to error_output.
    Returns error_output string.
    '''
    toponame = '{0}_topo'.format(os.path.basename(fc))
    topo_path = os.path.join(topo_output, toponame)
    arcpy.CreateTopology_management(topo_output, toponame)
    arcpy.AddFeatureClassToTopology_management(topo_path, fc, 1)
    arcpy.AddRuleToTopology_management(topo_path, rule, fc)
    arcpy.ValidateTopology_management(topo_path)
    arcpy.ExportTopologyErrors_management(
        topo_path, error_output, '{0}_errors'.format(os.path.basename(fc)))
    return error_output
Exemplo n.º 5
0
 def onClick(self):
     box = pythonaddins.MessageBox("You are about to look for gaps. This process could take a long time depending on the size of the fabric. Please be patient with the interface until it shows a finished message.", \
                                   "Select Parcel Fabric", 1)
     if box == "OK":
         workspace, PFname = os.path.split(parcelFabric)
         topology = os.path.join(workspace, PFname + "_topology")
         if arcpy.Exists(topology):
             arcpy.Delete_management(topology)
         arcpy.CreateTopology_management(workspace, PFname + "_topology")
         pfl = arcpy.mapping.ListLayers(mxd, "*Parcels")
         for layer in pfl:
             if layer.name == "Tax Parcels":
                 polygons = "FabricInvestigation\\Tax Parcels"
             elif layer.name == "Parcels":
                 polygons = "FabricInvestigation\\Parcels"
             else:
                 # Adding a message box here will cause tool to fail due to
                 # the looping of the layers when it finds layers not
                 # containing the Parcels or Tax Parcels.
                 pass
         arcpy.AddFeatureClassToTopology_management(topology, polygons)
         arcpy.AddFeatureClassToTopology_management(
             topology, "FabricInvestigation\\Lines")
         polygon_fc = os.path.join(workspace, PFname + "_Parcels")
         line_fc = os.path.join(workspace, PFname + "_Lines")
         arcpy.AddRuleToTopology_management(
             topology, "Boundary Must Be Covered By (Area-Line)",
             polygon_fc, "", line_fc)
         arcpy.ValidateTopology_management(topology)
         gdb, fds_name = os.path.split(workspace)
         arcpy.ExportTopologyErrors_management(topology, gdb, "Gaps")
         arcpy.mapping.MoveLayer(
             df,
             arcpy.mapping.ListLayers(mxd, "FabricInvestigation")[0],
             arcpy.mapping.ListLayers(mxd, "Gaps_line")[0], "BEFORE")
         arcpy.mapping.RemoveLayer(
             df,
             arcpy.mapping.ListLayers(mxd, "Gaps_point")[0])
         arcpy.mapping.RemoveLayer(
             df,
             arcpy.mapping.ListLayers(mxd, "Gaps_poly")[0])
         arcpy.mapping.RemoveLayer(
             df,
             arcpy.mapping.ListLayers(mxd, PFname + "_topology")[0])
         box2 = pythonaddins.MessageBox(
             "Finished Processing Gaps. Please proceed.",
             "Finsihed Processing Gaps", 0)
         ParcelLineGaps.checked = False
Exemplo n.º 6
0
# Created on: 2019-03-05 
# Updated on 2019-03-05
#
# Author: Phil Baranyai/GIS Manager
#
# Description: 
#  Export topology errors out to feature classes
# ---------------------------------------------------------------------------


# Set the necessary product code (sets neccesary ArcGIS product license needed for tools running)
import arceditor

# Import arcpy module
import arcpy

# Stop geoprocessing log history in metadata (stops program from filling up geoprocessing history in metadata with every run)
arcpy.SetLogHistory(False)

# Set topology path
SectionTopology = r"R:\\GIS\\Planning\\Zoning_Maps_Topology20190715.gdb\\Zoning\\Zoning_Topology"

print ("Begining Export")

# Export Topology Errors (set path-------------------->                                                             -----> and errors path)
arcpy.ExportTopologyErrors_management(SectionTopology, r"R:\\GIS\\Planning\\Zoning_Maps_Topology20190715.gdb\\Zoning", "Zoning_Topology_errors")

print ("Export completed")

del arcpy
Exemplo n.º 7
0
            arcpy.AddMessage("Proceso en ejecución")

            if arcpy.Exists(valor):
                arcpy.env.overwriteOutput = True
                arcpy.CopyFeatures_management(ingresados, path_mapa)
                arcpy.CreateTopology_management(path_dataset,
                                                "topology_" + str(date))
                arcpy.AddFeatureClassToTopology_management(
                    path_topo, path_mapa)
                arcpy.AddRuleToTopology_management(
                    path_topo, "Must Not Have Gaps (Area)", path_mapa)
                arcpy.AddRuleToTopology_management(path_topo,
                                                   "Must Not Overlap (Area)",
                                                   path_mapa)
                arcpy.ValidateTopology_management(path_topo, "Full_Extent")
                arcpy.ExportTopologyErrors_management(path_topo, path_dataset,
                                                      "topo" + str(date))

            else:
                arcpy.CreateFeatureDataset_management(resultados, valor, prj)
                arcpy.CopyFeatures_management(ingresados, path_mapa)
                arcpy.CreateTopology_management(path_dataset,
                                                "topology_" + str(date))
                arcpy.AddFeatureClassToTopology_management(
                    path_topo, path_mapa)
                arcpy.AddRuleToTopology_management(
                    path_topo, "Must Not Have Gaps (Area)", path_mapa)
                arcpy.AddRuleToTopology_management(path_topo,
                                                   "Must Not Overlap (Area)",
                                                   path_mapa)
                arcpy.ValidateTopology_management(path_topo, "Full_Extent")
                arcpy.ExportTopologyErrors_management(path_topo, path_dataset,
Exemplo n.º 8
0
import arcpy

fc = arcpy.GetParameterAsText(0)
topo = arcpy.GetParameterAsText(1)
search_radius = arcpy.GetParameterAsText(2)

arcpy.env.workspace = "IN_MEMORY"

arcpy.env.overwriteOutput = True

points = arcpy.ExportTopologyErrors_management(topo, "", "topo")[0]

lyr = arcpy.MakeFeatureLayer_management(points, "lyr")

expression = "{0} = 'Must Not Have Dangles'".format(
    arcpy.AddFieldDelimiters(lyr, "RuleDescription"))

arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", expression)

arcpy.Near_analysis(lyr, lyr, search_radius)

expression2 = "{0} > 0".format(arcpy.AddFieldDelimiters(lyr, "NEAR_DIST"))

arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", expression2)

arcpy.CopyFeatures_management(lyr, fc)
Exemplo n.º 9
0
def topology_repair(
    inFile=path_to_shapefile, 
    dissolve_field="", 
    gap_threshold=10000):    # threshold is max area of gaps that are considered to be errors

    # create variables for necessary paths, create gdb, import inFile into feature dataset
    gdb = os.path.basename(inFile[:-3] + 'gdb')
    gdbDir= os.path.dirname(inFile)
    arcpy.CreateFileGDB_management(gdbDir, gdb)
    arcpy.env.workspace = gdbDir + '/' + gdb
    feature_ds = arcpy.env.workspace + '/topology_ds'
    data = arcpy.env.workspace + '/topology_ds/' + os.path.basename(inFile[:-4])
    topology = feature_ds + '/Topology'
    arcpy.CreateFeatureDataset_management(arcpy.env.workspace, "topology_ds", inFile[:-3] + 'prj')
    arcpy.FeatureClassToGeodatabase_conversion([inFile], "topology_ds")

    # Create topology, add feature class, define rules
    arcpy.CreateTopology_management(feature_ds, "Topology")
    arcpy.AddFeatureClassToTopology_management(topology, data)
    arcpy.AddRuleToTopology_management(topology, "Must Not Overlap (Area)",data,"","","")
    arcpy.ValidateTopology_management(topology)

    # create polygon inFile from errors and delete
    arcpy.ExportTopologyErrors_management(topology, "", "overlapErrors")
    arcpy.AddField_management("overlapErrors_poly", dissolve_field, "STRING")
    o = "o"
    arcpy.CalculateField_management('overlapErrors_poly', dissolve_field,o)

    # Create topology, add feature class, define rules
    arcpy.CreateTopology_management(feature_ds, "Topology")
    arcpy.AddFeatureClassToTopology_management(topology, data)
    arcpy.AddRuleToTopology_management(topology, "Must Not Have Gaps (Area)",data,"","","")
    arcpy.ValidateTopology_management(topology)

    # create polygon inFile from errors and merge with original data
    arcpy.ExportTopologyErrors_management(topology, "", "gapErrors")
    arcpy.FeatureToPolygon_management("gapErrors_line","topo_errors_gaps")
    arcpy.SelectLayerByAttribute_management ("topo_errors_gaps", "NEW_SELECTION", '"Shape_Area" < ' + str(gap_threshold))
    arcpy.AddField_management("topo_errors_gaps", dissolve_field, "STRING")
    g = "g"
    arcpy.CalculateField_management('topo_errors_gaps', dissolve_field,g )
    arcpy.SelectLayerByAttribute_management ("topo_errors_gaps", "SWITCH_SELECTION")
    arcpy.DeleteRows_management("topo_errors_gaps")
    arcpy.Merge_management(["overlapErrors_poly", "topo_errors_gaps" ,inFile],"topomerged")

    # Get neighbor table and export to gdb
    arcpy.PolygonNeighbors_analysis('topomerged', 'topo_errors',['OBJECTID', dissolve_field])  # doesn't always find neighbors on all sides of polygon
    arcpy.TableToGeodatabase_conversion('topo_errors',arcpy.env.workspace)

    #table to array and array to dataframe
    nbr_field = 'nbr_' + dissolve_field
    arr = arcpy.da.FeatureClassToNumPyArray(("topo_errors"), ("src_OBJECTID", nbr_field, "LENGTH"))
    index = [str(i) for i in range(1, len(arr)+1)]
    df = pd.DataFrame(arr, index=index)
    df = df.groupby(['src_OBJECTID','nbr_TYPE'],as_index = False)['LENGTH'].sum()   #sum in case several sides of polygon have same neighbor

    #select rows from df and export to csv and to gdb
    idx = df.groupby(['src_OBJECTID'])['LENGTH'].transform(max) == df['LENGTH']
    df_select = df [idx]
    df_select.to_csv(gdbDir+'/joinme.csv', index=False)
    arcpy.TableToTable_conversion(gdbDir+'/joinme.csv', arcpy.env.workspace, "joinme")

    # Merge error polygons, join field, delete overlaps from infile, assign type to error polygons, merge all and dissolve
    arcpy.JoinField_management('topomerged', 'OBJECTID', 'joinme', 'src_OBJECTID', 'nbr_TYPE')
    arcpy.FeatureClassToFeatureClass_conversion('topomerged', "", 'topo_errors_join')
    arcpy.SelectLayerByAttribute_management("topo_errors_join", "NEW_SELECTION", "TYPE = 'o'")
    arcpy.SelectLayerByAttribute_management("topo_errors_join", "ADD_TO_SELECTION", "TYPE = 'g'")
    arcpy.SelectLayerByAttribute_management ("topo_errors_join", "SWITCH_SELECTION")
    arcpy.DeleteRows_management("topo_errors_join")   #leave only error polygons
    arcpy.AlterField_management('topo_errors_join', 'TYPE', 'orig_TYPE','orig_TYPE')
    arcpy.AlterField_management('topo_errors_join', 'nbr_TYPE', 'TYPE','TYPE')
    arcpy.Erase_analysis(inFile,'overlapErrors_poly','infile_overlaps_erased')
    arcpy.Merge_management(["topo_errors_join","infile_overlaps_erased"],"merged_neighbors")
    arcpy.Dissolve_management('merged_neighbors', 'dissolved_neighbors', 'TYPE')
def make_workspace_copy(inputfeatures, theworkspace, dotopologycheck,
                        dosimplify, dosimplify_method, dosimplify_tolerance,
                        thefield):
    """This function tests the input features for the topology error 'Must Be Single Part',
    and returns the Origin Feature's Object ID of the errant features to the calling module. Beware:
    the origing feature's object ID is that of the COPY of the input features. The object ID's of the copy
    may be different from the source "inputfeautures"!!!!. This is why the function passes back the name of the COPY so that the processing can
    continue on that feature class where the topologically errant features will be correctly identified
    by the values in the export topology errors geoprocessing tool."""

    ##    arcpy.AddMessage("funcs.make_workspace_copy")

    #Process the
    #roads with the simplify_line tool with the point_remove option at a tolerance of 0.001 meters so that redundant vertices on staight lines are removed.
    #If the user specifies their own parameters for simplify_line, THAT ARE NOT POINT_REMOVE AND THE TOLERANCE IS > 0.001 METERS, that is done additionally,
    #afterwards:

    #this section makes the feature class datasets, feature class names, and topology name:
    badfids = set()
    fdname = "KDOT_Topology_Check"  #the feature dataset name for the topology check
    fdnamepath = theworkspace + "\\" + fdname  #the complete pathname of the feature dataset
    tpname = "CheckTopology"  #the name of the topology
    topology_name = fdnamepath + "\\" + tpname  #the complete pathname of the topology
    ##    arcpy.AddMessage("make_workspace_copy, fdnamepath: "+fdnamepath)
    ##    arcpy.AddMessage("make_workspace_copy, topology_name: "+topology_name)
    fcname = arcpy.ParseTableName(
        inputfeatures,
        theworkspace)  #Split the inputfeatures to find the name from the path.
    namelist = fcname.split(
        ", "
    )  #the feature class name without the path. Used in creating a copy in the feature dataset.
    ##    arcpy.AddMessage('fcname = '+ namelist[2])
    topology_featureclass = fdnamepath + '\\' + namelist[
        2] + '_check'  #the copy of inputfeatures used for the topology check
    topology_featureclass_errors = namelist[
        2] + '_errors'  # the basename used for the export topology errors tool
    ##    arcpy.AddMessage(topology_featureclass)
    topology_featureclass_errors_line = fdnamepath + '\\' + namelist[
        2] + '_errors_line'  #the output name of LINE errors from the export topology errors tool

    #Delete if the feature dataset currently exists:
    doesexistfd = arcpy.Exists(fdnamepath)
    try:
        if doesexistfd:
            arcpy.AddMessage(
                'Previous topology check feature dataset exists. Now deleteing '
            )
            arcpy.Delete_management(fdnamepath)
    except arcpy.ExecuteError:
        print arcpy.GetMessages(2)
    except Exception as e:
        print e.args[0]

    #Re-create the topology feature dataset:
    arcpy.AddMessage('Generating the topology check scratch feature dataset')
    arcpy.CreateFeatureDataset_management(theworkspace, fdname, inputfeatures)

    #Make a copy of the input roads in the feature dataset that contains the topology:
    try:
        arcpy.AddMessage(
            'Generating a copy of the input feature class in the scratch feature dataset'
        )
        #This replaces the function "arcpy.CopyFeatures_management" so that we can retain the original FID:
        ##        make_copies_of_features(inputfeatures,  topology_featureclass, "Original_OID")
        make_copies_of_features(inputfeatures, topology_featureclass, thefield)
##        arcpy.CopyFeatures_management(inputfeatures, topology_featureclass)
    except arcpy.ExecuteError:
        print arcpy.GetMessages(2)
    except Exception as e:
        print e.args[0]

    #Perform the topology check, if checked ON in input parameters:
##    arcpy.AddMessage('make_workspace_copy, dotopology = ' + str(dotopologycheck))
##    if(dotopologycheck == True):
    if (str(dotopologycheck) == 'true'):
        arcpy.AddMessage('Creating the topology')
        arcpy.CreateTopology_management(fdnamepath, tpname)

        #Add the input roads to the topology
        arcpy.AddMessage(
            'Adding the copy of the input features to the topology')
        arcpy.AddFeatureClassToTopology_management(topology_name,
                                                   topology_featureclass, 1, 1)
        #Add a rule:
        arcpy.AddMessage('Adding rule "Must Be Single Part" to the topology')
        arcpy.AddRuleToTopology_management(topology_name,
                                           "Must Be Single Part (Line)",
                                           topology_featureclass)
        #Validate the topology:
        arcpy.AddMessage('Validating the topology')
        arcpy.ValidateTopology_management(topology_name)
        #Export the errant features to a feature class
        arcpy.AddMessage(
            'Exporting the topologically-errant feature to feature class ' +
            topology_featureclass_errors)
        arcpy.ExportTopologyErrors_management(topology_name, fdnamepath,
                                              topology_featureclass_errors)
        arcpy.AddMessage("Completed exporting topology errors")

        #Extract the values from field "OriginObjectID". This is a field generated to identify the OID's of errant features:
        ##        arcpy.AddMessage('Retrieving the object ID''s of the errant features')
        with arcpy.da.SearchCursor(topology_featureclass_errors_line,
                                   ["OriginObjectID"]) as cursor:
            for row in cursor:
                ##                arcpy.AddMessage(str(row[0]))
                badfids.add(row[0])

    #Perform at the least, the default line simplification of 0.001 meters or 0.00328084 feet
    #SimplifyLine(mergedFeatures, simplifiedFeatures, dosimplify_method, dosimplify_tolerance, "RESOLVE_ERRORS", "KEEP_COLLAPSED_POINTS", "CHECK")
    simplified_featureclass = fdnamepath + '\\_simplified_roads'
    arcpy.SimplifyLine_cartography(topology_featureclass,
                                   simplified_featureclass, dosimplify_method,
                                   dosimplify_tolerance, False, False, False)

    arcpy.AddMessage('completed creating a workspace copy....')
    ##    arcpy.AddMessage('completed funcs.make_workspace_copy')
    return badfids, simplified_featureclass
def validateCafTopology(inFds, outFds, outHtml):
    inCaf = getCaf(inFds)
    caf = os.path.basename(inCaf)
    nameToken = caf.replace('ContactsAndFaults', '')
    outCaf = outFds + '/' + caf
    inMup = inFds + '/' + nameToken + 'MapUnitPolys'
    outMup = outFds + '/' + nameToken + 'MapUnitPolys'
    inGel = inFds + '/' + nameToken + 'GeologicLines'
    outGel = outFds + '/' + nameToken + 'GeologicLines'

    # First delete any existing topology
    ourTop = os.path.basename(outFds) + '_topology'
    testAndDelete(outFds + '/' + ourTop)
    # copy CAF to _errors.gdb
    testAndDelete(outCaf)
    arcpy.Copy_management(inCaf, outCaf)
    # copy MUP to _errors.gdb
    testAndDelete(outMup)
    if arcpy.Exists(inMup):
        arcpy.Copy_management(inMup, outMup)
    # copy GeL to _errors.gdb
    testAndDelete(outGel)
    if arcpy.Exists(inGel):
        arcpy.Copy_management(inGel, outGel)

    # create topology
    addMsgAndPrint('  creating topology ' + ourTop)
    arcpy.CreateTopology_management(outFds, ourTop)
    ourTop = outFds + '/' + ourTop
    # add feature classes to topology
    arcpy.AddFeatureClassToTopology_management(ourTop, outCaf, 1, 1)
    if arcpy.Exists(outMup):
        arcpy.AddFeatureClassToTopology_management(ourTop, outMup, 2, 2)
    if arcpy.Exists(outGel):
        arcpy.AddFeatureClassToTopology_management(ourTop, outGel, 3, 3)
    # add rules to topology
    addMsgAndPrint('  adding rules to topology:')
    for aRule in ('Must Not Overlap (Line)', 'Must Not Self-Overlap (Line)',
                  'Must Not Self-Intersect (Line)',
                  'Must Be Single Part (Line)'):
        addMsgAndPrint('    ' + aRule)
        arcpy.AddRuleToTopology_management(ourTop, aRule, outCaf)
    if arcpy.Exists(outMup):
        for aRule in ('Must Not Overlap (Area)', 'Must Not Have Gaps (Area)'):
            addMsgAndPrint('    ' + aRule)
            arcpy.AddRuleToTopology_management(ourTop, aRule, outMup)
        addMsgAndPrint('    ' + 'Boundary Must Be Covered By (Area-Line)')
        arcpy.AddRuleToTopology_management(
            ourTop, 'Boundary Must Be Covered By (Area-Line)', outMup, '',
            outCaf)
    if arcpy.Exists(outGel):
        for aRule in ('Must Be Single Part (Line)',
                      'Must Not Self-Overlap (Line)',
                      'Must Not Self-Intersect (Line)'):
            addMsgAndPrint('    ' + aRule)
            arcpy.AddRuleToTopology_management(ourTop, aRule, outGel)

    # validate topology
    addMsgAndPrint('  validating topology')
    arcpy.ValidateTopology_management(ourTop)
    if nameToken == '':
        nameToken = 'GeologicMap'
    nameToken = 'errors_' + nameToken + 'Topology'
    for sfx in ('_point', '_line', '_poly'):
        testAndDelete(outFds + '/' + nameToken + sfx)
    # export topology errors
    addMsgAndPrint('  exporting topology errors')
    arcpy.ExportTopologyErrors_management(ourTop, outFds, nameToken)
    outHtml.write('<h3>Line and polygon topology</h3>\n')
    outHtml.write(
        '<blockquote><i>Note that the map boundary commonly results in a "Must Not Have Gaps" error</i></blockquote>'
    )
    for sfx in ('_point', '_line', '_poly'):
        fc = outFds + '/' + nameToken + sfx
        outHtml.write('&nbsp; ' + nameToken + sfx + '<br>\n')
        html_writeFreqTable(outHtml, fc, ['RuleDescription', 'RuleType'])
        addMsgAndPrint('    ' + str(numberOfRows(fc)) + ' rows in ' +
                       os.path.basename(fc))
        if numberOfRows(fc) == 0: testAndDelete(fc)
Exemplo n.º 12
0
def topologia(capa, folder_salida, ruleTopology):
    global gdb_salida
    try:
        nombre_gdb = "topologia_%s" % (
            datetime.datetime.now().strftime("%b_%d_%Y_%H_%M_%S"))
        nombre_gdb = nombre_gdb.replace(".", "")
        gdb_salida = "%s\\%s.gdb" % (folder_salida, nombre_gdb)
        nombre_dataset = "dt_topologia"

        arcpy.env.workspace = gdb_salida
        nombre_capa = arcpy.Describe(capa).name

        # Process: Create File GDB
        if not arcpy.Exists(gdb_salida):
            arcpy.CreateFileGDB_management(folder_salida, nombre_gdb)

        # Process: Create Feature Dataset
        arcpy.CreateFeatureDataset_management(
            gdb_salida, nombre_dataset,
            "PROJCS['MAGNA_Colombia_Bogota',GEOGCS['GCS_MAGNA',DATUM['D_MAGNA',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',1000000.0],PARAMETER['False_Northing',1000000.0],PARAMETER['Central_Meridian',-74.07750791666666],PARAMETER['Scale_Factor',1.0],PARAMETER['Latitude_Of_Origin',4.596200416666666],UNIT['Meter',1.0]];-4623200 -9510300 10000;-100000 10000;-100000 10000;0,001;0,001;0,001;IsHighPrecision"
        )
        path_dataset = os.path.join(gdb_salida, nombre_dataset)
        ##    correc_topo = "\\%s"%(nombre_dataset)
        # Process: Feature Class to Feature Class

        arcpy.FeatureClassToFeatureClass_conversion(capa, path_dataset,
                                                    nombre_capa)
        path_Feature = os.path.join(path_dataset, nombre_capa)
        ##    FeaClass = "%s\\%s"%(nombre_dataset, nombre_capa)
        # Process: Create Topology
        Capa_topologia = "topo_capa_%s" % (nombre_capa)
        arcpy.CreateTopology_management(path_dataset, Capa_topologia, "")
        path_topology = os.path.join(path_dataset, Capa_topologia)
        ##    Topology = "%s\\topo_capa_%s"%(nombre_dataset, nombre_capa)

        # Process: Add Feature Class To Topology
        arcpy.AddFeatureClassToTopology_management(path_topology, path_Feature,
                                                   "1", "1")

        ###########################################################################################################################

        if ruleTopology == "Huecos y sobreposición":
            contador = 0
            arcpy.AddRuleToTopology_management(path_topology,
                                               "Must Not Overlap (Area)",
                                               path_Feature, "", "", "")

            # Process: Add Rule To Topology (2)
            arcpy.AddRuleToTopology_management(path_topology,
                                               "Must Not Have Gaps (Area)",
                                               path_Feature, "", "", "")

            # Process: Validate Topology
            arcpy.ValidateTopology_management(path_topology, "Full_Extent")

            # Process: Exportar error topology
            arcpy.ExportTopologyErrors_management(path_topology, path_dataset,
                                                  "Errores")

            ErroresTopologia = "#####Validación de topología (Huecos - Sobreposición) ######\n\n"

            for fc in arcpy.ListFeatureClasses(feature_dataset=nombre_dataset):
                path = os.path.join(arcpy.env.workspace, nombre_dataset, fc)

                if "Errores" in arcpy.Describe(path).name:
                    Ctd_contar = arcpy.GetCount_management(path)
                    Ctd = int(Ctd_contar.getOutput(0))

                    if Ctd > 0:

                        if "line" in str(arcpy.Describe(path).name):
                            if Ctd > 1:
                                contador = +1
                                ErroresTopologia = ErroresTopologia + "La capa presenta %s casos de huecos. \n" % (
                                    Ctd)
                        elif "poly" in str(arcpy.Describe(path).name):
                            contador = +1
                            ErroresTopologia = ErroresTopologia + "La capa presenta %s casos de sobreposición. \n" % (
                                Ctd)

                            ####oid de los poligonos con sobreposicion
                            oid_campo = [
                                f.name for f in arcpy.Describe(capa).fields
                                if f.type == "OID"
                            ][0]
                            arreglo = []
                            with arcpy.da.SearchCursor(path, [
                                    "OID@", "OriginObjectID",
                                    "DestinationObjectID"
                            ]) as cursor:
                                for fila in cursor:
                                    arreglo.append(fila[1])
                                    arreglo.append(fila[2])
                            if len(arreglo) > 0:
                                if len(arreglo) == 1:
                                    ErroresTopologia = ErroresTopologia + 'Los siguientes OID presentan sobreposición %s in %s \n' % (
                                        oid_campo,
                                        str(tuple(list(set(arreglo)))).replace(
                                            ",", ""))
                                else:
                                    ErroresTopologia = ErroresTopologia + 'Los siguientes OID presentan sobreposición  %s in %s \n' % (
                                        oid_campo,
                                        str(tuple(list(set(arreglo)))))

                else:
                    pass

            ErroresTopologia = ErroresTopologia + "\n\n Para validar los errores topológicos por favor consulte el siguente dataset %s" % (
                path_dataset)

            ###########################################################################################################################

        elif ruleTopology == "Huecos":
            contador = 0

            # Process: Add Rule To Topology (2)
            arcpy.AddRuleToTopology_management(path_topology,
                                               "Must Not Have Gaps (Area)",
                                               path_Feature, "", "", "")

            # Process: Validate Topology
            arcpy.ValidateTopology_management(path_topology, "Full_Extent")

            # Process: Exportar error topology
            arcpy.ExportTopologyErrors_management(path_topology, path_dataset,
                                                  "Errores")

            ErroresTopologia = "#####Validación de topología (Huecos) ######\n\n"
            for fc in arcpy.ListFeatureClasses(feature_dataset=nombre_dataset):
                path = os.path.join(arcpy.env.workspace, nombre_dataset, fc)

                if "Errores" in arcpy.Describe(path).name:
                    Ctd_contar = arcpy.GetCount_management(path)
                    Ctd = int(Ctd_contar.getOutput(0))
                    if Ctd > 1:

                        if "line" in str(arcpy.Describe(path).name):
                            contador = +1
                            ErroresTopologia = ErroresTopologia + "La capa presenta %s casos de huecos. \n" % (
                                Ctd)
                else:
                    pass

            ErroresTopologia = ErroresTopologia + "\n\n Para validar los errores topológicos por favor consulte el siguente dataset %s" % (
                path_dataset)

        ###########################################################################################################################

        elif ruleTopology == "Sobreposición":
            contador = 0

            # Process: Add Rule To Topology
            arcpy.AddRuleToTopology_management(path_topology,
                                               "Must Not Overlap (Area)",
                                               path_Feature, "", "", "")

            # Process: Validate Topology
            arcpy.ValidateTopology_management(path_topology, "Full_Extent")

            # Process: Exportar error topology
            arcpy.ExportTopologyErrors_management(path_topology, path_dataset,
                                                  "Errores")

            ErroresTopologia = "#####Validación de topología (Sobreposición) ######\n\n"

            for fc in arcpy.ListFeatureClasses(feature_dataset=nombre_dataset):
                path = os.path.join(arcpy.env.workspace, nombre_dataset, fc)

                if "Errores" in arcpy.Describe(path).name:
                    Ctd_contar = arcpy.GetCount_management(path)
                    Ctd = int(Ctd_contar.getOutput(0))
                    if Ctd > 0:

                        if "poly" in str(arcpy.Describe(path).name):
                            contador = +1
                            ErroresTopologia = ErroresTopologia + "La capa presenta %s casos de sobreposición. \n" % (
                                Ctd)

                            ####oid de los poligonos con sobreposicion
                            oid_campo = [
                                f.name for f in arcpy.Describe(capa).fields
                                if f.type == "OID"
                            ][0]
                            arreglo = []
                            with arcpy.da.SearchCursor(path, [
                                    "OID@", "OriginObjectID",
                                    "DestinationObjectID"
                            ]) as cursor:
                                for fila in cursor:
                                    arreglo.append(fila[1])
                                    arreglo.append(fila[2])
                            if len(arreglo) > 0:
                                if len(arreglo) == 1:
                                    ErroresTopologia = ErroresTopologia + 'Los siguientes OID presentan sobreposición %s in %s \n' % (
                                        oid_campo,
                                        str(tuple(list(set(arreglo)))).replace(
                                            ",", ""))
                                else:
                                    ErroresTopologia = ErroresTopologia + 'Los siguientes OID presentan sobreposición  %s in %s \n' % (
                                        oid_campo,
                                        str(tuple(list(set(arreglo)))))

            ErroresTopologia = ErroresTopologia + "\n\n Para validar los errores topológicos por favor consulte el siguente dataset %s" % (
                path_dataset)
    except arcpy.ExecuteError:
        arcpy.AddMessage(arcpy.GetMessages())
        msg = arcpy.GetMessages()
        if msg.find("-2147467259") != -1:
            ErroresTopologia = "#######Evaluación Topología###### \n El Feature de entrada presenta demasiados errores topológicos, \n por lo que ArcGIS no puede crear la topología, se recomienda fraccionar en partes más pequeñas \n  e.x. sí es del territorio nacional, se puede dividir por departamentos, o emplear la herramienta Multitopology \n que se encuentra en U: \SCRIPTS_ANALISIS"
            contador = 1
    except:

        # By default any other errors will be caught here
        e = sys.exc_info()[1]
        msg = e.args[0]
        ErroresTopologia = "#######Evaluación Topología###### \n Se encontró el siguiente error %s" % (
            msg)
        contador = 1

    if contador > 0:
        return ErroresTopologia + "\n"
    elif contador == 0:
        return None
Exemplo n.º 13
0
    def add_topologyFeature2Topology(self, topology_folder, topology_name,
                                     feature_dictionary, csvRule_file,
                                     csvRuleBDTopage):

        ruleGS_dictionnary = {}

        datasetTopage = dataset()
        featureTopage = feature()
        topologyTopage = topology()

        rule_list = open(csvRule_file, "rb")
        rule_reader = csv.reader(rule_list, delimiter=';')

        ruleBDTopage_list = open(csvRuleBDTopage, "rb")
        ruleBDTopage_reader = csv.reader(ruleBDTopage_list, delimiter=';')

        # Création de la topology ArcGIS
        topologyTopage.create_ArcGISTopology(topology_folder, topology_name)

        try:
            # Ajout des feature dans le gestionnaire topo
            for id_featureTopo, featureTopo in feature_dictionary.items(
            ):  #@UnusedVariable
                if featureTopo['fileBool'] == 'True':
                    topologyTopage.add_feature2ArcGISTopology(
                        topology_folder, topology_name, featureTopo['name'])

            # Check de toutes les vérifications ajoutées dans le dictionnaire
            #print u"\n########## Règles topologiques à tester ##########\n"

            for id_featureTopo, featureTopo in feature_dictionary.items(
            ):  #@UnusedVariable
                if featureTopo['fileBool'] == 'True':
                    # Remise à zéro du csv des règles topologiques de la BD Topage
                    ruleBDTopage_list.seek(0)
                    # Recherche de la présence de règle topologique concernant le fichier en question
                    for ruleBDTopage_row in ruleBDTopage_reader:

                        # Paramétrage de la règle BD Topage
                        ruleBDtopage = {'topologyId': ruleBDTopage_row[13]}
                        ruleBDtopage['topologyTexte'] = ruleBDTopage_row[2]
                        ruleBDtopage['feature1Id'] = ruleBDTopage_row[4]
                        ruleBDtopage['feature2Id'] = ruleBDTopage_row[7]
                        ruleBDtopage['feature3Id'] = ruleBDTopage_row[10]
                        ruleBDtopage['field'] = ruleBDTopage_row[14]
                        ruleBDtopage['fieldValue'] = ruleBDTopage_row[15]

                        # Si la règle topologique concerne l'objet recherché
                        if ruleBDTopage_reader.line_num <> 1 and ruleBDtopage[
                                'feature1Id'] == featureTopo['id']:
                            # Recherche de la règle topologique dans les règles génériques ESRI
                            rule_list.seek(0)
                            for rule_row in rule_reader:

                                # Paramètre de la règle topologique
                                rule = {'id': rule_row[0]}
                                rule['feature2Bool'] = rule_row[6]
                                rule['attributBool'] = rule_row[8]
                                rule['arcgisBool'] = rule_row[12]
                                rule['fonction'] = rule_row[13]

                                # Si le numéro de la règle générique correspond au numéro de la règle indiqué dans les règles de la BD Topage
                                if rule_reader.line_num <> 1 and rule[
                                        'id'] == ruleBDtopage['topologyId']:
                                    # Appel de la fonction d'ajout des règles à la topologie
                                    ruleGS_dictionnary = topologyTopage.add_topologyRule(
                                        ruleGS_dictionnary, feature_dictionary,
                                        topology_folder, topology_name,
                                        featureTopo, ruleBDtopage, rule)

        finally:
            ruleBDTopage_list.close()
            rule_list.close()

        # Validation de la topologie
        print(u"\n########## Vérification de la topologie ArcGIS ##########\n")
        topologyTopage.check(topology_folder, topology_name)

        #Export des erreurs Arcgis
        featureTopage.delete(topology_folder[0:-1] + "_error" + "/" +
                             topology_name + "_point")
        featureTopage.delete(topology_folder[0:-1] + "_error" + "/" +
                             topology_name + "_line")
        featureTopage.delete(topology_folder[0:-1] + "_error" + "/" +
                             topology_name + "_poly")

        arcpy.ExportTopologyErrors_management(
            topology_folder + topology_name,
            datasetTopage.path(topology_folder) + "_error", topology_name)

        # Check de toutes les vérifications ajoutées dans le dictionnaire
        print u"\n########## Résultat de la vérifications des règles de topologies ##########\n"

        for ruleGS_dictionnary_key in ruleGS_dictionnary.keys():
            print ruleGS_dictionnary_key + " : " + ruleGS_dictionnary[
                ruleGS_dictionnary_key]

        return ruleGS_dictionnary
Exemplo n.º 14
0
        topology,
        rule_type="Must Not Have Gaps (Area)",
        in_featureclass=exportMergedFDSFullPath + "\\" + "MapUnitPolys",
        subtype="",
        in_featureclass2="#",
        subtype2="")
    arcpy.AddRuleToTopology_management(
        topology,
        rule_type="Must Be Covered By Boundary Of (Line-Area)",
        in_featureclass=exportMergedFDSFullPath + "\\" + "ContactsAndFaults",
        subtype="",
        in_featureclass2=exportMergedFDSFullPath + "\\" + "MapUnitPolys",
        subtype2="")
    arcpy.ValidateTopology_management(topology)
    arcpy.ExportTopologyErrors_management(in_topology=topology,
                                          out_path=exportMergedFDSFullPath,
                                          out_basename="TopologyErrors")

if exportToAGOL:
    arcpy.env.overwriteOutput = True
    arcpy.AddMessage("Updating Feature Service on AGOL")
    # Update these variables
    # The tempPath variable is a relative path which is the same directory
    # this script is saved to. You can modify this value to a path on your
    # system to hold the temporary files.
    serviceName = "Piute"
    tempPath = r"\\igswzcwwgsrio\mojo\Team\Crow\TEMP\agol"
    path2MXD = r"\\igswzcwwgsrio\mojo\Team\Crow\PiuteValleyGeoMaps_AGOL.mxd"

    # All paths are built by joining names to the tempPath
    SDdraft = os.path.join(tempPath, "tempdraft.sddraft")