官术网_书友最值得收藏!

Using variables to store data

In Chapter 1, Fundamentals of the Python Language for ArcGIS, we covered the topic of variables, so you should have a basic understanding of these structures. Variables are given a name and assigned a data value in your scripts. These named variables occupy space in your computer's memory and the data contained within these structures can change while a script is running. After the script has finished the memory space occupied by these variables is then released and can be used for other operations.

Getting ready

When writing geoprocessing scripts with Python there will be many times when you will need to create variables to hold data of one type or another. This data can then be used in your script as input parameters for tools and functions, as intermediate data for internal processing, to hold paths to datasets, and for other reasons. In addition, many of the ArcPy functions and tools also return data that can be stored in a variable for further use in your script. In this recipe, you will learn the basic techniques for creating variables and assigning data to them.

How to do it...

Follow these steps to create a script that contains variables that are hardcoded with values and that are returned from a function:

  1. Open IDLE and create a new script window.
  2. Save the script to c:\ArcpyBook\Ch2\WorkingWithVariables.py.
  3. Import the arcpy package:
    import arcpy
  4. Create a variable called path and assign a value to it:
    path = "c:/ArcpyBook/data"
  5. Use the newly-created variable to set the workspace:
    arcpy.env.workspace = path
  6. Call the ListFields() function and assign the returned value to a new variable called fields:
    fields = arcpy.ListFields("Building_Permits.shp")
  7. Start a for loop to process each of the field objects contained within the fields variable:
    for fld in fields:
  8. Print the name of each field:
    print fld.name
  9. The entire script should appear as follows:
    import arcpy
    path = "c:/ArcpyBook/data"
    
    arcpy.env.workspace = path
    fields = arcpy.ListFields("Building_Permits.shp")
    for fld in fields:
           print fld.name
  10. Save the script.

How it works...

We created three variables in this script. The first variable, path, was created and assigned a hard-coded value with a data path. This is an example of a literal variable, meaning that they literally mean exactly what they say. They are distinguished from variables, whose values are not directly determined by their name. The second variable, fields, is created from the returned value of the ListFields() function and is a Python list object containing one or more Field objects. Each Field represents a field from the attribute table of a feature class or a standalone table. The final variable is a dynamic variable called fld. As the for loop cycles through the list returned by the ListFields() function, each Field is assigned to the fld variable. The name of each field is then printed to the screen.

主站蜘蛛池模板: 冷水江市| 长治县| 宁武县| 南部县| 泰安市| 丰都县| 滨州市| 广东省| 土默特左旗| 北碚区| 闽清县| 黔南| 隆德县| 岳普湖县| 察隅县| 长治市| 尼玛县| 北安市| 义乌市| 靖西县| 玉环县| 连平县| 西丰县| 大港区| 阿鲁科尔沁旗| 九江县| 台江县| 邳州市| 阜新| 张家港市| 定结县| 闻喜县| 高台县| 肃宁县| 临洮县| 奇台县| 高碑店市| 张家口市| 陈巴尔虎旗| 合作市| 光泽县|