Created
April 23, 2018 16:34
-
-
Save stdavis/73fd185989fcec009af3babb4933704e to your computer and use it in GitHub Desktop.
calculate_from_to.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import arcpy | |
| streets = r'\\Mac\Downloads\streetsforlinearreferencing\streets_for_linear_referencing.shp' | |
| fldFULLNAME = 'FULLNAME' | |
| routes = {} | |
| with arcpy.da.SearchCursor(streets, [fldFULLNAME, 'Shape@', 'OID@'], sql_clause=(None, 'ORDER BY {}'.format(fldFULLNAME))) as cursor: | |
| for name, shape, oid in cursor: | |
| routes.setdefault(name, []).append((oid, shape)) | |
| edits = {} | |
| for route in routes: | |
| print(route) | |
| print(len(routes[route])) | |
| check_shapes = list(route) | |
| ordered_shapes = [] | |
| ordered_start_point = None | |
| ordered_end_point = None | |
| max_tries = 20 | |
| tries = 0 | |
| while len(ordered_shapes) < len(routes[route]) and tries <= max_tries: | |
| print(len(ordered_shapes)) | |
| for oid, shape in routes[route]: | |
| if len(ordered_shapes) == 0: | |
| ordered_shapes.append((oid, shape)) | |
| ordered_start_point = shape.firstPoint | |
| ordered_end_point = shape.lastPoint | |
| continue | |
| if shape.firstPoint.equals(ordered_end_point): | |
| #: add to the end of the ordered_shapes | |
| ordered_shapes.append((oid, shape)) | |
| ordered_end_point = shape.lastPoint | |
| continue | |
| if shape.lastPoint.equals(ordered_start_point): | |
| #: add to the beginning of ordered_shapes | |
| ordered_shapes.insert(0, (oid, shape)) | |
| ordered_start_point = shape.firstPoint | |
| continue | |
| tries = tries + 1 | |
| running_length = 0 | |
| for oid, shape in ordered_shapes: | |
| miles = round(shape.length * 0.00062137, 3) | |
| edits[oid] = (running_length, miles) | |
| running_length = running_length + miles | |
| print('making edits') | |
| with arcpy.da.UpdateCursor(streets, ['OID@', 'F_DISTANCE', 'T_DISTANCE']) as ucursor: | |
| for oid, f, t in ucursor: | |
| if oid in edits: | |
| new_from, new_to = edits[oid] | |
| ucursor.updateRow((oid, new_from, new_to)) | |
| print('done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment