<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: NameError: Global Name 'wuh_oh' is not defined in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244387#M66363</link>
    <description>&lt;P&gt;I just noticed a typo: the global is defined as "wu_oh", not "wuh_oh". I didn't catch it earlier, sorry.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Dec 2022 20:30:53 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2022-12-29T20:30:53Z</dc:date>
    <item>
      <title>NameError: Global Name 'wuh_oh' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244342#M66357</link>
      <description>&lt;P&gt;&lt;STRONG&gt;sort_links1.py&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;import geopandas as gpd&lt;BR /&gt;#import arcpy&lt;BR /&gt;import mapping_module1 as MM&lt;BR /&gt;#from arcpy.display import display&lt;/P&gt;&lt;P&gt;gdf_corr = gpd.read_file('G:/Jason Ma/2021Q1 corridors/Toronto1.shp')&lt;BR /&gt;corridor_name = 'Toronto1'&lt;/P&gt;&lt;P&gt;gdf_corridor_sorted = MM.Order_the_Links(corridor_name,gdf_corr)&lt;BR /&gt;display(gdf_corridor_sorted)&lt;/P&gt;&lt;P&gt;gdf_corridor_sorted.to_file('G:/Jason Ma/2021Q1 corridors/Toronto1_done.shp')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;mapping_module1.py&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;# -*- coding: utf-8 -*-&lt;BR /&gt;"""&lt;BR /&gt;Created on Thu Sep 5 09:35:39 2019&lt;/P&gt;&lt;P&gt;@author: SALVAIJ&lt;BR /&gt;"""&lt;/P&gt;&lt;P&gt;shp_2018Q3_path = 'N:/HERE_GIS_CANADA/2018Q3/2018Q3 corridors/ALL.shp'&lt;BR /&gt;shp_2018Q3_AADT_path = 'N:/HERE_GIS_CANADA/2018Q3/2018Q3 corridors/ALL_AADT.shp'&lt;BR /&gt;global wu_oh&lt;BR /&gt;attributionstring = 'Data from HERE Technologies. Basemap by OpenStreetMaps.'&lt;/P&gt;&lt;P&gt;zoom_lims = [10000.,&lt;BR /&gt;25000.,&lt;BR /&gt;50000.,&lt;BR /&gt;90000.,&lt;BR /&gt;]&lt;/P&gt;&lt;P&gt;fraction_shift = 0.01&lt;BR /&gt;zoom_shift = 0&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def calculate_map_extent(gdf_city):&lt;BR /&gt;import shapely.ops&lt;BR /&gt;&lt;BR /&gt;bounds = shapely.ops.cascaded_union(gdf_city.geometry).bounds&lt;BR /&gt;dx = bounds[2]-bounds[0]&lt;BR /&gt;dy = bounds[3]-bounds[1]&lt;BR /&gt;#map_extent = max([dx,dy,zoom_lims[0]])&lt;BR /&gt;map_extent = max([dx,dy])&lt;/P&gt;&lt;P&gt;return map_extent&lt;/P&gt;&lt;P&gt;def calculate_latitude(gdf_city):&lt;BR /&gt;import shapely.ops&lt;BR /&gt;&lt;BR /&gt;bounds = shapely.ops.cascaded_union(gdf_city.to_crs(epsg=4326).geometry).bounds&lt;BR /&gt;&lt;BR /&gt;latitude = (bounds[3]+bounds[1])/2&lt;BR /&gt;&lt;BR /&gt;return latitude&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def calculate_zoom(map_extent):&lt;BR /&gt;if map_extent &amp;lt;= zoom_lims[0]:&lt;BR /&gt;zoom=14&lt;BR /&gt;elif zoom_lims[0] &amp;lt; map_extent &amp;lt;= zoom_lims[1]:&lt;BR /&gt;zoom=13&lt;BR /&gt;elif zoom_lims[1] &amp;lt; map_extent &amp;lt;= zoom_lims[2]:&lt;BR /&gt;zoom=12&lt;BR /&gt;elif zoom_lims[2] &amp;lt; map_extent &amp;lt;= zoom_lims[3]:&lt;BR /&gt;zoom=11&lt;BR /&gt;elif zoom_lims[3] &amp;lt; map_extent:&lt;BR /&gt;zoom=10&lt;BR /&gt;&lt;BR /&gt;return zoom+zoom_shift&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;#zoom_lon = np.ceil(np.log2(360 * 2.0 / lon_length))&lt;BR /&gt;#zoom_lat = np.ceil(np.log2(360 * 2.0 / lat_length))&lt;BR /&gt;#zoom = np.max([zoom_lon, zoom_lat])&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def part_roads_corridor(corridor,df_corr,map_extent):&lt;BR /&gt;import shapely.geometry, shapely.affinity&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;offset_scale=map_extent*fraction_shift&lt;BR /&gt;&lt;BR /&gt;start_end = df_corr.sort_values(by='POS_ALONG').iloc[[0,-1],:].reset_index(drop=True)&lt;/P&gt;&lt;P&gt;start_point = start_end.loc[0,'geometry'].centroid&lt;BR /&gt;end_point = start_end.loc[1,'geometry'].centroid&lt;/P&gt;&lt;P&gt;d = end_point.distance(start_point)&lt;/P&gt;&lt;P&gt;unit_vec = shapely.geometry.LineString( [(0,0),&lt;BR /&gt;((end_point.x-start_point.x)/d,&lt;BR /&gt;(end_point.y-start_point.y)/d)])&lt;/P&gt;&lt;P&gt;theta = -90&lt;BR /&gt;&lt;BR /&gt;offset_vec = shapely.affinity.rotate(unit_vec, theta, origin=(0,0))&lt;BR /&gt;offset_x,offset_y = list(offset_vec.coords[1])&lt;BR /&gt;offset_x*=offset_scale&lt;BR /&gt;offset_y*=offset_scale&lt;BR /&gt;&lt;BR /&gt;def translate_row(row):&lt;BR /&gt;geometry = row.geometry&lt;BR /&gt;translated = shapely.affinity.translate(geometry,xoff=offset_x, yoff=offset_y, zoff=0.0)&lt;BR /&gt;return translated&lt;BR /&gt;&lt;BR /&gt;shifted_geometry = df_corr.apply(translate_row,axis=1)&lt;BR /&gt;return shifted_geometry&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;def add_corr_num(ax,gdf_corr,map_extent,corr_num,legend_fontsize):&lt;BR /&gt;import shapely.geometry&lt;BR /&gt;import matplotlib.patches as mpatches&lt;BR /&gt;from numpy import sqrt&lt;BR /&gt;offset_scale=map_extent*fraction_shift*4&lt;BR /&gt;N = len(gdf_corr)&lt;BR /&gt;midway = int(N/2)&lt;BR /&gt;&lt;BR /&gt;mid_start = gdf_corr.sort_values(by='POS_ALONG').iloc[ midway].geometry.centroid&lt;BR /&gt;mid_end = gdf_corr.sort_values(by='POS_ALONG').iloc[midway+1].geometry.centroid&lt;/P&gt;&lt;P&gt;mid_start = shapely.geometry.Point(mid_start)&lt;BR /&gt;mid_end = shapely.geometry.Point(mid_end)&lt;/P&gt;&lt;P&gt;d = mid_end.distance(mid_start)&lt;BR /&gt;unit_vec_x,unit_vec_y = (mid_end.x-mid_start.x)/d, (mid_end.y-mid_start.y)/d&lt;BR /&gt;unit_vec = shapely.geometry.LineString( [(0,0),(unit_vec_x,unit_vec_y)])&lt;/P&gt;&lt;P&gt;theta = -90&lt;BR /&gt;offset_vec = shapely.affinity.rotate(unit_vec, theta, origin=(0,0))&lt;BR /&gt;offset_x,offset_y = list(offset_vec.coords[1])&lt;BR /&gt;offset_x*=offset_scale&lt;BR /&gt;offset_y*=offset_scale&lt;/P&gt;&lt;P&gt;arrow_base_x,arrow_base_y = mid_start.x+offset_x,mid_start.y+offset_y&lt;BR /&gt;arrow_dx,arrow_dy = offset_scale*unit_vec_x,offset_scale*unit_vec_y&lt;/P&gt;&lt;P&gt;r = sqrt(arrow_dx**2 + arrow_dy**2)/2&lt;BR /&gt;circle = mpatches.Circle((arrow_base_x,arrow_base_y),&lt;BR /&gt;r,&lt;BR /&gt;facecolor='white',&lt;BR /&gt;edgecolor='black',&lt;BR /&gt;linewidth=0.5,&lt;BR /&gt;)&lt;BR /&gt;ax.add_artist(circle)&lt;/P&gt;&lt;P&gt;ax.text(arrow_base_x,arrow_base_y,&lt;BR /&gt;corr_num,&lt;BR /&gt;verticalalignment='center',&lt;BR /&gt;horizontalalignment='center',&lt;BR /&gt;fontsize=legend_fontsize-1&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def add_direction_arrow(ax,gdf_corr,map_extent,corridor):&lt;BR /&gt;import shapely.geometry, shapely.affinity&lt;BR /&gt;&lt;BR /&gt;problem_corridors = ['Toronto1_E',&lt;BR /&gt;#'Toronto1_W',&lt;BR /&gt;'Toronto3_E',&lt;BR /&gt;#'Toronto3_W',&lt;BR /&gt;'Toronto4_N',&lt;BR /&gt;#'Toronto4_S',&lt;BR /&gt;]&lt;BR /&gt;&lt;BR /&gt;offset_scale=map_extent*fraction_shift*4&lt;BR /&gt;N = len(gdf_corr)&lt;BR /&gt;midway = int(N/2)&lt;BR /&gt;&lt;BR /&gt;mid_start = gdf_corr.sort_values(by='POS_ALONG').iloc[ midway].geometry.centroid&lt;BR /&gt;mid_end = gdf_corr.sort_values(by='POS_ALONG').iloc[midway+1].geometry.centroid&lt;BR /&gt;&lt;BR /&gt;#mid_start,mid_end = midrows.iloc[0].centroid,midrows.iloc[1].centroid&lt;/P&gt;&lt;P&gt;mid_start = shapely.geometry.Point(mid_start)&lt;BR /&gt;mid_end = shapely.geometry.Point(mid_end)&lt;/P&gt;&lt;P&gt;d = mid_end.distance(mid_start)&lt;BR /&gt;unit_vec_x,unit_vec_y = (mid_end.x-mid_start.x)/d, (mid_end.y-mid_start.y)/d&lt;BR /&gt;unit_vec = shapely.geometry.LineString( [(0,0),(unit_vec_x,unit_vec_y)])&lt;/P&gt;&lt;P&gt;if any([corridor==problem_corridor for problem_corridor in problem_corridors]):&lt;BR /&gt;theta = +90&lt;BR /&gt;else:&lt;BR /&gt;theta = -90&lt;/P&gt;&lt;P&gt;offset_vec = shapely.affinity.rotate(unit_vec, theta, origin=(0,0))&lt;BR /&gt;offset_x,offset_y = list(offset_vec.coords[1])&lt;BR /&gt;offset_x*=offset_scale&lt;BR /&gt;offset_y*=offset_scale&lt;/P&gt;&lt;P&gt;arrow_base_x,arrow_base_y = mid_start.x+offset_x,mid_start.y+offset_y&lt;BR /&gt;arrow_dx,arrow_dy = offset_scale*unit_vec_x,offset_scale*unit_vec_y&lt;/P&gt;&lt;P&gt;ax.arrow(arrow_base_x,arrow_base_y, arrow_dx,arrow_dy,&lt;BR /&gt;width=offset_scale*.1,&lt;BR /&gt;fc='k', ec='k')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def add_basemap(ax_or_axes,&lt;BR /&gt;map_extent,&lt;BR /&gt;latitude,&lt;BR /&gt;zoom='auto',&lt;BR /&gt;url='&lt;A href="http://a.tile.openstreetmap.org/{z}/{x}/{y}.png" target="_blank"&gt;http://a.tile.openstreetmap.org/{z}/{x}/{y}.png&lt;/A&gt;',&lt;BR /&gt;# another good one is '&lt;A href="http://tile.stamen.com/toner-lite/{z}/{x}/{y}.png" target="_blank"&gt;http://tile.stamen.com/toner-lite/{z}/{x}/{y}.png&lt;/A&gt;'&lt;BR /&gt;alpha=1,&lt;BR /&gt;aspect='auto',&lt;BR /&gt;scalebar=True,&lt;BR /&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;from contextily import bounds2img&lt;BR /&gt;&lt;BR /&gt;if type(ax_or_axes)==list:&lt;BR /&gt;ax=ax_or_axes[0]&lt;BR /&gt;axes = ax_or_axes&lt;BR /&gt;else:&lt;BR /&gt;ax=ax_or_axes&lt;BR /&gt;&lt;BR /&gt;xmin, xmax, ymin, ymax = ax.axis()&lt;BR /&gt;&lt;BR /&gt;centre_loc_x,centre_loc_y=(xmax+xmin)/2.,(ymax+ymin)/2.&lt;BR /&gt;&lt;BR /&gt;floored_map_extent = map_extent = max([map_extent,zoom_lims[0]])&lt;BR /&gt;&lt;BR /&gt;if aspect == 'square':&lt;BR /&gt;xmin,xmax = centre_loc_x - floored_map_extent/2.,centre_loc_x + floored_map_extent/2.&lt;BR /&gt;ymin,ymax = centre_loc_y - floored_map_extent/2.,centre_loc_y + floored_map_extent/2.&lt;BR /&gt;elif aspect == 'auto':&lt;BR /&gt;pass&lt;BR /&gt;else:&lt;BR /&gt;print('aspect should be "square" or "auto". Using "auto".')&lt;BR /&gt;&lt;BR /&gt;if zoom=='auto':&lt;BR /&gt;zoom = calculate_zoom(floored_map_extent)&lt;BR /&gt;else:&lt;BR /&gt;pass&lt;BR /&gt;&lt;BR /&gt;basemap, extent = bounds2img(xmin, ymin, xmax, ymax, zoom=zoom, url=url)&lt;/P&gt;&lt;P&gt;if type(ax_or_axes)==list:&lt;BR /&gt;for ax in axes:&lt;BR /&gt;ax.imshow(basemap, extent=extent, interpolation='bilinear',alpha=alpha)&lt;BR /&gt;&lt;BR /&gt;# restore original x/y limits&lt;BR /&gt;ax.axis((xmin, xmax, ymin, ymax))&lt;BR /&gt;else:&lt;BR /&gt;ax.imshow(basemap, extent=extent, interpolation='bilinear',alpha=alpha)&lt;BR /&gt;&lt;BR /&gt;# restore original x/y limits&lt;BR /&gt;ax.axis((xmin, xmax, ymin, ymax))&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def add_scalebar(ax,latitude):&lt;BR /&gt;from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar&lt;BR /&gt;from numpy import cos,pi&lt;BR /&gt;&lt;BR /&gt;xmin, xmax, ymin, ymax = ax.axis()&lt;BR /&gt;&lt;BR /&gt;dx= xmax - xmin&lt;BR /&gt;&lt;BR /&gt;ground_meters = dx*cos(latitude*pi/180.)&lt;BR /&gt;&lt;BR /&gt;ground_kms = ground_meters/1000&lt;BR /&gt;&lt;BR /&gt;bar_kms = max([1, ground_kms//7])&lt;BR /&gt;&lt;BR /&gt;bar_length = bar_kms*1000/cos(latitude*pi/180.)&lt;BR /&gt;&lt;BR /&gt;#print(ground_meters,ground_kms,bar_length)&lt;BR /&gt;&lt;BR /&gt;bar_height = 100&lt;BR /&gt;&lt;BR /&gt;bar = AnchoredSizeBar(ax.transData, #transform&lt;BR /&gt;bar_length, #Horizontal length of the size bar, given in coordinates of transform.&lt;BR /&gt;'%d km'%(bar_kms), #Label to display.&lt;BR /&gt;4,#location&lt;BR /&gt;pad=0.5,#Padding around the label and size bar, in fraction of the font size. Defaults to 0.1.&lt;BR /&gt;sep=5, #Seperation between the label and the size bar, in points. Defaults to 2.&lt;BR /&gt;borderpad=0.5,#Border padding, in fraction of the font size. Defaults to 0.1.&lt;BR /&gt;frameon=True,#If True, draw a box around the horizontal bar and label. Defaults to True.&lt;BR /&gt;size_vertical=bar_length/13,#Vertical length of the size bar, given in coordinates of transform. Defaults to 0.&lt;BR /&gt;#color='black',#Defaults to black&lt;BR /&gt;#fontproperties=fontprops, #use FontManager&lt;BR /&gt;)&lt;BR /&gt;ax.add_artist(bar)&lt;BR /&gt;''' Locations:&lt;BR /&gt;'upper right' : 1,&lt;BR /&gt;'upper left' : 2,&lt;BR /&gt;'lower left' : 3,&lt;BR /&gt;'lower right' : 4,&lt;BR /&gt;'right' : 5,&lt;BR /&gt;'center left' : 6,&lt;BR /&gt;'center right' : 7,&lt;BR /&gt;'lower center' : 8,&lt;BR /&gt;'upper center' : 9,&lt;BR /&gt;'center' : 10&lt;BR /&gt;'''&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def add_cbar(vmin,vmax,cmap,fig,ax):&lt;BR /&gt;from mpl_toolkits.axes_grid1 import make_axes_locatable&lt;BR /&gt;from pylab import cm, Normalize&lt;BR /&gt;divider = make_axes_locatable(ax)&lt;BR /&gt;cax = divider.append_axes("right", size="5%", pad=0.1)&lt;BR /&gt;sm = cm.ScalarMappable(cmap=cmap, norm=Normalize(vmin=vmin, vmax=vmax))&lt;BR /&gt;# fake up the array of the scalar mappable. Urgh...&lt;BR /&gt;sm._A = []&lt;BR /&gt;cbar = fig.colorbar(sm, cax=cax)&lt;BR /&gt;return cbar,cax&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;def other_node_type(node_type):&lt;BR /&gt;if node_type == 'REF_IN_ID':&lt;BR /&gt;return 'NREF_IN_ID'&lt;BR /&gt;elif node_type == 'NREF_IN_ID':&lt;BR /&gt;return 'REF_IN_ID'&lt;BR /&gt;else:&lt;BR /&gt;print ('%s isnt a valid node type'%(node_type))&lt;BR /&gt;raise ValueError('argument node_type must be either "REF_IN_ID" or "NREF_IN_ID". Instead receieved %s'%(node_type))&lt;BR /&gt;&lt;BR /&gt;def pop_row(df,index):&lt;BR /&gt;#MAKES THE CHANGE TO df IN PLACE AND RETURNS THE REMOVED ROW&lt;BR /&gt;out_row = df.loc[index,:]&lt;BR /&gt;df_row_removed = df.loc[df.index.difference([index]),:]&lt;BR /&gt;return df_row_removed,out_row&lt;/P&gt;&lt;P&gt;def Order_the_Links(corridor_name,corr_df):&lt;BR /&gt;import pandas as pd&lt;BR /&gt;corridor_direction = corridor_name[-1]&lt;BR /&gt;&lt;BR /&gt;node_types = ['REF_IN_ID', 'NREF_IN_ID']&lt;BR /&gt;&lt;BR /&gt;df_corridor_unsorted = corr_df.copy()&lt;BR /&gt;df_corridor_sorted = pd.DataFrame(columns = corr_df.columns.to_list()) #initialize the empty df, to which rows will be added&lt;BR /&gt;&lt;BR /&gt;N_rows = len(df_corridor_unsorted)&lt;BR /&gt;&lt;BR /&gt;#find the NODE IDs that are unique to both rows&lt;BR /&gt;#in most cases, there will be 2: a start and an end point&lt;BR /&gt;#these may be REF_IN_ID or NREF_IN_ID&lt;BR /&gt;#in toronto because of express and collector, there are more than 2. deal with that later&lt;BR /&gt;all_nodes = pd.concat([df_corridor_unsorted[node_type] for node_type in node_types],&lt;BR /&gt;keys=node_types&lt;BR /&gt;).reset_index(level=0).rename(columns={'level_0':'NODE_COL',0:'NODE_ID'})&lt;BR /&gt;&lt;BR /&gt;start_end_nodes = all_nodes.drop_duplicates(subset='NODE_ID',keep=False)&lt;BR /&gt;&lt;BR /&gt;index_coord_A = start_end_nodes.index.values[0]&lt;BR /&gt;index_coord_B = start_end_nodes.index.values[1]&lt;BR /&gt;&lt;BR /&gt;coord_A = list(df_corridor_unsorted.loc[index_coord_A,'geometry'].coords)[0]&lt;BR /&gt;coord_B = list(df_corridor_unsorted.loc[index_coord_B,'geometry'].coords)[0]&lt;BR /&gt;&lt;BR /&gt;if corridor_direction == 'N':&lt;BR /&gt;if coord_A[1]-coord_B[1] &amp;gt; 0:&lt;BR /&gt;start_end_iter = 1&lt;BR /&gt;else:&lt;BR /&gt;start_end_iter = 0&lt;BR /&gt;elif corridor_direction == 'E':&lt;BR /&gt;if coord_A[0]-coord_B[0] &amp;gt; 0:&lt;BR /&gt;start_end_iter = 1&lt;BR /&gt;else:&lt;BR /&gt;start_end_iter = 0&lt;BR /&gt;elif corridor_direction == 'S':&lt;BR /&gt;if coord_A[1]-coord_B[1] &amp;gt; 0:&lt;BR /&gt;start_end_iter = 0&lt;BR /&gt;else:&lt;BR /&gt;start_end_iter = 1&lt;BR /&gt;elif corridor_direction == 'W':&lt;BR /&gt;if coord_A[0]-coord_B[0] &amp;gt; 0:&lt;BR /&gt;start_end_iter = 0&lt;BR /&gt;else:&lt;BR /&gt;start_end_iter = 1&lt;BR /&gt;else:&lt;BR /&gt;wuh_oh&lt;BR /&gt;&lt;BR /&gt;if len(start_end_nodes) == 2:&lt;BR /&gt;&lt;BR /&gt;unsorted_index = start_end_nodes.index.values[start_end_iter]&lt;BR /&gt;node_in_type,node_in_id = start_end_nodes.values[start_end_iter]&lt;BR /&gt;&lt;BR /&gt;last_node_type,last_node_id = start_end_nodes.values[(start_end_iter+1)%2]&lt;BR /&gt;&lt;BR /&gt;for i in range(0,N_rows):&lt;BR /&gt;#print(i,len(df_corridor_sorted),len(df_corridor_unsorted))&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;current_link_endpoints = [list(df_corridor_unsorted.loc[unsorted_index,'geometry'].coords)[ 0],&lt;BR /&gt;list(df_corridor_unsorted.loc[unsorted_index,'geometry'].coords)[-1]&lt;BR /&gt;]&lt;BR /&gt;#in one line, remove the row from UNSORTED and add it to SORTED&lt;BR /&gt;df_corridor_unsorted,df_corridor_sorted.loc[i] = pop_row(df_corridor_unsorted,unsorted_index)&lt;BR /&gt;node_out_type = other_node_type(node_in_type)&lt;BR /&gt;node_out_id = df_corridor_sorted.loc[i,node_out_type]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if i == N_rows-1:&lt;BR /&gt;#end of algorithm&lt;BR /&gt;print (corridor_name,last_node_id==node_out_id) #they should match&lt;BR /&gt;df_corridor_sorted.index+=1&lt;BR /&gt;df_corridor_sorted = df_corridor_sorted.reset_index().rename(columns={'index':'POS_ALONG_CORR'})&lt;BR /&gt;return df_corridor_sorted&lt;BR /&gt;else:&lt;BR /&gt;#update list of nodes&lt;BR /&gt;all_remaining_nodes = pd.concat([df_corridor_unsorted[node_type] for node_type in node_types],&lt;BR /&gt;keys=node_types&lt;BR /&gt;).reset_index(level=0&lt;BR /&gt;).rename(columns={'level_0':'NODE_COL',0:'NODE_ID'})&lt;BR /&gt;### update variables for next iteration of loop&lt;BR /&gt;unsorted_index = all_remaining_nodes[all_remaining_nodes['NODE_ID']==node_out_id].index.values[0]&lt;BR /&gt;node_in_type,node_in_id = all_remaining_nodes[all_remaining_nodes['NODE_ID']==node_out_id].values[0]&lt;BR /&gt;&lt;BR /&gt;next_link_endpoints = [list(df_corridor_unsorted.loc[unsorted_index,'geometry'].coords)[ 0],&lt;BR /&gt;list(df_corridor_unsorted.loc[unsorted_index,'geometry'].coords)[-1]&lt;BR /&gt;]&lt;BR /&gt;&lt;BR /&gt;if df_corridor_sorted.loc[i,'DIR_TRAVEL']=='B':&lt;BR /&gt;for j in [0,1]:&lt;BR /&gt;for k in [0,1]:&lt;BR /&gt;if current_link_endpoints[j]==next_link_endpoints[k]:&lt;BR /&gt;diffs = [a-b for (a,b) in zip(current_link_endpoints[j],current_link_endpoints[(j+1)%2])]&lt;BR /&gt;if diffs[1] &amp;gt; 0:&lt;BR /&gt;dir_travel = 'F'&lt;BR /&gt;elif diffs[1] &amp;lt; 0:&lt;BR /&gt;dir_travel = 'T'&lt;BR /&gt;elif diffs[1] == 0:&lt;BR /&gt;if diffs[0] &amp;gt; 0:&lt;BR /&gt;dir_travel = 'F'&lt;BR /&gt;elif diffs[0] &amp;gt; 0:&lt;BR /&gt;dir_travel = 'T'&lt;BR /&gt;&lt;BR /&gt;df_corridor_sorted.loc[i,'DIR_TRAVEL']=dir_travel&lt;BR /&gt;df_corridor_sorted.loc[i,'LINK_DIR']='%d%s'%(df_corridor_sorted.loc[i,'LINK_ID'],dir_travel)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;else:&lt;BR /&gt;print(corridor_name,'unsorted. Found',len(start_end_nodes),"start/end candidates. Should be only 2")&lt;BR /&gt;df_corridor_unsorted['POS_ALONG_CORR']=pd.Series([0 for i in range(0,len(df_corridor_unsorted))])&lt;BR /&gt;&lt;BR /&gt;cols = df_corridor_unsorted.columns.tolist()&lt;BR /&gt;df_corridor_unsorted = df_corridor_unsorted[[cols[-1],] + cols[0:-1]]&lt;BR /&gt;&lt;BR /&gt;return df_corridor_unsorted&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def quick_map_show(gdf,column=None):&lt;BR /&gt;import geopandas as gpd&lt;BR /&gt;import pylab as P&lt;BR /&gt;fig=P.figure(figsize=(10,10))&lt;BR /&gt;ax=fig.add_subplot(111)&lt;BR /&gt;&lt;BR /&gt;if column:&lt;BR /&gt;gdf.plot(ax=ax,&lt;BR /&gt;column=column&lt;BR /&gt;)&lt;BR /&gt;else:&lt;BR /&gt;gdf.plot(ax=ax)&lt;BR /&gt;ax.set_axis_off()&lt;BR /&gt;return fig,ax&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Issue is: The global_name is not found , I try a few times to defined , but don't know where the variable is defined where appropriate , any help from someone can look at this code is much appreciated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 19:23:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244342#M66357</guid>
      <dc:creator>JasonMa</dc:creator>
      <dc:date>2022-12-29T19:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: NameError: Global Name 'wuh_oh' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244354#M66358</link>
      <description>&lt;P&gt;I can think of three things.&lt;BR /&gt;It looks like "wuh_oh" might be supposed to refer to a function that covers error handling, but isn't present. If this is the case, the function has to be defined elsewhere, and it should be&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def wuh_oh(params):
 '''code'''



