ArcMap Draw Camera Range with ArcPy for all column

5028
4
10-14-2011 04:36 AM
AragonNette
New Contributor
hi,
i am working on some camera data. I have some points which consist of azimuth, angle, distance(1000 mt), and of course coordinate field attributes. In arcmap I want to draw shapes like pie chart ... i write some code inspritaions from some similar code .. but when i choose all necessity column from my shp file and make code run , i am getting error to my except point in code...





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")
 
Tags (2)
0 Kudos
4 Replies
AndrewChapkowski
Esri Regular Contributor
Here is a sample to draw an Arc and by using the center point of the circle, you can create a pie wedge.  You are most likely going to have modify this code to fit your needs.  You can create the wedge and then use an insert cursor to put the geometry into your feature class.  I just save it to disk.

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")


In addition to that, you might want to improve your error handling:
# Change this
arcpy.AddMessage("\n** Error **\n")
# To at least this:
arcpy.AddError(arcpy.GetMessages(2))

You might want to also consider adding a trace function to your except that will point out exactly what line is failing.  See: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000

Hope this helps.
0 Kudos
RaphaelSiebenmann
New Contributor III

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!

0 Kudos
AragonNette
New Contributor
thank you it will solve my problem...
0 Kudos
AhmedAlani
New Contributor
Thank you for the code its work perfect,
I adopted but because I have multiple points, each point present CCTV camera.
I make the script loop for each point, but when try to create the polygon I get one polygon for all cctv cameras. I don't know how to create multiple polygons on the same layer,
could you please help.

Thanks.
0 Kudos