예제 #1
0
def main():

    # Initialize variables and file locations
    arcpy.env.overwriteOutput = True

    watershed_folders = get_watershed_folders(root_folder)
    projectwide_output = os.path.join(root_folder, "00_ProjectWide", "Inputs", "Data_Networks")
    projectwide_network = os.path.join(root_folder, "00_ProjectWide",  "Inputs", "Stream_Network", "Stream_Network.shp")
    delete_old(projectwide_output)
    total_count = len(data_networks_list_in)

    network_names = []

    for _ in data_networks_list_in:
        network_names.append([])

    for data_network, network_slot in zip(data_networks_list_in, network_names):
        name = data_network.split("\\")[-1]
        new_name = name.replace(".shp", "")
        network_slot.append(new_name)
        network_slot.append(data_network)

    sorted_list = sorted(network_names, key=lambda s: s[0].lower())

    for watershed in watershed_folders:
        # Clear old data
        delete_old(os.path.join(watershed, "Inputs", "Data_Networks"))

    for current_count, network_data in enumerate(sorted_list):

        name = network_data[0]
        network = network_data[1]
        arcpy.AddMessage("\nSaving {} Files ({}/{})...".format(name, current_count+1, total_count))

        if '.shp' not in name:
            name += '.shp'

        for watershed in watershed_folders:

            arcpy.AddMessage("\tStarting " + watershed + "...")

            # Get network to clip by
            old_stream_network = os.path.join(watershed, "Inputs", "Stream_Network", "Stream_Network.shp")

            # Clip the current data network to this watershed
            new_network_save = os.path.join(watershed, "Inputs", "Data_Networks", name)
            arcpy.Clip_analysis(network, old_stream_network, new_network_save)


            # Don't create an empty shapefile
            if is_empty(new_network_save):
                arcpy.AddMessage("Did not save {}, as it was empty".format(new_network_save))
                arcpy.Delete_management(new_network_save)

        arcpy.AddMessage("\tSaving Projectwide...")
        new_network_save = os.path.join(projectwide_output, name)
        arcpy.Clip_analysis(network, projectwide_network, new_network_save)

    finish()
예제 #2
0
def main():
    # Initialize variables and file locations
    arcpy.env.overwriteOutput = True

    watershed_folders = get_watershed_folders(root_folder)
    projectwide_output = os.path.join(root_folder, "00_ProjectWide", "Outputs",
                                      "Extracted_Data")
    delete_old(projectwide_output)
    to_merge_points = []
    req_fields = ["RchID", "FID", "Shape"]

    # This loops for each watershed folder
    for watershed in watershed_folders:
        arcpy.AddMessage("Working on {}...".format(watershed))

        # Initialize list of all unique data networks within this watershed
        point_list = get_data_points(watershed)
        output_folder = os.path.join(watershed, "Outputs", "Extracted_Data")
        delete_old(output_folder)

        # Create temporary shapefiles to store spatially joined data
        all_joined = os.path.join(output_folder, "temp.shp")

        # Join the first and second network's data together, and store them into a temporary shapefile
        arcpy.AddMessage("\t Merging first points...")

        arcpy.Copy_management(point_list[0], all_joined)
        all_fields = get_fields(all_joined)
        for field in req_fields:
            if field in all_fields:
                all_fields.remove(field)

        point_list.pop(0)

        # Check to make sure there are still networks to join
        if len(point_list) > 0:

            # This repeats for each of the two remaining networks
            for data in point_list:

                arcpy.AddMessage("\t\tMerging more points...")
                data_temp = os.path.join(output_folder, "data_temp.shp")
                arcpy.Copy_management(data, data_temp)
                data = data_temp
                remove_existing_fields(all_fields, data)

                # Join the current network to the previous network containing all other data
                arcpy.JoinField_management(all_joined, "RchID", data, "RchID")
                arcpy.DeleteField_management(all_joined, "RchID_1")
                all_fields = get_fields(all_joined)
                for field in req_fields:
                    if field in all_fields:
                        all_fields.remove(field)

        # Save the output into the correct folder
        save = arcpy.Copy_management(
            all_joined,
            os.path.join(output_folder, "Extraction_Merge_Points.shp"))

        to_merge_points.append(save)

        create_csv(os.path.join(output_folder, "All_Data.csv"), save)

        # Delete both temp shapefiles
        arcpy.Delete_management(all_joined)
        arcpy.Delete_management(data_temp)

    arcpy.AddMessage("Working on Projectwide...")

    make_csv = arcpy.Merge_management(
        to_merge_points,
        os.path.join(projectwide_output, "Extraction_Merge_Points.shp"))
    create_csv(os.path.join(projectwide_output, "All_Data.csv"), make_csv)
    finish()
