Operations on ArcGIS Python graphics

Source: Internet
Author: User
Tags mathematical constants

Calculated field (Data Management)

ArcGIS 10 Summary

Calculates the value of a field for the element class, element layer, or raster directory.

View examples of using the calculated Field Tool

Usage
  • Python expressions can be created through attributes (type, extent, centroid, firstpoint, lastpoint, area, length, ismultipart, and partcount) in geometric objects.

    !shape.area!
  • Python expressions can use the geometric area and length attributes with an area or linear unit to convert values to different measurement units (for example! Shape. Length @ kilometers !). If the data is stored in a geographic coordinate system and has a linear unit (for example, a mile), the length is calculated using the geographic line algorithm. Using area units in geographic data produces incorrect results, because the decimal degrees along the globe are inconsistent.

    • Keyword of area measurement unit:

      • Acre | public acre | hectare | square centimeter | square meter | square inch | square foot | square kilometer | square meter | square millimeter | square meter | square map unit | unknown
    • Keyword of linear measurement unit:
      • Centimeter | decimal | meters | feet | inches | km | meters | Miles | millimeters | nautical miles | lbs | unknown | code
  • In the tool dialog box, you can enter the expression directly into the expression parameter, or use the "field calculator" to build the expression in interactive mode.

  • This tool updates only selected records when you use the selected feature set (for example, the feature set created from the create element layer or the Query within the select layer by attribute.

  • Each operation can only be applied to one field.

  • The existing field value will be overwritten. If you want to retain the original field value, create a copy of the input table

  • For Python computing, you must add an exclamation point (! Field name !).

    For VB calculation, the field name must be enclosed in square brackets ([field name]).

  • When calculating the string of a text or character field, double quotation marks ("string") must be added to the string in the dialog box. In the script, you must also add single quotation marks ('"string"') to strings with double quotation marks "').

  • This tool can also be used to update character items. Single quotation marks (for example, [charitem] = 'new string') should be added using a string expression '). However, if the string already contains single quotes, you must add double quotation marks to the string, for example, [charitem] = "type 'A '".

  • To calculate a value field, you can enter a value in the expression parameter. No quotation marks are required on either side of the value.

  • If a python expression is specified, this tool supports the ArcGIS. Rand () function. The ArcGIS. Rand () function has been created for the ArcGIS tool and should not be confused with the python rand () function. The available distribution Syntax of ArcGIS. Rand () functions is described in the random value distribution syntax.

  • Expressions and code blocks are connected to each other. The code block must return an association with the expression. The result of the code block should be passed into the expression.

  • Code block parameters can be used to create complex expressions. You can enter a code block directly in the dialog box or use the code block as a continuous string in the script.

  • Python mathematical modules and formats are available for code block parameters. You can import additional modules. The Mathematical Module provides number theory functions and expression functions, power functions and logarithm functions, trigonometric functions, angle conversion functions, hyperbolic functions, and mathematical constants. For more information about the Mathematical Module, see Python help.

  • The saved VB. Cal files of earlier versions of ArcGIS can be directly used or used after a few modifications. If you have the VBA code of the previous version that uses ArcObjects, the calculation must be modified before it can be used in 10.0.

  • When calculating the connection data, you cannot directly calculate the connection column. However, you can directly calculate the columns in the source table. To calculate the connection data, you must first add the connection table or connection layer to ArcMap. Then, you can calculate the data separately. These changes will be reflected in the connection column.

  • Calculated field example
Syntax calculatefield_management (in_table, field, expression, {expression_type}, {code_block })
Parameters Description Data Type
In_table

This table contains fields that will be updated through the new calculation.

Mosaic layer; raster catalog layer; raster layer; Table View
Field

Fields updated through the new calculation.

Field
Expression

Values created using a simple computing expression are used to fill the selected row.

SQL expression
Expression_type (optional)

Specifies the type of the expression to be used.

  • The VB-expression is written in the standard VB format. This is the default setting.
  • Python-expressions are written in standard Python format. The geographic processor method and attributes are used in the same way as the geographic processor version 9.2 is created.
  • Python_9.3-the expression will be written in standard Python format. The geographic processor method and attributes are used in the same way as the geographic processor version 9.3 is created.
String
Code_block (optional)

You can enter code blocks for complex expressions.

String
Code example: calculatefield example (Python window)

The following Python window script demonstrates how to use the calculatefield function in immediate mode.

import arcpyfrom arcpy import envenv.workspace = "C:/data"arcpy.AddField_management("vegtable.dbf", "VEG_TYP2", "TEXT", "", "", "20")arcpy.CalculateField_management("vegtable.dbf", "VEG_TYP2",                                 '!VEG_TYPE!.split(" ")[-1]', "PYTHON")
Calculatefield example: Calculate the centroid

Use calculatefield to allocate the centroid value to the new field.

# Name: CalculateField_Centroids.py# Description: Use CalculateField to assign centroid values to new fields # Import system modulesimport arcpyfrom arcpy import envtry:     # Set environment settings    env.workspace = "C:/data/airport.gdb"     # Set local variables    inFeatures = "parcels"    fieldName1 = "xCentroid"    fieldName2 = "yCentroid"    fieldPrecision = 18    fieldScale = 11    # Expressions are calculated using the Shape Field's geometry property    expression1 = "float(!SHAPE.CENTROID!.split()[0])"    expression2 = "float(!SHAPE.CENTROID!.split()[1])"     # Execute AddField    arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE",                               fieldPrecision, fieldScale)    arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE",                               fieldPrecision, fieldScale)     # Execute CalculateField     arcpy.CalculateField_management(inFeatures, fieldName1, expression1,                                    "PYTHON")    arcpy.CalculateField_management(inFeatures, fieldName2, expression2,                                    "PYTHON")except Exception, e:    # If an error occurred, print line number and error message    import traceback, sys    tb = sys.exc_info()[2]    print "Line %i" % tb.tb_lineno    print e.message
Calculatefield example: calculation range

Use calculatefield with code blocks to Calculate range-based values.

# Name: CalculateField_Ranges.py# Description: Use CalculateField with a codeblock to calculate values#  based on ranges # Import system modulesimport arcpyfrom arcpy import env # Set environment settingsenv.workspace = "C:/data/airport.gdb" # Set local variablesinTable = "parcels"fieldName = "areaclass"expression = "getClass(float(!SHAPE.area!))"codeblock = """def getClass(area):    if area <= 1000:        return 1    if area > 1000 and area <= 10000:        return 2    else:        return 3""" # Execute AddFieldarcpy.AddField_management(inTable, fieldName, "SHORT") # Execute CalculateField arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON",                                 codeblock)
Calculatefield example: Calculate random Value

Use calculatefield to assign random values to new fields.

# Name: CalculateField_Random.py# Description: Use CalculateField to assign random values to a new field   # Import system modulesimport arcpyfrom arcpy import env # Set environment settingsenv.workspace = "C:/data/airport.gdb"  # Set local variablesinFeatures = "parcels"fieldName = "RndValue"expression = "arcgis.rand('Integer 0 10')" # Execute AddFieldarcpy.AddField_management(inFeatures, fieldName, "LONG") # Execute CalculateField arcpy.CalculateField_management(inFeatures, fieldName, expression, "PYTHON")
Overview of the environment's current Workspace-related topic Field Tool Set

Copyright 1995-2011 ESRI. All rights reserved.

 

From help

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.