def transfer_line(fcInLine, fcToLine, strStreamSide):
    outputWorkspace = arcpy.Describe(fcInLine).path
    fcOutput = gis_tools.newGISDataset(
        outputWorkspace, "LineNetworkConfinement" + strStreamSide)

    # Split Line Network by Line Ends
    fcSplitPoints = gis_tools.newGISDataset(
        outputWorkspace, "SplitPoints_Confinement" + strStreamSide)
    arcpy.FeatureVerticesToPoints_management(fcInLine, fcSplitPoints,
                                             "BOTH_ENDS")
    tblNearPointsConfinement = gis_tools.newGISDataset(
        outputWorkspace, "NearPointsConfinement" + strStreamSide)
    arcpy.GenerateNearTable_analysis(fcSplitPoints,
                                     fcToLine,
                                     tblNearPointsConfinement,
                                     location="LOCATION",
                                     angle="ANGLE")
    lyrNearPointsConfinement = gis_tools.newGISDataset(
        "Layer", "lyrNearPointsConfinement" + strStreamSide)
    arcpy.MakeXYEventLayer_management(tblNearPointsConfinement, "NEAR_X",
                                      "NEAR_Y", lyrNearPointsConfinement,
                                      fcToLine)
    arcpy.SplitLineAtPoint_management(fcToLine,
                                      lyrNearPointsConfinement,
                                      fcOutput,
                                      search_radius="0.01 Meters")

    # Prepare Fields
    strConfinementField = "Con_" + strStreamSide
    arcpy.AddField_management(fcOutput, strConfinementField, "LONG")

    # Transfer Attributes by Centroids
    fcCentroidPoints = gis_tools.newGISDataset(
        outputWorkspace, "CentroidPoints_Confinement" + strStreamSide)
    arcpy.FeatureVerticesToPoints_management(fcInLine, fcCentroidPoints, "MID")
    tblNearPointsCentroid = gis_tools.newGISDataset(
        outputWorkspace, "NearPointsCentroid" + strStreamSide)
    arcpy.GenerateNearTable_analysis(fcCentroidPoints,
                                     fcToLine,
                                     tblNearPointsCentroid,
                                     location="LOCATION",
                                     angle="ANGLE")
    lyrNearPointsCentroid = gis_tools.newGISDataset(
        "Layer", "lyrNearPointsCentroid" + strStreamSide)
    arcpy.MakeXYEventLayer_management(tblNearPointsCentroid, "NEAR_X",
                                      "NEAR_Y", lyrNearPointsCentroid,
                                      fcToLine)
    lyrToLineSegments = gis_tools.newGISDataset("Layer", "lyrToLineSegments")
    arcpy.MakeFeatureLayer_management(fcOutput, lyrToLineSegments)

    arcpy.SelectLayerByLocation_management(
        lyrToLineSegments,
        "INTERSECT",
        lyrNearPointsCentroid,
        selection_type="NEW_SELECTION")  #"0.01 Meter","NEW_SELECTION")
    arcpy.CalculateField_management(lyrToLineSegments, strConfinementField, 1,
                                    "PYTHON")

    return fcOutput
Exemple #2
0
def get_currnode_buffernode_dist_dict():
    near_table = "in_memory//t1"
    arcpy.Project_management(old_n, temp1, arcpy.SpatialReference(102039))
    arcpy.GenerateNearTable_analysis(temp1, new_n, near_table, "7 Miles",
                                     "NO_LOCATION", "NO_ANGLE", "ALL", "100",
                                     "PLANAR")
    df = pandas.DataFrame(
        arcpy.da.TableToNumPyArray(near_table,
                                   ['IN_FID', 'NEAR_FID', 'NEAR_DIST']))
    new_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(new_n)
    }
    old_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(old_n)
    }
    df['IN_FID'] = df['IN_FID'].map(old_fid_ids_dict)
    df['NEAR_FID'] = df['NEAR_FID'].map(new_fid_ids_dict)
    df_dict = df.transpose().to_dict()
    ids_nearids_dict = {}
    for key, values in df_dict.iteritems():
        if values['IN_FID'] in ids_nearids_dict:
            ids_nearids_dict[values['IN_FID']][
                values["NEAR_FID"]] = values["NEAR_DIST"] / 1609.04
        else:
            ids_nearids_dict[values['IN_FID']] = {
                values['NEAR_FID']: values["NEAR_DIST"] / 1609.04
            }
    return ids_nearids_dict
def get_buffer_nodes_dist_dict():
    near_table = "in_memory//t1"
    temp = "C:/GIS/temp.shp"
    arcpy.Project_management(new_n, temp, sr_p)  #
    arcpy.MakeFeatureLayer_management(
        temp, "new_n")  # do not select the dangling nodes
    arcpy.GenerateNearTable_analysis(
        temp, old_n, near_table,
        str(curr_node_buff_node_dist + 2) + " Miles", "NO_LOCATION",
        "NO_ANGLE", "ALL", "10", "PLANAR")
    df = pandas.DataFrame(
        arcpy.da.TableToNumPyArray(near_table,
                                   ['IN_FID', 'NEAR_FID', 'NEAR_DIST']))
    new_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(new_n)
    }
    old_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(old_n)
    }
    df['IN_FID'] = df['IN_FID'].map(new_fid_ids_dict)
    df['NEAR_FID'] = df['NEAR_FID'].map(old_fid_ids_dict)

    df = df[df.IN_FID.isin(potential_ids.keys())]

    df_dict = df.transpose().to_dict()
    ids_nearids_dict = {}
    for key, value in df_dict.iteritems():
        ids_nearids_dict.setdefault(value['NEAR_FID'], []).append(
            [value['IN_FID'], value['NEAR_DIST'] / 1609.04])
        # ids_nearids_dict.setdefault(value['NEAR_FID'], []).append(value['IN_FID'])
    return ids_nearids_dict
def getClosestSegments(segFC1, segFC2, table, search_radius):
    location = "NO_LOCATION"
    angle = "NO_ANGLE"
    closest = 'ALL'
    segmentsToGet = 10
    arcpy.GenerateNearTable_analysis(segFC1, segFC2, table, search_radius,
                                     location, angle, closest, segmentsToGet)
Exemple #5
0
def near_table(in_features, near_features, out_table):
    arcpy.GenerateNearTable_analysis(in_features,
                                     near_features,
                                     out_table,
                                     angle='ANGLE',
                                     method='GEODESIC',
                                     search_radius='10000 kilometers')
Exemple #6
0
def compareToBoundary(target_feats, target_pts_fc, compare_feats, compare_name,
                      output_field):
    # Keep in mind that if a target feature is further from all near features than the search radius (which is
    # very small in this case, it will not have any entry in the near table)
    compare_table = 'in_memory/' + compare_name + '_compare_table'
    search_radius = '1 FEET'
    arcpy.GenerateNearTable_analysis(target_pts_fc, compare_feats,
                                     compare_table, search_radius)

    compare_dict = {}
    fields = ['IN_FID', 'NEAR_DIST']
    with arcpy.da.SearchCursor(compare_table, fields) as cursor:
        for target_fid, near_dist in cursor:
            if near_dist == 0:
                compare_dict[target_fid] = 'yes'

    f_type = 'TEXT'
    arcpy.AddField_management(target_feats, output_field, f_type)

    fields = ['OID@', output_field]
    with arcpy.da.UpdateCursor(target_feats, fields) as cursor:
        for oid, compare_output in cursor:
            try:
                compare_output = compare_dict[poly2point_dict[oid]]
            except:
                compare_output = 'no'

            cursor.updateRow((oid, compare_output))
Exemple #7
0
def get_buffer_nodes_dist_dict():
    near_table = "in_memory//t1"
    arcpy.Project_management(new_n, temp1, arcpy.SpatialReference(102039))
    arcpy.GenerateNearTable_analysis(temp1, old_n, near_table, "6 Miles",
                                     "NO_LOCATION", "NO_ANGLE", "ALL", "10",
                                     "PLANAR")
    df = pandas.DataFrame(
        arcpy.da.TableToNumPyArray(near_table,
                                   ['IN_FID', 'NEAR_FID', 'NEAR_DIST']))
    new_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(new_n)
    }
    old_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(old_n)
    }
    df['IN_FID'] = df['IN_FID'].map(new_fid_ids_dict)
    df['NEAR_FID'] = df['NEAR_FID'].map(old_fid_ids_dict)
    df_dict = df.transpose().to_dict()
    ids_nearids_dict = {}
    for key, value in df_dict.iteritems():
        ids_nearids_dict.setdefault(value['NEAR_FID'], []).append(
            [value['IN_FID'], value['NEAR_DIST'] / 1609.04])
    return ids_nearids_dict
