1

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://i.sstatic.net/DUUYT.jpg

2 Answers 2

1

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)
0
-1

check this. Hope it will help. https://github.com/ImranAhsan15/Chainage-creation-using-arcpy

3
  • Please do not write the same answer in multiple questions. If the same answer actually applies to more than one question chances are that the questions are duplicates and only one of them should remain open. Commented Aug 8, 2023 at 3:17
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Aug 8, 2023 at 3:19
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. Commented Aug 8, 2023 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.