예제 #3
0
def main():

    # Initialize variables
    arcpy.env.overwriteOutput = True

    watershed_folders = get_watershed_folders(root_folder)

    delete_old(
        os.path.join(root_folder, "00_ProjectWide", "Intermediates",
                     "Reach_Editing", "Inputs"))

    project_networks = []
    project_points = []
    temps_to_delete = []

    if fixed_points:
        network = os.path.join(root_folder, "00_ProjectWide", "Inputs",
                               "Stream_Network", "Stream_Network.shp")
        fixed_folder = os.path.join(root_folder, "00_ProjectWide",
                                    "Intermediates", "Points",
                                    "Unsnapped_Fixed")
        save_fixed_points(network, fixed_folder, watershed_folders)

    # For each watershed:
    for watershed_folder in watershed_folders:

        arcpy.AddMessage("Starting {}...".format(watershed_folder))

        # Get all file names
        output_folder = os.path.join(watershed_folder, "Intermediates",
                                     "Reach_Editing", "Inputs")
        network = os.path.join(watershed_folder, "Inputs", "Stream_Network",
                               "Stream_Network.shp")

        delete_old(output_folder)

        new_tor_filename = "temp_tor.shp"
        new_tor_points = os.path.join(watershed_folder, new_tor_filename)
        temps_to_delete.append(new_tor_points)

        new_bor_filename = "temp_bor.shp"
        new_bor_points = os.path.join(watershed_folder, new_bor_filename)
        temps_to_delete.append(new_bor_points)

        old_tor_points = os.path.join(watershed_folder, "Intermediates",
                                      "Points", "Snapped",
                                      "TOR_Points_Snapped.shp")
        old_bor_points = os.path.join(watershed_folder, "Intermediates",
                                      "Points", "Snapped",
                                      "BOR_Points_Snapped.shp")

        if fixed_points:
            # Merge the now fixed points with the snapped points, and use this going forward
            tor_temp_name = "temp_tor_merge.shp"
            tor_temp_merge = os.path.join(watershed_folder, tor_temp_name)
            tor_fixed = \
                os.path.join(watershed_folder, "Intermediates", "Points", "Unsnapped_Fixed", "TOR_Points_Fixed.shp")
            if not is_empty(tor_fixed):
                arcpy.Merge_management([tor_fixed, old_tor_points],
                                       tor_temp_merge)
                temps_to_delete.append(tor_temp_merge)
                old_tor_points = tor_temp_merge

            bor_temp_name = "temp_bor_merge.shp"
            bor_temp_merge = os.path.join(watershed_folder, bor_temp_name)
            bor_fixed = \
                os.path.join(watershed_folder, "Intermediates", "Points", "Unsnapped_Fixed", "BOR_Points_Fixed.shp")
            if not is_empty(bor_fixed):
                arcpy.Merge_management([bor_fixed, old_bor_points],
                                       bor_temp_merge)
                temps_to_delete.append(bor_temp_merge)
                old_bor_points = bor_temp_merge

        arcpy.CopyFeatures_management(old_tor_points, new_tor_points)
        arcpy.CopyFeatures_management(old_bor_points, new_bor_points)

        points_list = [new_tor_points, new_bor_points]
        tor_bor_list = ("\"TOR\"", "\"BOR\"")

        # This loops once for TOR points, once for BOR points
        for points, tor_bor in zip(points_list, tor_bor_list):

            # Add and populate TOR_BOR Field
            arcpy.AddField_management(points, "TOR_BOR", "TEXT")
            arcpy.CalculateField_management(points, "TOR_BOR", tor_bor)

        # Merge TOR_BOR Points
        merge_location = os.path.join(watershed_folder, "Intermediates",
                                      "Reach_Editing", "Inputs",
                                      "Points_Merge.shp")
        merge_edit_location = os.path.join(watershed_folder, "Intermediates",
                                           "Reach_Editing", "Outputs",
                                           "Points_Merge_To_Edit.shp")
        arcpy.Merge_management(points_list, merge_location)
        arcpy.Merge_management(points_list, merge_edit_location)
        project_points.append(merge_location)

        # Dissolve the network
        new_network = os.path.join(watershed_folder, "temp_network.shp")
        temps_to_delete.append(new_network)
        arcpy.Dissolve_management(network, new_network)

        network = new_network
        new_network = os.path.join(watershed_folder, "temp_network2.shp")
        temps_to_delete.append(new_network)

        # Split network at points
        arcpy.SplitLineAtPoint_management(network, merge_location, new_network,
                                          "10 METERS")
        network = new_network

        network_layer = "Network"
        arcpy.MakeFeatureLayer_management(network, network_layer)

        # Make a new layer of only segments that intersect the field points
        arcpy.SelectLayerByLocation_management\
            (network_layer, 'INTERSECT', merge_location)

        save_location = os.path.join(watershed_folder, "Intermediates",
                                     "Reach_Editing", "Inputs",
                                     "Stream_Network_Segments.shp")
        edit_location = os.path.join(watershed_folder, "Intermediates",
                                     "Reach_Editing", "Outputs",
                                     "Stream_Network_Segments_To_Edit.shp")
        arcpy.CopyFeatures_management(network_layer, save_location)
        arcpy.CopyFeatures_management(network_layer, edit_location)
        project_networks.append(save_location)

    arcpy.AddMessage("Saving ProjectWide...")
    make_projectwide(root_folder, project_points, project_networks)

    delete_temps(temps_to_delete)
    finish()
