Use ArcGIS script tool to sort and number point data

Author : houxue   2023-03-10 09:33:38 Browse: 1008

Abstract: Today we will share the point data sorted and numbered by ArcGIS script tool. At the end of the article, we will attach the poin...

Today we will share the point data sorted and numbered by ArcGIS script tool. At the end of the article, we will attach the point data and the script tool directly used.

Let's look at the renderings we want to achieve first:

design sketch

Detailed steps:

Step 1: Open the point feature and add X and Y fields in the attribute table.

Property sheet

Step 2: Use the sort tool to generate sorted point data by using the X and Y fields as sorting fields.

Use the sort tool

Step 3: Add a sorting result field and assign the OBJECTID field value to this field.

OBJECTID field value assignment

Step 4: Create a script tool

① First create the toolbox

Create the toolbox

② Add script

Add script

③ Open the script tool creation wizard and fill in the information

fill in the information

④ Add Python script and load the written Python file (*. py format)

The main ideas of Python scripts are:

First, plan how many parameters the tool needs;

# Script Tool Parameters
InputFeature = arcpy.GetParameterAsText(0)
Order = arcpy.GetParameterAsText(1)
Sort_X = arcpy.GetParameterAsText(2)
Sort_Y = arcpy.GetParameterAsText(3)
OutputFeature = arcpy.GetParameterAsText(4)

Then use arcpy. CopyFeatures_ Management () Copy the original data;

Reuse arcpy. AddXY_ Management (), add two XY fields to the copied data, and then judge the general direction of point sorting.

Note here that due to the coding problem, when the code has Chinese characters, you need to use the .decode() method to decode Chinese, otherwise the tool will report an error when running.

if Order == 'Droadwise'.decode('UTF-8'):
sortfield = [["POINT_Y", Sort_Y],["POINT_X", Sort_X] ]
elif Order == 'Direction'.decode('UTF-8'):
sortfield = [["POINT_X", Sort_X],["POINT_Y", Sort_Y]]

Then use arcpy. AddField_ Management (), add sorting number field, use arcpy. Sort_ Management(), generate sorted data; Final Use Tool arcpy. CalculateField_ Management (), assign the OBJECTID field value to this field;

Python script completed! All codes are as follows:

import arcpy
import time
# Script Tool Parameters
InputFeature = arcpy.GetParameterAsText(0)
Order = arcpy.GetParameterAsText(1)
Sort_X = arcpy.GetParameterAsText(2)
Sort_Y = arcpy.GetParameterAsText(3)
OutputFeature = arcpy.GetParameterAsText(4)
tempfea = InputFeature+str(time.time()).split('.')[0][-5:-1]
arcpy.CopyFeatures_management(InputFeature, tempfea)
arcpy.AddXY_management(tempfea)
if Order == 'Droadwise'.decode('UTF-8'):
sortfield = [["POINT_Y", Sort_Y],["POINT_X", Sort_X] ]
elif Order == 'Direction'.decode('UTF-8'):
sortfield = [["POINT_X", Sort_X],["POINT_Y", Sort_Y]]
arcpy.Sort_management(tempfea,OutputFeature,sortfield)
arcpy.AddField_management(OutputFeature,'order1','DOUBLE')
arcpy.CalculateField_management(OutputFeature, 'order1','!OBJEC

Step 5: Next, add interface parameters. This step is very important. Each parameter attribute should be filled in one by one, as follows: Order, Sort_ XSort_ Y belongs to the list parameter.

Add interface parameters

Add interface parameter Order

Add interface parameter Sort_ X

After setting, click finish to create the footstep tool!

Step 6: Set the list for the three parameters above

① Click the properties of the script tool

Attribute

② Switch to the Validation tab, click Edit, edit the validation code, and add the following three lines of code

self.params[1].filter.list = ['Broadwise','Direction']
self.params[2].filter.list = ["ASCENDING","DESCENDING"]
self.params[3].filter.list = ["ASCENDING","DESCENDING"]

Basically completed

So far, ArcGIS's point sorting and numbering script tool has been basically completed. For the convenience of others, you can add descriptions and other auxiliary tools.

① Open tool description

Open tool description

② Edit tool information

Edit tool information

Experience this tool:

Tool interface

View result number:

Tool results

Numbering result

    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).