Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. # import libraries
  2. import arcpy, os
  3.  
  4. # set input/output parameters
  5. polyFC = arcpy.GetParameterAsText(0) # input polygons
  6. lineSpacing = arcpy.GetParameterAsText(1) # line spacing in ft
  7. ##buffDist = arcpy.GetParameterAsText(3) # inner buffer distance
  8. shortboolean = arcpy.GetParameterAsText(2) # True if checked
  9. invertboolean = arcpy.GetParameterAsText(3) #Option to change the side
  10. outParallel = arcpy.GetParameterAsText(4) # output parallel lines
  11. outfc = arcpy.GetParameterAsText(5)
  12. outFC2 = arcpy.GetParameterAsText(6)
  13.  
  14.  
  15. # parse numbers from parameters
  16. lineSpaceNum = float(lineSpacing.split(' ')[0]) * 0.3048
  17. ##buffNum = float(buffDist.split(' ')[0])
  18. buffNum = 0
  19.  
  20. # establish spatial reference
  21. desc = arcpy.Describe(polyFC)
  22. SR = desc.spatialReference
  23.  
  24. # set overwrite environment
  25. arcpy.env.overwriteOutput = True
  26. arcpy.env.outputCoordinateSystem = SR
  27.  
  28. parallels = []
  29. # loop through each input shape
  30. for row in arcpy.da.SearchCursor(polyFC, ["SHAPE@"], spatial_reference=SR):
  31.  
  32. # create inner buffer
  33. polyBuff = row[0].buffer(buffNum * -1)
  34.  
  35. # create hull rectangle to establish a rotated area of interest
  36. coordSplit = row[0].hullRectangle.split(' ')
  37.  
  38. # collect corner coordinates
  39. coordList = arcpy.Array([arcpy.Point(coordSplit[0],coordSplit[1]),arcpy.Point(coordSplit[2],coordSplit[3]),arcpy.Point(coordSplit[4],coordSplit[5]),arcpy.Point(coordSplit[6],coordSplit[7]),arcpy.Point(coordSplit[0],coordSplit[1])])
  40.  
  41. # create lines from hull rectangle
  42. currentLines = []
  43. for pointNum in range(0,4):
  44. arcpy.Array([coordList.getObject(pointNum),coordList.getObject(pointNum+1)])
  45. hullRecLine = arcpy.Polyline(arcpy.Array([coordList.getObject(pointNum),coordList.getObject(pointNum+1)]))
  46. currentLines.append(hullRecLine)
  47.  
  48. # compare first and second line to determine if first line is short or long
  49. firstLong = 0
  50. if currentLines[0].length < currentLines[1].length:
  51. if shortboolean == 'true':
  52. firstLong = 0
  53. else:
  54. firstLong = 1
  55. if currentLines[0].length > currentLines[1].length:
  56. if shortboolean == 'true':
  57. firstLong = 1
  58. else:
  59. firstLong = 0
  60.  
  61. # calculate number of points needed along short axis
  62. numPoints = int(math.floor(currentLines[firstLong].length/lineSpaceNum))
  63.  
  64. # create and join points to create parallel lines
  65.  
  66. for point in range(0,numPoints+1):
  67. shortPoint1 = currentLines[firstLong].positionAlongLine(lineSpaceNum*point)
  68. shortPoint2 = currentLines[firstLong + 2].positionAlongLine(currentLines[firstLong + 2].length - (lineSpaceNum*point))
  69. parallel = arcpy.Polyline(arcpy.Array([shortPoint1.centroid,shortPoint2.centroid]), SR)
  70.  
  71. # intersect parallel lines with buffer
  72. parallelBuff = parallel.intersect(polyBuff,2)
  73. parallels.append(parallelBuff)
  74.  
  75. # write geometries to disk
  76. arcpy.CopyFeatures_management(parallels, outParallel)
  77.  
  78. # add to map
  79. mxd = arcpy.mapping.MapDocument("CURRENT")
  80. dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
  81. addLayer = arcpy.mapping.Layer(outParallel)
  82. arcpy.mapping.AddLayer(dataFrame, addLayer)
  83.  
  84. del row
  85.  
  86. arcpy.RepairGeometry_management (outParallel)
  87.  
  88.  
  89. infc = outParallel
  90.  
  91. x_field = arcpy.ListFields(infc,"alternate")
  92. if not x_field:
  93. arcpy.AddField_management(infc,"alternate","TEXT")
  94.  
  95. n = 1
  96. with arcpy.da.UpdateCursor(infc, "alternate") as cursor:
  97. for row in cursor:
  98. if invertboolean == 'true':
  99. if n == 1:
  100. row[0] = "False"
  101. n = n - 1
  102. else:
  103. row[0] = "True"
  104. n = n + 1
  105. cursor.updateRow(row)
  106. else:
  107. if n == 1:
  108. row[0] = "True"
  109. n = n - 1
  110. else:
  111. row[0] = "False"
  112. n = n + 1
  113. cursor.updateRow(row)
  114.  
  115.  
  116. # split the output into a folder and file name
  117. OutPath = os.path.dirname(outfc)
  118. OutName = os.path.basename(outfc)
  119. NameOfFile,ext = os.path.splitext(OutName) # separate file name from extension (if any)
  120. NameOfFile = NameOfFile + ".shp" # include shape file extension
  121. #OutFC_Clean = OutPath + "\\" + NameOfFile # clean full path to output
  122. OutFC_Clean = outfc
  123.  
  124. # Get the spatial reference from the input feature class
  125. desc = arcpy.Describe(infc)
  126. SR = desc.spatialReference
  127.  
  128. n = 1
  129. arcpy.CreateFeatureclass_management(OutPath,NameOfFile,"POINT",spatial_reference = SR)
  130. arcpy.AddField_management(OutFC_Clean,"Waypoint","LONG")
  131.  
  132. # Enter for loop for each feature
  133. #
  134. with arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@", "alternate"]) as sCur:
  135. with arcpy.da.InsertCursor(OutFC_Clean,["SHAPE@","Waypoint"]) as iCur:
  136. for row in sCur:
  137. if row[2] == "False":
  138. #insert the feature
  139. iCur.insertRow((row[1].firstPoint,n))
  140. n = n + 1
  141. iCur.insertRow((row[1].lastPoint,n))
  142. n = n + 1
  143. else:
  144. iCur.insertRow((row[1].lastPoint,n))
  145. n = n + 1
  146. iCur.insertRow((row[1].firstPoint,n))
  147. n = n + 1
  148.  
  149. infc = outfc
  150.  
  151. # Enter for loop for each feature
  152. #
  153. perimPntLst = []
  154.  
  155. with arcpy.da.SearchCursor(infc, ["Waypoint", "SHAPE@XY"]) as sCur:
  156. for row in sCur:
  157. perimPntLst.append([row[0], row[1][0], row[1][1]])
  158. del sCur
  159. print perimPntLst
  160.  
  161. # Get the spatial reference from the input feature class
  162. desc = arcpy.Describe(infc)
  163. SR = desc.spatialReference
  164.  
  165. cur = None
  166. try:
  167. # Create the output feature class
  168. #
  169. arcpy.CreateFeatureclass_management(os.path.dirname(outFC2),
  170. os.path.basename(outFC2),
  171. "POLYLINE",spatial_reference = SR)
  172.  
  173. # Open an insert cursor for the new feature class
  174. #
  175. cur = arcpy.da.InsertCursor(outFC2, ["SHAPE@"])
  176.  
  177. # Create an array object needed to create features
  178. #
  179. array = arcpy.Array()
  180. for coords in perimPntLst:
  181. array.add(arcpy.Point(coords[1], coords[2], ID=coords[0]))
  182. cur.insertRow([arcpy.Polyline(array)])
  183.  
  184.  
  185. except Exception as e:
  186. print e.message
  187. finally:
  188. # Cleanup the cursor if necessary
  189. #
  190. if cur:
  191. del cur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement