<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating geoprocessing scripts by using tools from toolbox in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/creating-geoprocessing-scripts-by-using-tools-from/m-p/21420#M1666</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Two routes:&lt;BR /&gt;&lt;BR /&gt;1) Use model builder and the "Export to Python" functionality (File / Export / Python) &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;2) Check out the documentation on the support site for the tools you would like to string together.&amp;nbsp; This helped me a bunch &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_references/002t0000000z000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_references/002t0000000z000000/&lt;/A&gt;.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Except Option 1 sucks... Exported models usually don't work, and they are incredibly poorly written - which makes them unbelievably confusing!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Go with Option 2, but following are some Arcpy basics... It will also be handy to have done some basic Python tutorials before attempting to do too much.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;1. Every script using Arc functions must start with import arcpy&lt;/STRONG&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;2. Get user inputs with 'GetParameterAsText'&lt;/STRONG&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;This gets the first user entered parameter as text. If you haven't specified any parameters when adding to Arc (see step 3) it will cause the script to fail.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;3. Using the tool in Arc&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Right click on a toolbox you can edit (if you haven't already, you will have to create one in "My Toolboxes" or within a folder), go down to 'Add' and select 'Script...', give it a name and label, select the script file, then add the parameters. For this example we are getting a Feature Class, so call it InputFeatureClass (or something meaningful) and set the Data Type to Feature Class. Click Finish. Double click on your new script tool to use it. At the moment it does nothing, but if you give it a feature class and click OK, it should run happily and say it was successful.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From now on we can just edit the script where it is and the changes will carry through, you won't need to import it again.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is important to note that paths to feature classes are just strings, e.g. 'C:\\GIS Data\\NYC\\2007\\Networks.gdb\\BusLines'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;4. Adding messages to the display with 'AddMessage'&lt;/STRONG&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)

# inform user of selected feature class path
arcpy.AddMessage('&amp;nbsp; Input Feature Class: '+inputFeatureClass)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Save the script and run it again, and it will now print the feature class path before finishing happily.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;5. Performing a geoprocessing operation&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Let's do a buffer on the input features, and also get the user to input the buffer size for the operation; you will need to right click on the tool in Arc, go down to Properties, and in the parameters tab add the parameter BufferSize_m as a Long (integer). Buffer also requires an output feature class, so you will have to add that to the parameters as well; it's data type should be feature class, but you will need to set the Direction (under Parameter Properties) to Output. The script now becomes (with the message made a little more meaningful):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)

# get buffer size 
buffer = arcpy.GetParameterAsText(1)

# get output feature class
outputFeatureClass = arcpy.GetParameterAsText(2)

# inform user of selected feature class path
arcpy.AddMessage('&amp;nbsp; Buffering input Feature Class, '+inputFeatureClass+' by '+buffer+' Meters and writing to: '+outputFeatureClass)

buffer_corrected = buffer + ' Meters'