예제 #4
0
def main():

    # Initialize variables and file locations
    arcpy.env.overwriteOutput = True

    watershed_folders = get_watershed_folders(root_folder)
    projectwide_output = os.path.join(root_folder, "00_ProjectWide",
                                      "Intermediates", "Extraction", "Outputs")
    delete_old(projectwide_output)

    to_merge = []
    # Add a bunch of blank lists to the to_merge list, one space for each data network type
    for _ in get_data_networks(watershed_folders[0]):
        to_merge.append([])
    to_delete = []

    # This loops for every watershed
    for watershed_folder in watershed_folders:

        output_folder = os.path.join(watershed_folder, "Intermediates",
                                     "Extraction", "Outputs")

        network_list = get_data_networks(watershed_folder)

        arcpy.AddMessage("Starting " + watershed_folder + "...")
        for data_network_count, data_network in enumerate(network_list):
            if not is_empty(data_network):

                old_reaches = os.path.join(watershed_folder, "Intermediates",
                                           "Extraction", "Inputs",
                                           "Field_Reaches_Clean.shp")

                # Create a name for this data network so it can have a unique save location
                data_network_name = data_network.replace(
                    os.path.join(watershed_folder, "Inputs", "Data_Networks"),
                    "")
                data_network_name = data_network_name.replace(".shp", "")
                data_network_name = data_network_name.replace("\\", "")

                arcpy.AddMessage("\tStarting {}...".format(data_network_name))

                data_network_folder = make_folder(output_folder,
                                                  data_network_name)
                delete_old(data_network_folder)

                reaches = os.path.join(data_network_folder, "Reaches_Temp.shp")

                to_delete.append(reaches)

                reaches_save = os.path.join(
                    data_network_folder,
                    data_network_name + "_Points_Extracted.shp")

                arcpy.CopyFeatures_management(old_reaches, reaches)

                # Clip the data network to the Field reaches. This is important for calculating the math later
                clip = os.path.join(data_network_folder, "Clip_Temp.shp")
                to_delete.append(clip)

                arcpy.Clip_analysis(data_network, reaches, clip)
                clipped_data_network = clip

                # Adds field CLIP_LEN, which is the length of the clipped data segment
                arcpy.AddField_management(clipped_data_network, "CLIP_LEN",
                                          "DOUBLE")
                arcpy.CalculateField_management(clipped_data_network,
                                                "CLIP_LEN",
                                                "!shape.length@meters!",
                                                "PYTHON_9.3", "")

                # These fields is unnecessary and causes issues with merging, so they are deleted
                field_names = [
                    f.name for f in arcpy.ListFields(clipped_data_network)
                ]
                fields_to_delete = ["Join_Count", "TARGET_FID", "Join_Cou_1"]
                for field in fields_to_delete:
                    if field in field_names:
                        arcpy.DeleteField_management(clipped_data_network,
                                                     field)

                data_network_fields = get_fields(data_network)
                pnet_fields = get_fields(old_reaches)
                fields_to_keep = pnet_fields + data_network_fields

                # Extracts data from the data network to PIBO reaches using a weighted average system.
                extract_network(reaches, clipped_data_network, reaches_save,
                                data_network_folder, pnet_fields)

                # Remove all unnecessary fields
                keep_fields(reaches_save, fields_to_keep)
                #remove_empty_fields(reaches_save, pnet_fields)

                # Delete any temporary shape files created
                delete_temps(to_delete)

                create_csv(
                    os.path.join(data_network_folder,
                                 "{}.csv".format(data_network_name)),
                    reaches_save)
                to_merge[data_network_count].append(
                    [reaches_save, data_network_name])

    # Iterate through to_merge, and save a point and network shapefile for each data network

    arcpy.AddMessage("Saving Projectwide...")
    for data_network_type in to_merge:
        to_merge_networks = []
        save_name = data_network_type[0][1]
        save_folder = make_folder(projectwide_output, save_name)
        for watershed in data_network_type:
            to_merge_networks.append(watershed[0])
        csv_save = arcpy.Merge_management(
            to_merge_networks,
            os.path.join(save_folder, save_name + "_Points_Extracted.shp"))
        create_csv(os.path.join(save_folder, "{}.csv".format(save_name)),
                   csv_save)

    finish()
