4

I am trying to write a python script to determine flow direction on a water pipe network. I've used this script to find the mid point along each polyline, now i need to convert this data into a point feature class, which will become an arrow of flow direction.

I've tried saving it as a list but cannot convet the list to a point feature. Can anyone suggest ways to save the mid point locations as a point feature class?

  #FlowArrows.py
  import arcpy
  #setting the environment
  arcpy.env.workspace = "J:/PYTHON/Flow_Direction.gdb"
  #arcpy.env.overwriteOutput = True

  #Setting the containers
  Pipes = r"J:\PYTHON\Flow_Direction.gdb\Pipes"
  Nodes = r"J:\PYTHON\Flow_Direction.gdb\Nodes"
  MidList = []

  #Getting the mid point
  Cursor = arcpy.SearchCursor(Pipes)
      for i in Cursor:
      Midpoint = i.shape.positionAlongLine(0.50,True).firstPoint
      MidList.append(Midpoint)

  print ("done")       
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
AnythingMapping
  • 635
  • 6
  • 20

3 Answers3

6

You need to open an InsertCursor on the other feature class:

import arcpy

pipes = r"J:\PYTHON\Flow_Direction.gdb\Pipes"
nodes = r"J:\PYTHON\Flow_Direction.gdb\Nodes"

#Getting the mid point
with arcpy.da.SearchCursor(pipes, "SHAPE@") as in_cursor, \
     arcpy.da.InsertCursor(nodes, "SHAPE@") as out_cursor:
    for row in in_cursor:
        midpoint = row[0].positionAlongLine(0.50,True).firstPoint
        out_cursor.insertRow([midpoint])

print ("done")       
Jason Scheirer
  • 18,002
  • 2
  • 53
  • 72
5

If using ArcPy for this is not a mandatory requirement, and if you have an Advanced (formerly called ArcInfo) level license, then there is an out-of-the-box tool called Feature Vertices To Points (Data Management) that has a MID option:

MID —A point will be created at the midpoint, not necessarily a vertex, 
of each input line or polygon boundary. 
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
3

You can do it using FeatureVertices to Points (as mentioned above) - using your code and 1 line addition.

#FlowArrows.py
import arcpy

#setting the environment
arcpy.env.workspace = "J:/PYTHON/Flow_Direction.gdb"

#Setting the containers
Pipes = r"J:\PYTHON\Flow_Direction.gdb\Pipes"

# This will give you the mid point - which is not necessarily a vertex on the line.
arcpy.FeatureVerticesToPoints_management(Pipes, "ARROW_PNTS", "MID")

You can also use the answer in this post using SHAPELY module to find a point at a given distance along a line:

Get a point on a polyline from end points given distance along poly line

dklassen
  • 2,920
  • 15
  • 25