Hello,
I'm using the latest version of ArcGIS Pro. I have a dataset of individual bus stops which I am trying to map as the bus routes. The routes are identified within a string of additional information. Some strings contain multiple routes within one row. I don' t know Python. How can I extract the numbers from the "stop_desc" field in a way that I can then dissolve the resulting field numbers into a single route. In the image below, row 1 indicates that routes 1, 5 and 10 are represented at that stop. I have created a new field titled "route" for the numbers to be extracted.
Thank you
Solved! Go to Solution.
Does this do what you want?
Open the Python window:
Copy and paste this script, hit enter twice:
# create the output feature class
route_stops = arcpy.management.CreateFeatureclass("memory", "RouteStops", "POINT")
arcpy.management.AddField(route_stops, "stop_id","LONG")
arcpy.management.AddField(route_stops, "route","TEXT")
# start inserting into the new fc
with arcpy.da.InsertCursor(route_stops, ["SHAPE@", "stop_id", "route"]) as i_cursor:
# loop through the stops
with arcpy.da.SearchCursor("bus_stops", ["SHAPE@", "stop_id", "stop_desc"]) as s_cursor:
for shp, id, desc in s_cursor:
# extract the routes from the field stop_desc:
# delete spaces, get everthing after "routes:" and split that at "/"
routes = desc.replace(" ", "").split("routes:")[-1].split("/")
# for each route, write a point into the new fc
for route in routes:
i_cursor.insertRow([shp, id, route])
This will create a feature class in RAM (so export it if you want to keep it). This feature class will have two fields: The stop_id and the route. It will have a point for each route at each stop. So in your example, it will have 3 overlapping points for stop_id 12143, one for each of the routes 1, 5, and 10.
Does this do what you want?
Open the Python window:
Copy and paste this script, hit enter twice:
# create the output feature class
route_stops = arcpy.management.CreateFeatureclass("memory", "RouteStops", "POINT")
arcpy.management.AddField(route_stops, "stop_id","LONG")
arcpy.management.AddField(route_stops, "route","TEXT")
# start inserting into the new fc
with arcpy.da.InsertCursor(route_stops, ["SHAPE@", "stop_id", "route"]) as i_cursor:
# loop through the stops
with arcpy.da.SearchCursor("bus_stops", ["SHAPE@", "stop_id", "stop_desc"]) as s_cursor:
for shp, id, desc in s_cursor:
# extract the routes from the field stop_desc:
# delete spaces, get everthing after "routes:" and split that at "/"
routes = desc.replace(" ", "").split("routes:")[-1].split("/")
# for each route, write a point into the new fc
for route in routes:
i_cursor.insertRow([shp, id, route])
This will create a feature class in RAM (so export it if you want to keep it). This feature class will have two fields: The stop_id and the route. It will have a point for each route at each stop. So in your example, it will have 3 overlapping points for stop_id 12143, one for each of the routes 1, 5, and 10.
That did it. Thanks Johannes!