import arcpy, string, operator, math
try:
#Collect azimuth, angle and x,y parameters
fc = string.replace(arcpy.GetParameterAsText(0),"\\","/")
fields = arcpy.ListFields(fc)
descfc = arcpy.Describe(fc)
sr = descfc.spatialReference
azimuth = arcpy.GetParameterAsText(1)
angle = arcpy.GetParameterAsText(2)
x = arcpy.GetParameterAsText(3)
y = arcpy.GetParameterAsText(4)
#Check for output featureclass
outfc = string.replace(fc, ".shp", "_range.shp")
lstfc = string.split(fc, "/")
for fd in lstfc:
fn = fd
#If existance
if arcpy.Exists(outfc):
arcpy.AddMessage("Deleting " + outfc)
arcpy.Delete_management(outfc)
#folder name
folder = string.replace(fc, "/" + fn, "")
arcpy.RefreshCatalog(folder)
arcpy.env.workspace = folder
#Create the feature class
arcpy.AddMessage("Creating a polygon featureclass " + outfc)
sf = string.replace(outfc,folder + "/", "")
arcpy.CreateFeatureclass_management(folder, sf, "POLYGON", fc, "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", sr)
arcpy.RefreshCatalog(folder)
#Open search and insert cursors
n = 0
insrecs = arcpy.InsertCursor(outfc)
recs = arcpy.SearchCursor(fc)
rec = recs.next()
while rec:
x = rec.getValue(x)
y = rec.getValue(y)
angle = rec.getValue(angle)
azimuth = rec.getValue(azimuth)
azimuth = operator.mod(azimuth, 360)
if azimuth <= 90:
azimuth = 90 - azimuth
elif azimuth <= 360:
azimuth = 360 - (azimuth -90)
arr = arcpy.Array()
azimuth1 = azimuth - (angle/2)
azimuth2 = azimuth + (angle/2)
azimuthSo = azimuth1
arr.add(x,y)
while True:
tx = x + math.cos((math.pi/180) * azimuthSo) * 1000
ty = y + math.sin((math.pi/180) * azimuthSo) * 1000
point = arcpy.Point()
point.x = tx
point.y = ty
arr.add(point)
if azimuthSo >= azimuth2:
break
azimuthSo +=5
arr.add(x,y)
#arcpy.AddMessage(add)
#Add feature
insrec = insrecs.newRow()
insrec.Shape = arr
fld = fields.reset()
fld = fields.next()
while fld:
if fld.name <> "Shape" and fld.name <> "FID":
insrec.setValue ( fld.name, rec.getValue(fld.name))
fld = fields.next()
insrecs.insertRow(insrec)
#Next records
n = n + 1
rec = recs.next()
#arcpy.AddMessageno of features
arcpy.RefreshCatalog(folder)
arcpy.AddMessage("\n" + str(n) + " features stored")
#Completion message
arcpy.AddMessage("\n** Finished **\n")
except:
#Error
arcpy.AddMessage("\n** Error **\n")
finally:
#Cleanup
del arcpy
#arcpy.AddMessage("\n** Finished **\n")
import math import arcpy def toCoord(centerX,centerY, radius, angle): """ Calculates the position on the arc """ radians = float((angle/180) * math.pi) x = float(centerX + math.cos(radians) * radius) y = float(centerY + math.sin(radians) * radius) return [x, y] def arc(centerX,centerY,radius,startAngle,endAngle,segments): """ centerX and centerY - X,Y center points of arc segment radius - distance from center to edge of circle in map units startAngle - starting position as degrees endAngle ending position as degrees segments - number of points to add in the Arc returns: Array of Coordinates [[X1,X1],...,[Xn,Yn]] """ angle = startAngle path = [] coords = toCoord(centerX,centerY,radius, angle) path.append(coords) while (angle <= endAngle): coords = toCoord(centerX,centerY,radius, angle) path.append(coords) angle +=float(((endAngle-startAngle)/segments)) return path centerPt = arcpy.Point(1.0,1.0) arcPts = arc(centerPt.X,centerPt.Y,5.0,10.0,50.0,10) array = arcpy.Array() array.add(centerPt) for pt in arcPts: arcPt = arcpy.Point(pt[0],pt[1]) array.add(arcPt) array.add(centerPt) geom = arcpy.Polygon(array) arcpy.CopyFeatures_management(geom,r"c:\temp\testArc.shp")
# Change this
arcpy.AddMessage("\n** Error **\n")
# To at least this:
arcpy.AddError(arcpy.GetMessages(2))
Andrew -
Thank you for the beautiful code example.
I ran into a few issues with the radian conversion and I replaced line 7 with python's built in angle to radians function:
radians = math.radians(angle)
Other than that, this worked like a charm!