Compilation of introductory knowledge points for Python in ArcGIS

Insert image description here

Everyone should be familiar with Arcpy. Python is easy to use and data processing is fast and good! In ArcGIS, ArcPy is a site package built on the successful arcgisscripting module and inherits the arcgisscripting functionality.

The purpose is to create a foundation for performing geographic data analysis, data transformation, data management, and map automation through Python in a practical and efficient way. ArcPy provides a rich and pure Python experience with code auto-completion function. Today we will take a look at the use of Arcpy related processing, and get to know Arcpy with everyone through some simple codes.

1. What compiler to use?

1. ArcGIS has automatically installed the corresponding version of Python during installation. Find the installation path of arcgis directly in the [Start] menu, then find [IDLE (Python GUI)] and double-click the graphical user interface, as shown in the figure below. Show:
Insert image description here

At this time, a window called Python shell will pop up. The shell is actually a program that accepts commands entered from the keyboard and passes them to the operating system for execution. At this time, it is invalid for us to enter the code in this window. This window is actually used to display the results of the code running or error messages. So where do we enter the code? [File]-[New Window].

Insert image description here

At this time, you can write the code in the new pop-up window [Untitled]. After writing, click [Run]-[Run Model] on the menu bar to save it as a .py file and run the code. (But it seems there is no code prompt function...)

Insert image description here

2. Use the editor in ArcMap desktop version. First, after opening ArcMap, click on the [Python] window above the menu bar, as shown in the figure below:

Insert image description here

In the opened interface, left-click and select [Format] to set the font and text color in the form. The form provides two themes, black and white. I personally prefer black. The left side of this interface is used to write code and display running results and error messages. The right side can also prompt error messages, as shown in the following figure:

Insert image description here

  1. Of course, you can also use a third-party compiler, such as Pycharm. As long as you specify the Python interpreter in the settings as Python.exe that comes with arcgis installation, you can import related packages normally and start enjoying a comfortable writing environment. You don’t need to open GIS to run the code. software.

Insert image description here

2. How to write Arcpy specifically?

Just go to the code. There are very detailed comments in it. If you still don’t understand, you can leave a message.

  1. To achieve vector cropping effect:

As shown in the figure below, the blue area is the data of [Changsha City.shp], and the yellow area is the data of [Changsha City Built-up Area.shp]:

Insert image description here

In order to let [Changsha City Built-up Area.shp] crop [Changsha City.shp], I entered the following code:

Insert image description here

This code is a very simple use of the [Crop] tool in arcmap, as shown in the figure below. Three necessary parameters are required to open the tool.

Insert image description here

Open the [Tool Help] below and you can also see the syntax explanation of the [Clip_analysis] method and some simple implementation examples. You can refer to this to learn and understand when using other function methods, as shown in the following figure:

Insert image description here

I did not open the arcmap software when running this code. After running it, open it again. You can see that the [clip.shp] file is not generated under the [ArcpyBook] folder under the D drive. Don’t worry, right-click [ArcpyBook]. ] folder and click [Refresh]. At this time, there is an additional vector data [clip.shp] in the subdirectory. Drag it into the data frame and you will find that it is the same as [Changsha City Built-up Area.shp].

Insert image description here

  1. Do buffer analysis on the previously generated cropping object:

Insert image description here

After refreshing, drag it to the data frame as shown below:

Insert image description here

  1. Select features by location (intersect):

Highlight the company point elements within the previously generated buffer range by position, and output the number of selected point elements on the console. The [Company Points.shp] data is as shown in the figure below:

Insert image description here

The relevant code is shown below:
Insert image description here

Regarding the method [arcpy.MakeFeatureLayer_management(a,b)], I will say more here. This method is equivalent to temporarily copying a piece of data a in the folder to the content list and naming it b. If it is in a third-party compiler When writing code, this will not display b in the content list, and if this method is used multiple times, the name of b can be the same. However, if you use the second method mentioned earlier to write code, each time you call this method, the name of b is required to be different, otherwise an error will be reported that this element already exists.

After running the code, the console outputs the number of selected point elements, but they are not highlighted in arcmap. For this reason, we can use the second method introduced earlier to write the code.

Insert image description here

The output is as follows:

Insert image description here

You can see that there is an additional temporary data of [GSD] in the content list, which is exactly the same as the storage path of [Company Point.shp] below.
Insert image description here

The source code is as follows: (There is a source code download link below at the end of the article)

Insert image description here

Guess you like

Origin blog.csdn.net/qq_43173805/article/details/127706212