''' CreatePolyDemo.py Demonstrates calculating various properties for polygons ''' import arcpy 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 print "\n", labels," Coordinates: ", a_poly for pair in a_poly: pnt.X = pair[0] pnt.Y = pair[1] array.add(pnt) print " X %20.16f Y %20.16f" % (pnt.X, pnt.Y) array.add(array.getObject(0)) poly = arcpy.Polygon(array) 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)
It just needs to be present on the computer. It need not be running but arcpy needs to be imported which means it must be on the computer. gp.Addmessage statements need to be replaced with print statements. The following is an example''' CreatePolyDemo.py Demonstrates calculating various properties for polygons ''' import arcpy 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 print "\n", labels," Coordinates: ", a_poly for pair in a_poly: pnt.X = pair[0] pnt.Y = pair[1] array.add(pnt) print " X %20.16f Y %20.16f" % (pnt.X, pnt.Y) array.add(array.getObject(0)) poly = arcpy.Polygon(array) 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)
Frank...unfortunately it doesn't print to other IDE's output...don't use console...too dark and gloomy and reminds me of the old DOS days
# Helper methods for writing messages, warnings and errors to the geoprocessor as well as a logfile # By default, prepends a timestamp to the message; set second argument to 0 or False to disable def message(strMessage, stamp = True): global gp, logFile if stamp == True: strMessage = time.asctime() + " - " + strMessage #print strMessage gp.AddMessage(strMessage) if logFile: logFile.write(strMessage + "\n") logFile.flush() def warning(strMessage, stamp = True): global gp, logFile if stamp == True: strMessage = time.asctime() + " - " + strMessage #print strMessage gp.AddWarning(strMessage) if logFile: logFile.write(strMessage + "\n") logFile.flush() def error(strMessage, stamp = True): global gp, logFile if stamp == True: strMessage = time.asctime() + " - " + strMessage #print strMessage gp.AddError(strMessage) if logFile: logFile.write(strMessage + "\n") logFile.flush()
Frank...unfortunately it doesn't print to other IDE's output...don't use console...too dark and gloomy and reminds me of the old DOS days
It's for that reason that I wrote my own message(), warning() and error() methods. In addition to logging to a text file, it's exactly 3 places that I need to change if I need print statements or not, not dozens of lines littered throughout the script.
Most of the time I don't need print messages, but if I'm debugging in PyScripter or PythonWin (but not winpdb which runs the script in a separate console window from the GUI), I'll uncomment my print statement in each of the 3 methods.# Helper methods for writing messages, warnings and errors to the geoprocessor as well as a logfile # By default, prepends a timestamp to the message; set second argument to 0 or False to disable def message(strMessage, stamp = True): global gp, logFile if stamp == True: strMessage = time.asctime() + " - " + strMessage #print strMessage gp.AddMessage(strMessage) if logFile: logFile.write(strMessage + "\n") logFile.flush() def warning(strMessage, stamp = True): global gp, logFile if stamp == True: strMessage = time.asctime() + " - " + strMessage #print strMessage gp.AddWarning(strMessage) if logFile: logFile.write(strMessage + "\n") logFile.flush() def error(strMessage, stamp = True): global gp, logFile if stamp == True: strMessage = time.asctime() + " - " + strMessage #print strMessage gp.AddError(strMessage) if logFile: logFile.write(strMessage + "\n") logFile.flush()
import logging import sys _GP_MESSAGE_DEFS = { logging.CRITICAL : "AddError", logging.ERROR : "AddError", logging.WARNING : "AddWarning", logging.INFO : "AddMessage", logging.DEBUG : "AddMessage", } class GPHandler(logging.Handler): """ Geoprocessor friendly logging module """ def __init__(self, level=logging.NOTSET, gp=None): self.gp = gp logging.Handler.__init__(self, level) def emit(self, record): """ Woop. """ message = self.format(record) if self.gp: source = gp else: source = __import__("arcpy") # retrive the functor and call it with the message getattr(source, _GP_MESSAGE_DEFS[record.levelno])(message) _logger = logging.getLogger("test") _logger.setLevel(logging.DEBUG) # sadily i have no idea where this object type is located if type(sys.stdout) != "<type 'geoprocessing sys.stdout object'>": handler = logging.StreamHandler() else: # change for arc9.3 handler = GPHandler() handler.setFormatter(logging.Formatter("%(asctime)10s | %(name)s | %(message)s", "%Y%m%d %H:%M:%S")) _logger.addHandler(handler) _logger.debug("Message") _logger.info("Info message") _logger.warning("Warning") _logger.error("error") _logger.critical("error")
20101103 21:36:45 | test | Message 20101103 21:36:52 | test | Info message WARNING: 20101103 21:36:52 | test | Warning ERROR: 20101103 21:36:52 | test | error ERROR: 20101103 21:36:52 | test | error
print x is a syntax error in Python 3.0, so you're not future-proofing your code nearly as well as you think.
Starting from 2.6, you can import print as a function from the future with from __future__ import print_function directive and use print as a 3.x style function across the board in your script.