Self-increment field Calculator by ArcMap property fields tips for using Python

Source: Internet
Author: User

1. Preface

some days ago, someone asked me, what is the way to make a field's value self-increment in ArcMap? I first think of a numeric field like SQL Server that can be set to self-increment. So I open the arccatalog. See only the default values are provided, there is no way to see if there is a better method in the field calculator. The beginning of my method is that the value that will be self-increment in the field calculator equals the default Objectid value, but again, if you delete an object, the Objectid value becomes chaotic and does not achieve self-increment. I learn the main language is C #, for the field calculator supported by the VB script and Python do not understand, for C # to write A For loop is easy to achieve self-increment. Finally, refer to the Help document to find the Python implementation method, the following share to you.

2. Help document resolution

Open The calculated field sample document found (cumulative and sequential calculations)

First analyze the code provided by the Help document (note: The red part is commented)

Parser:
Python #程序语言

Expression:
AutoIncrement () #表达式方法

Code Block: #下面这部分是具体实现, note that only the following section is required when placing a field calculator
Rec=0
Def autoincrement (): #定义方法名
Global REC
Pstart = 1 #初始值, can be adjusted
Pinterval = 1 #自增间隔, can be adjusted
if (rec = = 0):
REC = Pstart
Else
REC = rec + pinterval
Return rec
Here's how to use it in the field calculator

Objective to achieve the self-increment of GHG field

Open the field calculator to set the following

Note: Python will force indentation as part of the syntax. Use two or four spaces to define each level of logic. Aligns the beginning and end of the statement block and remains consistent.

View results for self-increment

3. Improve

In fact, using Python is very convenient, if there is no python to achieve self-enhancement for a large project is very labor-intensive. Speaking of the above method, the custom function does not have input parameters, if we need to input parameters how to write it. See the Help documentation for an example that calculates the accumulated value of a numeric field we analyze.

Parser:
Python

Expression:
Accumulate (!   fielda!) #该函数需要输入字段做参数

Code Block: #下面这部分是具体实现, note that only the following section is required when placing a field calculator
Total = 0
def accumulate (increment):
Global Total
If total:
Total + = Increment
Else
Total = Increment
Return Total

Objective to realize the field of Objectid accumulated value to the GHG

Note: The Python calculation expression field will be enclosed with an exclamation point (!!).

Results:

How to use the 4.Python

It's easy to get started with a simple Python syntax for someone with programming experience, like the basic grammar of java,c# and other languages,

For example, control flow,if statement 2. While statement 3. For loop for: In 4. Break Statement 5. Continue statements are available in almost all major languages.

Other Python libraries, such as the random function, can also be used in arcmap to implement random values.

For example, a random floating-point value between 0.0 and 1.0 is calculated by NumPy the site package.

Parser:
Python

Expression:
Getrandomvalue ()

Code Block:
Import Numpy.random as R

Def getrandomvalue ():
Return R.random ()
5. Summary

Language is a tool, you can be more confident about programming as long as you are proficient in a language. You can solve the problem no matter what language you use. The idea of programming can greatly improve your productivity, so friends you must at least learn a language in order to work better.

Reference: ArcGIS10.2 Help Library

Techniques for using python from the ArcMap property field self-increment field calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.