Split into equal length features

Request came in last week for a way to split a line feature into 10 equal length line features.

The input looked like this
stream1

The accepted solution came from Dave on the team who sent this elegant and efficient solution.

in_fc = r'c:\projects\waterway.gdb\stream'
out_fc = r'c:\projects\waterway.gdb\stream10'
out_count = 10 # how many features desired

line = arcpy.da.SearchCursor(in_fc, ("SHAPE@",)).next()[0]
arcpy.CopyFeatures_management([line.segmentAlongLine(i/float(out_count), ((i+1)/float(out_count)), True) for i in range(0, out_count)], out_fc)

Which outpus a new feature class containing 10 line features like so
stream10

How does it work? The first 3 lines are self explanatory. So we will skip.

The following line is also fairly simple, what it does is get the geometry (shape@) of the first record (we only ask for next() once). The [0] is needed to get just the first value in the record (remember cursors return lists of values).


polyline = arcpy.da.SearchCursor(in_fc, ("SHAPE@",)).next()[0]

Next is where the magic happens, python list comprehension is used to turn the polyline object into a list of 10 (as per the out_count variable) equal length segments generated by the segmentAlongLine function. This list of polyline is then used as input to CopyFeatures (as per Using geometry objects with geoprocessing tools) which writes out the 10 polyline as individual features into the output feature class (out_fc).


arcpy.CopyFeatures_management([polyline.segmentAlongLine(i/float(out_count), ((i+1)/float(out_count)), True) for i in range(0, out_count)], out_fc)

EDIT: As was pointed out in the comments, the segmentAlongLine is new at 10.3.

Cheers

Advertisements

pretty print fields

This is a piece of code i’ve written and re-written dozens of times. Typically I code it on a whim for a single use.

Today I went the extra mile and made the output really nice, so here it is in case somebody has use for it.

And this is what you get as output in the python window*.
pywin_pprint

* this is the nice and new ArcGIS Pro’s python window.

The output is a bit too wide for use in terminal (80 characters) but within the app it’s nice and clear.

Happy coding.

Getting more arcpy help

A lot of people are posting requests asking for a particular snippet in the comments section of this blog.

By all means, continue to do so, we do enjoy tackling the more generic and broadly useful requests… but we just can’t tackle all of them, and not in a timely manner.

So if you need help with code, and you need it fairly soon, please consider also opening a discussion on the Esri geonet site.

Happy coding.

Python & arcpy presentations at the 2014 user conference

Once upon a time, there was but a single tech workshop at the Esri international user conference which featured everybody’s favorite scripting language. No longer is the Python offering so paltry. Behold, the 2014 user conference technical workshops & demo theaters returned when you search for “python”.

Consult the online agenda for date and time when these are offered.

Technical workshops

  • Python: Getting started
  • Python: Beyond the Basics
  • Python: Map Automation
  • Python Map Automation: An Introduction to arcpy.mapping
  • Python Map Automation: Beyond the Basics of arcpy.mapping
  • Python: Raster Analysis
  • Python: Building Geoprocessing Tools
  • Python for Analysis
  • Analyzing Multidimensional Scientific Data in ArcGIS
  • Automating Geodatabase Creation with Geoprocessing Tools
  • Creating Geoprocessing Services
  • Customizing ArcPad with JavaScript, Python, and VBScript
  • Desktop Mapping: Building Map Books
  • ArcGIS Online: Administering your ArcGIS Organization Through Scripting
  • ArcGIS for Server Administrative Scripting and Automation
  • ArcGIS Network Analyst: Automating Workflows with Geoprocessing
  • Integrating Open-Source Statistical Packages with ArcGIS
  • Useflul Python Libraries for The GIS Professional

Demo theater presentations

  • Administering ArcGIS for Server with Python
  • Building Live Data Feeds Using Python
  • Consuming Geoprocessing and Hosted Analytic Services in ArcGIS for Desktop
  • Debugging Python Scripts
  • Esri Production Mapping: Automating Map Production Workflows Using the ArcPyProduction Site Package
  • Geoprocessing on Image Services
  • Getting Started With Map Algebra Using the Raster Calculator and Python
  • Getting Started with Python in ArcGIS
  • Interacting with the ArcGIS Server Admin API using Python
  • Managing an Online Data Warehouse: Using Python to Publish Data to ArcGIS Online
  • Python Add-ins: Tips and Tricks

python/arcpy code from the 2014 dev summit

Hey Everyone,

it was great meeting and talking to everybody at the dev summit.

A number of python/arcpy presenters already shared their code to github repos below.

Happy coding.