예제 #5
0
def main():

    # Initialize variables
    arcpy.env.overwriteOutput = True

    watershed_folders = get_watershed_folders(root_folder)
    delete_old(
        os.path.join(root_folder, "00_ProjectWide", "Intermediates",
                     "Reach_Editing", "Outputs"))
    temps_to_delete = []
    to_merge_points = []
    to_merge_reaches = []

    for watershed in watershed_folders:
        arcpy.AddMessage("Starting {}...".format(watershed))

        # Get file names
        input_folder = os.path.join(watershed, "Intermediates",
                                    "Reach_Editing", "Inputs")
        output_folder = os.path.join(watershed, "Intermediates",
                                     "Reach_Editing", "Outputs")
        delete_old(output_folder)

        stream_seg = os.path.join(input_folder, "Stream_Network_Segments.shp")
        points = os.path.join(input_folder, "Points_Merge.shp")

        stream_seg_copy = os.path.join(
            input_folder, "Stream_Network_Segments_To_Edit_Temp.shp")
        points_copy = os.path.join(input_folder,
                                   "Points_Merge_To_Edit_Temp.shp")

        temps_to_delete.append(stream_seg_copy)
        temps_to_delete.append(points_copy)

        arcpy.Copy_management(stream_seg, stream_seg_copy)
        arcpy.Copy_management(points, points_copy)

        stream_seg = stream_seg_copy
        points = points_copy

        # Spatial jon stream network segments by points

        fields_to_remove = ["TARGET_FID", "JOIN_FID", "Join_Count"]

        spatial_joined = os.path.join(input_folder, "Spatial_Joined_Temp.shp")
        temps_to_delete.append(spatial_joined)
        remove_fields(fields_to_remove, stream_seg)
        arcpy.SpatialJoin_analysis(stream_seg, points, spatial_joined,
                                   "JOIN_ONE_TO_MANY")

        # Get an attribute table list of the joined shapefile to analyze
        stream_seg_list = attribute_table_to_list(spatial_joined)
        reach_id_index = get_field_index("TARGET_FID", spatial_joined)
        point_id_index = get_field_index("SiteID", spatial_joined)
        tor_bor_index = get_field_index("TOR_BOR", spatial_joined)

        new_list = split_list_by_id(stream_seg_list, reach_id_index)
        to_delete_list = []
        keep_points_list = []

        # Check which segments we need to delete
        for segment in new_list:
            if delete_segment(segment, point_id_index, tor_bor_index,
                              reach_id_index):
                to_delete_list.append(segment[0][reach_id_index])
            else:
                keep_points_list.append(segment[0][point_id_index])

        segments_layer = "Segments"
        arcpy.MakeFeatureLayer_management(stream_seg, segments_layer)

        # Delete the segments we need to from initial segments
        for to_delete in to_delete_list:
            arcpy.SelectLayerByAttribute_management(
                segments_layer, 'ADD_TO_SELECTION',
                'FID = {}'.format(to_delete))

        # Save the reaches we want to keep
        arcpy.SelectLayerByAttribute_management(segments_layer,
                                                'SWITCH_SELECTION')
        reach_save_location = os.path.join(output_folder, "Field_Reaches.shp")
        arcpy.CopyFeatures_management(segments_layer, reach_save_location)
        to_merge_reaches.append(reach_save_location)

        # Save the points we want to keep
        points_layer = "Points"
        arcpy.MakeFeatureLayer_management(points, points_layer)

        for to_keep in keep_points_list:
            arcpy.SelectLayerByAttribute_management(
                points_layer, 'ADD_TO_SELECTION',
                'SiteID = {}'.format(to_keep))

        point_save_location = os.path.join(output_folder, "Field_Points.shp")
        arcpy.CopyFeatures_management(points_layer, point_save_location)
        to_merge_points.append(point_save_location)

        num_points = int(arcpy.GetCount_management(point_save_location)[0])
        num_reaches = int(arcpy.GetCount_management(reach_save_location)[0])

        # Check that everything was done correctly
        if (num_points / 2) != num_reaches:
            arcpy.AddMessage(
                "\t This watershed does not have one field reach per two field points!"
            )

    arcpy.AddMessage("Saving ProjectWide...")
    projectwide_folder = os.path.join(root_folder, "00_ProjectWide",
                                      "Intermediates", "Reach_Editing",
                                      "Outputs")
    arcpy.Merge_management(
        to_merge_points, os.path.join(projectwide_folder, "Field_Points.shp"))
    arcpy.Merge_management(
        to_merge_reaches, os.path.join(projectwide_folder,
                                       "Field_Reaches.shp"))
    delete_temps(temps_to_delete)
    finish()
