session 3: python geoprocessing - dave scripts in arcmap workshop march 24, 2014 arcpy geoprocessing...

Click here to load reader

Post on 05-May-2018

215 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 1

    Session 3: Python Geoprocessing

    In this session we use ArcGIS geoprocessing tools in the Python window. Typically

    you first set your environment and extensions. For example, copy (Ctrl-C)

    following from the arcpy_Geoprocessing.txt file and paste (Ctrl-V) each line into

    your arcmap Python window

    This is analogous to when you are working interactively and manually set these

    environment variables:

    You must also load an extension to get access to the geoprocessing tools

    associated with an extension. For example,

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 2

    arcpy.CheckInExtension("spatial")

    randomRaster = arcpy.sa.CreateRandomRaster()

    results in..

    So you need to check out the spatial analyst extension first, then you have

    access to all the tools associated with that extension.

    arcpy.CheckOutExtension("spatial")

    press the up-arrow key to recall your .CreateRandomRaster() line and then execute that line

    once again by pressing the Enter key

    Your random raster will have pixel values ranging from 0.0 up to 1.0

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 3

    Executing Geoprocessing Tools

    First use ArcGIS Help to read about the geoprocessing tool and look at an example

    Python script..

    .

    .

    Test drive the tool to create one shapefile containing 20 random pointscopy and

    paste the Python geoprocessing line from your arcpy_Geoprocessing.txt file to

    your arcmap Python window.

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 4

    If you want to skip a geoprocessing tool parameter, use the #..what 2

    parameters are skipped in the above example?

    See if you can figure out how to run the Add XY geoprocessing tool with your

    random points layer in your Python window.this geoprocessing tool will create

    the fields Point_X, Point_Y:

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 5

    Geoprocessing Results

    Every time you execute a Geoprocessing tool, the results are output to the Results

    window

    You can access the same messages as text strings in your Python window.

    Copy and paste the following from your arcpy_Geoprocessing.txt file:

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 6

    So Message 0 is :

    Message 1 is :

    Message 2 is :

    Geoprocessing Loops

    Now lets create 20 shapefiles, each containing 20 random pointsand output to a

    log file the results of the geoprocessing. Copy and paste the following from your

    text file

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 7

    Here you opened a new file names CreateRandomPoints.log, then created a

    loop going from 1 to 20, naming our output shapefiles RandomPts1.shp,

    RandomPts2.shp . . . RandomPts20.shp and outputting the geoprocessing results

    to your file.

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 8

    Arcpy Listing Commands

    There is a suite of arcpy .List commands..

    Try the following to obtain a list of all feature classes that are POINT shapetype,

    and end with .shp.

    Lets create a list of point feature classes that start with Random and merge

    these into one point feature class:

    Open your point attribute tableyou should have 400 records

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 9

    Or you can use the arcpy .GetCount() geoprocessing command:

    The .ListFields command returns a list of field objects. Each field object has

    properties

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 10

    Try the following:

    So the testFC has three fields: FID, Shape, and ID

    To access field values, you will use the data access cursorswe will cover that in

    the last session.

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 11

    Getting Feature Counts

    You can use the arcpy .GetCount command to get the count of features in a

    feature class. The result of the .GetCount command can then be converted to a

    string. For example:

    Describing Feature Classes

    The arcpy .Describe command can be used to get properties about objects such as

    workspace, table, raster, and feature class properties. Try the following:

    shapeType will be a text string..

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 12

    Extent will be an object with properties.

    Try the following:

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 13

    Lets work with a feature class that has the projection defined, and then look at its

    spatial reference properties.

    If a polygon feature class is projected, you can use the Shape.area property to

    compute polygon area in Hectares, Acres, etc.

    Try the following Python scriptif the feature class is a polygon shape type, check

    that the coordinate system is projected, if it is, add a field named Ha and

    calculate the polygon areas in hectares

  • Python Scripts in ArcMap Workshop March 24, 2014

    arcpy Geoprocessing Page 14

    Typically your script will add the field if it does not already exist. Try the

    followinguse the .ListFields to search for a field named Hectares and then

    search for a field named Ha

    And finally the modified script that checks if the field Ha already exists

    Take a 10-minute break.

    The next session will be creating Python script tools in your personal toolbox

View more