Monday, 1 July 2013

Working with lines in Arcmap

This is a brief demo on how to set up methods to create lines in Arcmap with Python.  Note that you need to create a spatial reference if you want high precision lines.

'''
LineDemo.py

Author
  D.E. Patterson

Purpose
  Demonstrates various properties for polylines
  See
http://resources.arcgis.com/en/help/main/10.1/index.html#//018z00000008000000
  for more options
'''
import math
import arcpy
pnt = arcpy.Point()

polylines = []
vals = [0.001, 0.1, 10.0, math.sqrt(2.0), 2.0]
for a_val in vals:
  polylines.append( [[0.0,0.0],[a_val,0.0]] )  #positive out
  polylines.append( [[0.0,0.0],[-a_val,0.0]] ) #negative out
  polylines.append( [[a_val,0.0],[0.0,0.0]] )  #positive in
  polylines.append( [[-a_val,0.0],[0.0,0.0]] ) #negative in
array = arcpy.Array()
polys = []
for i in range(len(polylines)):
  a_poly = polylines[i]
  print "\n", "Coordinates: ", a_poly
  for pair in a_poly:
    pnt.X = pair[0]
    pnt.Y = pair[1]
    array.add(pnt)
    #print " X: %20.16f  Y: %20.16f" % (pnt.X, pnt.Y)
  '''
  add an empty spatial reference when creating the
  geometry to go from low to high precision
  '''
  poly = arcpy.Polyline(array,arcpy.SpatialReference())
  cal_leng = poly.length
  centroidX = poly.centroid.X
  centroidY = poly.centroid.Y

  print "Polyline properties:"
  print " length:        %20.16f" % (cal_leng)
  print " centroid (x,y: %20.16f, %10.16f" % (centroidX, centroidY)

  array.removeAll()
  polys.append(poly)  #for further use

No comments:

Post a Comment