arcpy.Buffer_analysis(inputFeatureClass,&amp;nbsp; outputFeatureClass, buffer_corrected)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This should help you get started... Just remember that reading the documentation is absolutely essential!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 20:52:30 GMT</pubDate>
    <dc:creator>StacyRendall1</dc:creator>
    <dc:date>2021-12-10T20:52:30Z</dc:date>
    <item>
      <title>Creating geoprocessing scripts by using tools from toolbox</title>
      <link>https://community.esri.com/t5/python-questions/creating-geoprocessing-scripts-by-using-tools-from/m-p/21418#M1664</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Part A: Create two geoprocessing scripts. Each should utilize at least two tools from ArcToolbox &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Part B: Using one of two scripts created in Part A, create a script tool with at least one user input parameter.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jul 2011 02:43:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-geoprocessing-scripts-by-using-tools-from/m-p/21418#M1664</guid>
      <dc:creator>NeemaMohseni</dc:creator>
      <dc:date>2011-07-05T02:43:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating geoprocessing scripts by using tools from toolbox</title>
      <link>https://community.esri.com/t5/python-questions/creating-geoprocessing-scripts-by-using-tools-from/m-p/21419#M1665</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Two routes:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) Use model builder and the "Export to Python" functionality (File / Export / Python) &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) Check out the documentation on the support site for the tools you would like to string together.&amp;nbsp; This helped me a bunch &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_references/002t0000000z000000/"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_references/002t0000000z000000/&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Jul 2011 17:48:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-geoprocessing-scripts-by-using-tools-from/m-p/21419#M1665</guid>
      <dc:creator>ChristopherFricke1</dc:creator>
      <dc:date>2011-07-07T17:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: Creating geoprocessing scripts by using tools from toolbox</title>
      <link>https://community.esri.com/t5/python-questions/creating-geoprocessing-scripts-by-using-tools-from/m-p/21420#M1666</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Two routes:&lt;BR /&gt;&lt;BR /&gt;1) Use model builder and the "Export to Python" functionality (File / Export / Python) &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w00000001000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;2) Check out the documentation on the support site for the tools you would like to string together.&amp;nbsp; This helped me a bunch &lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_references/002t0000000z000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_geoprocessing_tool_references/002t0000000z000000/&lt;/A&gt;.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Except Option 1 sucks... Exported models usually don't work, and they are incredibly poorly written - which makes them unbelievably confusing!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Go with Option 2, but following are some Arcpy basics... It will also be handy to have done some basic Python tutorials before attempting to do too much.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;1. Every script using Arc functions must start with import arcpy&lt;/STRONG&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;2. Get user inputs with 'GetParameterAsText'&lt;/STRONG&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;This gets the first user entered parameter as text. If you haven't specified any parameters when adding to Arc (see step 3) it will cause the script to fail.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;3. Using the tool in Arc&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Right click on a toolbox you can edit (if you haven't already, you will have to create one in "My Toolboxes" or within a folder), go down to 'Add' and select 'Script...', give it a name and label, select the script file, then add the parameters. For this example we are getting a Feature Class, so call it InputFeatureClass (or something meaningful) and set the Data Type to Feature Class. Click Finish. Double click on your new script tool to use it. At the moment it does nothing, but if you give it a feature class and click OK, it should run happily and say it was successful.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From now on we can just edit the script where it is and the changes will carry through, you won't need to import it again.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is important to note that paths to feature classes are just strings, e.g. 'C:\\GIS Data\\NYC\\2007\\Networks.gdb\\BusLines'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;4. Adding messages to the display with 'AddMessage'&lt;/STRONG&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)

# inform user of selected feature class path
arcpy.AddMessage('&amp;nbsp; Input Feature Class: '+inputFeatureClass)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Save the script and run it again, and it will now print the feature class path before finishing happily.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;5. Performing a geoprocessing operation&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Let's do a buffer on the input features, and also get the user to input the buffer size for the operation; you will need to right click on the tool in Arc, go down to Properties, and in the parameters tab add the parameter BufferSize_m as a Long (integer). Buffer also requires an output feature class, so you will have to add that to the parameters as well; it's data type should be feature class, but you will need to set the Direction (under Parameter Properties) to Output. The script now becomes (with the message made a little more meaningful):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''script.py
'''
import arcpy

# get input feature class
inputFeatureClass = arcpy.GetParameterAsText(0)

# get buffer size 
buffer = arcpy.GetParameterAsText(1)

# get output feature class
outputFeatureClass = arcpy.GetParameterAsText(2)

# inform user of selected feature class path
arcpy.AddMessage('&amp;nbsp; Buffering input Feature Class, '+inputFeatureClass+' by '+buffer+' Meters and writing to: '+outputFeatureClass)

buffer_corrected = buffer + ' Meters'

arcpy.Buffer_analysis(inputFeatureClass,&amp;nbsp; outputFeatureClass, buffer_corrected)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This should help you get started... Just remember that reading the documentation is absolutely essential!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:52:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/creating-geoprocessing-scripts-by-using-tools-from/m-p/21420#M1666</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-10T20:52:30Z</dc:date>
    </item>
  </channel>
</rss>