Exemple #8
0
    def execute(self):
        # Create output GDB and add domain for asset type
        self.gdb = arcpy.CreateFileGDB_management(*os.path.split(self.gdb))[0]

        # Since NA.solve uses OID for start/end points, the OID of the feature classes being copied should
        # be maintained so that the attribute information can be linked back, if desired
        remote_temp = copy_features(self.remote,
                                    common.unique_name("in_memory/remoteTemp"))
        fixed_temp = copy_features(self.fixed,
                                   common.unique_name("in_memory/fixedTemp"))

        # The information that Near creates is not needed (distance to nearest feature and feature ID), so delete it.
        # Sorting the features by descending distance ensures that the furthest assets are backhauled first.
        arcpy.Near_analysis(remote_temp, fixed_temp)
        sort_temp = arcpy.Sort_management(remote_temp,
                                          common.unique_name("in_memory/sort"),
                                          [["NEAR_DIST", "DESCENDING"]])[0]
        common.delete(remote_temp)
        arcpy.DeleteField_management(sort_temp, ("NEAR_DIST", "NEAR_FID"))

        # For each remote asset, find the surrounding nearest fixed assets. This Near Table will be used
        # during backhaul to only load particular assets into the Facilities sublayer. Because this table is sorted
        # where the nearest features are first (NEAR_DIST and NEAR_RANK are ascending), an arbitrarily defined number of
        # fixed assets can be loaded into the Closest Facility via list slicing.

        # In practice, set closest_count to a reasonable number (such as 100). If closest_count=None, then the resultant
        # Near Table will have (nrows in remote * nrows in fixed) rows. This quickly gets very large (17m+ during dev).
        count = arcpy.GetCount_management(sort_temp)
        arcpy.AddMessage("\t{} Processing {} features...".format(
            common.timestamp(), count))
        method = "GEODESIC" if arcpy.Describe(
            sort_temp).spatialReference.type == "Geographic" else "PLANAR"
        near_temp = arcpy.GenerateNearTable_analysis(
            sort_temp,
            fixed_temp,
            common.unique_name("in_memory/near"),
            closest="ALL",
            closest_count=NEAR_TABLE_SIZE,
            method=method)[0]
        arcpy.AddMessage("\t{} Saving Near Table...".format(
            common.timestamp()))
        arcpy.DeleteField_management(near_temp, ["NEAR_DIST", "NEAR_RANK"])
        near_out = arcpy.CopyRows_management(
            near_temp, os.path.join(self.gdb, NEAR_TABLE))[0]
        common.delete(near_temp)

        arcpy.ResetProgressor()
        self.calc_update(sort_temp, "Remote")
        self.calc_update(fixed_temp, "Fixed")
        remote_out = arcpy.CopyFeatures_management(
            sort_temp, os.path.join(self.gdb, REMOTE))[0]
        fixed_out = arcpy.CopyFeatures_management(
            fixed_temp, os.path.join(self.gdb, FIXED))[0]
        common.delete([sort_temp, fixed_temp])

        return remote_out, fixed_out, near_out
def get_nearest_ground_truth_dict():
    temp2 = "C:/gis/temp.shp"
    near_table = "in_memory//t2"
    memory_ngt = "in_memory//ngt1"
    #
    arcpy.CopyFeatures_management(
        new_ngt,
        memory_ngt)  # since near analysis overwrites in the original shp
    arcpy.Near_analysis(
        memory_ngt, reduced_nodes)  # do near analysis only to potential nodes
    #
    arcpy.Project_management(reduced_nodes, temp2,
                             sr_p)  #to find nodes around a distance
    #
    new_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(reduced_nodes)
    }
    newn_fid_ids_dict = {
        row.getValue("ID"): row.getValue("FID")
        for row in arcpy.SearchCursor(memory_ngt)
    }
    # #
    nearfid_to_ground_id = {
        newn_fid_ids_dict[row.getValue("ID")]:
        new_fid_ids_dict[row.getValue("NEAR_FID")]
        for row in arcpy.SearchCursor(memory_ngt)
    }
    #
    arcpy.GenerateNearTable_analysis(
        temp2, memory_ngt, near_table,
        str(curr_node_buff_node_dist + 3) + " Miles", "NO_LOCATION",
        "NO_ANGLE", "ALL", "10", "PLANAR")
    #
    df = pandas.DataFrame(
        arcpy.da.TableToNumPyArray(near_table,
                                   ['IN_FID', 'NEAR_FID', 'NEAR_DIST']))
    #
    df['IN_FID'] = df['IN_FID'].map(new_fid_ids_dict)
    df['NEAR_FID'] = df['NEAR_FID'].map(nearfid_to_ground_id)
    df['NEAR_DIST'] = df['NEAR_DIST'] / 1609.04
    #
    df = df.loc[df.groupby(['IN_FID', 'NEAR_FID']).NEAR_DIST.idxmin(
    )]  #since some gt nodes donot have any reduced nodes nearby (snaps to wrong node)
    df = df.reset_index()[['IN_FID', 'NEAR_FID',
                           'NEAR_DIST']]  #since 1^ removes some indexes
    #
    ids_nearids_dict = {}
    for i in range(len(df)):
        if df.IN_FID[i] in ids_nearids_dict:
            ids_nearids_dict[df.IN_FID[i]][df.NEAR_FID[i]] = df.NEAR_DIST[i]
        else:
            ids_nearids_dict[df.IN_FID[i]] = {df.NEAR_FID[i]: df.NEAR_DIST[i]}
    # ids_nearids_dict = {x:y for x,y in ids_nearids_dict.iteritems() if x in potential_ids.keys()}
    return ids_nearids_dict
def calc_address_roadinfra(estamap_version):
    logging.info('environment')
    em = gis.ESTAMAP(estamap_version)
    conn = dbpy.create_conn_pyodbc(em.server, em.database_name)

    logging.info('dropping tables:')
    if dbpy.check_exists('ADDRESS_ROADINFRA', conn):
        logging.info('ADDRESS_ROADINFRA')
        conn.execute('drop table ADDRESS_ROADINFRA')

    logging.info('creating ADDRESS_ROADINFRA')
    conn.execute('''
    CREATE TABLE [dbo].[ADDRESS_ROADINFRA](
        [PFI] [nvarchar](10) NOT NULL,
        [UFI] [int] NULL
    ) ON [PRIMARY]
    ''')
    conn.commit()

    logging.info('reading ADDRESS FIDs')
    address_fids = {}
    with arcpy.da.SearchCursor(in_table=os.path.join(em.sde, 'ADDRESS'),
                               field_names=['PFI', 'OBJECTID']) as sc:
        for pfi, oid in sc:
            address_fids[oid] = pfi

    logging.info('reading ROAD_INFRASTRUCTURE FIDs')
    roadinfra_fids = {}
    with arcpy.da.SearchCursor(in_table=os.path.join(em.sde,
                                                     'ROAD_INFRASTRUCTURE'),
                               field_names=['UFI', 'OBJECTID']) as sc:
        for ufi, oid in sc:
            roadinfra_fids[oid] = ufi

    logging.info('Generate Near Table analysis...')
    arcpy.GenerateNearTable_analysis(
        in_features=os.path.join(em.sde, 'ADDRESS'),
        near_features=os.path.join(em.sde, 'ROAD_INFRASTRUCTURE'),
        out_table='in_memory\\near_table')

    logging.info('inserting')
    with dbpy.SQL_BULK_COPY(em.server, em.database_name, 'ADDRESS_ROADINFRA') as sbc, \
         arcpy.da.SearchCursor(in_table='in_memory\\near_table',
                               field_names=['IN_FID', 'NEAR_FID']) as sc:
        for enum, (address_fid, roadinfra_fid) in enumerate(sc):
            sbc.add_row(
                (address_fids[address_fid], roadinfra_fids[roadinfra_fid]))
            if enum % 100000 == 0:
                logging.info(enum)
                sbc.flush()
        logging.info(enum)
Exemple #11
0
def createTable(pendienteCentroide, cotizado, longBuffer):
    cantidadCotizados = 10
    tb_near = arcpy.GenerateNearTable_analysis(
        pendienteCentroide, cotizado, os.path.join(pathgdb, "TB_resumen"),
        '{} Meters'.format(longBuffer), 'NO_LOCATION', 'NO_ANGLE', 'ALL',
        cantidadCotizados, 'GEODESIC')
    codigoCotizado = {
        x[0]: [x[1], x[2], x[3]]
        for x in arcpy.da.SearchCursor(
            cotizado, ["OBJECTID", CODIGO, "SHAPE@X", "SHAPE@Y"])
    }
    codigoPendiente = {
        x[0]: [x[1], x[2], x[3]]
        for x in arcpy.da.SearchCursor(
            pendienteCentroide, ["OBJECTID", CODIGO, "SHAPE@X", "SHAPE@Y"])
    }

    arcpy.AddField_management(tb_near, "X_COTIZADO", "DOUBLE")
    arcpy.AddField_management(tb_near, "Y_COTIZADO", "DOUBLE")
    arcpy.AddField_management(tb_near, "X_PENDIENTE", "DOUBLE")
    arcpy.AddField_management(tb_near, "Y_PENDIENTE", "DOUBLE")
    arcpy.AddField_management(tb_near, "DISTANCIA", "DOUBLE")
    arcpy.AddField_management(tb_near, "CODIGO_PENDIENTE", "TEXT", "#", "#",
                              100)
    arcpy.AddField_management(tb_near, "CODIGO_COTIZADO", "TEXT", "#", "#",
                              100)
    arcpy.AddField_management(tb_near, "CODIGO_N", "TEXT", "#", "#", 100)

    with arcpy.da.UpdateCursor(tb_near, [
            "IN_FID", "NEAR_FID", "NEAR_DIST", "CODIGO_COTIZADO",
            "CODIGO_PENDIENTE", "DISTANCIA", "X_COTIZADO", "Y_COTIZADO",
            "X_PENDIENTE", "Y_PENDIENTE", "CODIGO_N"
    ]) as cursor:
        for x in cursor:
            x[3] = codigoCotizado.get(x[1])[0]
            x[4] = codigoPendiente.get(x[0])[0]
            x[5] = round(x[2], 2)

            x[6] = codigoCotizado.get(x[1])[1]
            x[7] = codigoCotizado.get(x[1])[2]

            x[8] = codigoPendiente.get(x[0])[1]
            x[9] = codigoPendiente.get(x[0])[2]

            if len(x[4].split("_")) == 1:
                x[10] = "ClusterSimple_" + x[4]
            else:
                x[10] = "SuperCluster_" + x[4]
            cursor.updateRow(x)
    return tb_near
