TransWikia.com

Iterating over table to create chainages using Python in ModelBuilder

Geographic Information Systems Asked by stanleykubrick on February 1, 2021

I routinely have to do the following workflow:

  • Map GPS points;-

  • Create lines based on those GPS points;

  • Split the lines at a set interval distance (every 50m, every 100m etc.)

  • Add from / to fields for chainages along the line.

I use ModelBuilder to automate this process as much as possible, but to generate the chainages, I need to use Excel because I have very limited experience with Python. So I generate the chainage values in Excel, then in the editor context in ArcGIS copy those values from Excel into the attribute table of my polyline features.

How can I transfer the logic I have in my Excel sheet over to Python to use in the Calculate Field tool within ModelBuilder?

The format of the from / to fields ends up being “K+MMM” where the K is the kilometer and the M’s represent meters. So if I’ve got a starting point of 0km and 0m and an interval of 100m, the first chainage would read “0+000”, followed by “0+100”, “0+200”.

Here are some screenshots of the logic in the Excel file: https://imgur.com/a/LHYyi0u

One Answer

I found a snippet of code somewhere previously that does a lot of the work you are looking for. Here is a first cut at solving your problem. I have not looked at creating the GPS points or the lines. I assume you are starting with the lines completed.

import arcpy

arcpy.env.overwriteOutput = True
spatial_reference = arcpy.Describe(dids).spatialReference

# Create points along roads
loc = 't:/right_here'
lines = loc + '/lines.shp'
distance = 50 # meters
spatial_reference = arcpy.Describe(lines).spatialReference

mem_point = loc + '/points.shp'
arcpy.CreateFeatureclass_management(loc, points, "POINT", "", "DISABLED", "DISABLED", spatial_reference)
arcpy.AddField_management(mem_point, "LINE_ID", "INTEGER")
arcpy.AddField_management(mem_point, "FROM", "INTEGER")
arcpy.AddField_management(mem_point, "TO", "INTEGER")
search_fields = ["SHAPE@", "OID@"]
insert_fields = ["SHAPE@", "LINE_ID", "FROM", "TO"]
with arcpy.da.SearchCursor(lines, search_fields) as search:
    with arcpy.da.InsertCursor(mem_point, insert_fields) as insert:
        last_pt = -1
        for row in search:
            cur_point = 0
            try:
                line_geom = row[0]
                length = float(line_geom.length)
                count = 0
                oid = str(row[1])
                while count <= length:
                    point = line_geom.positionAlongLine(count, False)
                    insert.insertRow((point, oid, last_pt, cur_pt))
                    last_pt += 1
                    cur_point += 1
                    count += distance

            except Exception as e:
                print str(e.message)

Answered by D_C on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP