﻿{"id":8569,"date":"2019-04-12T04:24:17","date_gmt":"2019-04-12T02:24:17","guid":{"rendered":"http:\/\/www.sigterritoires.fr\/?p=8569"},"modified":"2023-04-28T14:57:05","modified_gmt":"2023-04-28T12:57:05","slug":"arcgis-and-python-3-your-first-script","status":"publish","type":"post","link":"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/","title":{"rendered":"ArcGis and Python (3 ): Your first script"},"content":{"rendered":"\n<p>If you have read the two previous articles &nbsp; ArcGis and Python: Before your first steps and\n&nbsp; <a href=\"https:\/\/translate.google.com\/translate?hl=fr&amp;prev=_t&amp;sl=fr&amp;tl=en&amp;u=http:\/\/www.sigterritoires.fr\/index.php\/arcgis-et-python-2-pour-bien-demarrer\/\">ArcGis and Python: getting started<\/a> , you are\nready to get started. <\/p>\n\n\n\n<p>There are two things you need to learn: the Python language and how to\nuse the ArcGis Python geoprocessing library. As for Python, tutorials abound. But\ndo not forget that you will only need some basics of the language. You will\nbasically string together already existing ArcGis tools. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Here, we will discuss a first script example and its integration in your\npersonal toolbox. We will, still, use the <strong><em>Clip<\/em><\/strong> tool of a layer. Let&rsquo;s\nsay we want to adapt the <strong><em>Clip<\/em><\/strong> tool so that it, automatically,\nsplit all layers in a workspace (directory, geodatabase), storing the results\nin another workspace. <\/p>\n\n\n\n<p>We could make a model with Model Builder using a data input iterator. And\nbesides that&rsquo;s probably what you would do now if you&rsquo;re just starting with\nPython. So let&rsquo;s see how to do the same thing with Python. <\/p>\n\n\n\n<p>As example we will use <em>PyScripter,<\/em> but in this case, you do not\neven need IDE. A text editor can do the trick. On the other hand be aware that with\nPython, the lines indentation is part of the syntax and that typing a code line\nat the beginning of the line or after one or two tabs does not mean the same\nthing. <\/p>\n\n\n\n<p>An IDE will automatically position you where it is needed. <\/p>\n\n\n\n<p>Launch your IDE or open your text editor.  <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py30.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"329\" data-attachment-id=\"8570\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/121-5\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/121.png?fit=630%2C329&amp;ssl=1\" data-orig-size=\"630,329\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"121\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/121.png?fit=630%2C329&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/121.png?resize=630%2C329&#038;ssl=1\" alt=\"\" class=\"wp-image-8570\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/121.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/121.png?resize=300%2C157&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/figure>\n\n\n\n<p>You will type your unavoidable line <\/p>\n\n\n\n<p><strong><em>arcpy<\/em><\/strong> <strong><em>import<\/em><\/strong> <\/p>\n\n\n\n<p>without which Python does not know ArcGis processes at all. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py31.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"348\" data-attachment-id=\"8571\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/122-5\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/122.png?fit=440%2C348&amp;ssl=1\" data-orig-size=\"440,348\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"122\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/122.png?fit=440%2C348&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/122.png?resize=440%2C348&#038;ssl=1\" alt=\"\" class=\"wp-image-8571\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/122.png?w=440&amp;ssl=1 440w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/122.png?resize=300%2C237&amp;ssl=1 300w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/a><\/figure>\n\n\n\n<p>You will add a second, called the <strong><em>os<\/em><\/strong> module that provides\nquick access to the basic tools of the operating system. Some managing methods for\nthe file names of the <strong><em>os<\/em><\/strong> module will be used in this script. <\/p>\n\n\n\n<p><strong><em>import<\/em><\/strong> <strong><em>os<\/em><\/strong> \u00a0 <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py32.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"491\" data-attachment-id=\"8572\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/123-5\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/123.png?fit=630%2C491&amp;ssl=1\" data-orig-size=\"630,491\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"123\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/123.png?fit=630%2C491&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/123.png?resize=630%2C491&#038;ssl=1\" alt=\"\" class=\"wp-image-8572\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/123.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/123.png?resize=300%2C234&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/figure>\n\n\n\n<p>The script will have two parts: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the first one where you will\ndefine input and output data <\/li><li>the second where you will perform\nthe processing<\/li><\/ul>\n\n\n\n<p><strong>Definition of input<\/strong> <strong>and<\/strong> <strong>output data<\/strong> <\/p>\n\n\n\n<p>This part has a double interest. Of course for the processes execution it\nis necessary that the tool knows on data has to work. But also because to run\nthe script as a toolbox tool it will be necessary to create the parameter\nwindow that will be displayed when launching the tool. It will then resume, one\nby one, the definitions of our script to create an input field in the window. Therefore,\nyou have to run your lines of code to find yourself at last in&nbsp; the process.<\/p>\n\n\n\n<p>We will use 4 input \/ output variables <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the workspace where the layers to\nbe cut are located <\/li><li>the layer that will be used to\ncut the others <\/li><li>the workspace where to put the\nresulting cut layers <\/li><li>the tolerance to be used by the\nCutting tool <\/li><\/ul>\n\n\n\n<p>Enter the following &nbsp;code lines: <\/p>\n\n\n\n<p><strong><em># Workspace<\/em><\/strong> <strong><em>Input:<\/em><\/strong> <strong><em>Type WORKSPACE<\/em><\/strong> <br>\n<strong><em>arcpy.env.workspace = arcpy.GetParameterAsText (0)<\/em><\/strong> <\/p>\n\n\n\n<p><strong><em>#Class of entities used to cut Type FEATURECLASS<\/em><\/strong> <br>\n<strong><em>clipFeatures =<\/em><\/strong> <strong><em>arcpy.GetParameterAsText (<\/em><\/strong> <strong><em>1)<\/em><\/strong>\n<\/p>\n\n\n\n<p><strong><em>#<\/em><\/strong> <strong><em>Output<\/em><\/strong> <strong><em>Workspace<\/em><\/strong> <strong><em>:<\/em><\/strong>\n<strong><em>Type WORKSPACE<\/em><\/strong> <br>\n<strong><em>outWorkspace = arcpy.GetParameterAsText (2)<\/em><\/strong> <\/p>\n\n\n\n<p><strong><em># XY Tolerance of<\/em><\/strong> <strong><em>Cut<\/em><\/strong> <strong><em>Tool<\/em><\/strong> <strong><em>:<\/em><\/strong> <strong><em>DOUBLE Type<\/em><\/strong> <br> <strong><em>clusterTolerance = arcpy.GetParameterAsText (3)<\/em><\/strong>  <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py33.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"468\" height=\"428\" data-attachment-id=\"8573\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/124-5\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/124.png?fit=468%2C428&amp;ssl=1\" data-orig-size=\"468,428\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"124\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/124.png?fit=468%2C428&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/124.png?resize=468%2C428&#038;ssl=1\" alt=\"\" class=\"wp-image-8573\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/124.png?w=468&amp;ssl=1 468w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/124.png?resize=300%2C274&amp;ssl=1 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/a><\/figure>\n\n\n\n<p>The <strong><em>GetParameterAsText<\/em><\/strong> function retrieves the script input\nparameters. If you want to run it on the command line (in a command window) you\nwill use a command such as the following: <\/p>\n\n\n\n<p><strong><em>python.exe \u00ab\u00a0patrameter1\u00a0\u00bb\n\u00ab\u00a0parameter2\u00a0\u00bb \u00ab\u00a0parameter3\u00a0\u00bb Number<\/em><\/strong> <\/p>\n\n\n\n<p>If, as we will see later, you use a tool from the toolbox, you will have\nto match the order of the fields of the dialog window with the order defined in\nthe script variables: the first field of the dialogue window should be the\ninput work space, the second the cutting layer, etc. <\/p>\n\n\n\n<p>In our four definitions, the first is a definition of an ArcGis\nvariable, this is the current workspace, while the other three are variables\nspecific to this script. <\/p>\n\n\n\n<p><strong>The body of the<\/strong> <strong>script:<\/strong> <strong>the process<\/strong> <\/p>\n\n\n\n<p>Before starting to enter the code lines of the process itself, we are\ngoing to enter a weird statement. We will simply type <\/p>\n\n\n\n<p><strong><em>try<\/em><\/strong> <strong><em>:<\/em><\/strong> <\/p>\n\n\n\n<p>At the end of our process, we will return the complementary instruction\nof this one: <strong><em>except<\/em><\/strong> <\/p>\n\n\n\n<p>They are not mandatory. If you do not enter them, the script will work\nwithout any problem except &#8230; when there is a problem. If an error occurs\nduring processing and you do not have these instructions, you will have a lovely\nwindow \u00ab\u00a0ArcGis has encountered an error and must be closed &#8230; \u00ab\u00a0. <br>\nOn the other hand, if you have mi try \/ except, if an error occurs in the body\nof the processing, instead of crashing the system, the script will execute the\ninstructions found &nbsp;in the except\nparagraph (usually displaying an error message and an explanation).<\/p>\n\n\n\n<p>The first instruction that we will enter corresponds to the creation of\nthe layers list present in the input workspace and that it will be necessary to\ncut. <\/p>\n\n\n\n<p><strong><em>fcs<\/em><\/strong> <strong><em>= arcpy.ListFeatureClasses ()<\/em><\/strong> \u00a0 <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py34.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"337\" data-attachment-id=\"8574\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/125-5\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/125.png?fit=630%2C337&amp;ssl=1\" data-orig-size=\"630,337\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"125\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/125.png?fit=630%2C337&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/125.png?resize=630%2C337&#038;ssl=1\" alt=\"\" class=\"wp-image-8574\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/125.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/125.png?resize=300%2C160&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/figure>\n\n\n\n<p>Note that the lines do not start at the beginning of the line. The\nindent tells Python that this command is inside the <strong><em>try<\/em><\/strong> command .\n<br>\nThe <strong><em>fcs<\/em><\/strong> variable will contain the list of layers to be cut. The <strong><em>ListFeatureClasses<\/em><\/strong>\nstatement generates the list of feature classes for the current workspace. Remember,\nwe defined it in the first instruction of the script, the parameter 0. <\/p>\n\n\n\n<p>Now that we have the list of layers to process, we will put a loop-like\nstatement so that the processing is repeated on each layer of the list <\/p>\n\n\n\n<p><strong><em>for<\/em><\/strong> <strong><em>fc in fcs:<\/em><\/strong> <\/p>\n\n\n\n<p>In this loop we will cut an input layer and create an output layer. Before\nwe cut it out, we need to handle some things we&rsquo;ve done so far on ArcGis,\neither on the commands or toolbox tools. For example, if our input workspace is\na geodatabase and we define our output space as a directory, the output layers\nwill be shapefiles. No problem, except that <a href=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/shapefile-vs-geopackage\/\">shapefile<\/a> names are much more\nrestrictive than those in the geodatabase. We may have names that are too long\nor with dots or dashes. In a script, it&rsquo;s up to us to handle this type of\nproblem. Rest assured. It&rsquo;s not complicated, but you have to do it. <\/p>\n\n\n\n<p>Just retrieve the function <strong><em>ValidateTableName<\/em><\/strong> which\ntransforms the input names into the correct output names, while ensuring that\nthere are no repeated output names. <\/p>\n\n\n\n<p><strong><em>featureClassName<\/em><\/strong> <strong><em>= arcpy.ValidateTableName (fc, outWorkspace)<\/em><\/strong> <br> <strong><em>outFeatureClass = os.path.join (outWorkspace, featureClassName)<\/em><\/strong>  <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py35.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"416\" data-attachment-id=\"8575\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/126-16\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/126.png?fit=630%2C416&amp;ssl=1\" data-orig-size=\"630,416\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"126\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/126.png?fit=630%2C416&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/126.png?resize=630%2C416&#038;ssl=1\" alt=\"\" class=\"wp-image-8575\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/126.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/126.png?resize=300%2C198&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/figure>\n\n\n\n<p>Now we are going to cut the input layer with the cutting layer. But if\nthe cutting layer is in the input workspace, we do not want to cut it! We put a\ntest to make the cutting provided that the name of the layer to be cut is\ndifferent from that which cuts: <\/p>\n\n\n\n<p><strong><em>if<\/em><\/strong> <strong><em>fc! = os.path.basename (clipFeatures):<\/em><\/strong>\n<\/p>\n\n\n\n<p>This function just gets the name of the file, without the path, because\nin our input list we only have the names without path. <br>\nThen the cutting function: <\/p>\n\n\n\n<p><strong><em>arcpy.Clip_analysis<\/em><\/strong> <strong><em>(<\/em><\/strong> <strong><em>fc, clipFeatures, outFeatureClass, clusterTolerance)<\/em><\/strong> <\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py36.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"437\" data-attachment-id=\"8576\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/127-4\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/127.png?fit=630%2C437&amp;ssl=1\" data-orig-size=\"630,437\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"127\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/127.png?fit=630%2C437&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/127.png?resize=630%2C437&#038;ssl=1\" alt=\"\" class=\"wp-image-8576\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/127.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/127.png?resize=300%2C208&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/figure>\n\n\n\n<p>It&rsquo;s over for the processing. <\/p>\n\n\n\n<p>Now, we must inform what to do in case of error: <\/p>\n\n\n\n<p><strong><em>except<\/em><\/strong> <strong><em>Exception as err:<\/em><\/strong> <br>\n<strong><em>arcpy.AddError (err)<\/em><\/strong> <br>\n<strong><em>print err<\/em><\/strong> <\/p>\n\n\n\n<p>Since the script can be run by a toolbox tool or all by itself, we have two error displays. <strong><em>AddError<\/em><\/strong> will add the error message in the result window of the toolbos tool. <strong><em>Print<\/em><\/strong> will display the error in the command window if the script is executed by itself. <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py37.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"472\" data-attachment-id=\"8577\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/128-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/128.png?fit=630%2C472&amp;ssl=1\" data-orig-size=\"630,472\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"128\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/128.png?fit=630%2C472&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/128.png?resize=630%2C472&#038;ssl=1\" alt=\"\" class=\"wp-image-8577\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/128.png?w=630&amp;ssl=1 630w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/128.png?resize=300%2C225&amp;ssl=1 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/a><\/figure>\n\n\n\n<p>Note the indentation of <strong><em>except<\/em><\/strong> . It is at the same level\nas <strong><em>try<\/em><\/strong> . <\/p>\n\n\n\n<p>Save your file. It is ready to switch to the Toolbox. <\/p>\n\n\n\n<p><strong>Adding the script in the toolbox<\/strong> <\/p>\n\n\n\n<p>In the Toolbox, load a personal toolbox (here <strong><em>My Scripts<\/em><\/strong> ). <br> Right click on the toolbox and select <strong><em>Add-> Script<\/em><\/strong> \u00a0 <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py38.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"530\" height=\"384\" data-attachment-id=\"8578\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/129-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/129.png?fit=530%2C384&amp;ssl=1\" data-orig-size=\"530,384\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"129\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/129.png?fit=530%2C384&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/129.png?resize=530%2C384&#038;ssl=1\" alt=\"\" class=\"wp-image-8578\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/129.png?w=530&amp;ssl=1 530w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/129.png?resize=300%2C217&amp;ssl=1 300w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/a><\/figure>\n\n\n\n<p>The first window allows you to define the Name of the tool (without spaces or special characters) and the label that will appear in the toolbox display (with spaces and special characters if you want).  <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py39.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"448\" height=\"560\" data-attachment-id=\"8579\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/attachment\/1210\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1210.png?fit=448%2C560&amp;ssl=1\" data-orig-size=\"448,560\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"1210\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1210.png?fit=448%2C560&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1210.png?resize=448%2C560&#038;ssl=1\" alt=\"\" class=\"wp-image-8579\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1210.png?w=448&amp;ssl=1 448w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1210.png?resize=240%2C300&amp;ssl=1 240w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/a><\/figure>\n\n\n\n<p>The second window allows to link with our script. \u00a0 <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py40.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"556\" data-attachment-id=\"8580\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/attachment\/1211\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1211.png?fit=444%2C556&amp;ssl=1\" data-orig-size=\"444,556\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"1211\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1211.png?fit=444%2C556&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1211.png?resize=444%2C556&#038;ssl=1\" alt=\"\" class=\"wp-image-8580\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1211.png?w=444&amp;ssl=1 444w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1211.png?resize=240%2C300&amp;ssl=1 240w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/a><\/figure>\n\n\n\n<p>The last window is the one that\nrequires the most work. You will define the input parameters of the tool\nwindow. <\/p>\n\n\n\n<p>For each parameter of the script (we have four) it will be needed one\nline. In the <strong><em>Name<\/em><\/strong> column, type the text that you want to see next\nto the input field in the dialog window. <\/p>\n\n\n\n<p>In the <strong><em>Data Type<\/em><\/strong> column you must specify the type of data to enter in the dialog box field.  <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py41.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"552\" data-attachment-id=\"8581\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/attachment\/1213\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1213.png?fit=440%2C552&amp;ssl=1\" data-orig-size=\"440,552\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"1213\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1213.png?fit=440%2C552&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1213.png?resize=440%2C552&#038;ssl=1\" alt=\"\" class=\"wp-image-8581\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1213.png?w=440&amp;ssl=1 440w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1213.png?resize=239%2C300&amp;ssl=1 239w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/a><\/figure>\n\n\n\n<p>For the first parameter of the\nscript (parameter 0) we indicate \u00a0\u00bb <strong><em>Workspace input<\/em><\/strong> \u00a0\u00bb\nand the data type \u00a0\u00bb <strong><em>Workspace<\/em><\/strong> \u00ab\u00a0. <\/p>\n\n\n\n<p>Here is the screenshot with all the definitions for our script. \u00a0 <br><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2015\/06\/py42.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"444\" height=\"552\" data-attachment-id=\"8582\" data-permalink=\"https:\/\/www.sigterritoires.fr\/index.php\/en\/arcgis-and-python-3-your-first-script\/attachment\/1214\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1214.png?fit=444%2C552&amp;ssl=1\" data-orig-size=\"444,552\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"1214\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1214.png?fit=444%2C552&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1214.png?resize=444%2C552&#038;ssl=1\" alt=\"\" class=\"wp-image-8582\" srcset=\"https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1214.png?w=444&amp;ssl=1 444w, https:\/\/i0.wp.com\/www.sigterritoires.fr\/wp-content\/uploads\/2019\/04\/1214.png?resize=241%2C300&amp;ssl=1 241w\" sizes=\"auto, (max-width: 444px) 100vw, 444px\" \/><\/a><\/figure>\n\n\n\n<p>The order of the lines is very\nimportant. The first line will be the first parameter defined in the script,\nparameter 0. The second line will be ai = u second parameter, parameter 1. And\nso on. <\/p>\n\n\n\n<p>When finished, close the Add Script window. You can now run it as any\nother tool, by double clicking in the Toolbox. <\/p>\n\n\n\n<p>You find the four parameters to\nenter. <\/p>\n\n\n\n<p>Not only do you have a new tool that can run from the toolbox but you\ncan include it in a Model Builder template. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have read the two previous articles &nbsp; ArcGis and Python: Before your first steps and &nbsp; ArcGis and Python: getting started , you are ready to get started. There are two things you need&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"give_campaign_id":0,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sfsi_plus_gutenberg_text_before_share":"","sfsi_plus_gutenberg_show_text_before_share":"","sfsi_plus_gutenberg_icon_type":"","sfsi_plus_gutenberg_icon_alignemt":"","sfsi_plus_gutenburg_max_per_row":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1260],"tags":[],"class_list":["post-8569","post","type-post","status-publish","format-standard","hentry","category-non-classe-en"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p6XU0A-2ed","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/8569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/comments?post=8569"}],"version-history":[{"count":0,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/posts\/8569\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/media?parent=8569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/categories?post=8569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sigterritoires.fr\/index.php\/wp-json\/wp\/v2\/tags?post=8569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}