Exemple #12
0
def generate_near_table_df():  # uses node_shp and transfer_xl_shp
    arcpy.GenerateNearTable_analysis(transfer_xl_shp, node_shp, transfer_table,
                                     "", "", "", "ALL", closest_count,
                                     "GEODESIC")
    df = Dbf5(transfer_table).to_dataframe()
    fid_to_id = {
        row.getValue("FID"): row.getValue("ID")
        for row in arcpy.SearchCursor(node_shp)
    }
    fid_to_id_df = pandas.DataFrame(fid_to_id,
                                    index=[0]).transpose().reset_index()
    fid_to_id_df.columns = ["FID", "ID"]
    df = df.merge(fid_to_id_df, left_on='NEAR_FID', right_on='FID')
    df['NEAR_DIST'] = df['NEAR_DIST'] / 1609.34
    return df
Exemple #13
0
def push_nodes_to_streets(nodes_in, streets_in, name_nodes_in, output_gdb_in,
                          output_fds_in):
    """
    This function pushes the input nodes to the closest input line (street) for the future routing. 
    Shall be done after all the post-processing of the streets to avoid the cycles. 

    :param nodes_in:        input nodes/ points / demands 
    :param streets_in:      input lines/ streets / roads
    :param name_nodes_in:   the name for the output nodes
    :param output_dir_in:   the path, where the pushed nodes will be saved 
    :return:                the path to the result 
    """
    arcpy.overwriteOutput = 1

    # Get the original spatial reference
    descript = arcpy.Describe(nodes_in)
    spatial_ref_orig = descript.spatialReference

    # Calculate the near table -> find the closest line to every point
    out_table = os.path.join(output_gdb_in, 'near_table')
    check_exists(out_table)
    # GenerateNearTable_analysis (in_features, near_features, out_table, {search_radius}, {location}, {angle},
    # {closest}, {closest_count}, {method})
    tmp_table = arcpy.GenerateNearTable_analysis(nodes_in,
                                                 streets_in,
                                                 out_table,
                                                 method='GEODESIC',
                                                 closest_count=1,
                                                 location='LOCATION')
    # fields = arcpy.ListFields(tmp_table)
    # fc_fields = [field.name for field in fields if field.type != 'Geometry']

    # Create a layer from the pushed points
    out_layer = os.path.join('in_memory', 'tmp')
    tmp_bs = arcpy.MakeXYEventLayer_management(tmp_table, 'NEAR_X', 'NEAR_Y',
                                               out_layer,
                                               spatial_ref_orig).getOutput(0)

    # Save pushed points
    out_result = os.path.join(output_fds_in,
                              '{0}_pushed'.format(name_nodes_in))
    check_exists(out_result)
    arcpy.CopyFeatures_management(tmp_bs, out_result)

    # Delete temprorary results
    arcpy.Delete_management(out_table)

    return out_result
def addAddressAttributes(sgid, parcels, county, cntyFipsDict, ws):

    arcpy.env.workspace = ws
    arcpy.env.overwriteOutput = True

    pts = os.path.join(sgid, 'SGID.LOCATION.AddressPoints')

    nearFLDS = ['IN_FID', 'NEAR_FID', 'NEAR_DIST']

    nearTBL = os.path.join(ws, 'NearTbl_{}'.format(county))
    sql = """"{}" = '{}'""".format('CountyID', cntyFipsDict[county])
    cntyPts_FL = arcpy.MakeFeatureLayer_management(pts, 'cntyPts_FL', sql)

    print('Adding Address Attributes')

    arcpy.GenerateNearTable_analysis(parcels, cntyPts_FL, nearTBL, '2 Meters',
                                     'NO_LOCATION', 'NO_ANGLE', 'CLOSEST')

    pt2Poly_Dict = {}
    polyDict = {}

    with arcpy.da.SearchCursor(nearTBL, nearFLDS) as sCursor:
        for row in sCursor:
            pt2Poly_Dict[row[0]] = row[1]
            polyDict.setdefault(row[1])
    with arcpy.da.SearchCursor(pts, ['OBJECTID', 'FullAdd']) as sCursor:
        for row in sCursor:
            if row[0] in polyDict:
                polyDict[row[0]] = row[1]

    parcelFlds = ['OBJECTID', 'PARCEL_ADD']

    ucursor = arcpy.da.UpdateCursor(parcels, parcelFlds)
    for urow in ucursor:
        try:
            if pt2Poly_Dict[urow[0]] in polyDict:
                urow[1] = polyDict[pt2Poly_Dict[urow[0]]]
        except:
            urow[1] = ''

        ucursor.updateRow(urow)

    del ucursor
    arcpy.Delete_management(nearTBL)


##parcels = r'C:\ZBECK\BlueStakes\stagingBS.gdb\SGID_GEOGRAPHIC\Parcels_Beaver'
##addAddressAttributes(sgid, parcels, cnty)
Exemple #15
0
def CalcDistArray(inval):
    '''
    Calculate Distance Array using the Generate Near Function.
    
    Called By:
    CalcDistancesLayer
    CalcDistanceLayerMultiple
    
    Calls:
    
    Arguments:
    inval = [UPConfig,layername]
    
    
    Returns:
    Distarray: [OBJECTID, distance, BaseGeom_id, attracter]
    
    '''

    UPConfig = inval[0]
    layername = inval[1]

    gn_table = arcpy.GenerateNearTable_analysis(
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     UPConfig['BaseGeom_cent']),
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     layername), 'in_memory/temp_up_dist', "", "", "",
        "CLOSEST")
    # Convert gn_table to a Numpy Array
    gn_array = arcpy.da.TableToNumPyArray(gn_table, ['IN_FID', 'NEAR_DIST'])
    desc = arcpy.Describe(
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     UPConfig['BaseGeom_cent']))
    oidfieldname = desc.OIDFieldName
    gn_array.dtype.names = str(oidfieldname), 'distance'
    bg_array = arcpy.da.TableToNumPyArray(
        os.path.join(UPConfig['paths']['dbpath'], UPConfig['paths']['dbname'],
                     UPConfig['BaseGeom_cent']),
        [oidfieldname, UPConfig['BaseGeom_id']])
    arr = rfn.join_by(oidfieldname, gn_array, bg_array, 'outer')
    arr = AddNumpyField(arr, [('attracter', '<a50')])
    for ln in arr:
        ln['attracter'] = layername
    arcpy.Delete_management('in_memory/temp_up_dist')
    return (arr)
Exemple #16
0
 def __init__(self, selected_address, side, ST_PREFIX, axis):
     """Only function in class."""
     if ST_PREFIX == "W" or ST_PREFIX == "E":
         self.axis_val = int(axis[0])
         self.axis_dir = axis[2]
         self.Neighbor_GRID_Val = str(self.axis_val - 1000)
         self.axis_val_field = "AxisX_Val"
         self.axis_dir_field = "AxisX_Dir"
     else:
         self.axis_val = int(axis[1])
         self.axis_dir = axis[3]
         self.Neighbor_GRID_Val = str(self.axis_val - 1000)
         self.axis_val_field = "AxisY_Val"
         self.axis_dir_field = "AxisY_Dir"
     self.selection = ("{0}='{1}' AND {2}='{3}'".format(
         self.axis_val_field, self.Neighbor_GRID_Val, self.axis_dir_field,
         ST_PREFIX))
     arcpy.SelectLayerByAttribute_management("Addressing_Grid",
                                             "NEW_SELECTION",
                                             self.selection)
     arcpy.CopyFeatures_management("Addressing_Grid", "neighbor_grid")
     self.near_table = arcpy.GenerateNearTable_analysis(
         in_features=selected_address,
         near_features="neighbor_grid",
         out_table="Near_Table")
     self.cursor = arcpy.SearchCursor(self.near_table)
     Address_Dist = 0
     for row in self.cursor:
         Address_Dist = int((row.getValue("NEAR_DIST") / 5280) * 1000)
     if side == "E" or side == "N":
         EorO_1 = "O"
     else:
         EorO_1 = "E"
     if (Address_Dist % 2) == 0:
         EorO_2 = "E"
     else:
         EorO_2 = "O"
     if EorO_1 == EorO_2:
         pass
     else:
         Address_Dist = (Address_Dist + 1)
     self.Address_Dist = Address_Dist
     self.HOUSENUM = self.axis_val + self.Address_Dist
