Monday, 1 July 2013

Not all numbers are equal

In this link on polygon errors in ArcMap's Polygon class it was evident that in ArcMap 10.0 that one needed a spatial reference in order to create polygon's with  an appropriate accuracy.  This still seems to be the case in 10.1.  The attached demo script contains a few calculations and the option to change the spatial reference in order to see the effect. 

'''
import arcpy
import os

#uncomment your preferred coordinate system
#coord_sys = "Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj"
#coord_sys = "Coordinate Systems/Geographic Coordinate Systems/North America/NAD 1983.prj"
#coord_sys = "Coordinate Systems/Projected Coordinate Systems/National Grids/Canada/NAD 1983 MTM 9.prj"
coord_sys = "Coordinate Systems/Projected Coordinate Systems/UTM/NAD 1983/NAD 1983 UTM Zone 18N.prj"
#coord_sys = "#"
if coord_sys == "#":
  SR = None
else:
  prj_file = os.path.join(arcpy.GetInstallInfo()['InstallDir'],coord_sys)
  SR = arcpy.SpatialReference(prj_file)

pnt = arcpy.Point()
triangle = [[0,0],[0,1],[1,0]]
square = [[0,0],[0,1],[1,1],[1,0]]
rectangle = [[0,0],[0,1],[2,1],[2,0]]
polygons = [triangle, square, rectangle]
labels = ["Triangle", "Square", "Rectangle"]
array = arcpy.Array()
polys = []

for i in range(len(polygons)):
  a_poly = polygons[i]
  print "\n", labels[i]," Coordinates: ", a_poly
  for pair in a_poly:
    pnt.X = float(pair[0])
    pnt.Y = float(pair[1])
    array.add(pnt)
    print " X %20.16f  Y %20.16f" % (pnt.X, pnt.Y)
  array.add(array.getObject(0))
  if SR =="#":
    poly = arcpy.Polygon(array)
  else:
    poly = arcpy.Polygon(array, SR)
  print "Polygon properties:"
  print " area %20.16f\n length %s" % (poly.area, poly.length)
  print " centroid %s\n true centroid %s " % (poly.centroid, poly.trueCentroid)
  print " first point %s\n last point %s " % (poly.firstPoint, poly.lastPoint)
  print " extent %s " % (poly.extent)
  print " hull %s\n " % (poly.hullRectangle)
  array.removeAll()
  polys.append(poly)

No comments:

Post a Comment