def NearAnalysisBudynki(): #coblicza odleglosc miedzy budynkami arcpy.Near_analysis(budynek_P, budynek_P) # powtarza dla wszystkich funkcji, kt?re znajduj? si? w odleg?o?ci 50 cur = arcpy.UpdateCursor(budynek_P, '"NEAR_DIST" < 58') # polowa przekoatnej 90x60 row1 = cur.next() # zwraca nast?pny wiersz wej?ciowy while row1: cur.deleteRow( row1 ) #ten punkt znajduje si? w odleg?o?ci mniej niz 40 od s?siada, wi?c usuwa go arcpy.Near_analysis( budynek_P, budynek_P ) # jeszcze raz oblicza i aktualizuje odleg?oglosci aby nie usunac punktu bliskiego ktory juz nie istenieje del row1, cur # teraz ponownie uruchom t? procedur? w nowym zestawie danych cur = arcpy.UpdateCursor(budynek_P, '"NEAR_DIST" < 58') arcpy.Near_analysis( budynek_P, budynek_P ) # jeszcze raz oblicza i aktualizuje odleg?oglosci aby nie usunac punktu bliskiego ktory juz nie istenieje row1 = cur.next() NearAnalysisBudynki arcpy.FeatureClassToFeatureClass_conversion('Budynek_P_layer', dane_wyjsc, 'Budynek_P')
def GPS_road_analysis(GPS_date): print("Intersect_analysis_ start") for i in range(6): print("input GPSdata:GPS201612%d_%02d" % (GPS_date, i)) inputpath = 'E:/MapMatch/GPS2shp/201612%d/GPS201612%d_%02d.shp' % ( GPS_date, GPS_date, i) outputpath_141 = 'E:/MapMatch/Intersect_analysis/bh_141/201612%d/bh141_201612%d_%02d' % ( GPS_date, GPS_date, i) outputpath_142 = 'E:/MapMatch/Intersect_analysis/bh_142/201612%d/bh142_201612%d_%02d' % ( GPS_date, GPS_date, i) inFeatures_141 = [inputpath, 'buffer_141'] inFeatures_142 = [inputpath, 'buffer_142'] #Intersect_analysis print 'Intersect_analysis_141' arcpy.Intersect_analysis(inFeatures_141, outputpath_141, 'ALL', '#', 'INPUT') print 'Intersect_analysis_142' arcpy.Intersect_analysis(inFeatures_142, outputpath_142, 'ALL', '#', 'INPUT') # Near_analysis crosspath_141 = 'E:/MapMatch/Intersect_analysis/bh_141/201612%d/bh141_201612%d_%02d.shp' % ( GPS_date, GPS_date, i) crosspath_142 = 'E:/MapMatch/Intersect_analysis/bh_142/201612%d/bh142_201612%d_%02d.shp' % ( GPS_date, GPS_date, i) print 'Near_analysis_141' arcpy.Near_analysis(crosspath_141, 'bh141_dissolved', '#', 'LOCATION', 'ANGLE', 'PLANAR') print 'Near_analysis_142' arcpy.Near_analysis(crosspath_142, 'bh142_dissolved', '#', 'LOCATION', 'ANGLE', 'PLANAR') print('Finish Projection:' + " GPS201612%d_%02d" % (GPS_date, i)) del inFeatures_141 del inFeatures_142
def fill_polygon_gaps(points, master_polys, ac_polys, counter): #calculate dist of points from ac polygons arcpy.Near_analysis(points, ac_polys) #copy result into second field arcpy.AddField_management(points, "NEAR_DIST2", "FLOAT") arcpy.CalculateField_management(points, "NEAR_DIST2", "!NEAR_DIST!", "PYTHON_9.3") #calculate dist of points from bankfull polys arcpy.Near_analysis(points, master_polys) #select points that are adj to ac_polys, but not adj to bankfull polys query = "NEAR_DIST2<=5 AND NEAR_DIST>0" lyr = arcpy.MakeFeatureLayer_management(points, "layera", query) #save these points into new feature class copy_features(lyr, env.workspace, naming + "gap_points" + str(counter)) gap_points = os.path.join(env.workspace, naming + "gap_points" + str(counter)) #calculate dist of gap points to other points arcpy.Near_analysis(points, gap_points) #select points that are adj to gap points and not in ac_polys query = "NEAR_DIST<=7.071 AND NEAR_DIST2>0" lyr = arcpy.MakeFeatureLayer_management(points, "layerb", query) #save these points into new feature class copy_features(lyr, env.workspace, naming + "gap_points2" + str(counter)) gap_points = os.path.join(env.workspace, naming + "gap_points2" + str(counter)) #turn these points into polygons gap_polys = os.path.join( env.workspace, naming + "bankfull_polys_with_gaps" + str(counter)) arcpy.AggregatePoints_cartography(gap_points, gap_polys, "7.5 meters") #merge these polygons with the master bankfull polygons filled_polys = os.path.join(env.workspace, naming + "bankfull_beforeagg" + str(counter)) arcpy.Merge_management([gap_polys, master_polys], filled_polys) #aggregate polys final_polys = os.path.join(env.workspace, naming + "bankfull_polys_final" + str(counter)) arcpy.AggregatePolygons_cartography(filled_polys, final_polys, "7.5 Meters") #after aggreagating polygons there will be a bunch of BS little polygons that we don't need #use dissolve tool to eliminate these little guys, but first you must use near tool to get proper #parameters for dissolving. we are going to dissolve by nearest stream so run near on polygons and streams. ## arcpy.Near_analysis(pred_final_polys,ac_polys) ## ## final_polys=os.path.join(env.workspace,naming+"bankfull_polys_final"+str(counter)) ## arcpy.Dissolve_management(pred_final_polys, final_polys,["NEAR_FID", "TAXCODE"]) return final_polys
def distToMkt(poi, pop_polygon, outputLocation): #Convert the AOI to a point that's stored in memory out_src_point = r"in_memory\src_point" aoiCentroid = arcpy.FeatureToPoint_management(poi, out_src_point) #Determine if centroid is in a Projected Coordinate System or a Geographic Coordinate System #This is necessary when executing the Near tool. SR = arcpy.Describe(aoiCentroid).spatialReference if SR.type == "Projected": arcpy.Near_analysis(in_features=aoiCentroid, near_features=pop_polygon, location="LOCATION", method="PLANAR") elif SR.type == "Geographic": arcpy.Near_analysis(in_features=aoiCentroid, near_features=pop_polygon, location="LOCATION", method="GEODESIC") else: arcpy.AddMessage( "Did not recognize projection. Could not calculate distance to market. Reproject Point of Interest to commonly used projection, such as GCS WGS1984." ) return None #Read the first row of the AOI Centroid shapefile. This row specifies the distance to the nearest #urban center with a population greater than 1,000 people per sq km. with arcpy.da.SearchCursor(aoiCentroid, ["NEAR_DIST", "NEAR_X", "NEAR_Y"]) as cursor: for row in cursor: dist = row[0] x_loc = row[1] y_loc = row[2] break #Output distance to nearest urban center as well as the lat and long of the urban center. dist_3dec = float("{0:.3f}".format(dist)) dist_km = dist_3dec / 1000 #Write the output to a text file f = open(os.path.join(outputLocation, "DistanceToUrban.txt"), "w") f.write("DISTANCE TO NEAREST URBAN CENTER \n") f.write( 'Note: An urban center is defined as a location where population density is greater than or equal to 1,000 people per sq km. \n' ) f.write( 'This is the definition used by the OECD. Citation: OECD (2012), Redefining "Urban": A New Way to Measure Metropolitan Areas, OECD Publishing. \n' ) f.write('\nThe distance to the nearest urban center is: ' + str(dist_3dec) + ' meters (' + str(dist_km) + ' km). \n') f.write('The longitude and latitude of this urban center in meters is (' + str(x_loc) + ', ' + str(y_loc) + ').') f.close()
def calculateDistanceFromPointsToPolylines(input_geodatabase, fcPoint, fcPolyline): arcpy.env.workspace = input_geodatabase #Quick exception handling for file type describePoint = arcpy.Describe(fcPoint) describePoly = arcpy.Describe(fcPolyline) if describePoint.shapetype != "Point" or describePoly.shapetype != "Polyline" or describeWork.dataType != "Workspace": print("You did not give me the correct file types.") else: in_features = fcPoint near_features = fcPolyline try: # set local variables in_features = fcPoint near_features = fcPolyline # execute the function, distance value is created in point #feature class as field "NEAR_DIST" in meters arcpy.Near_analysis(in_features, near_features) # get geoprocessing messages print(arcpy.GetMessages()) except arcpy.ExecuteError: print(arcpy.GetMessages(2)) except Exception as err: print(err.args[0])
def add_manuals(): manual_shp_list = [] arcpy.Near_analysis(transfer_manual_shp, node_shp, "", "NO_LOCATION", "NO_ANGLE", "GEODESIC") manual_shp_dictionary = { row.getValue("FID"): [row.getValue("RR1"), row.getValue("RR2"), row.getValue("NEAR_FID")] for row in arcpy.SearchCursor(transfer_manual_shp) } near_fid_to_node_id_dictionary = { row.getValue("FID"): row.getValue("ID") for row in arcpy.SearchCursor(node_shp) } for key, value in manual_shp_dictionary.iteritems(): manual_shp_dictionary[key][2] = near_fid_to_node_id_dictionary[ manual_shp_dictionary[key][2]] for key, value in manual_shp_dictionary.iteritems(): a = [] a.append(key) a = a + value manual_shp_list.append(a) manual_shp_df = pandas.DataFrame(manual_shp_list) manual_shp_df.columns = [["??", "FROM", "TO", "ID"]] manual_shp_df['BIDIR'] = 2 manual_shp_df = manual_shp_df[['ID', 'FROM', 'TO', 'BIDIR']] return manual_shp_df
def Near_Analysis(facilityFeature, searchRadius=500): ''' Conduct near analysis to select positions which have facilities within 500 meters (default) around :param facilityFeature: facility feature class name :return: ''' try: inLayer = "facility_selected" arcpy.Near_analysis("basemap_Fishnet", facilityFeature, searchRadius, "LOCATION") arcpy.MakeFeatureLayer_management("basemap_Fishnet", inLayer) arcpy.CopyFeatures_management(inLayer, "coverage_" + facilityFeature) arcpy.AddField_management("coverage_" + facilityFeature, "Types", "INTEGER") cursor = arcpy.UpdateCursor("coverage_" + facilityFeature) for row in cursor: dist = row.getValue("NEAR_DIST") if dist >= 0: row.setValue("Types", 1) else: row.setValue("Types", 0) cursor.updateRow(row) del row, cursor print("Complete Near Analysis for " + facilityFeature + "!") except: print(arcpy.GetMessages())
def near_function(int_point, intersections): print("Finish near feature") return (arcpy.Near_analysis(in_features=int_point, near_features=intersections, location=False, angle=True, search_radius=31))
def nearvalue(inputFC, inputAnalysis): arcpy.Near_analysis(inputFC, inputAnalysis) arcpy.JoinField_management(inputFC, "NEAR_FID", inputAnalysis, "OBJECTID", "Iterate") arcpy.AddField_management(inputfc, fc, "DOUBLE") arcpy.CalculateField_management(inputfc, fc, "!Iterate!", "PYTHON", "") arcpy.DeleteField_management(inputfc, "Iterate")
def CalcCorridorDistance(pointLayer, workspace): arcpy.AddMessage("\tCalculating distances between structures...") if workspace == '': workspace = arcpy.env.Workspace elif workspace == "in_memory": sumTable = workspace + os.sep + "Core_summary" iTable = workspace + os.sep + "Iteration_summary" else: sumTable = workspace + os.sep + "Core_summary.dbf" iTable = workspace + os.sep + "Iteration_summary.dbf" cLayer = workspace + os.sep + "coresLayer" whereClause = '"NEAR_DIST" <> -1' # Calculate distance between points arcpy.Near_analysis(pointLayer, pointLayer) # Process: Make Feature Layer ... arcpy.MakeFeatureLayer_management(pointLayer, cLayer, "", "", "Input_FID Input_FID VISIBLE NONE") # Process: Select Near Distance <> -1 (distance between a point and itself)... arcpy.SelectLayerByAttribute_management(cLayer, "NEW_SELECTION", whereClause) # Process: Summary Statistics on selected set... arcpy.Statistics_analysis(cLayer, iTable, "NEAR_DIST MEAN") arcpy.Delete_management(cLayer, "") return iTable #send iteration table back for further processing
def Pow_od_lini(): desired_distance1 = 68 temp_points = r'in_memory\points' #Tworzy punkt srodkowy wielok?ta, calculate near distance and angle and join this to polygons arcpy.FeatureToPoint_management( budynek_A, 'temp_points' ) #Tworzy klas? obiekt?w zawieraj?c? punkty wygenerowane z reprezentatywnych lokalizacji obiekt?w wej?ciowych. arcpy.Near_analysis( 'temp_points', droga, "", "NO_LOCATION", "ANGLE", "PLANAR" ) # Oblicza odleg?o?? i k?t mi?dzy obiektami wej?ciowymi a najbli?szymi obiektami w innej warstwie lub klasie obiekt?w. arcpy.MakeFeatureLayer_management( budynek_A, 'polygon_lyr' ) #Tworzy warstw? obiekt?w z wej?ciowej klasy obiekt?w lub pliku warstwy. arcpy.AddJoin_management( 'polygon_lyr', 'OBJECTID', 'temp_points', 'OBJECTID' ) #??czy warstw? z inn? warstw? lub tabel? na podstawie wsp?lnego pola (dodaje do nowej warstwy Budynek A informacje o odleg?osci do najblizszej drogi "NEAR_DIST" z warstwy punktowej ) #arcpy.CopyFeatures_management('polygon_lyr',budynek) #Kopiuje obiekty z wej?ciowej klasy obiekt?w lub warstwy do nowej klasy obiekt?w. arcpy.FeatureClassToFeatureClass_conversion('polygon_lyr', dane_wyjsc, 'Budynek_A') #Move the polygons with arcpy.da.UpdateCursor('Budynek_A', [ 'SHAPE@X', 'SHAPE@Y', 'temp_points_NEAR_DIST', 'temp_points_NEAR_ANGLE' ]) as cursor: for row in cursor: xnew = row[0] - (row[2] - desired_distance1) * math.sin( bearing_to_radians(row[3])) ynew = row[1] - (row[2] - desired_distance1) * math.cos( bearing_to_radians(row[3])) row[0] = xnew row[1] = ynew cursor.updateRow(row)
def task3(): inraster_path = os.path.normcase("D:/NDVI Process/Climate layer/Prcp") inmask_path = os.path.normcase("D:/NDVI Parks/CalNorth/CalNorth Parks") output_path = os.path.normcase( "D:/NDVI Parks/CalNorth/near_dis_prcp_maysep") if os.path.exists(output_path) is False: os.mkdir(output_path) inraster_file_list = glob.glob((os.path.join(inraster_path, "*.shp"))) year_labels = [str(year) for year in range(2000, 2014)] inmask_file_list = glob.glob((os.path.join(inmask_path, "*.shp"))) data_dict = dict() for inmask_file in inmask_file_list: inmask_file_name = os.path.split(inmask_file)[-1][:-4] # park name local_dist_list = list() local_dist_list.append(inmask_file_name) for inraster_file in inraster_file_list: inraster_file_name = os.path.split(inraster_file)[-1] year_list = range(2000, 2014) arcpy.Near_analysis(inmask_file, inraster_file) temp_txt_file = os.path.join(output_path, "dist.txt") arcpy.ExportXYv_stats(inmask_file, "NEAR_DIST", "space", temp_txt_file, "ADD_FIELD_NAMES") with open(temp_txt_file, "rb") as txt_rd: txt_rd.readline() value = txt_rd.readline().strip().split(" ")[2] local_dist_list.append(value) data_dict[inmask_file_name] = local_dist_list output_file_name = "prcp_near_distance.csv" output_file = os.path.join(output_path, output_file_name) with file(output_file, "wb") as output_fd: output_writer = csv.writer(output_fd) output_writer.writerow([' '] + year_labels) for key in sorted(data_dict.keys()): output_writer.writerow(data_dict[key])
def calculateDistanceFromPointsToPolylines(input_geodatabase, fcPoint, fcPolyline): #set workspace to input geodatabase arcpy.env.workspace = input_geodatabase #iterate through list of feature classes fcList = arcpy.ListFeatureClasses() #print each feature class in the feature list for fc in fcList: print(fc) #define variables for the spatial join target_features = fcPolyline join_features = fcPoint #describe the input feature classes desc1 = arcpy.Describe(fcPolyline) desc2 = arcpy.Describe(fcPoint) #ensure that the spatial reference systems are projected the same print(desc1.spatialReference.name, desc2.spatialReference.name) #define variables in_features = fcPoint near_features = fcPolyline search_radius = "#" location = "LOCATION" angle = "NO_ANGLE" method = "GEODESIC" #use near analysis to calculate distance of facilites to nearest bike route arcpy.Near_analysis(in_features, near_features, search_radius, location, angle, method)
def onMouseDownMap(self, x, y, button, shift): # make sure layer exists fc = "./endpoint.shp" if self.first: check(fc, 'endpoint.shp') self.first = False # check if point exists xy = (x, y) if int(arcpy.GetCount_management(fc).getOutput(0)) == 0: cursor = arcpy.da.InsertCursor(fc, ["SHAPE@XY"]) cursor.insertRow([xy]) else: cursor = arcpy.da.UpdateCursor(fc, ["SHAPE@XY"]) for row in cursor: row[0] = xy cursor.updateRow(row) # find nearest road segment to point near_features = layer_combobox.value search_radius = "100 Meters" location = "NO_LOCATION" angle = "NO_ANGLE" arcpy.Near_analysis(fc, near_features, search_radius, location, angle) cursor = arcpy.da.SearchCursor(fc, ["NEAR_FID"]) for row in cursor: if row[0] != -1: id = row[0] else: pythonaddins.MessageBox('Punkt za daleko od drogi', "Error") routing_type_combobox.value = '' routing_type_combobox.refresh() path_button_button.enabled = False return # snap point to nearest segment end exp = "FID = " + str(id) cursor = arcpy.da.SearchCursor( near_features, ["FID", 'START_X', 'START_Y', 'END_X', 'END_Y'], exp) for row in cursor: startx = float(row[1]) starty = float(row[2]) endx = float(row[3]) endy = float(row[4]) l1 = length(x, y, startx, starty) l2 = length(x, y, endx, endy) if l1 < l2: xy = (startx, starty) else: xy = (endx, endy) cursor = arcpy.da.UpdateCursor(fc, ["SHAPE@XY"]) for row in cursor: row[0] = xy cursor.updateRow(row) # refresh view to see changes arcpy.RefreshActiveView() arcpy.RefreshTOC() routing_type_combobox.enabled = True
def calculoNearGeneral( capaOrigenI, capaObjetoC ): # función que asigna a cada uno de los poligonos de la grilla el punto más cercano arcpy.AddMessage(time.strftime("%c") + " " + "Ejecutando Calculo Near...") capaNear = arcpy.Near_analysis(capaObjetoC, capaOrigenI, '#', 'NO_LOCATION', 'NO_ANGLE', 'PLANAR') arcpy.AddMessage("Finaliza Calculo Near") return capaNear
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 further_process_blended(): env.workspace = outBlendedWS env.overwriteOutput = True GISDBASCL = r'S:\LV_Valley_Imagery\2017\SwimmingPool2017\gdb\general_data.gdb\GISDBA_SCL_STREETS' fcs = arcpy.ListFeatureClasses() arcpy.MakeFeatureLayer_management(projectAreaTiles, 'TileClipLayer') for fc in fcs: print 'clipping ' + fc arcpy.MakeFeatureLayer_management(fc, 'lyr') arcpy.AddField_management('lyr', 'YARD', 'TEXT', '', '', '5') arcpy.AddField_management('lyr', 'TILENAME', 'Text', '', '', '8') arcpy.AddField_management('lyr', 'ERROR_TYPE', 'SHORT') arcpy.SelectLayerByAttribute_management( 'TileClipLayer', 'NEW_SELECTION', "BOOKSEC_PT = 'o" + fc[4:] + "'") arcpy.Clip_analysis(fc, 'TileClipLayer', outClippedBlendedWS + '\\' + fc + '_Clip') arcpy.SelectLayerByAttribute_management('TileClipLayer', 'CLEAR_SELECTION') env.workspace = outClippedBlendedWS env.overwriteOutput = True fcs = arcpy.ListFeatureClasses() arcpy.MakeFeatureLayer_management(projectAreaParcels, 'ProjAreaAOXLyr') arcpy.MakeFeatureLayer_management(GISDBASCL, 'GISDBA_SCL_STREETS') for fc in fcs: print "Performing Identity and Near Analysis on " + fc + "_Id" arcpy.Identity_analysis(fc, 'ProjAreaAOXLyr', outClippedBlendedIDWS + '\\' + fc + '_Id', 'ALL', '', 'NO_RELATIONSHIPS') arcpy.Near_analysis(outClippedBlendedIDWS + '\\' + fc + '_Id', 'GISDBA_SCL_STREETS', "300 Feet", "LOCATION", "NO_ANGLE", "PLANAR") env.workspace = outClippedBlendedIDWS env.overwriteOutput = True arcpy.MakeFeatureLayer_management(GISDBASCL, 'GISDBA_SCL_STREETS') fcs = arcpy.ListFeatureClasses() for fc in fcs: print "calculating frequency and stats on " + fc arcpy.MakeFeatureLayer_management(fc, 'lyr') arcpy.AddJoin_management('lyr', "NEAR_FID", 'GISDBA_SCL_STREETS', 'OBJECTID', 'KEEP_ALL') arcpy.Frequency_analysis( 'lyr', outClippedBlendedIDWS + '\\' + fc[:-8] + '_Frequen', '"{}.gridcode;{}.APN"'.format(fc, fc), '"{}.Shape_Area"'.format(fc)) arcpy.Statistics_analysis( outClippedBlendedIDWS + '\\' + fc[:-8] + '_Frequen', outClippedBlendedIDWS + '\\' + fc[:-8] + '_TOTAREA', "FREQUENCY COUNT;" + "{i}_Shape_Area SUM".format(i=fc), "{x}_APN".format(x=fc))
def near_anls(inShp, distToShp, searchRadius=None, joinData=None): """ Find near Features in other Feature Class """ arcpy.Near_analysis(inShp, distToShp, "" if not searchRadius else searchRadius, "NO_LOCATION" if not joinData else "LOCATION", "NO_ANGLE", "PLANAR") return inShp
def calculateDistanceFromPointsToPolylines(input_geodatabase, fcPoint, fcPolyline): if arcpy.Exists(input_geodatabase): # check for valid workspace arcpy.env.workspace = input_geodatabase # sets workspace # Run near analysis tool between the point feature class and polygon feature class arcpy.Near_analysis(fcPoint, fcPolyline, "", "", "", GEODESIC) # This tool automatically creates 2 new fields called NEAR_FID and NEAR_DIST. NEAR_DIST will store the distances of each facility to the closest bike route print(arcpy.GetMessages()) else: print("Invalid workspace")
def get_node(X, Y): points = arcpy.Point() point_geometry = [] points.X = X points.Y = Y point_geometry.append(arcpy.PointGeometry(points)) arcpy.CopyFeatures_management(point_geometry, disk_shp) arcpy.Near_analysis(disk_shp, node_shp) near_fid = [ row.getValue("NEAR_FID") for row in arcpy.SearchCursor(disk_shp) ][0] nodeID = nodeFID_to_nodeID[near_fid] return nodeID
def calculateNearFP(inShpT0, inShpT1, newCities): #Add fields to cities shapefile for use in calculating nearest footprints inFields = {f.name for f in arcpy.ListFields(newCities)} if not t0FID in inFields: tryAddField(newCities, t0FID, "LONG") tryAddField(newCities, t1FID, "LONG") tryAddField(newCities, t0Dist, "FLOAT") tryAddField(newCities, t1Dist, "FLOAT") #For each feature in the input cities, calculate distance to the nearest footprint and rename the field arcpy.Near_analysis(newCities, inShpT0) arcpy.CalculateField_management(newCities, t0FID, "!NEAR_FID!", "PYTHON_9.3") arcpy.CalculateField_management(newCities, t0Dist, "!NEAR_DIST!", "PYTHON_9.3") arcpy.Near_analysis(newCities, inShpT1) arcpy.CalculateField_management(newCities, t1FID, "!NEAR_FID!", "PYTHON_9.3") arcpy.CalculateField_management(newCities, t1Dist, "!NEAR_DIST!", "PYTHON_9.3") inShpT0 = None inShpT1 = None
def find_extraneous_polys(polygons, near_ref, counter, prev_adj_polygon_count): #calculate distance of polygons to current wcs arcpy.Near_analysis(polygons, near_ref) #copy near dist values into another field so values can be saved when near tool is run a second time arcpy.AddField_management(polygons, "NEAR_DIST" + str(counter) + str(wcs_name), "FLOAT") arcpy.CalculateField_management(polygons, "NEAR_DIST" + str(counter) + str(wcs_name), "!NEAR_DIST!", "PYTHON_9.3") extra = "(NEAR_DIST1" + str(wcs_name) + "<5 AND NEAR_DIST1" + str( wcs_name) + ">=0" for i in range(counter): if i > 0: extra += " OR NEAR_DIST" + str(i + 1) + str(wcs_name) + "=0" #give a bit more ditance leeway on the first run because the pump point #may not be placed exactly where it should be query = extra + ") AND gridcode<5" #isolate polygons that are adjacent to wcs iso_polys = arcpy.MakeFeatureLayer_management(polygons, "iso_polys", query, "", "Habitat") arcpy.CopyFeatures_management( iso_polys, os.path.join( env.workspace, str(pool_name) + "_wcs" + str(wcs_name) + "_gauge" + replace + "_isoPolys" + str(counter))) counter += 1 #counter number of polys identifed as adj adj_polygon_count = arcpy.management.GetCount(iso_polys)[0] #check to see if this is the same as previous number of polys in adj polys feature class. #if it is the same it means no more new adj polys are out there to be found and we can #end the recursion process if int(adj_polygon_count) == int(prev_adj_polygon_count): return counter else: return find_extraneous_polys(polygons, iso_polys, counter, adj_polygon_count)
def snap_junction_points(from_line_lyr, to_line_lyr, search_distance): """ Shifts junction points (i.e. tributary confluences) in the 'From' network to same coordinates of junction points in the 'To' network, found within a user-specified search distance. :param from_line_lyr: polyline layer representing 'From' stream network :param to_line_lyr: polyline layer representing 'To' stream network :param search_distance: buffer distance around each 'To' junction point, in meters :return: line feature class """ tempWorkspace = "in_memory" arcpy.AddMessage("GNAT TLA: snapping junction points in 'From' network to 'To' network") snapped_from_line = gis_tools.newGISDataset(tempWorkspace, "snapped_from_line") arcpy.CopyFeatures_management(from_line_lyr, snapped_from_line) snap_line_lyr = gis_tools.newGISDataset("Layer", "snap_line_lyr") arcpy.MakeFeatureLayer_management(snapped_from_line, snap_line_lyr) list_field_objects = arcpy.ListFields(snap_line_lyr) list_from_fields = [f.name for f in list_field_objects if f.type != "OID" and f.type != "Geometry"] # Plot junction points for 'From' and 'To' stream networks from_junction_pnts = plot_junction_points(snap_line_lyr, "from") to_junction_pnts = plot_junction_points(to_line_lyr, "to") lyr_from_junc_pnts = gis_tools.newGISDataset("Layer", "lyr_from_junc_pnts") arcpy.MakeFeatureLayer_management(from_junction_pnts, lyr_from_junc_pnts) from_line_oidfield = arcpy.Describe(snap_line_lyr).OIDFieldName from_vrtx = gis_tools.newGISDataset(tempWorkspace, "from_vrtx") arcpy.FeatureVerticesToPoints_management(snap_line_lyr, from_vrtx, point_location="ALL") arcpy.AddXY_management(from_vrtx) from_vrtx_lyr = gis_tools.newGISDataset("Layer", "from_vrtx_lyr") arcpy.MakeFeatureLayer_management(from_vrtx, from_vrtx_lyr) arcpy.Near_analysis(from_vrtx_lyr, to_junction_pnts, search_distance, "LOCATION") arcpy.SelectLayerByLocation_management(from_vrtx_lyr, "INTERSECT", lyr_from_junc_pnts, "#", "NEW_SELECTION") update_xy_coord(from_vrtx_lyr) arcpy.MakeXYEventLayer_management(from_vrtx_lyr, "POINT_X", "POINT_Y", "xy_events", from_vrtx_lyr) xy_events_pnt = gis_tools.newGISDataset(tempWorkspace, "xy_events_pnt") arcpy.CopyFeatures_management("xy_events", xy_events_pnt) arcpy.MakeFeatureLayer_management(xy_events_pnt, "xy_events_lyr") xy_line = gis_tools.newGISDataset(tempWorkspace, "xy_line") arcpy.PointsToLine_management("xy_events_lyr", xy_line, "ORIG_FID") arcpy.JoinField_management(xy_line, "ORIG_FID", snap_line_lyr, from_line_oidfield, list_from_fields) arcpy.DeleteFeatures_management(snap_line_lyr) arcpy.Append_management(xy_line, snap_line_lyr, "NO_TEST") return snap_line_lyr
def update_nearest_node_dictionary(): arcpy.Near_analysis(fips_shp, node_shp, "", "NO_LOCATION", "NO_ANGLE", "GEODESIC") fips_to_near_fid = { row.getValue("FIPS"): row.getValue("NEAR_FID") for row in arcpy.SearchCursor(fips_shp) } near_fid_to_node_id = { row.getValue("FID"): row.getValue("ID") for row in arcpy.SearchCursor(node_shp) } fips_nearnodeid_dictionary = { x: near_fid_to_node_id[y] for x, y in fips_to_near_fid.iteritems() } return fips_nearnodeid_dictionary
def GPS_road_analysis(buffer_file, dissolved_file, shp_path, output_path): for shpfile in os.listdir(shp_path): if os.path.splitext(shpfile)[1] == '.shp': # 过滤其他后缀文件 # print('processing ' + shpfile) inputfile = shp_path + shpfile outputfile = output_path + shpfile inFeatures = [inputfile, buffer_file] # Interscetion arcpy.Intersect_analysis(inFeatures, outputfile, 'ALL', '#', 'INPUT') # Near_analysis # arcpy.Near_analysis(outputfile, dissolved_file, '#', 'LOCATION', 'ANGLE', 'PLANAR') arcpy.Near_analysis(outputfile, dissolved_file, '#', 'LOCATION', 'NO_ANGLE', 'PLANAR') # 感觉这个移动的角度没啥用 print('Finish Analysis Projection:' + shpfile) del inFeatures
def snap_points(points, lines, distance): """ Ogi's updated snap_points function. """ points = arcpy.Near_analysis(points, lines, str(distance), "LOCATION") # Create an update cursor for the points Feature Class # making sure that the NEAR_X and NEAR_Y fields are included # in the return data with arcpy.da.UpdateCursor(points, ["NEAR_X", "NEAR_Y", "SHAPE@XY"]) as cursor: for row in cursor: x, y, shape_xy = row shape_xy = (x, y) cursor.updateRow([x, y, shape_xy]) return(points)
def find_nearest_stations(config_json): # This function finds the nearest bikeshare stations to the travel points, # creates buffers around the stations, intersects the buffers with the # near stations, and exports them to a new layer # set directory and path for network geodatabase config_data = read_config_json(config_json) project_dir = config_data['directories']['project_dir'] gdb_dir = project_dir + r'/Data' gdb_name = r'Bikeshare_GDB' gdb_path = gdb_dir + r'/' + gdb_name + r'.gdb' feature_dataset_name = 'Bikeshare' gdb_feature_path = gdb_path + r'/' + feature_dataset_name # set arcpy workspace arcpy.env.workspace = gdb_path # find the nearest stations arcpy.Near_analysis( (gdb_feature_path + r'/Travel_Points'), # input feature (gdb_feature_path + r'/Bike_Stations'), # near feature "", # search radius "NO_LOCATION", # write near location to near feature "NO_ANGLE", # find angle to near feature "PLANAR" # method ) # buffer the travel points to the nearest stations arcpy.Buffer_analysis( (gdb_feature_path + r'/Travel_Points'), # input feature (gdb_feature_path + r'/Travel_Points_Buffer'), # output buffer feature "NEAR_DIST", # buffer distance field "FULL", # line side "ROUND", # line end type "NONE", # dissolve type "", # dissolve field "PLANAR" # method ) # create new layer with nearest stations arcpy.Intersect_analysis( "Bike_Stations #;Travel_Points_Buffer #", # input path (gdb_feature_path + r'/Bike_Stations_Intersect'), # output path ) print('Nearest Stations and Buffers Added!')
def get_nearest_onode_with_orr(origin_fips, origin_rr): global fips_orr_to_node_odist_df # if present in dataframe, return yes_or_no = get_if_available(origin_fips, origin_rr) if yes_or_no != 0: return yes_or_no arcpy.SelectLayerByAttribute_management(link_shpf, "NEW_SELECTION", get_where_clause(origin_rr)) arcpy.FeatureVerticesToPoints_management(link_shpf, "in_memory/p1", "BOTH_ENDS") arcpy.MakeFeatureLayer_management("in_memory/p1", "p1") arcpy.SelectLayerByAttribute_management(fips_shpf, "NEW_SELECTION", """ "FIPS" = %d""" % origin_fips) # select the FIPS arcpy.Near_analysis(fips_shpf, "p1", "", "NO_LOCATION", "NO_ANGLE", "GEODESIC") dumm = { row.getValue("NEAR_FID"): row.getValue("NEAR_DIST") for row in arcpy.SearchCursor(fips_shpf) } # gets the FID of th nearest Node arcpy.SelectLayerByAttribute_management("p1", "NEW_SELECTION", """ "FID" = %d""" % dumm.keys()[0]) arcpy.SpatialJoin_analysis("p1", node_shpf, "in_memory/p2", "", "", "", "CLOSEST", "", "") # ID for nodes gets changed to ID_1 automatically origin_node_id = [ row.getValue("ID_1") for row in arcpy.SearchCursor("in_memory/p2") ][0] fips_to_node_id_snap_distance = dumm.values( )[0] / 1609.34 # converting meters to miles fips_orr_to_node_odist_df = fips_orr_to_node_odist_df.append( { "FIPS": origin_fips, "RR": origin_rr, "NODE": origin_node_id, "DIST": fips_to_node_id_snap_distance }, ignore_index=True) global count count = count + 1 if count % 10 == 0: # every 10 calls envokes saving action fips_orr_to_node_odist_df[['FIPS', 'RR', 'NODE', 'DIST']].to_csv(fips_to_rr_csv) print("Saved") return [origin_node_id, fips_to_node_id_snap_distance]
def get_near_node_dict(nodelayer): arcpy.Project_management(nodelayer, temp1, sr_p) fid_id_dict = { row.getValue("FID"): row.getValue("_ID_") for row in arcpy.SearchCursor(temp1) } arcpy.Near_analysis(temp1, temp1) near_node_dict = { row.getValue("_ID_"): [row.getValue("NEAR_Fid"), row.getValue("NEAR_DIST") / 1609.34] for row in arcpy.SearchCursor(temp1) } near_node_dict = { x: [fid_id_dict[y[0]], y[1]] for x, y in near_node_dict.iteritems() } return near_node_dict