Exemple #17
0
def near_segments(index,tempData,roadway,tempTable,searchRadius,gpsDict,snapDict):
    nearList = []
    upCursor = arcpy.da.UpdateCursor(tempData,"SHAPE@XY")
    row = upCursor.next()
    row[0] = (gpsDict[index]["gpsPoint"][0],gpsDict[index]["gpsPoint"][1])
    upCursor.updateRow(row)
    del upCursor

    arcpy.GenerateNearTable_analysis(tempData,roadway,tempTable,searchRadius,"LOCATION","ANGLE","ALL")
    with arcpy.da.SearchCursor(tempTable,["NEAR_DIST","NEAR_FID","NEAR_X","NEAR_Y"]) as tempCursor:
        for row in tempCursor:
            nearList.append(row)
    nearList.sort()
    ## print "id:{} -- point:{} -- nearList:{}".format(index,gpsDict[index]["gpsPoint"],nearList)
    del tempCursor
    
    snapDict[index] = nearList

    return snapDict
Exemple #18
0
def get_nearest_ground_truth_dict():
    new_gt_new_id_dict = {}
    near_table = "in_memory//t1"
    memory_ngt = "in_memory//ngt1"
    arcpy.CopyFeatures_management(new_ngt, memory_ngt)
    arcpy.Near_analysis(memory_ngt, new_n)
    nearfid_to_ground_id = {
        row.getValue("ID"): row.getValue("NEAR_FID")
        for row in arcpy.SearchCursor(memory_ngt)
    }
    new_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(new_n)
    }
    nearfid_to_ground_id = {
        x: new_fid_ids_dict[y]
        for x, y in nearfid_to_ground_id.iteritems()
    }
    arcpy.Project_management(new_n, temp1, arcpy.SpatialReference(102039))
    arcpy.GenerateNearTable_analysis(temp1, new_ngt, near_table, "10 Miles",
                                     "NO_LOCATION", "NO_ANGLE", "ALL", "10",
                                     "PLANAR")
    df = pandas.DataFrame(
        arcpy.da.TableToNumPyArray(near_table,
                                   ['IN_FID', 'NEAR_FID', 'NEAR_DIST']))
    newn_fid_ids_dict = {
        row.getValue("FID"): row.getValue("ID")
        for row in arcpy.SearchCursor(new_ngt)
    }
    df['IN_FID'] = df['IN_FID'].map(new_fid_ids_dict)
    df['NEAR_FID'] = df['NEAR_FID'].map(newn_fid_ids_dict).map(
        nearfid_to_ground_id)
    df['NEAR_DIST'] = df['NEAR_DIST'] / 1609.04
    #df.dropna(inplace=True)
    ids_nearids_dict = {}
    for i in range(len(df)):
        if df.IN_FID[i] in ids_nearids_dict:
            ids_nearids_dict[df.IN_FID[i]][df.NEAR_FID[i]] = df.NEAR_DIST[i]
        else:
            ids_nearids_dict[df.IN_FID[i]] = {df.NEAR_FID[i]: df.NEAR_DIST[i]}
    return ids_nearids_dict
def getNearFeatures(arg):
    '''Get Nearest River features for building centroids
        (used as input to the multiprocessing pool)
        arg = a series of arguments to feed into the multiprocessing pool
        *Important: arg should be in the following format:
        ((c, riverLines, outfolder) for c in inPoints)
        inPoints = the list of file locations for each centroid chunk
        riverLines = location of the newly created subset of river
        line features
        outfolder = where to store the output table or feature
        class with added Near fields
    '''
    inPoints, riverLines, outfolder = arg

    # set up outputs for GenerateNearTable_analysis function
    outChunkGdb = setup_folder(
        outfolder,
        str(inPoints).replace('Layer', 'Table') + '.gdb', 'gdb')
    arcpy.env.workspace = outChunkGdb
    arcpy.env.outputCoordinateSystem = \
        arcpy.SpatialReference('North America Albers Equal Area Conic')
    outTableName = os.path.basename(inPoints).split('.')[0].replace(
        'Layer', 'Table')
    outTable = os.path.join(outChunkGdb, outTableName)

    # if user specifies run Near_analysis tool, then run (feature class output)
    if nearAnalysis:
        arcpy.Near_analysis(inPoints,
                            riverLines,
                            location='LOCATION',
                            method='GEODESIC')
    # if user specifies not to run Near_analysis tool,
    # then run GenerateNearTable (table output)
    elif not nearAnalysis:
        arcpy.GenerateNearTable_analysis(inPoints,
                                         riverLines,
                                         outTable,
                                         location='location',
                                         method='GEODESIC')
def get_currnode_buffernode_dist_dict():
    near_table = "in_memory//t1"
    temp = "C:/GIS/temp.shp"
    arcpy.Project_management(old_n, temp1, sr_p)
    arcpy.MakeFeatureLayer_management(new_n, "new_n")
    # arcpy.SelectLayerByAttribute_management("new_n", "SWITCH_SELECTION", get_where_clause("_ID_", dangling_ids)) # the list was too big, python crashed
    arcpy.CopyFeatures_management("new_n", temp)
    arcpy.GenerateNearTable_analysis(
        temp1, temp, near_table,
        str(curr_node_buff_node_dist + 2) + " Miles", "NO_LOCATION",
        "NO_ANGLE", "ALL", "200", "PLANAR")
    df = pandas.DataFrame(
        arcpy.da.TableToNumPyArray(near_table,
                                   ['IN_FID', 'NEAR_FID', 'NEAR_DIST']))
    new_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(temp)
    }
    old_fid_ids_dict = {
        row.getValue("FID"): row.getValue("_ID_")
        for row in arcpy.SearchCursor(old_n)
    }
    df['IN_FID'] = df['IN_FID'].map(old_fid_ids_dict)
    df['NEAR_FID'] = df['NEAR_FID'].map(new_fid_ids_dict)
    # print df
    df = df[df.NEAR_FID.isin(potential_ids.keys())]
    df_dict = df.transpose().to_dict()
    ids_nearids_dict = {}
    for key, values in df_dict.iteritems():
        if values['IN_FID'] in ids_nearids_dict:
            ids_nearids_dict[values['IN_FID']][
                values["NEAR_FID"]] = values["NEAR_DIST"] / 1609.04
        else:
            ids_nearids_dict[values['IN_FID']] = {
                values['NEAR_FID']: values["NEAR_DIST"] / 1609.04
            }
    return ids_nearids_dict
