Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import libraries
- import arcpy, os
- # set input/output parameters
- polyFC = arcpy.GetParameterAsText(0) # input polygons
- lineSpacing = arcpy.GetParameterAsText(1) # line spacing in ft
- ##buffDist = arcpy.GetParameterAsText(3) # inner buffer distance
- shortboolean = arcpy.GetParameterAsText(2) # True if checked
- invertboolean = arcpy.GetParameterAsText(3) #Option to change the side
- outParallel = arcpy.GetParameterAsText(4) # output parallel lines
- outfc = arcpy.GetParameterAsText(5)
- outFC2 = arcpy.GetParameterAsText(6)
- # parse numbers from parameters
- lineSpaceNum = float(lineSpacing.split(' ')[0]) * 0.3048
- ##buffNum = float(buffDist.split(' ')[0])
- buffNum = 0
- # establish spatial reference
- desc = arcpy.Describe(polyFC)
- SR = desc.spatialReference
- # set overwrite environment
- arcpy.env.overwriteOutput = True
- arcpy.env.outputCoordinateSystem = SR
- parallels = []
- # loop through each input shape
- for row in arcpy.da.SearchCursor(polyFC, ["SHAPE@"], spatial_reference=SR):
- # create inner buffer
- polyBuff = row[0].buffer(buffNum * -1)
- # create hull rectangle to establish a rotated area of interest
- coordSplit = row[0].hullRectangle.split(' ')
- # collect corner coordinates
- 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])])
- # create lines from hull rectangle
- currentLines = []
- for pointNum in range(0,4):
- arcpy.Array([coordList.getObject(pointNum),coordList.getObject(pointNum+1)])
- hullRecLine = arcpy.Polyline(arcpy.Array([coordList.getObject(pointNum),coordList.getObject(pointNum+1)]))
- currentLines.append(hullRecLine)
- # compare first and second line to determine if first line is short or long
- firstLong = 0
- if currentLines[0].length < currentLines[1].length:
- if shortboolean == 'true':
- firstLong = 0
- else:
- firstLong = 1
- if currentLines[0].length > currentLines[1].length:
- if shortboolean == 'true':
- firstLong = 1
- else:
- firstLong = 0
- # calculate number of points needed along short axis
- numPoints = int(math.floor(currentLines[firstLong].length/lineSpaceNum))
- # create and join points to create parallel lines
- for point in range(0,numPoints+1):
- shortPoint1 = currentLines[firstLong].positionAlongLine(lineSpaceNum*point)
- shortPoint2 = currentLines[firstLong + 2].positionAlongLine(currentLines[firstLong + 2].length - (lineSpaceNum*point))
- parallel = arcpy.Polyline(arcpy.Array([shortPoint1.centroid,shortPoint2.centroid]), SR)
- # intersect parallel lines with buffer
- parallelBuff = parallel.intersect(polyBuff,2)
- parallels.append(parallelBuff)
- # write geometries to disk
- arcpy.CopyFeatures_management(parallels, outParallel)
- # add to map
- mxd = arcpy.mapping.MapDocument("CURRENT")
- dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
- addLayer = arcpy.mapping.Layer(outParallel)
- arcpy.mapping.AddLayer(dataFrame, addLayer)
- del row
- arcpy.RepairGeometry_management (outParallel)
- infc = outParallel
- x_field = arcpy.ListFields(infc,"alternate")
- if not x_field:
- arcpy.AddField_management(infc,"alternate","TEXT")
- n = 1
- with arcpy.da.UpdateCursor(infc, "alternate") as cursor:
- for row in cursor:
- if invertboolean == 'true':
- if n == 1:
- row[0] = "False"
- n = n - 1
- else:
- row[0] = "True"
- n = n + 1
- cursor.updateRow(row)
- else:
- if n == 1:
- row[0] = "True"
- n = n - 1
- else:
- row[0] = "False"
- n = n + 1
- cursor.updateRow(row)
- # split the output into a folder and file name
- OutPath = os.path.dirname(outfc)
- OutName = os.path.basename(outfc)
- NameOfFile,ext = os.path.splitext(OutName) # separate file name from extension (if any)
- NameOfFile = NameOfFile + ".shp" # include shape file extension
- #OutFC_Clean = OutPath + "\\" + NameOfFile # clean full path to output
- OutFC_Clean = outfc
- # Get the spatial reference from the input feature class
- desc = arcpy.Describe(infc)
- SR = desc.spatialReference
- n = 1
- arcpy.CreateFeatureclass_management(OutPath,NameOfFile,"POINT",spatial_reference = SR)
- arcpy.AddField_management(OutFC_Clean,"Waypoint","LONG")
- # Enter for loop for each feature
- #
- with arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@", "alternate"]) as sCur:
- with arcpy.da.InsertCursor(OutFC_Clean,["SHAPE@","Waypoint"]) as iCur:
- for row in sCur:
- if row[2] == "False":
- #insert the feature
- iCur.insertRow((row[1].firstPoint,n))
- n = n + 1
- iCur.insertRow((row[1].lastPoint,n))
- n = n + 1
- else:
- iCur.insertRow((row[1].lastPoint,n))
- n = n + 1
- iCur.insertRow((row[1].firstPoint,n))
- n = n + 1
- infc = outfc
- # Enter for loop for each feature
- #
- perimPntLst = []
- with arcpy.da.SearchCursor(infc, ["Waypoint", "SHAPE@XY"]) as sCur:
- for row in sCur:
- perimPntLst.append([row[0], row[1][0], row[1][1]])
- del sCur
- print perimPntLst
- # Get the spatial reference from the input feature class
- desc = arcpy.Describe(infc)
- SR = desc.spatialReference
- cur = None
- try:
- # Create the output feature class
- #
- arcpy.CreateFeatureclass_management(os.path.dirname(outFC2),
- os.path.basename(outFC2),
- "POLYLINE",spatial_reference = SR)
- # Open an insert cursor for the new feature class
- #
- cur = arcpy.da.InsertCursor(outFC2, ["SHAPE@"])
- # Create an array object needed to create features
- #
- array = arcpy.Array()
- for coords in perimPntLst:
- array.add(arcpy.Point(coords[1], coords[2], ID=coords[0]))
- cur.insertRow([arcpy.Polyline(array)])
- except Exception as e:
- print e.message
- finally:
- # Cleanup the cursor if necessary
- #
- if cur:
- del cur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement