Python Script Arcpy Upload Csv to Geocode
Writing arcpy scripts¶
At this point nosotros have created a nice interface for our Python tool in ArcGIS:
Now we need to write the functionalities for our tool. The aim of our tool is to convert Shapefiles into rasters and give the raster a value that the user specifies in the user interface. Let'southward see how this can be done.
Start, we need start the ArcGIS IDLE (if it is non open up already) from All Programs –> ArcGIS:
Create a new script chosen PolyToRaster.py in IDLE (File –> New File) and save it to your calculator:
Let's commencement writing the functionalities of our tool to the script that we just created.
Importing arcpy¶
Showtime thing to do is to import the arcpy module:
# Import arcpy module so we can employ ArcGIS geoprocessing tools import arcpy import os Annotation
Discover that arcpy tin can only be imported with Python interpreter that comes with ArcGIS. If you try to import information technology eastward.g. in Spyder, you will receive an fault ImportError: No module named 'arcpy' .
Well...actually at that place is a way to import arcpy from other places besides and besides import it into Spyder simply this is not a topic of this course.
Adding a new field into aspect table¶
Next, we need to add together a new field that is called in a way that the user wants it. The field name is stored in the attribute_name variable. Adding new cavalcade tin be done past using a function chosen AddField_management() (see help).
# 2. Add together a new field into the input shapefile with 'AddField_management' method #------------------------------------------------------------------------------ # Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/add-field.htm arcpy . AddField_management ( in_table = input_species_shp , field_name = attribute_name , field_type = "SHORT" ) # Other possible parameters can be left every bit default Updating column with Field Reckoner¶
Permit'south update the newly created column with the Presence value that was asked from the user and will be assigned to the raster cells. We tin do calculations in attribute table with CalculateField_management() -function. Let's update the column with value that is stored in presence_value variable.
# 3. Update the presence value for our newly created attribute with 'CalculateField_management' method #----------------------------------------------------------------------------------------------------- # Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/information-management-toolbox/summate-field.htm arcpy . CalculateField_management ( in_table = input_species_shp , field = attribute_name , expression = presence_value ) Iterating over values in aspect table¶
As we wanted to salvage individual species into separate raster files, nosotros need to determine the unique species in our attribute tabular array. In Pandas / Geopandas there is a overnice function called .unique() for this purpose just unfortunately arcpy does not take such a function that would work with Shapefiles. Hence, nosotros demand to create the "unique" -function ourselves.
Let'southward create a role that iterates over the values in a column and returns a listing of unique values that are present in that cavalcade. We can iterate over the rows in attribute table past using SearchCursor() -function (read-only) in arcpy.
#----------------------------------------------------------------------------------------------------------------------------------- # 4. Get a list of unique species in the tabular array using 'SearchCursor' method # Method info: http://desktop.arcgis.com/en/arcmap/latest/clarify/arcpy-data-access/searchcursor-class.htm # More than elegant version of the office in ArcPy Cafe: https://arcpy.wordpress.com/2012/02/01/create-a-list-of-unique-field-values/ # ---------------------------------------------------------------------------------------------------------------------------------- # iv.1 CREATE a part that returns unique values of a 'field' within the 'table' def unique ( table , field ): # Create a cursor object for reading the table cursor = arcpy . da . SearchCursor ( tabular array , [ field ]) # A cursor iterates over rows in table # Create an empty list for unique values unique_values = [] # Iterate over rows and append value into the listing if it does not be already for row in cursor : if non row [ 0 ] in unique_values : # Suspend only if value does not exist unique_values . suspend ( row [ 0 ]) return sorted ( unique_values ) # Return a sorted list of unique values Let's utilize our function in post-obit manner:
# 4.2 USE the role to get a listing of unique values unique_species = unique ( table = input_species_shp , field = species_attribute ) Note
If your data is in Geodatabase, y'all tin can utilise DISTINCT operator in a sql_clause that you tin laissez passer to the SearchCursor (see assist).
Hint
Updating rows
If you lot demand to update rows using similar iteration approach, information technology is possible to do with UpdateCursor() -office (see help).
Selecting information¶
At present that we have a list of unique species values we tin can iterate over that list and select all rows that represent to a selected species and and so rasterize those rows (polygons).
Before we tin do selections in arcpy, we demand to "prepare" the option by creating a temporary feature layer (enables to make selections) using MakeFeatureLayer_management() -office (run into help):
#-------------------------------------------------------------------------------------------------------------------------------- # v. Create a feature layer from the shapefile with 'MakeFeatureLayer_management' method that enables us to select specific rows # Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/brand-feature-layer.htm #-------------------------------------------------------------------------------------------------------------------------------- species_lyr = arcpy . MakeFeatureLayer_management ( in_features = input_species_shp , out_layer = "species_lyr" ) Now the feature layer "lives" temporarily in the variable species_lyr that we utilise for making the selections.
Next, nosotros tin can start iterating over those unique species that are stored in unique_species -listing and select rows with SelectLayerByAttribute_management() -role (encounter help) based on the species name (in a similar way that you would exercise with SelectByAttributes -query in ArcGIS, and save those selections into divide Shapefiles using CopyFeatures_management() -function (see assist).
#--------------------------------------------------- # vi. Iterate over unique_species list and: # 6.1) consign individual species as Shapefiles and # vi.two) catechumen those shapefiles into Raster Datasets #--------------------------------------------------- for individual in unique_species : # 6.1): # Create an expression for pick using Python String manipulation expression = " %s = ' %s '" % ( species_attribute , private ) # Select rows based on individual breed using 'SelectLayerByAttribute_management' method # Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/select-layer-by-attribute.htm arcpy . SelectLayerByAttribute_management ( species_lyr , "NEW_SELECTION" , where_clause = expression ) # Create an output path for Shapefile shape_name = private + ".shp" individual_shp = os . path . bring together ( output_folder , shape_name ) # Export the pick as a Shapefile into the output binder using 'CopyFeatures_management' method # Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/data-direction-toolbox/copy-features.htm arcpy . CopyFeatures_management ( in_features = species_lyr , out_feature_class = individual_shp ) Convert Polygons to raster¶
At present we are saving the species into divide Shapefiles which we can convert to rasters using PolygonToRaster_conversion() -function (encounter help). Allow's as well send information to the user about the process with AddMessage() -function (come across assistance). Let'south add the post-obit lines in the same loop that we started previously:
# 6.ii): # Create an output path for the Raster Dataset (*.tif) tif_name = individual + ".tif" individual_tif = os . path . bring together ( output_folder , tif_name ) # Convert the newly created Shapefile into a Raster Dataset using 'PolygonToRaster_conversion' method # Method info: http://desktop.arcgis.com/en/arcmap/latest/tools/conversion-toolbox/polygon-to-raster.htm arcpy . PolygonToRaster_conversion ( in_features = individual_shp , value_field = attribute_name , out_rasterdataset = individual_tif ) # Print progress info for the user info = "Candy: " + individual arcpy . AddMessage ( info ) The full script¶
Here is the total script that we prepared previously:
# Import arcpy module and then nosotros can use ArcGIS geoprocessing tools import arcpy import sys , os input_species_shp = arcpy . GetParameterAsText ( 0 ) output_folder = arcpy . GetParameterAsText ( one ) species_attribute = arcpy . GetParameterAsText ( 2 ) attribute_name = arcpy . GetParameterAsText ( three ) presence_value = arcpy . GetParameterAsText ( 4 ) # 2. Add a new field into the table using 'AddField_management' method arcpy . AddField_management ( in_table = input_species_shp , field_name = attribute_name , field_type = "Brusque" ) # 3. Update the presence value for our newly created attribute with 'CalculateField_management' method arcpy . CalculateField_management ( in_table = input_species_shp , field = attribute_name , expression = presence_value ) # 4. Become a list of unique species in the tabular array using 'SearchCursor' method # 4.one CREATE a office that returns unique values of a 'field' within the 'table' def unique_values ( tabular array , field ): # Create a cursor object for reading the table cursor = arcpy . da . SearchCursor ( table , [ field ]) # A cursor iterates over rows in table # Create an empty list for unique values unique_values = [] # Iterate over rows and append value into the list if information technology does not exist already for row in cursor : if not row [ 0 ] in unique_values : # Append only if value does non exist unique_values . append ( row [ 0 ]) return sorted ( unique_values ) # Return a sorted list of unique values # 4.2 Utilize the function to go a listing of unique values unique_species = unique_values ( table = input_species_shp , field = species_attribute ) # 5. Create a feature layer from the shapefile with 'MakeFeatureLayer_management' method that enables united states to select specific rows species_lyr = arcpy . MakeFeatureLayer_management ( in_features = input_species_shp , out_layer = "species_lyr" ) # six. Iterate over unique_species list and: # 6.i) consign individual species as Shapefiles and # 6.2) convert those shapefiles into Raster Datasets for private in unique_species : # 6.i): # Create an expression for selection using Python String manipulation expression = " %s = ' %s '" % ( species_attribute , individual ) # Select rows based on individual brood using 'SelectLayerByAttribute_management' method arcpy . SelectLayerByAttribute_management ( species_lyr , "NEW_SELECTION" , where_clause = expression ) # Create an output path for Shapefile shape_name = private + ".shp" individual_shp = os . path . join ( output_folder , shape_name ) # Export the option every bit a Shapefile into the output folder using 'CopyFeatures_management' method arcpy . CopyFeatures_management ( in_features = species_lyr , out_feature_class = individual_shp ) # half dozen.two): # Create an output path for the Raster Dataset (*.tif) tif_name = individual + ".tif" individual_tif = os . path . bring together ( output_folder , tif_name ) # Convert the newly created Shapefile into a Raster Dataset using 'PolygonToRaster_conversion' method arcpy . PolygonToRaster_conversion ( in_features = individual_shp , value_field = attribute_name , out_rasterdataset = individual_tif ) # Print progress info for the user info = "Processed: " + individual arcpy . AddMessage ( info ) # vii. Print that the process was finished successfully info = "Process was a great success! Wuhuu!" arcpy . AddMessage ( info ) At present, we take a script that we tin use from our Toolbox in ArcGIS! Let's adjacent see how information technology tin can exist used.
Source: https://automating-gis-processes.github.io/2016/Lesson6-arcpy-script.html
0 Response to "Python Script Arcpy Upload Csv to Geocode"
Post a Comment