Geoprocessing using Python- Introduction of ArcPy in ArcGIS Pro

Python for Spatial Analysis in ArcGIS Using Python in ArcGIS Pro
17 minutes
Share the link to this page


In this tutorial, I'm going to introduce arc GIS pro as an application that will allow you to visualize, edit, and analyze your geographic data in both 2d and 3d. We are also going to learn about the package arc pi that will provide us useful tools to perform data processing in Python s3 offers different products to implement GIS that is using the arc map as the arc GIS Pro software. The first uses Python two, while the second uses Python three. So if you make scripts on arc map, you will need to update your code to work on arc GIS pro program. In this sense, there is two to three command line utility that can be used to automate much of the process, which is available with both Python two, and three, but it's not a complete solution, additional changes may be necessary. The first thing you must do install a later version of arc GIS pro 1.3 because there were significant changes with that version.

In this case, I will use the latest release which is 1.4 at the moment of the video recording for this tutorial, we are going to use the Python window in arc GIS Pro. So open Ark and GIS pro create a new project based on a blank template, name it London project and located on the root directory that is C, colon, backslash. We don't have any data to process yet. We are going to create them. The data goes to contents located on the left. Living Atlas is a collection data under the portal tab.

Now right London in the search box and press enter in the list of search results, right click on London diversity feature layer. Click Add to new map dot map shows the 33 London boroughs rows in blue. If you click on a Burro, a pop up window opens with the attributes for that region, and you will observe the name, the total area, and some ethnicities, percentage. Now click on View, and then click on Python. You will notice that a window appears called Python. This window can extend from a single line to complex, multiple line blocks of code.

It has the prompt and the transcript section. So you write and enter your code in the prompt, while the transcript area provides the record of the previously entered code. In the sense, the Python window is a useful information To learn about and experiment with Python in arc GIS Pro, if you right click on the transcript section, the safe transcript option will appear. Click there if you want to save your code. However, if you do the same in the prompt, you will get the load code option to run your code program. Let's make some examples using the package arc pi.

So right import arc pie in the prompt. When you do that, you will be able to run its model functions and classes. Then you must set the current workspace environment. Here you need to be careful since entering the past may generate mistakes There is three types of path, absolute, relative and catalog. The last one is the path that only arc GIS recognizes and it consists in two parts, the workspace and the base name. So the workspace should point out to a database.

Remember, Windows use back slashes for the path. While in Python, the backslash is an escape character. For example, the backslash and the letter T represent a top one technique is escaping the backslash. That is using double backslashes when you write the path, the other consists of typing the our derivative. As I show you in the video, we create a raw string. When you do that, Python will ignore backslashes.

On the other hand, an absolute path begins with a driver letter followed by a colon, while the relative path refers to the relative location to a current directory. For example, if the absolute path is C, colon, backslash folder one backslash folder two backslash data and the current path is C. colon, backslash folder one, the relative path will be dot backslash folder to backslash data The dot represents the current directory. However, you can use double.if you want to return to the previous directory. So, if you write double dots, you describe the path that is C colon, backslash. What would happen if you type in double dots, backslash, quarter three, you will access folder three, which is in C at the same level as quarter one. That is C colon, backslash coder three.

Try to combine these commands and observe the outcomes. We are going to set a variable environment. So set the variable r p y dot E and V spell Each letter dot workspace to the catalog path of the project. Remember to point out to the default database that is C colon, backslash, London project, backslash Londyn project dot g d B, and press Enter. Now print the variable and check the outcome on the transcript section. In this case, the word E and V represents a class of arc pi.

While the workspace is a property of this class, keep in mind that you will access any class of arc pi after the first start, and you will get to the property of each class with the SEC dot. Sometimes you don't need to import an entire model. In this case, you can use the from import statement. For example, if you want to import only the E and V class from arc pi, you have to write from arc pi, import E and V. Instead of writing arc pi, at first, you will only write E and the So, set the variable e NP dot workspace to the previous path. On the other hand, if you want to assign a nickname to this class, you have to use the S word that means after envy you have to to type a s, and then the nickname, in this case, E and V in uppercase, set the variable e Nv dot workspace to the previous path.

Okay, we can go further using the from import asterisk statement. In this case, you will import all elements of the module without the need of having a prefix. However, there are some risks associated with this approach. You might not access to the proper element, since other objects variable models with the same name may override the previous ones. So let's make an example. Right from our PY, import asterisk and press enter.

Then you can set the variable e Nv dot workspace to the previous path directly. arc pi gives you access to arc GIS ArcGIS geoprocessing tools. Let's do an example using the describe function which returns an object with properties, such as data type, fields, indexes, and many others. If you want to know the properties of the feature layer, London diversity, you must set the variable D sc. To arc pi dot d, s c see Wait just a second. press TAB key.

Python will autocomplete the word. After that. A pop up window opens with a yellow rectangle and the layer London diversity appears. So click on that, I move the cursor out of the parenthesis and press Enter. Now take your time and copy in the prompt. The three statements that I show you in the screen, you will get the name of the feature, the name of the layer and its path.

Up to this point, you don't know the shape type that is either polyline polygon or point print the verbal d c dot shape type and observe the outcome. You will get a polygon. If you want to check it, click on the arrow point it will unfold and the symbol showing a polygon will be displayed next to London diversity. If you click on it, you will notice the area change because that is the feature layer. Right click on it and click on Properties. Then click on source and you will observe the geometry type is polygon.

As I told you before, arc p y has all your processing tools as functions where each one does a particular task. The syntax for calling a tool by its function is arc p y dot the tool The tool Alliance and then the parameters of each function. For example, suppose that you want to copy the feature London diversity. In this case, go to the search, geo processing box and search copy feature, which is from data management tools. Then click here, introduce the parameters and click on Run. It's easy, but what happens if you need to do it several times.

You can do the same using one command line. So right arc pi dot the name of the tool, that is copy feature, type in underscore and the toolbox Elias that is management and open parenthesis. Observe that you could introduce many parameters, but two of them are required. The others are enclosed by curly brackets. So they are optional. Click on London diversity, since it will be your input feature, then write, comma and the name of the output feature class.

In this case, new feature, move the cursor out of the parenthesis and press enter. Notice how arc GIS is processing your instruction. Now go to database and check if the new feature has been saved there. There is another way of calling the tool functions. So right arc p y dot type in the alliance of the toolbox Which is management. After that, write copy feature, open parenthesis and introduce the parameters.

Remember, Python is case sensitive. So if you write copy feature in lowercase, you will get an error. Using the previous example, how would you specify the special grid and skip the others? Well, you can set those options as parameters to an empty string of the assigned number as I show you in the video. The other way is specifying the name of the parameter that you need to be set. That's all for this tutorial.

I hope you like it and you want to watch more tutorials upon Python and GIS

Sign Up


Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.