예제 #6
0
def main():

    # Initialize variables and file locations
    arcpy.env.overwriteOutput = True

    watershed_folders = get_watershed_folders(root_folder)
    projectwide_output = os.path.join(root_folder, "00_ProjectWide",
                                      "Intermediates", "Extraction", "Inputs")
    temps_to_delete = []
    keep_fields = [
        "Shape", "FID", "SiteID", "RchID", "POINT_X", "POINT_Y", "SnapDist"
    ]
    to_merge_reaches = []
    to_merge_points = []
    delete_old(
        os.path.join(root_folder, "00_ProjectWide", "Inputs", "Parameters"))

    delete_old(projectwide_output)

    for watershed in watershed_folders:
        arcpy.AddMessage("Starting {}...".format(watershed))

        # Get necessary files
        output_folder = os.path.join(watershed, "Intermediates", "Extraction",
                                     "Inputs")
        in_reaches = os.path.join(watershed, "Intermediates", "Reach_Editing",
                                  "Outputs", "Field_Reaches.shp")
        in_points = os.path.join(watershed, "Intermediates", "Reach_Editing",
                                 "Outputs", "Field_Points.shp")
        points_temp = os.path.join(output_folder, "p_temp.shp")
        reaches_temp = os.path.join(output_folder, "r_temp.shp")
        reaches_joined = os.path.join(output_folder, "r_join.shp")
        points_joined = os.path.join(output_folder, "p_join.shp")
        temps_to_delete.extend(
            [points_temp, reaches_temp, reaches_joined, points_joined])
        points_final = os.path.join(output_folder, "Field_Points_Clean.shp")
        reaches_final = os.path.join(output_folder, "Field_Reaches_Clean.shp")

        # Add field for the length of the field reach
        arcpy.Copy_management(in_reaches, reaches_temp)
        field_to_add = "FldRchLen"
        keep_fields.append(field_to_add)
        field_names = [f.name for f in arcpy.ListFields(reaches_temp)]

        if field_to_add not in field_names:
            arcpy.AddField_management(reaches_temp, field_to_add, "DOUBLE")

        arcpy.CalculateField_management(reaches_temp, field_to_add,
                                        "!shape.length@meters!", "PYTHON_9.3",
                                        "")

        # Reduce points to only BOR points
        points_layer = "Points"
        arcpy.MakeFeatureLayer_management(in_points, points_layer)
        arcpy.SelectLayerByAttribute_management(points_layer, 'NEW_SELECTION',
                                                "\"TOR_BOR\" = \'BOR\'")
        arcpy.CopyFeatures_management(points_layer, points_temp)

        # Add all point data to the reaches
        arcpy.SpatialJoin_analysis(reaches_temp, points_temp, reaches_joined,
                                   "JOIN_ONE_TO_ONE")

        # Add all reach data to the points
        arcpy.SpatialJoin_analysis(points_temp, reaches_temp, points_joined,
                                   "JOIN_ONE_TO_ONE")

        # Only keep fields we need
        shapes = [points_joined, reaches_joined]
        for shape in shapes:

            # Removes all fields from the shapefile that are not in the above list of fields to keep
            field_names = [f.name for f in arcpy.ListFields(shape)]
            delete_fields = []
            for field in field_names:
                if field not in keep_fields:
                    delete_fields.append(field)

            arcpy.DeleteField_management(shape, delete_fields)

        # Save the points and reaches
        to_merge_points.append(
            arcpy.CopyFeatures_management(points_joined, points_final))
        to_merge_reaches.append(
            arcpy.CopyFeatures_management(reaches_joined, reaches_final))

    arcpy.AddMessage("Saving Projectwide...")
    arcpy.Merge_management(
        to_merge_points, os.path.join(projectwide_output,
                                      "Field_Points_Clean"))
    arcpy.Merge_management(
        to_merge_reaches,
        os.path.join(projectwide_output, "Field_Reaches_Clean"))

    delete_temps(temps_to_delete)
    finish()
