Select to view content in your preferred language

Multiple number extraction from field

407
2
Jump to solution
09-22-2022 09:07 PM
Labels (2)
dgmoore099
New Contributor II

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.  

Screenshot 2022-09-22 210504.jpg

 

 

 

Thank you

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

 

Does this do what you want?

 

Open the Python window:

JohannesLindner_0-1663915551766.png

 

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.


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

 

Does this do what you want?

 

Open the Python window:

JohannesLindner_0-1663915551766.png

 

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.


Have a great day!
Johannes
dgmoore099
New Contributor II

That did it.  Thanks Johannes!

0 Kudos