def locations_add_links(logger, the_scenario, modal_layer_name,
                        max_artificial_link_distance_miles):

    # ADD LINKS LOGIC
    # first we near the mode to the locations fc
    # then we iterate through the near table and build up a dictionary of links and all the near XYs on that link.
    # then we split the links on the mode (except pipeline) and preserve the data of that link.
    # then we near the locations to the nodes on the now split links.
    # we ignore locations with near dist == 0 on those nodes.
    # then we add the artificial link and note which locations got links.
    # then we set the connects_to  field if the location was connected.

    logger.debug(
        "start: locations_add_links for mode: {}".format(modal_layer_name))

    scenario_gdb = the_scenario.main_gdb
    fp_to_modal_layer = os.path.join(scenario_gdb, "network", modal_layer_name)

    locations_fc = the_scenario.locations_fc
    arcpy.DeleteField_management(fp_to_modal_layer, "LOCATION_ID")
    arcpy.AddField_management(os.path.join(scenario_gdb, modal_layer_name),
                              "LOCATION_ID", "long")

    arcpy.DeleteField_management(fp_to_modal_layer, "LOCATION_ID_NAME")
    arcpy.AddField_management(os.path.join(scenario_gdb, modal_layer_name),
                              "LOCATION_ID_NAME", "text")

    if float(max_artificial_link_distance_miles.strip(" Miles")) < 0.0000001:
        logger.warning(
            "Note: ignoring mode {}. User specified artificial link distance of {}"
            .format(modal_layer_name, max_artificial_link_distance_miles))
        logger.debug(
            "Setting the definition query to artificial = 99999, so we get an empty dataset for the "
            "make_feature_layer and subsequent near analysis")

        definition_query = "Artificial = 999999"  # something to return an empty set
    else:
        definition_query = "Artificial = 0"  # the normal def query.

    if "pipeline" in modal_layer_name:

        if arcpy.Exists(
                os.path.join(scenario_gdb, "network",
                             fp_to_modal_layer + "_points")):
            arcpy.Delete_management(
                os.path.join(scenario_gdb, "network",
                             fp_to_modal_layer + "_points"))

        # limit near to end points
        arcpy.FeatureVerticesToPoints_management(
            in_features=fp_to_modal_layer,
            out_feature_class=fp_to_modal_layer + "_points",
            point_location="BOTH_ENDS")
        logger.debug("start:  make_featurelayer_management")
        arcpy.MakeFeatureLayer_management(fp_to_modal_layer + "_points",
                                          "modal_lyr_" + modal_layer_name,
                                          definition_query)

    else:
        logger.debug("start:  make_featurelayer_management")
        arcpy.MakeFeatureLayer_management(fp_to_modal_layer,
                                          "modal_lyr_" + modal_layer_name,
                                          definition_query)

    logger.debug(
        "adding links between locations_fc and mode {} with max dist of {}".
        format(modal_layer_name, max_artificial_link_distance_miles))

    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_near")):
        logger.debug("start:  delete tmp near")
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near"))

    logger.debug("start:  generate_near")
    arcpy.GenerateNearTable_analysis(locations_fc,
                                     "modal_lyr_" + modal_layer_name,
                                     os.path.join(scenario_gdb, "tmp_near"),
                                     max_artificial_link_distance_miles,
                                     "LOCATION", "NO_ANGLE", "CLOSEST")

    edit = arcpy.da.Editor(os.path.join(scenario_gdb))
    edit.startEditing(False, False)
    edit.startOperation()

    id_fieldname = arcpy.Describe(os.path.join(scenario_gdb,
                                               modal_layer_name)).OIDFieldName

    seenids = {}

    # SPLIT LINKS LOGIC
    # 1) first search through the tmp_near fc and add points from the near on that link.
    # 2) next we query the mode layer and get the mode specific data using the near FID.
    # 3) then we split the old link, and use insert cursor to populate mode specific data into fc for the two new links.
    # 4) then we delete the old unsplit link
    logger.debug("start:  split links")
    with arcpy.da.SearchCursor(
            os.path.join(scenario_gdb, "tmp_near"),
        ["NEAR_FID", "NEAR_X", "NEAR_Y", "NEAR_DIST"]) as scursor:

        for row in scursor:

            # if the near distance is 0, then its connected and we don't need to
            # split the line.
            if row[3] == 0:
                # only give debug warnring if not pipeline.
                if "pipleine" not in modal_layer_name:
                    logger.warning(
                        "Split links code: LOCATION MIGHT BE ON THE NETWORK. Ignoring NEAR_FID {} with NEAR_DIST {}"
                        .format(row[0], row[3]))

            if not row[3] == 0:

                # STEP 1: point geoms where to split from the near XY
                # ---------------------------------------------------
                # get the line ID to split
                theIdToGet = str(row[0])  # this is the link id we need.

                if not theIdToGet in seenids:
                    seenids[theIdToGet] = []

                point = arcpy.Point()
                point.X = float(row[1])
                point.Y = float(row[2])
                point_geom = arcpy.PointGeometry(point,
                                                 ftot_supporting_gis.LCC_PROJ)
                seenids[theIdToGet].append(point_geom)

        # STEP 2 -- get mode specific data from the link
        # ------------------------------------------------
        if 'pipeline' not in modal_layer_name:

            for theIdToGet in seenids:

                # initialize the variables so we dont get any gremlins.
                in_line = None  # the shape geometry
                in_capacity = None  # road + rail
                in_volume = None  # road + rail
                in_vcr = None  # road + rail | volume to capacity ratio
                in_fclass = None  # road | fclass
                in_speed = None  # road | rounded speed
                in_stracnet = None  # rail
                in_density_code = None  # rail
                in_tot_up_dwn = None  # water

                if modal_layer_name == 'road':
                    for row in arcpy.da.SearchCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                "SHAPE@", "Capacity", "Volume", "VCR",
                                "FCLASS", "ROUNDED_SPEED"
                            ],
                            where_clause=id_fieldname + " = " + theIdToGet):
                        in_line = row[0]
                        in_capacity = row[1]
                        in_volume = row[2]
                        in_vcr = row[3]
                        in_fclass = row[4]
                        in_speed = row[5]

                if modal_layer_name == 'rail':
                    for row in arcpy.da.SearchCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                "SHAPE@", "Capacity", "Volume", "VCR",
                                "STRACNET", "DENSITY_CODE"
                            ],
                            where_clause=id_fieldname + " = " + theIdToGet):
                        in_line = row[0]
                        in_capacity = row[1]
                        in_volume = row[2]
                        in_vcr = row[3]
                        in_stracnet = row[4]
                        in_density_code = row[5]

                if modal_layer_name == 'water':
                    for row in arcpy.da.SearchCursor(
                            os.path.join(scenario_gdb, modal_layer_name),
                        ["SHAPE@", "Capacity", "Volume", "VCR", "TOT_UP_DWN"],
                            where_clause=id_fieldname + " = " + theIdToGet):
                        in_line = row[0]
                        in_capacity = row[1]
                        in_volume = row[2]
                        in_vcr = row[3]
                        in_tot_up_dwn = row[4]

                # STEP 3: Split and populate with mode specific data from old link
                # ------------------------------------------------------------------
                split_lines = arcpy.management.SplitLineAtPoint(
                    in_line, seenids[theIdToGet], arcpy.Geometry(), 1)

                if not len(split_lines) == 1:

                    # ROAD
                    if modal_layer_name == 'road':

                        icursor = arcpy.da.InsertCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES',
                                'FCLASS', 'ROUNDED_SPEED', 'Volume',
                                'Capacity', 'VCR'
                            ])

                        # Insert new links that include the mode-specific attributes
                        for new_line in split_lines:
                            len_in_miles = Q_(new_line.length,
                                              "meters").to("miles").magnitude
                            icursor.insertRow([
                                new_line, 0, modal_layer_name, len_in_miles,
                                in_fclass, in_speed, in_volume, in_capacity,
                                in_vcr
                            ])

                        # Delete cursor object
                        del icursor

                    elif modal_layer_name == 'rail':
                        icursor = arcpy.da.InsertCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES',
                                'STRACNET', 'DENSITY_CODE', 'Volume',
                                'Capacity', 'VCR'
                            ])

                        # Insert new rows that include the mode-specific attributes
                        for new_line in split_lines:
                            len_in_miles = Q_(new_line.length,
                                              "meters").to("miles").magnitude
                            icursor.insertRow([
                                new_line, 0, modal_layer_name, len_in_miles,
                                in_stracnet, in_density_code, in_volume,
                                in_capacity, in_vcr
                            ])

                        # Delete cursor object
                        del icursor

                    elif modal_layer_name == 'water':

                        icursor = arcpy.da.InsertCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES',
                                'TOT_UP_DWN', 'Volume', 'Capacity', 'VCR'
                            ])

                        # Insert new rows that include the mode-specific attributes
                        for new_line in split_lines:
                            len_in_miles = Q_(new_line.length,
                                              "meters").to("miles").magnitude
                            icursor.insertRow([
                                new_line, 0, modal_layer_name, len_in_miles,
                                in_tot_up_dwn, in_volume, in_capacity, in_vcr
                            ])

                        # Delete cursor object
                        del icursor

                    else:
                        logger.warning(
                            "Modal_layer_name: {} is not supported.".format(
                                modal_layer_name))

                    # STEP 4:  Delete old unsplit data
                    with arcpy.da.UpdateCursor(os.path.join(
                            scenario_gdb, modal_layer_name), ['OID@'],
                                               where_clause=id_fieldname +
                                               " = " + theIdToGet) as ucursor:
                        for row in ucursor:
                            ucursor.deleteRow()

                # if the split doesn't work.
                else:
                    logger.detailed_debug(
                        "the line split didn't work for ID: {}. "
                        "Might want to investigate. "
                        "Could just be an artifact from the near result being the end of a line."
                        .format(theIdToGet))

    edit.stopOperation()
    edit.stopEditing(True)

    # delete the old features
    # ------------------------
    logger.debug(
        "start:  delete old features (tmp_near, tmp_near_2, tmp_nodes)")
    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_near")):
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near"))

    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_near_2")):
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near_2"))

    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_nodes")):
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_nodes"))

    # Add artificial links now.
    # now that the lines have been split add lines from the from points to the nearest node
    # --------------------------------------------------------------------------------------
    logger.debug(
        "start:  add artificial links now w/ definition_query: {}".format(
            definition_query))
    logger.debug("start:  make_featurelayer 2")
    fp_to_modal_layer = os.path.join(scenario_gdb, "network", modal_layer_name)
    arcpy.MakeFeatureLayer_management(fp_to_modal_layer,
                                      "modal_lyr_" + modal_layer_name + "2",
                                      definition_query)
    logger.debug("start:  feature vertices to points 2")
    arcpy.FeatureVerticesToPoints_management(
        in_features="modal_lyr_" + modal_layer_name + "2",
        out_feature_class=os.path.join(scenario_gdb, "tmp_nodes"),
        point_location="BOTH_ENDS")
    logger.debug("start:  generate near table 2")
    arcpy.GenerateNearTable_analysis(locations_fc,
                                     os.path.join(scenario_gdb, "tmp_nodes"),
                                     os.path.join(scenario_gdb, "tmp_near_2"),
                                     max_artificial_link_distance_miles,
                                     "LOCATION", "NO_ANGLE", "CLOSEST")

    logger.debug("start:  delete tmp_nodes")
    arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_nodes"))

    logger.debug("start:  start editor")
    edit = arcpy.da.Editor(os.path.join(scenario_gdb))
    edit.startEditing(False, False)
    edit.startOperation()

    icursor = arcpy.da.InsertCursor(
        os.path.join(scenario_gdb, modal_layer_name), [
            'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES', 'LOCATION_ID',
            'LOCATION_ID_NAME'
        ])  # add location_id for setting flow restrictions

    location_id_name_dict = get_location_id_name_dict(the_scenario, logger)
    connected_location_ids = []
    connected_location_id_names = []
    logger.debug("start:  search cursor on tmp_near_2")
    with arcpy.da.SearchCursor(
            os.path.join(scenario_gdb, "tmp_near_2"),
        ["FROM_X", "FROM_Y", "NEAR_X", "NEAR_Y", "NEAR_DIST", "IN_FID"
         ]) as scursor:

        for row in scursor:

            if not row[4] == 0:

                # use the unique objectid (in_fid) from the near to determine
                # if we have an in or an out location.
                # then set the flow restrictions appropriately.

                in_fid = row[5]
                location_id_name = location_id_name_dict[in_fid]
                location_id = location_id_name.split("_")[0]
                connected_location_ids.append(location_id)
                connected_location_id_names.append(location_id_name)

                coordList = []
                coordList.append(arcpy.Point(row[0], row[1]))
                coordList.append(arcpy.Point(row[2], row[3]))
                polyline = arcpy.Polyline(arcpy.Array(coordList))

                len_in_miles = Q_(polyline.length,
                                  "meters").to("miles").magnitude

                # insert artificial link attributes
                icursor.insertRow([
                    polyline, 1, modal_layer_name, len_in_miles, location_id,
                    location_id_name
                ])

            else:
                logger.warning(
                    "Artificial Link code: Ignoring NEAR_FID {} with NEAR_DIST {}"
                    .format(row[0], row[4]))

    del icursor
    logger.debug("start:  stop editing")
    edit.stopOperation()
    edit.stopEditing(True)

    arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near_2"))

    # ALSO SET CONNECTS_X FIELD IN POINT LAYER
    # -----------------------------------------
    logger.debug("start:  connect_x")
    arcpy.AddField_management(os.path.join(scenario_gdb, locations_fc),
                              "connects_" + modal_layer_name, "SHORT")
    arcpy.CalculateField_management(os.path.join(scenario_gdb, locations_fc),
                                    "connects_" + modal_layer_name, 0,
                                    "PYTHON_9.3")

    edit = arcpy.da.Editor(scenario_gdb)
    edit.startEditing(False, False)
    edit.startOperation()
    with arcpy.da.UpdateCursor(
            os.path.join(scenario_gdb, locations_fc),
        ["LOCATION_ID_NAME", "connects_" + modal_layer_name]) as cursor:

        for row in cursor:

            if row[0] in connected_location_id_names:
                row[1] = 1
                cursor.updateRow(row)

    edit.stopOperation()
    edit.stopEditing(True)

    logger.debug("finish: locations_add_links")