예제 #7
0
def main():

    # Initialize Variables
    arcpy.env.overwriteOutput = True
    saved_tor_points_snapped = []
    saved_bor_points_snapped = []
    saved_tor_points_unsnapped = []
    saved_bor_points_unsnapped = []

    watershed_folders = get_watershed_folders(root_folder)

    # Delete old content from this tool being re run.
    delete_old(
        os.path.join(root_folder, "00_ProjectWide", "Intermediates", "Points",
                     "Snapped"))
    delete_old(
        os.path.join(root_folder, "00_ProjectWide", "Intermediates", "Points",
                     "Unsnapped"))

    # This loops for every watershed
    for watershed_folder in watershed_folders:

        arcpy.AddMessage("Starting {}...".format(watershed_folder))

        # Get all file names
        output_folder = os.path.join(watershed_folder, "Intermediates",
                                     "Points")
        network = os.path.join(watershed_folder, "Inputs", "Stream_Network",
                               "Stream_Network.shp")

        delete_old(os.path.join(output_folder, "Snapped"))
        delete_old(os.path.join(output_folder, "Unsnapped"))

        bor_points_old = os.path.join(watershed_folder, "Inputs", "Points",
                                      "BOR_Points.shp")
        tor_points_old = os.path.join(watershed_folder, "Inputs", "Points",
                                      "TOR_Points.shp")

        bor_points_new_temp = os.path.join(output_folder,
                                           "BOR_Points_Temp.shp")
        tor_points_new_temp = os.path.join(output_folder,
                                           "TOR_Points_Temp.shp")

        bor_points_new_snapped = os.path.join(output_folder, "Snapped",
                                              "BOR_Points_Snapped.shp")
        tor_points_new_snapped = os.path.join(output_folder, "Snapped",
                                              "TOR_Points_Snapped.shp")
        saved_bor_points_snapped.append(bor_points_new_snapped)
        saved_tor_points_snapped.append(tor_points_new_snapped)

        bor_points_new_unsnapped = os.path.join(output_folder, "Unsnapped",
                                                "BOR_Points_Unsnapped.shp")
        tor_points_new_unsnapped = os.path.join(output_folder, "Unsnapped",
                                                "TOR_Points_Unsnapped.shp")
        saved_bor_points_unsnapped.append(bor_points_new_unsnapped)
        saved_tor_points_unsnapped.append(tor_points_new_unsnapped)

        arcpy.CopyFeatures_management(bor_points_old, bor_points_new_temp)
        arcpy.CopyFeatures_management(tor_points_old, tor_points_new_temp)

        points_list = [tor_points_new_temp, bor_points_new_temp]

        # This loops once for TOR and once for BOR, snaps all points
        for counter, points in enumerate(points_list):
            if counter == 0:
                label = "TOR"
            else:
                label = "BOR"
            snap_dist = 0
            all_snapped = False
            total_points = arcpy.GetCount_management(points)
            base_list = []

            # TODO make more general
            for row in arcpy.da.SearchCursor(points, "RchID"):
                base_list.append(row[0])
            snap_value_list = [999] * len(base_list)
            arcpy.AddField_management(points, "SnapDist", "SHORT")

            # This loops until all points are snapped to the network, incrementing the snap distance by 10 each time.
            while all_snapped is False:

                # Increment snap distance by an increment
                snap_dist += custom_increment
                snap_name = str(snap_dist) + " Meters"
                arcpy.AddMessage("\t Snapping {} {}".format(label, snap_name))

                # Snap the points
                arcpy.Snap_edit(points, [[network, "EDGE", snap_name]])
                temp = os.path.join(watershed_folder, "temporary.shp")

                # Create an intersect of the points and the network
                arcpy.Intersect_analysis([points, network], temp)
                current_snapped = arcpy.GetCount_management(temp)

                # Update each snap distance value for points that were just snapped
                # TODO make more general
                for row in arcpy.da.SearchCursor(temp, "RchID"):
                    if snap_value_list[base_list.index(row[0])] == 999:
                        snap_value_list[base_list.index(row[0])] = snap_dist

                # Checks to see if every point has been snapped yet
                if (str(current_snapped) == str(total_points)) or \
                        (use_threshold is True and snap_dist >= threshold_range):

                    # All points have been snapped, or are beyond the given threshold
                    all_snapped = True

                    # Delete temporary file
                    arcpy.Delete_management(temp)

            # Add XY data to each point
            arcpy.AddXY_management(points)

            # Populate the snap distance field
            with arcpy.da.UpdateCursor(points, "SnapDist") as cursor:
                for count, row in enumerate(cursor):
                    row[0] = snap_value_list[count]
                    cursor.updateRow(row)

            # Create a layer to select from
            points_layer = "Points"
            arcpy.MakeFeatureLayer_management(points, points_layer)

            # Save snapped and unsnapped points
            if label == "TOR":
                arcpy.SelectLayerByAttribute_management(
                    points_layer, 'NEW_SELECTION', 'SnapDist >= 999')
                arcpy.CopyFeatures_management(points_layer,
                                              tor_points_new_unsnapped)
                arcpy.SelectLayerByAttribute_management(
                    points_layer, 'SWITCH_SELECTION')
                arcpy.CopyFeatures_management(points_layer,
                                              tor_points_new_snapped)
                arcpy.SelectLayerByAttribute_management(
                    points_layer, 'CLEAR_SELECTION')

            if label == "BOR":
                arcpy.SelectLayerByAttribute_management(
                    points_layer, 'NEW_SELECTION', 'SnapDist >= 999')
                arcpy.CopyFeatures_management(points_layer,
                                              bor_points_new_unsnapped)
                arcpy.SelectLayerByAttribute_management(
                    points_layer, 'SWITCH_SELECTION')
                arcpy.CopyFeatures_management(points_layer,
                                              bor_points_new_snapped)
                arcpy.SelectLayerByAttribute_management(
                    points_layer, 'CLEAR_SELECTION')

        # Delete temporary files
        arcpy.Delete_management(bor_points_new_temp)
        arcpy.Delete_management(tor_points_new_temp)

    arcpy.AddMessage("Saving ProjectWide Files...")

    output_folder = os.path.join(root_folder, "00_ProjectWide",
                                 "Intermediates", "Points")

    bor_points_new_snapped = os.path.join(output_folder, "Snapped",
                                          "BOR_Points_Snapped.shp")
    tor_points_new_snapped = os.path.join(output_folder, "Snapped",
                                          "TOR_Points_Snapped.shp")
    bor_points_new_unsnapped = os.path.join(output_folder, "Unsnapped",
                                            "BOR_Points_Unsnapped.shp")
    tor_points_new_unsnapped = os.path.join(output_folder, "Unsnapped",
                                            "TOR_Points_Unsnapped.shp")

    arcpy.Merge_management(saved_bor_points_snapped, bor_points_new_snapped)
    arcpy.Merge_management(saved_tor_points_snapped, tor_points_new_snapped)
    arcpy.Merge_management(saved_bor_points_unsnapped,
                           bor_points_new_unsnapped)
    arcpy.Merge_management(saved_tor_points_unsnapped,
                           tor_points_new_unsnapped)

    arcpy.Copy_management(
        bor_points_new_unsnapped,
        os.path.join(output_folder, "Unsnapped_Fixed", "To_Fix_BOR.shp"))
    arcpy.Copy_management(
        tor_points_new_unsnapped,
        os.path.join(output_folder, "Unsnapped_Fixed", "To_Fix_TOR.shp"))

    finish()
