python for the non-scripter

17
© 2013 TeachMeGIS All rights reserved. Revised 05/13 1 1 [email protected] Python for the Non-Scripter 2 Presentation Overview • What is Python? • Python Essentials • Extra Tidbits • Using Python in ArcMap • Label Expression • Field Calculator Python For the Non-Scripter

Upload: subodh-bhargav

Post on 07-Nov-2015

230 views

Category:

Documents


1 download

DESCRIPTION

Python for the Non-Scripter

TRANSCRIPT

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 1

    1

    [email protected]

    Python for the Non-Scripter

    2

    Presentation Overview

    What is Python?

    Python Essentials

    Extra Tidbits

    Using Python in ArcMap

    Label Expression

    Field Calculator

    Python For the Non-Scripter

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 2

    3

    And how does it fit into ArcGIS?

    What is Python?

    4

    Python

    Programming language

    Object-oriented

    Started in early 1990s by Guido van Rossum

    Named after Monty Pythons Flying Circus

    Free

    Quick to learn

    Easy to code

    Easy to read

    Many users (lots of help)

    What is Python?

    Why Do We Love It?

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 3

    5

    Python and ArcGIS Desktop

    Python is infiltrating ArcMap

    More with each release

    Label Expression

    Field Calculator

    Python Window

    6

    Python Essentials

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 4

    7

    Python the Essentials

    Keywords

    Special instructions

    Case sensitive

    Shown in blue in Python window

    Examples: print

    import

    if

    not

    and

    Variables

    Hold a value

    Set on the fly

    Value sets type

    Case sensitive

    No keywords

    Start with letter

    No special characters

    Functions

    Action

    Multiply

    Concatenate

    Extract

    Upper case

    Split a list

    HINT:

    Variables are objects

    8

    Python Window

    Command line window in ArcMap

    Great place for practicing Python code (and getting help)

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 5

    9

    Data Types for Variables

    VARIABLE

    Boolean

    True

    False

    String

    Number

    Integer

    Float

    String

    Text value

    Double quotes (O&G)

    Number

    Integer (whole number)

    Float (decimal)

    Boolean

    True (1)

    False (0) =

    10

    Python Syntax: Numbers

    Integers and floats

    Variable assignment determines type

    + - * /

    Dont forget operator precedence:

    Parentheses

    Exponents

    Multiplication, division

    Addition, subtraction

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 6

    11

    Python Syntax: Strings

    Surrounded by quotes

    Double

    Single

    Backslash (\) is a special character

    Escape

    12

    String Tools

    Length

    len()

    Concatenation

    +

    Repetition

    *

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 7

    13

    Functions

    Typical format: .()

    hello.upper()

    Use dir() to get a list of functions

    14

    Extra Tidbits

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 8

    15

    Directory Paths

    Cannot use single backslashes in a directory path

    Use:

    \\

    /

    r

    C:\\Student\\ICTPython

    C:/Student/ICTPython

    rC:\Student\ICTPython

    16

    String Indexing

    Square brackets [ ]

    Index returns single character

    0-based

    Negative counts backwards from end

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 9

    17

    String Slicing

    Slice returns a substring:

    2 inputs (simple)

    Start, end

    3 inputs (extended)

    Start, end, and stride

    All inputs are 0-based

    Negative inputs count backwards from end

    Non-inclusive

    18

    On-the-Fly Conversion

    Convert text and numeric data types on the fly:

    str()

    int()

    float()

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 10

    19

    Lists

    Surrounded by square brackets []

    Elements separated by a comma

    Can contain different data types

    Can be sliced and indexed like strings

    20

    Using Python in ArcMap

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 11

    21

    Label Expression

    Switch the Parser to Python

    Write the expression

    Combine values

    Add static strings

    Add new lines

    Convert values

    Multiple by conversion factor

    All done on-the-fly

    No write access to data required

    [CO_NAME] + "\n" + [PROD_CODE]

    22

    Combination Tricks

    E X P R E S S I O N R E S U LT

    [PointX] + , + [PointY] 223.43, 5356.5

    [County_Name] + County, +

    [State]Harris County, TX

    [Company] + \n + [Address]TeachMeGIS3000 Wilcrest

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 12

    23

    Field Calculator

    Switch the Parser to Python

    Write the expression

    Copy values

    Combine values

    Add strings

    Convert values

    Multiple by conversion factor

    Change data types

    QA data

    Set string case

    "Submerged Pile" + !OBJNAM!

    24

    Number Tricks

    E X P R E S S I O N R E S U LT

    !Elevation_ft! * 0.3048 FeetMeters

    !Elevation_m! * 3.2808399 Meters Feet

    !Area_km2! * 247.105381 km2 Acres

    !Area_acres! * 0.00404685642 Acres km2

    round(!PROD! * !Royalty!, 2) $

    !Part1! / !Total! %

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 13

    25

    String Tricks

    Built-in Functions

    .upper()

    .lower()

    .title()

    .replace(, )

    String slicing!Company!.upper()

    26

    Now Put it All Together

    Functions

    Field Values

    Static Values

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 14

    27

    SCENARIO:

    Have a string formatted like: nnnn ssss ttt vvvv xxx yyyy

    TASK:

    Format in new field as: ttt vvvv xxx yyyy

    Example: String Functions

    WELL_NAME:

    1200 RAAA SUA HOLY BEA CALI

    NEW WELL_NAME FIELD:

    SUA HOLY BEA CALI

    28

    Example: String Functions

    SOLUTION: " ".join(!WELL_NAME!.split()[2:])

    WELL_NAME Column with original data1200 RAAA SUA HOLY BEA CALI

    !WELL_NAME!.split()

    Splits the string at the spaces and puts the pieces in a list

    1200, RAAA, SUA , HOLY,BEA, CALI

    !WELL_NAME!.split()[2:]

    Removes the first 2 values from the list

    SUA , HOLY,BEA, CALI

    .join()Creates a string from the values in separated by a space

    SUA HOLY BEA CALI

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 15

    29

    Help and Resources

    Learning Python

    Python in a Nutshell

    Books

    http://support.esri.com

    http://resources.arcgis.com

    http://www.python.org

    http://www.diveintopython.org

    Websites

    30

    Show Codeblock

    Advanced Calculations

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 16

    31

    Show Codeblock Calculations

    Write function in Pre-Logic Script Code section

    Call function (with inputs) in Expression section

    Returned value is written to the table

    Expression

    Function

    32

    Pick Non-Null Value

    TASK:

    Populate a column with either API or UWI (whichever exists)

    PickNonNullString(str(!API!), str(!UWI!))

    def PickNonNullString(val1, val2):valOut = val2if val1:valOut = val1

    return valOut

  • 2013 TeachMeGIS All rights reserved.

    Revised 05/13 17

    33

    Any questions?

    [email protected]