Exemple #22
0
output_dir = "D:/LANL/GIS/compileddata/process"

# The NA layer's data will be saved to the workspace specified here
arcpy.env.workspace = os.path.join(output_dir, "process.gdb")
arcpy.env.overwriteOutput = True

gaugelocations = "all_arctic_gage"
neartable = "all_arctic_gage_NearTable"

#Create a Near table to select the closest reaches to the gauge points
arcpy.GenerateNearTable_analysis(
    in_features=gaugelocations,
    near_features=
    "D:/process/FFR_river_network.gdb/RiverNetwork/FFR_river_network_v1_1",
    out_table=neartable,
    search_radius="2000 Meters",
    location="NO_LOCATION",
    angle="NO_ANGLE",
    closest="ALL",
    closest_count="10",
    method="GEODESIC")

# Join table for the reach attribute from Grill
arcpy.JoinField_management(
    in_data=neartable,
    in_field="NEAR_FID",
    join_table=
    "D:/process/FFR_river_network.gdb/RiverNetwork/FFR_river_network_v1_1",
    join_field="OBJECTID",
    fields=
    "REACH_ID;GOID;NOID;NUOID;NDOID;CON_ID;CONTINENT;COUNTRY;BAS_ID;BAS_NAME;LENGTH_KM;VOLUME_TCM;UPLAND_SKM;DIS_AV_CMS;RIV_ORD;ERO_YLD_TON;HYFALL;BB_ID;BB_NAME;BB_LEN_KM;BB_DIS_ORD;BB_VOL_TCM;BB_OCEAN;INC;DOF;DOR;SED;USE;URB;RDD;FLD;CSI;CSI_D;CSI_FF;CSI_FF1;CSI_FF2;CSI_FFID"
Exemple #23
0
nearMultifamily = workspace + "Street_Near_Multifamily"
nearSocialService = workspace + "Street_Near_Social_Service"
nearSocialServiceFreq = workspace + "Street_Near_Social_Service_Freq"
nearSignalReqPhb = workspace + "Street_Near_Signal_Req_PHB"
nearSignalReqPhbFreq = workspace + "Street_Near_Signal_Req_PHB_Freq"
nearStreetlight = workspace + "Street_Near_Streetlight"
nearSignal = workspace + "Street_Near_Signal"
nearPedCrash2 = workspace + "Street_Near_Ped_Crash_200"
nearPedCrash2Freq = workspace + "Street_Near_Ped_Crash_200_Freq"
nearPedCrash4 = workspace + "Street_Near_Ped_Crash_400"
nearPedCrash4Freq = workspace + "Street_Near_Ped_Crash_400_Freq"

# ***LARGE RETAIL***
print("\n" + "Large Retail: Generate Near Table, Frequency, Add Index")
arcpy.GenerateNearTable_analysis(streetSelect, largeRetail, nearLargeRetail,
                                 "100 feet", "NO_LOCATION", "NO_ANGLE", "ALL",
                                 4, "PLANAR")
print("\n" + arcpy.GetMessages())
arcpy.Frequency_analysis(nearLargeRetail, nearLargeRetailFreq, "IN_FID", "")
print("\n" + arcpy.GetMessages())
arcpy.AddIndex_management(nearLargeRetailFreq, ["IN_FID"], "LargeRetailInd",
                          "UNIQUE", "ASCENDING")
print("\n" + arcpy.GetMessages())

# ***SCHOOLS***
print("\n" + "Schools: Generate Near Table, Frequency, Add Index")
arcpy.GenerateNearTable_analysis(streetSelect, school, nearSchool, "100 feet",
                                 "NO_LOCATION", "NO_ANGLE", "ALL", 4, "PLANAR")
print("\n" + arcpy.GetMessages())
arcpy.Frequency_analysis(nearSchool, nearSchoolFreq, "IN_FID", "")
print("\n" + arcpy.GetMessages())
Exemple #24
0
def main(input_fc=None, output_fc=None, closest=False, mode=settings.mode):

    # convert 'closest' string value
    closest_count = ""
    if closest == 'true':
        closest_count = 1

    # does the input fc exist?
    if not arcpy.Exists(input_fc):
        utils.msg("Input, %s, doesn't exist.", mtype='error')
        sys.exit()

    input_fc_mem = 'in_memory/input_fc'
    try:
        utils.msg("Copying features into memory...")
        arcpy.CopyFeatures_management(input_fc, input_fc_mem)
    except Exception as e:
        utils.msg("Unable to copy features into memory.",
                  mtype='error',
                  exception=e)
        sys.exit()

    # add POINT_X and POINT_Y columns
    try:
        arcpy.AddXY_management(input_fc_mem)
    except Exception as e:
        message = "Error creating point coordinate values for {0}".format(
            input_fc)
        utils.msg(message, mtype='error', exception=e)
        sys.exit()

    # get the spatial reference of our input, determine the type
    desc = arcpy.Describe(input_fc_mem)
    sr = desc.spatialReference
    if sr.type not in ['Geographic', 'Projected']:
        utils.msg("This tools only works with geographic or projected data.",
                  mtype='error')
        sys.exit()

    # yes, all this mucking about is necessary to get a row count
    row_count = int(arcpy.GetCount_management(input_fc_mem).getOutput(0))

    if row_count < 500 or closest:
        near_table = 'in_memory/near_table'
        output_fc_mem = 'in_memory/output_fc'
    else:
        utils.msg(
            "Too many features (%i) to load pairwise into memory. Using disk, which decreases performance."
            % row_count)
        near_table = os.path.join(arcpy.env.scratchGDB, 'near_table')
        output_fc_mem = output_fc

    # create a look-up table which maps individual observations to all others.
    # FIXME: do we need to filter this in some way? by group, ...?
    try:
        # generates an output table with IN_FID, NEAR_FID, NEAR_X, NEAR_Y [...]
        utils.msg("Creating near table...")
        arcpy.GenerateNearTable_analysis(input_fc_mem, input_fc_mem, near_table, \
                "", "LOCATION", "NO_ANGLE", "ALL")

        time.sleep(5)
    except Exception as e:
        utils.msg("Error creating near table.", mtype='error', exception=e)
        sys.exit()

    try:
        utils.msg("Joining attributes...")
        in_fields = arcpy.ListFields(input_fc_mem)
        # find the OID field.
        oid_field = [f.name for f in in_fields if f.type == 'OID'][0]

        # join back our 'true' x & y columns to the original data.
        arcpy.JoinField_management(near_table, "IN_FID", input_fc_mem,
                                   oid_field, ['POINT_X', 'POINT_Y'])
        # clean up
        arcpy.Delete_management(input_fc_mem)

        in_fields = arcpy.ListFields(near_table)
        oid_field = [f.name for f in in_fields if f.type == 'OID'][0]

        id_field = 'ID'
        # add an 'ID' field other than the OID so we can copy it over using XYToLine
        arcpy.AddField_management(near_table, id_field, "LONG")
        expression = "!{0}!".format(oid_field)
        arcpy.CalculateField_management(near_table, id_field, expression,
                                        "PYTHON_9.3")

    except Exception as e:
        utils.msg("Error joining data.", mtype='error', exception=e)
        sys.exit()

    # Now compute the lines between these locations.
    try:
        utils.msg("Computing pairwise geodesic lines...")
        arcpy.XYToLine_management(near_table, output_fc_mem, \
                "NEAR_X", "NEAR_Y", "POINT_X", "POINT_Y", "GEODESIC", id_field)

        utils.msg("Remapping output columns...")
        arcpy.JoinField_management(output_fc_mem, id_field, near_table,
                                   id_field, ['IN_FID', 'NEAR_FID'])

        # column modifications necessary
        remap_cols = [
            ('IN_FID', 'Source_ID', '!IN_FID!'),
            ('POINT_X', 'Source_X', '!POINT_X!'),
            ('POINT_Y', 'Source_Y', '!POINT_Y!'),
            ('NEAR_FID', 'Dest_ID', '!NEAR_FID!'),
            ('NEAR_X', 'Dest_X', '!NEAR_X!'),
            ('NEAR_Y', 'Dest_Y', '!NEAR_Y!'),
            (None, 'Distance_in_km', "!Shape.length@kilometers!"
             )  # output distance in kilometers
        ]

        fn = [f.name for f in arcpy.ListFields(output_fc_mem)]
        for (in_name, out_name, expr) in remap_cols:
            # create a new field for storing the coord
            arcpy.AddField_management(output_fc_mem, out_name, "DOUBLE")
            # copy the field over from the original
            arcpy.CalculateField_management(output_fc_mem, out_name, expr,
                                            "PYTHON_9.3")
            # delete the original field
            if in_name in fn:
                arcpy.DeleteField_management(output_fc_mem, in_name)

        # can't delete length; part of the data model which is
        # unfortunate -- it's degrees, and has no linear distance.
        # arcpy.DeleteField_management(output_fc_mem, "Shape_Length")

        # copy the final result back to disk.
        if output_fc_mem != output_fc:
            utils.msg("Writing results to disk...")
            output_fc = os.path.abspath(output_fc)
            arcpy.CopyFeatures_management(output_fc_mem, output_fc)

        utils.msg("Created shortest distance paths successfully: {0}".format(
            output_fc))
    except Exception as e:
        utils.msg("Error creating geodesic lines.", mtype='error', exception=e)
        sys.exit()

    # clean up: remove intermediate steps.
    try:
        for layer in [near_table, output_fc_mem]:
            arcpy.Delete_management(layer)
    except Exception as e:
        utils.msg("Unable to delete temporary layer",
                  mtype='error',
                  exception=e)
        sys.exit()
Exemple #25
0
        else:
            #Create empty list for storing paths of datasets to delete
            toDel = list()

            #Create layer from current feature
            sql = '"' + "ID" + '" = ' + str(focId)
            focLyr = arcpy.MakeFeatureLayer_management(parcels, "focalPar",
                                                       sql)
            focPar = scratch + "\\par" + str(focId)
            arcpy.CopyFeatures_management(focLyr, focPar)
            toDel.append(focPar)

            #Identify nearest parcels via generate near table operation using parameters specified above
            nearPar = scratch + "\\par" + str(focId) + "near"
            arcpy.GenerateNearTable_analysis(focPar, parcels, nearPar, dist,
                                             "NO_LOCATION", "NO_ANGLE", "ALL",
                                             count, "PLANAR")
            toDel.append(nearPar)

            #Create empty list for storing near features that have similar names
            currGroup = list()

            #Iterate through results in the near table, retrieving the near feature's FIDs
            with arcpy.da.SearchCursor(nearPar, 'NEAR_FID') as nearCursor:
                for nearRow in nearCursor:
                    nearId = nearRow[0]

                    #If the current near parcel ID is the same as the current focal parcel ID, pass as this is identifying the focal polygon in the parcel layer
                    if nearId == focId:
                        pass
Exemple #26
0
def three_points(contour, p_contour_low, p_contour_high):
    near_table_low = "near_table_low"
    near_table_high = "near_table_high"
    closest_count = 1
    f_con = "FINAL_vrstevnice"
    fc = "linie_spojnice"
    return_feature = "returned"
    arcpy.CreateFeatureclass_management("/output/db.gdb", f_con, "POLYLINE",
                                        "", "", "", 5514)
    arcpy.AddField_management("/output/db.gdb/" + f_con, "Contour", "SHORT")
    with arcpy.da.SearchCursor(
            contour,
        ["OID@", "SHAPE@", "Contour"]) as row:  # prochazi vsechny vrstevnice
        i = 1
        for line in row:
            array_c = arcpy.Array()
            if not line[
                    1]:  # kontrola prazdneho atributu, musim poresit, co se tady deje
                print("preskoceno line")
                continue

            p_contour = arcpy.FeatureVerticesToPoints_management(
                line[1], "/output/db.gdb/p_contour",
                "ALL")  # jednu vrstevnici prevede na body
            with arcpy.da.SearchCursor(
                    p_contour, ["OID@", "SHAPE@", "SHAPE@X", "SHAPE@Y"
                                ]) as c_row:  # cursor bodu ve vrstevnici
                for point in c_row:
                    array = arcpy.Array()
                    arcpy.GenerateNearTable_analysis(
                        point[1], p_contour_low, near_table_low, "#",
                        "LOCATION", "NO_ANGLE", "ALL",
                        closest_count)  # nejblizsi body u vyskoveho bufferu
                    arcpy.GenerateNearTable_analysis(point[1], p_contour_high,
                                                     near_table_high, "#",
                                                     "LOCATION", "NO_ANGLE",
                                                     "ALL", closest_count)
                    with arcpy.da.SearchCursor(
                            near_table_low,
                        ['NEAR_X', 'NEAR_Y'
                         ]) as n_low_row:  # najde nejblizsi body
                        for p in n_low_row:
                            p1 = arcpy.Point(p[0], p[1])
                            array.append(p1)
                    del n_low_row
                    with arcpy.da.SearchCursor(
                            near_table_high,
                        ['NEAR_X', 'NEAR_Y']) as n_high_row:
                        for p in n_high_row:
                            p1 = arcpy.Point(p[0], p[1])
                            array.append(p1)
                    del n_high_row
                    existed_p = arcpy.Point(point[2], point[3])
                    array.append(existed_p)
                    out_feature_class = "centroid_polygonu"
                    polygon = arcpy.Polygon(array)
                    arcpy.FeatureToPoint_management(
                        polygon, "/output/db.gdb/" + out_feature_class,
                        "CENTROID")
                    with arcpy.da.SearchCursor(
                            "/output/db.gdb/" + out_feature_class,
                        ["OID@", "SHAPE@X", "SHAPE@Y"]) as c_point:
                        for p in c_point:
                            p1 = arcpy.Point(p[1], p[2])
                            array_c.append(p1)
                    del c_point

            del c_row
            if not array_c:  #kontrola prazdneho listu, nebo co se deje tady
                print("preskoceno prazdne array_c")
                continue
            linie_final = arcpy.Polyline(array_c)
            arcpy.FeatureClassToFeatureClass_conversion(
                linie_final, "/output/db.gdb", fc)
            arcpy.AddField_management("/output/db.gdb/" + fc, "Contour",
                                      "SHORT")
            with arcpy.da.UpdateCursor("/output/db.gdb/" + fc,
                                       "Contour") as row_for_one:
                for value in row_for_one:
                    value[0] = row[2]
                    row_for_one.updateRow(value)
            del row_for_one
            del value

            with arcpy.da.SearchCursor("/output/db.gdb/" + fc,
                                       ["SHAPE@", "Contour"]) as row_for_one:
                for geom in row_for_one:
                    cursor = arcpy.da.InsertCursor("/output/db.gdb/" + f_con,
                                                   ["SHAPE@", "Contour"])
                    cursor.insertRow((geom[0], geom[1]))

            del cursor
            del row_for_one
            print(i)
            i = i + 1

    arcpy.CopyFeatures_management("/output/db.gdb/" + f_con,
                                  "/output/db.gdb/" + return_feature)
    del row
    return return_feature
Exemple #27
0
                     str(ncurrentstep) + "/" + str(nstep))
    SplitLine = dS.SLEM(PolygonToLine, DisaggregationStepForWidth,
                        "%ScratchWorkspace%\\SplitLine", ScratchW, "true")

    #/calculation of the valley bottom width
    ncurrentstep += 1
    arcpy.AddMessage("Converting to points - Step " + str(ncurrentstep) + "/" +
                     str(nstep))
    SplitLineToPoints = arcpy.FeatureVerticesToPoints_management(
        SplitLine, "%ScratchWorkspace%\\SplitLineToPoints", "MID")

    ncurrentstep += 1
    arcpy.AddMessage("ProxyTable between Points and Centerline - Step " +
                     str(ncurrentstep) + "/" + str(nstep))
    ProxyTable = arcpy.GenerateNearTable_analysis(SplitLineToPoints,
                                                  Centerline, "ProxyTable", "",
                                                  "LOCATION", "NO_ANGLE", "")

    ncurrentstep += 1
    arcpy.AddMessage("Generating XY layer - Step " + str(ncurrentstep) + "/" +
                     str(nstep))
    SpatialRef = arcpy.Describe(Polygon).spatialReference
    ProxyPtsTEMP = arcpy.MakeXYEventLayer_management("ProxyTable", "NEAR_X",
                                                     "NEAR_Y", "ProxyPtsTEMP",
                                                     SpatialRef, "")

    ncurrentstep += 1
    arcpy.AddMessage("Final point shapefile with width information - Step " +
                     str(ncurrentstep) + "/" + str(nstep))
    WidthPts = arcpy.CopyFeatures_management(ProxyPtsTEMP, Output)
    arcpy.AddField_management(WidthPts, "Width", "DOUBLE", "", "", "", "",
Exemple #28
0
def create_lnk_tbl(corefc, core_pairs, frm_cores):
    """Create link table file and limit based on near table results."""
    # Temporary query layers
    fcore_vw = "fcore_vw"
    tcore_vw = "tcore_vw"

    # No output if near table in gdb need to use dbf instead
    near_tbl = os.path.join(cc_env.scratch_dir, "neartbl.dbf")
    jtocore_fn = cc_env.core_fld[:8] + "_1"  # dbf field length

    link_file = os.path.join(lm_env.DATAPASSDIR, "linkTable_s2.csv")

    link_tbl, srow, srows = None, None, None

    try:
        link_tbl = open(link_file, 'w')
        writer = csv.writer(link_tbl, delimiter=',')
        headings = [
            "# link", "coreId1", "coreId2", "cluster1", "cluster2", "linkType",
            "eucDist", "lcDist", "eucAdj", "cwdAdj"
        ]
        writer.writerow(headings)

        core_list = set()
        no_cores = str(len(frm_cores))
        i = 1

        coreid_fld = arcpy.AddFieldDelimiters(corefc, cc_env.core_fld)
        oid_fld = arcpy.Describe(corefc).oidFieldName

        for core_no, frm_core in enumerate(frm_cores):
            # From cores
            expression = coreid_fld + " = " + frm_core
            arcpy.MakeFeatureLayer_management(corefc, fcore_vw, expression)

            # To cores
            to_cores_lst = [x[1] for x in core_pairs if frm_core == x[0]]
            to_cores = ', '.join(to_cores_lst)
            expression = coreid_fld + " in (" + to_cores + ")"
            arcpy.MakeFeatureLayer_management(corefc, tcore_vw, expression)

            lm_util.gprint("Calculating Euclidean distance/s from Core " +
                           frm_core + " to " + str(len(to_cores_lst)) +
                           " other cores" + " (" + str(core_no + 1) + "/" +
                           no_cores + ")")

            # Generate near table for these core pairings
            arcpy.GenerateNearTable_analysis(fcore_vw, tcore_vw, near_tbl,
                                             cc_env.max_euc_dist,
                                             "NO_LOCATION", "NO_ANGLE", "ALL")

            # Join near table to core table
            arcpy.JoinField_management(near_tbl, "IN_FID", corefc, oid_fld,
                                       cc_env.core_fld)
            arcpy.JoinField_management(near_tbl, "NEAR_FID", corefc, oid_fld,
                                       cc_env.core_fld)

            # Limit pairings based on inputed Euclidean distances
            srow, srows = None, None
            euc_dist_fld = arcpy.AddFieldDelimiters(near_tbl, "NEAR_DIST")
            expression = (euc_dist_fld + " > " + str(cc_env.min_euc_dist))
            srows = arcpy.SearchCursor(near_tbl,
                                       where_clause=expression,
                                       fields=jtocore_fn + "; NEAR_DIST",
                                       sort_fields=jtocore_fn +
                                       " A; NEAR_DIST A")

            # Process near table and output into a link table
            srow = next(srows)
            if srow:
                core_list.add(int(frm_core))
                while srow:
                    to_coreid = srow.getValue(jtocore_fn)
                    dist_value = srow.getValue("NEAR_DIST")
                    writer.writerow([
                        i, frm_core, to_coreid, -1, -1, 1, dist_value, -1, -1,
                        -1
                    ])
                    core_list.add(to_coreid)
                    srow = next(srows)
                    i += 1

    except Exception:
        raise
    finally:
        lm_util.delete_data(near_tbl)
        if link_tbl:
            link_tbl.close()
        if srow:
            del srow
        if srows:
            del srows

    return core_list
update_name = arcpy.GetParameterAsText(2)
polyline_name = arcpy.GetParameterAsText(3)
#Feature class to be updated
#Feature class containing the attribute we want is nearest

arcpy.MakeFeatureLayer_management(update_feature_class,"update_lyr")
arcpy.MakeFeatureLayer_management(polyline_feature_class,"near_lyr")

print("Created Layers: " + strftime("%Y-%m-%d %H:%M:%S"))
#create a temporary table of near results
out_table = currgdb+'\\LOCATIONS_CLOSE'
if arcpy.Exists(out_table):
  arcpy.Delete_management(out_table)
  print("Deleted Near Table: " + strftime("%Y-%m-%d %H:%M:%S"))

arcpy.GenerateNearTable_analysis("update_lyr", "near_lyr", out_table)
  
print("Generated Near Table:" + strftime("%Y-%m-%d %H:%M:%S"))
#create a list of attributes from the near feature class where
#the second field is the attribute we want to know is the nearest
nearFieldsList = ["OBJECTID",polyline_name]
nearDict = {r[0]:r[1] for r in arcpy.da.SearchCursor("near_lyr", nearFieldsList)}

matchFieldsList = ["IN_FID", "NEAR_FID"]
matchDict = {r[0]:nearDict[r[1]] for r in arcpy.da.SearchCursor(out_table, matchFieldsList)}
#write the nearest attribute to the update feature class
#where the second field is the attribute that is being updated with
#the name of the nearest feature
updateFieldsList = ["OBJECTID", update_name]
with arcpy.da.UpdateCursor("update_lyr", updateFieldsList) as updateRows:  
    for updateRow in updateRows:  
Exemple #30
0
        arcpy.AddMessage("Creating breaks points - Step " + str(ncurrentstep) +
                         "/" + str(nstep))
        TEMPinFC = arcpy.CopyFeatures_management(
            InputFC, "%ScratchWorkspace%\\TEMPinFC")
        Intersect = arcpy.Intersect_analysis([TEMPinFC, Centerline],
                                             "%ScratchWorkspace%\\Intersect",
                                             "ONLY_FID", "", "POINT")
        arcpy.AddXY_management(Intersect)
        arcpy.DeleteIdentical_management(Intersect, ["POINT_X", "POINT_Y"])

        ncurrentstep += 1
        arcpy.AddMessage(
            "Generating near table between break points and the input polygon - Step "
            + str(ncurrentstep) + "/" + str(nstep))
        NearTable_TEMP = arcpy.GenerateNearTable_analysis(
            Intersect, [TEMPinFC], "%ScratchWorkspace%\\NearTable_TEMP", "0",
            "NO_LOCATION", "NO_ANGLE", "ALL", "3")
        arcpy.JoinField_management(
            NearTable_TEMP, "NEAR_FID", TEMPinFC, "OBJECTID",
            ["Order_ID", "Rank_UGO", "Rank_DGO", "Distance", Metric])

        NearTable = arcpy.Sort_management(
            NearTable_TEMP, "%ScratchWorkspace%\\NearTable",
            [["IN_FID", "ASCENDING"], ["Distance", "ASCENDING"]])
        arcpy.AddField_management(NearTable, "Ratio", "FLOAT", "", "", "", "",
                                  "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(NearTable, "Type", "TEXT", "", "", "", "",
                                  "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(NearTable, "Up_DGO", "SHORT", "", "", "", "",
                                  "NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(NearTable, "Down_DGO", "SHORT", "", "", "",