예제 #8
0
def main():

    # Initialize Variables
    arcpy.env.overwriteOutput = True
    watershed_list = []
    to_merge_tor = []
    to_merge_bor = []
    to_merge_network = []
    temps_to_delete = []
    watershed_layer = "Watershed"
    arcpy.MakeFeatureLayer_management(watersheds, watershed_layer)

    # Remove all existing content
    for folder in get_folder_list(root_folder, True):
        remove_folder(folder)

    # Get the name of every watershed
    # TODO make more general
    for row in arcpy.da.SearchCursor(watersheds, ["Name"]):

        watershed_list.append(row[0])

    # This loops for each watershed
    for watershed in watershed_list:

        arcpy.AddMessage("Starting " + watershed + "...")

        # Create folder structure within root folder for this watershed
        watershed_folder = make_structure(root_folder, watershed)

        # Get the boundary of this watershed
        query = 'NAME = \'' + watershed + '\''
        arcpy.SelectLayerByAttribute_management(watershed_layer,
                                                'NEW_SELECTION', query)
        clipped_watershed = os.path.join(watershed_folder, "Inputs",
                                         "Watershed_Boundary",
                                         "Watershed_Boundary.shp")
        arcpy.CopyFeatures_management(watershed_layer, clipped_watershed)
        arcpy.SelectLayerByAttribute_management(watershed_layer,
                                                "CLEAR_SELECTION")

        # Clip the TOR points to this watershed and save them
        tor_save_location = os.path.join(watershed_folder, "Inputs", "Points",
                                         "TOR_Points.shp")
        tor_temp_location = os.path.join(watershed_folder, "Inputs", "Points",
                                         "TOR_Points_Temp.shp")
        temps_to_delete.append(tor_temp_location)
        arcpy.Clip_analysis(tor_points, clipped_watershed, tor_temp_location)

        # Only save one point per reach and site
        year_dif_field = create_year_distance(tor_temp_location, "yr",
                                              data_year)
        delete_identical(tor_temp_location, "SiteID", year_dif_field, "RchID",
                         tor_save_location)
        to_merge_tor.append(tor_save_location)

        # Clip the BOR points to this watershed and save them
        bor_save_location = os.path.join(watershed_folder, "Inputs", "Points",
                                         "BOR_Points.shp")
        bor_temp_location = os.path.join(watershed_folder, "Inputs", "Points",
                                         "BOR_Points_Temp.shp")
        temps_to_delete.append(bor_temp_location)
        arcpy.Clip_analysis(bor_points, clipped_watershed, bor_temp_location)

        # Only save one point per reach and site
        create_year_distance(bor_temp_location, "yr")
        delete_identical(bor_temp_location, "SiteID", year_dif_field, "RchID",
                         bor_save_location)
        to_merge_bor.append(bor_save_location)

        # Clip the stream_network to this watershed and save it
        stream_save_location = os.path.join(watershed_folder, "Inputs",
                                            "Stream_Network",
                                            "Stream_Network.shp")
        arcpy.Clip_analysis(stream_network, clipped_watershed,
                            stream_save_location)
        # TODO make more general
        to_merge_network.append(stream_save_location)

    arcpy.AddMessage("Starting Project Wide...")

    # Make a folder to contain Project Wide outputs and inputs
    project_folder = make_structure(root_folder, "00_ProjectWide")

    arcpy.AddMessage("\t Saving TOR Points...")
    # Merge every Watershed's TOR points, and save it to the ProjectWide folder
    tor_save_location = os.path.join(project_folder, "Inputs", "Points",
                                     "TOR_Points.shp")
    arcpy.Merge_management(to_merge_tor, tor_save_location)

    arcpy.AddMessage("\t Saving BOR Points...")
    # Merge every Watershed's BOR points, and save it to the ProjectWide folder
    bor_save_location = os.path.join(project_folder, "Inputs", "Points",
                                     "BOR_Points.shp")
    arcpy.Merge_management(to_merge_bor, bor_save_location)

    arcpy.AddMessage("\t Saving Stream Network...")

    # Take Stream Network, and save it to the ProjectWide folder
    stream_save_location = os.path.join(project_folder, "Inputs",
                                        "Stream_Network", "Stream_Network.shp")
    arcpy.Copy_management(stream_network, stream_save_location)

    arcpy.AddMessage("\t Saving Watersheds...")
    # Take Watershed Boundaries, and save it to the ProjectWide folder
    wat_save_location = os.path.join(project_folder, "Inputs",
                                     "Watershed_Boundary",
                                     "Watershed_Boundary.shp")
    arcpy.Copy_management(watersheds, wat_save_location)

    delete_temps(temps_to_delete)

    finish()