If you want a standard template for working with python, Esri provided a great blog post on just that!
http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2011/08/04/PythonTemplate.aspx
I recommend you use it to make life easier.
Showing posts with label PythonWin. Show all posts
Showing posts with label PythonWin. Show all posts
Monday, August 8, 2011
Tuesday, April 27, 2010
Google Charts
Google charts is a great/free resource for doing charting. Many organizations have restrictions on what software you can install, but with google charts no software is needed, just an internet connection. See this link.
Here is a simple example:
To get this image, all you have to do is reference this url: http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
You can simply couple this with python using the webbrowser library. To open the image in a web browser via python try this:
webbrowser.open(url)
Here is a simple example:
To get this image, all you have to do is reference this url: http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
You can simply couple this with python using the webbrowser library. To open the image in a web browser via python try this:
import webbrowser
url = http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World
webbrowser.open(url)
Thursday, March 4, 2010
Singleton Pattern Design for Python
There are three categoris of OO Patterns:
A Singleton is a way to ensure that you cannot create more than one instance of a class. A class attribute could be used to check the number of instantiations of the class.
The singleton patter requires a mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among class objects. The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made protected.
As a note: "The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation." ~From http://en.wikipedia.org/wiki/Singleton_pattern
Here are two examples of Singleton Patterns:

This will produce an output of:
Here is a second singleton implementation that returns the same instance of the singleton instead of throwing an error:

I would recommend starting to use Design Patterns in your ArcGIS Python code. With the coming of 10, it will be more important than ever to have code that conforms to standard coding practices.
The goal of any OO programming is the following:
- Creational - patterns that can be used to create objects
- Structural - patterns that are used to combine object and classes in order to build structured objects
- Behavioral - patterns that can be used to build computation and control the flow of data
A Singleton is a way to ensure that you cannot create more than one instance of a class. A class attribute could be used to check the number of instantiations of the class.
The singleton patter requires a mechanism to access the singleton class member without creating a class object and a mechanism to persist the value of class members among class objects. The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made protected.
As a note: "The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation." ~From http://en.wikipedia.org/wiki/Singleton_pattern
Here are two examples of Singleton Patterns:
This will produce an output of:
I would recommend starting to use Design Patterns in your ArcGIS Python code. With the coming of 10, it will be more important than ever to have code that conforms to standard coding practices.
The goal of any OO programming is the following:
- Genericity - is a technique to define parameters for a module exactly as you define parameters for a function thus making the module more general
- Flexibility - very difficult to achieve, but functions should be written as general as possible to allow for multiple data types
- Inheritance - is the ability in OO Programming to derive a class from another, either by extending it or specialize it
- Overloading - refers to the possibility for an operator or method to behave differently according to the actual data types of their arguments
- Polymorphism - is the possibility for something to have several forms.
Monday, October 12, 2009
Listing Only Directories Using Python
Sometimes it's necessary to list directories, but when the os.listdir() method is called, the return list contains both folders and files. To fix this do the following:
Methods like this are very helpful when you need to use tool validation. It allows you to quickly list all data in a directory, so users can select what they need.
You'll notice that the script has a find(). This is used because file geodatabases are essentially folders and therefore need to be excluded from the generated list.
Enjoy
dirs = [ name for name in os.listdir(--Directory--)) if os.path.isdir(os.path.join(str(--Directory--), name))]
for d in dirs:
d = str(d)
d = d.upper()
if d.find(".GDB") == -1:
outString = outString + str(d) + "; "
Methods like this are very helpful when you need to use tool validation. It allows you to quickly list all data in a directory, so users can select what they need.
You'll notice that the script has a find(). This is used because file geodatabases are essentially folders and therefore need to be excluded from the generated list.
Enjoy
Wednesday, June 24, 2009
Create an Extent Polygon using Python
If you are like me, sometimes you want to give the user options on how they can select data. So I came up with this little number. I use this if the user does not draw a polygon, polyline, or point. If that condition is met, then the script will generate a full extent polygon and use that as it's selecting bounding box.
The function CreatePolygonExtent takes two objects: the first is the gp object, and the second is the input feature. It exports a geometry object that contains the polygon shape. The next function, InsertGeom() just is a simple insert cursor function. It takes a multiple number of inputs, and they are: gp, polyGeom, feat, ws. The gp object is the geoprocessing reference, polyGeom is the value from the CreatePolygonExtent(), feat is the name of the output feature, and ws is the saved workspace.
Outside of the two functions, the script generates a file geodatabase in the scratch workspace. This is where the temporary results will be saved.
Enjoy
Here is the code:
The function CreatePolygonExtent takes two objects: the first is the gp object, and the second is the input feature. It exports a geometry object that contains the polygon shape. The next function, InsertGeom() just is a simple insert cursor function. It takes a multiple number of inputs, and they are: gp, polyGeom, feat, ws. The gp object is the geoprocessing reference, polyGeom is the value from the CreatePolygonExtent(), feat is the name of the output feature, and ws is the saved workspace.
Outside of the two functions, the script generates a file geodatabase in the scratch workspace. This is where the temporary results will be saved.
Enjoy
Here is the code:
import arcgisscripting, os, sys
def CreatePolygonExtent(gp, in_Feat):
desc = gp.describe(in_Feat)
extent = desc.extent
# Create an Array object.
#
ary = gp.createobject("Array")
# List of coordinates.
#
coordList = [str(extent.xmin)+";"+str(extent.ymin),str(extent.xmin)+";"+str(extent.ymax),str(extent.xmax)+";"+str(extent.ymax),str(extent.xmax)+";"+str(extent.ymin)]
# For each coordinate set, create a point object and add the x- and
# y-coordinates to the point object, then add the point object
# to the array object.
#
for coordPair in coordList:
pnt = gp.createobject("Point")
x, y = coordPair.split(";")
pnt.x = x
pnt.y = y
ary.add(pnt)
# Create a polygon geometry object using the array object
# created from the coordinate list above.
#
polyGeom = gp.createobject("geometry", "polygon", ary)
return polyGeom
def InsertGeom(gp, polyGeom, feat, ws):
out_feat_class = "ExtentFeature"
gp.CreateFeatureclass(ws, feat, "POLYGON")
rows = gp.insertcursor(ws + os.sep + feat)
row = rows.newrow()
row.Shape = polyGeom
rows.insertrow(row)
del rows, row
return feat
gp = arcgisscripting.create(9.3)
gp.scratchworkspace = r"c:\temp"
in_Feat = gp.getparameter(0)
polyGeom = CreatePolygonExtent(gp, in_Feat)
out_feat_class = "ExtentFeature"
gp.CreateFileGDB(gp.scratchworkspace, "BBoxHolder.gdb")
ws = gp.scratchworkspace + "\\BBoxHolder.gdb"
out_feat_class = "ExtentFeature"
returnFeat = InsertGeom(gp, polyGeom, out_feat_class, ws)
gp.setparameterastext(1, str(ws + os.sep + out_feat_class))
Thursday, June 18, 2009
PythonWin and Windows XP
When I reformatted my hard drive the other day, I didn't think installing pythonwin would be such a hassle, but it was.
Apparently Microsoft has deprecated some libraries in later versions of its dot NET framework. In order to solve the missing dll problem that you might encounter, go here: http://starship.python.net/crew/mhammond/win32
and download the dlls posted on this site. Place them in the ..\windows\system32 directory.
Pythonwin should now work.
Apparently Microsoft has deprecated some libraries in later versions of its dot NET framework. In order to solve the missing dll problem that you might encounter, go here: http://starship.python.net/crew/mhammond/win32
and download the dlls posted on this site. Place them in the ..\windows\system32 directory.
Pythonwin should now work.
Subscribe to:
Posts (Atom)