'''code'''
def Order_the_Links(corridor_name,corr_df)
'''code'''
else:
    wuh_oh(params)&lt;/LI-CODE&gt;&lt;P&gt;If it's supposed to be literally "wuh_oh", just put it in quotes&lt;/P&gt;&lt;LI-CODE lang="python"&gt;else:
    "wuh_oh"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But if it's actually supposed to be a variable, then you just have to define it in that function, unless you're planning to use it elsewhere.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def Order_the_Links(corridor_name,corr_df):

var wuh_oh = #something

'''Code'''&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 19:37:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244354#M66358</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-12-29T19:37:56Z</dc:date>
    </item>
    <item>
      <title>Re: NameError: Global Name 'wuh_oh' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244367#M66359</link>
      <description>&lt;P&gt;Please see code formatting link &lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 19:55:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244367#M66359</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-12-29T19:55:15Z</dc:date>
    </item>
    <item>
      <title>Re: NameError: Global Name 'wuh_oh' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244383#M66361</link>
      <description>&lt;P&gt;Thanks for the reply, Alfred , wu_oh could be defined as variable which I can put as part in ORder_the_links functions&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Runtime error&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt;File "&amp;lt;string&amp;gt;", line 16, in &amp;lt;module&amp;gt;&lt;BR /&gt;File "mapping_module1.py", line 370, in Order_the_Links&lt;BR /&gt;wuh_oh&lt;BR /&gt;NameError: global name 'wuh_oh' is not defined&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Global name is supposed to be defined all the way at top of the script Global variable where the function picks up , I was wondering I am debugging this code(from someone) and I not sure how this wuh_oh is defined , I give these a these steps you have previous provided, I get back to you if issues arises&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 20:25:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244383#M66361</guid>
      <dc:creator>JasonMa</dc:creator>
      <dc:date>2022-12-29T20:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: NameError: Global Name 'wuh_oh' is not defined</title>
      <link>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244387#M66363</link>
      <description>&lt;P&gt;I just noticed a typo: the global is defined as "wu_oh", not "wuh_oh". I didn't catch it earlier, sorry.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 20:30:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/nameerror-global-name-wuh-oh-is-not-defined/m-p/1244387#M66363</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-12-29T20:30:53Z</dc:date>
    </item>
  </channel>
</rss>

