From the resource center of ESRI, comes a pdf that contains all the different type of GeoProcessing data types with correlating ArcObjects in it. This is very helpful, and any .NET GP developer should have it printed out and right next to them.
Enjoy
Wednesday, July 15, 2009
Thursday, July 9, 2009
2 GP Functions That Make Life Easier
Here are two functions that make life easier for custom GP development. The find parameter value by the parameter name, return parameter type IGPValue. The second method is get parameter by name, which returns IGPParameter3. Parameter value by name allows you to retrieve
Get Parameter By Name
Get Parameter By Name
public IGPParameter3 GetParameterByName( IArray paramvalues, string name )
{
IGPParameter3 gpParameter;
for (int i = 0; i < paramvalues.Count; i++)
{
gpParameter = (IGPParameter3)paramvalues.get_Element( i );
if (gpParameter.Name.Equals( name, StringComparison.OrdinalIgnoreCase ))
return gpParameter;
}
return null;
}
Get Parameter Value By Name
public IGPValue GetParameterValueByName( IArray paramvalues, string name )
{
IGPUtilities2 gpUtils = new GPUtilitiesClass();
IGPParameter3 gpParameter;
for (int i = 0; i < paramvalues.Count; i++)
{
gpParameter = (IGPParameter3)paramvalues.get_Element( i );
if (gpParameter.Name.ToUpper() == name.ToUpper())
return gpUtils.UnpackGPValue( gpParameter );
}
return null;
}
Wednesday, July 1, 2009
Creating a Feature Set via C#
A very powerful model builder feature is the create variable option. This will allow users to create almost any type of user input desire. The feature set variable is a very helpful variable when a user needs to interact with the map or recordset. The feature set allows users to draw points, polylines, or polygons on the fly. To do this in C#, you need to use the IGPRecordSetLayerClass(), but the process of making the interactive part work isn't that straight forward.
To get the interactive part of the Feature set to work, you must define the Schema with either a layer file or feature class in model builder. When you take a look at the ArcObject description of a parameter, you notice a pointer called Schema, but this isn't where you define the template data for a GPFeatureRecordSet, you define it in Value().
To get the interactive part of the Feature set to work, you must define the Schema with either a layer file or feature class in model builder. When you take a look at the ArcObject description of a parameter, you notice a pointer called Schema, but this isn't where you define the template data for a GPFeatureRecordSet, you define it in Value().
inputParameter.DataType = new GPFeatureRecordSetLayerTypeClass();
//
// create the GP Feature Recordset Layer Object
// from template data
//
IFeatureClass FC = (IFeatureClass)m_GPUtilities.OpenDatasetFromLocation( @"" );
IRecordSetInit recordset = new RecordSetClass();
recordset.SetSourceTable( (ITable)FC, null );
IGPRecordSet gpRS = new GPRecordSetClass();
gpRS.RecordSet = (IRecordSet)recordset;
IGPRecordSet gpR = new GPFeatureRecordSetLayerClass();
gpR.RecordSet = new RecordSetClass();
gpR.RecordSet = (IRecordSet)recordset;
IGPFeatureRecordSetLayer gpFRSL = (IGPFeatureRecordSetLayer)gpR;
//
// Make template Data the default value
//
inputParameter.Value = (IGPValue)gpFRSL;
//
// Set schema to Geometry Type and
// add it to schema parameter value
//
IGPFeatureSchema fgs = new GPFeatureSchemaClass();
fgs.GeometryType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline;
IGPSchema GPschema = (IGPSchema)fgs;
inputParameter.Schema = GPschema;
//
// set the geometery type domain
//
IGPFeatureClassDomain geomTypeDomain = new GPFeatureClassDomainClass();
geomTypeDomain.AddType( esriGeometryType.esriGeometryPolyline );
inputParameter.Domain = (IGPDomain)geomTypeDomain;
//
// Enable the value
//
inputParameter.Enabled = true;
//
// Set input direction
//
inputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;
//
// Set the name and Dispaly Name
//
inputParameter.DisplayName = "Barriers";
inputParameter.Name = "LineBarrier";
//
// Set required, optional, or derivived data
//
inputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeRequired;
// add parameter
parameters.Add( inputParameter );
The result should be an interactive dataset that you can use for interactive Geoprocessing.
Enjoy
Friday, June 26, 2009
Using a ToolValidator to create a Value List
New at 9.3, ESRI has introduced the ToolValidator. To many this is old news, because we are on 9.3.1, but to some, it's new due to the fact that it's not talked about much. The toolvalidator allows for more control of the GUI without messing around with ArcObjects or .NET.
More information on ToolValidators can be found here.
Moving on let's get to an example. Say you want to populate a string field with a predefined list of object or what is known as a value list to the GP world. Simply right click your script and click on the Validation Tab. Next click on edit, and you are ready to begin.
By default, IDLE should open, unless you customized your system to use pythonwin. You should see something like this:

Since you want the value like to show up when the GUI is fired, use the initializeParameters(self) method.
Now just create a simple search cursor, and append in the values from the table you want to force the user to choose like such:
Now you have a drop down menu from which a user is forced to select from. The best thing is that it's dynamic, so when the table changes so does the selection.
Enjoy
More information on ToolValidators can be found here.
Moving on let's get to an example. Say you want to populate a string field with a predefined list of object or what is known as a value list to the GP world. Simply right click your script and click on the Validation Tab. Next click on edit, and you are ready to begin.
By default, IDLE should open, unless you customized your system to use pythonwin. You should see something like this:
Since you want the value like to show up when the GUI is fired, use the initializeParameters(self) method.
Now just create a simple search cursor, and append in the values from the table you want to force the user to choose like such:
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
import os, sys, arcgisscripting as ARC
self.GP = ARC.create(9.3)
self.rows = self.GP.searchcursor(r"\CountryCodes.dbf")
self.row = self.rows.next()
self.holder = []
while self.row:
self.holder.append(self.row.getvalue('CC'))
self.row = self.rows.next()
self.holder.append('*')
self.holder.sort()
self.params[1].Filter.List = self.holder
return
Now you have a drop down menu from which a user is forced to select from. The best thing is that it's dynamic, so when the table changes so does the selection.
Enjoy
Labels:
GP Task,
Python,
ToolValidator
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.
Monday, June 15, 2009
Custom GP Task Parameter in C#
I'm current writing a custom GP tool that maps out all the input parameters for a given mxd file, and I have been having trouble with the input parameter of type file. After much searching, I realized that the input parameter needs to be of type DEFileTypeClass. This will allow a user to select any file type. So in my case, a user can select the .mxd file.
Here is a snippet on how to create that input:
param = new GPParameterClass();
param.DataType = new DEFileTypeClass();
param.Name = "MXD_FILE";
param.Value = new DEFileTypeClass().CreateValue( "" );
param.DisplayName = "MXD_FILE";
param.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;
param.ParameterType = esriGPParameterType.esriGPParameterTypeRequired;
param.Enabled = true;
Subscribe to:
Posts (Atom)