Learn How to use Python with ArcGIS in various conditions

Python is an excellent language to work. It is powerful, and it has lots of features that you need to get your work done. One of the reasons why Python is dominant in the application is through Python toolboxes. Sometimes you have toolkits that either for confidentiality reasons or because maybe as part of a certification process. You don’t have to share source code to prying eyes.

We will be discussing the encrypted Python toolboxes to solve this problem. All we need to do is open the toolbox select encrypt, enter a password, and optionally create an unencrypted backup copy of the file. Python toolboxes are stored securely, and we can now ship it off to a client, and we don’t have to worry about revealing any of the internal source code.

Python Improvements

Do YOU Know: Tools are added regularly to ArcGIS in recent time Esri has added over 250 tools to the pro 2.1 release alone.

But as you know, you can use those tools directly, you can also use them with model builder whenever you want, and to dig deeper, you need Python for GIS. Python made our working efficiently with its tools, and even better is the additional ability to take any tool and directly send it to the Python window. When we do this, it’s going to open up the Python window for us. It is going to initialise all of our parameters, and from here, we can make any changes to the code or maybe even copy and paste it into a script.

So now we are at the command line let’s look at some codes. When we’re working with data, it’s often the case that we need to explore what attributes and properties it supports. We are using PI to describe for a long time, which is the only tool that allows us to work with the data. The latest version pro 2.1 is introduced with the ability to add to use arc by da describe and to look at all attributes supported by an object at once. It will return a Python dictionary that contains all the attributes. So, before moving ahead, take a look, and get back to everything that supported the nested information available there. We don’t have to look at any documentation or switch back and forth between multiple pages.

All right, so we looked at some ways by which we can work faster with Python, but what about when we’re working with other people’s code or even code from yourselves. What can we do there? We have code, and we want to run it.

A working Example

Suppose we only have the code, and we want it to run well. We are working on a project in Delaware Bay, which we are preparing to understand the effect of shipping noise. Now some of our colleagues who are one of them who are helping us in this project gave us a toolbox and told us that it works very well on their machine. We thought it was okay, so we opened it up and tried to run, something seems to be good, and then our attempt fails.

We came to know that it is missing an import, but we know that learn ArcGIS can find it, but then we realize that this is not a problem powered by Anaconda, we have to manage thousands of packages and can also use Python backstage to install it, so let’s go ahead and get that one missing import and install it.

So, now we found and installed its missing import. We are getting on to our right track, and use this, if we have conflicting dependencies, then we can install this Python to backstage to manage multiple environments. You can configure them and make sure they work exactly as we want in our environment.

Python Debugging

Now, the package is installed, then let’s go back and try rerunning this code. It worked merely alright, so we have looked, how we can use the backstage for handling dependencies, but what about debugging. Debugging is a critical part of working with code, and now we can add print statements, log files, and piecemeal get back tidbits of information.

A rich debugging experience makes our lives much more comfortable, but with Visual Studio 2017, you can interactively debug your Python script tools with a pro. Let’s see what it looks like we are going to visual studio and attach to the pro process. We have connected, and we have to go back to pro and try opening a tool that we have been working on. We know that there’s something or there’s some bug in this tool and that it’s not working exactly the way we want it to. So, let’s see how Visual Studio can help us handle this bug.

Debugging with Visual Studio

Let’s go ahead and try opening it and run it now it starts just fine everything’s looking okay. Then boom, we hit an exception right, and now we get dropped directly into Visual Studio initialize precisely at the line of our failure. We can explore all the mutable state we can look at the call stack is a great place to figure out exactly what’s going on in this code. If you haven’t caught it this time, it’s pretty easy it is (I before E, we have corrected our spelling of neighbourhood) before except for the times alright.

So let’s save that change press continue. Initially, we can see it’s still going to fail in pro because a pro is using a cached copy to rerun this tool, and now we are going to see this time it is successful.

It was a much better debugging experience, alright. So we have shown you how you can deal with missing modules how you can handle debugging with your script tools, but what about tool validation? It has been a challenging area because you can’t see what’s happening in the middle of your code execution. Well, the visual studio can help us with this as well.

Let’s go we have some saved out our validation code to an external Python file so that visual studio can see it. Let’s switch over to it and add a breakpoint in the middle of the updated parameters section of the validation code. Now see what happens, so we are going back to pro we have to open the tool. As soon as we open the tool, it immediately fires that breakpoint, and now we have initialized it right and in the middle of the validation of codes where we can make necessary changes. It is a much easier solution than what we had in the past.

Related Posts

Enquiry Form