Use classes in Python

Une classe est analogue à un plan architectural. Le plan fournit la structure pour une création. Les classes permettent de créer des objets, souvent nommés instances. Les classes ArcPy, telles que les classes SpatialReference et Extent, sont souvent utilisées comme raccourcis vers des paramètres d’outil de géotraitement complets qui présenteraient autrement un équivalent de chaîne plus compliqué.

ArcPy includes several classes, such as SpatialReference, ValueTable, and Point. Once instantiated, A class's properties and methods can be used. Classes include one or more methods called constructors. A constructor is a method for initializing a new instance of a class. In the example below, SpatialReference(prjFile) is the class constructor—it creates the spatialRef object by reading a projection file.


import arcpy

prjFile = "c:/projections/North America Equidistant Conic.prj"
spatialRef = arcpy.SpatialReference(prjFile)

As with most other classes, SpatialReference contains a number of methods and properties. Building on the previous sample, you can access the properties of spatialRef.


import arcpy

prjFile = "c:/projections/North America Equidistant Conic.prj"
spatialRef = arcpy.SpatialReference(prjFile)

# Print the SpatialReference's name, and type
#
print(spatialRef.name)
print(spatialRef.type)

Classes can be used repeatedly. In the following example, two unique point objects are created using the Point class:


import arcpy

pointA = arcpy.Point(2.0, 4.5)
pointB = arcpy.Point(3.0, 7.0)
Remarque :

The CreateObject function can also be used to create many of the objects that can be created using classes. However, using classes is easier and more readable.

Use classes with geoprocessing tools

Tool parameters are usually defined using simple text strings. Dataset names, paths, keywords, field names, tolerances, and domain names can be specified using a quoted string.

Some parameters are harder to define using simple strings; they are more complex parameters that require many properties. Instead of using long, complicated text strings to define these parameters, you can use classes (for example, SpatialReference, ValueTable, and Point classes). The documentation for each tool contains a scripting example of how each tool parameter is defined and used.

In the following example, a SpatialReference object is created and used to define the output coordinate system of a new feature class created using the Create Feature Class tool.


import arcpy

inputWorkspace = "c:/temp"
outputName =  "rivers.shp"

prjFile = "c:/projections/North America Equidistant Conic.prj"
spatialRef = arcpy.SpatialReference(prjFile)

# Run CreateFeatureclass using the spatial reference object
arcpy.CreateFeatureclass_management(inputWorkspace, outputName, "POLYLINE", 
                                    spatial_reference=spatialRef)

The string equivalent for this parameter is similar to this: PROJCS["North_America_Equidistant_Conic",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Equidistant_Conic"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-96.0],PARAMETER["Standard_Parallel_1",20.0],PARAMETER["Standard_Parallel_2",60.0],PARAMETER["Latitude_Of_Origin",40.0],UNIT["Meter",1.0]];IsHighPrecision

Rubriques connexes