Arcpy Tutorial

Author:

Md Aminul Haque Laskor (ahl.aminlaskor@gmail.com)


In this tutorial, we will learn about Arcpy, which is a popular Python library used in geographic information system. Here we will explore geoprocessing tools by importing Arcpy also we will learn some indices such as NDVI, NDBI, NDWI, NDMI.

N.B. Currently ArcPy is not readily supported in Google Colab. So please use these codes in the ArcGIS Python Environment.


Creating a Folder and Add a shapefile

1. Creating a Folder

In [ ]:
# Specify the path of the parent directory
parent_path = "e:/"

# Specify the name of the new folder
Folder_name = "PythonExercise"

# Create the new folder
arcpy.CreateFolder_management(parent_path,Folder_name)

2. Add Shapefile as a Layer

In [ ]:
import arcpy

# Set up the workspace and overwrite output
arcpy.env.workspace = r"E:\Files_E\GIS Data Documentation"
arcpy.env.overwriteOutput = True

# Define the path to the shapefile
shapefile = "E:\Files_E\GIS Data Documentation\BD Data"

# Add shapefile as a layer
arcpy.MakeFeatureLayer_management(shapefile,"Sylhet")

image.png

Geoprocessing Tool

1. Buffer Analysis

A buffer refers to a zone or area that is created around a specific geographic feature, such as a point, line, or polygon. This buffer zone is defined by a specified distance or radius and is used for various analytical and visualization purposes.

In [ ]:
import arcpy

# Set up the workspace and overwrite output
arcpy.env.workspace = r"E:\Files_E\GIS Data Documentation"
arcpy.env.overwriteOutput = True

# Define the path to the input feature
input_feature = "E:\Files_E\GIS Data Documentation\BD Data\Sylhet.shp"

# Specify the output location and name for the buffer
output_feature = "E:\Files_E\GIS Data Documentation\BD Data\Sylhet_Buffer.shp"

# Specify the buffer distance
buffer_distance = "10 Kilometers"

# Perform the buffer analysis
arcpy.Buffer_analysis(input_feature,output_feature,buffer_distance)

image.png

2. Clip

Clip refers to a spatial operation that involves cutting or extracting a portion of a geographic dataset or shapefile based on the boundaries of another geographic dataset or shapefile. The process essentially clips or trims the original dataset to retain only the areas that fall within the boundary of the second dataset.

In [ ]:
import arcpy

# Set up the workspace and overwrite output
arcpy.env.workspace = r"E:\Files_E\GIS Data Documentation"
arcpy.env.overwriteOutput = True

# Define the path to the input feature class
input_feature = "E:\Files_E\GIS Data Documentation\BD Data\BD_districts.shp"

# Define the path to the clip feature
clip_feature = "E:\Files_E\GIS Data Documentation\BD Data\Sylhet.shp"

# Specify the output location and name for the clipped feature class
output_feature = "E:\Files_E\GIS Data Documentation\BD Data\Sylhet_Clip.shp"

# Perform the clip analysis
arcpy.Clip_analysis(input_feature,clip_feature,output_feature)

image.png

3. Intersect

Intersect is a spatial operation that involves finding and retaining only the areas or features that are common to two or more geographic datasets. The intersect operation creates a new dataset that includes only the spatially overlapping portions of the input datasets.

In [ ]:
import arcpy

# Set up the workspace and overwrite output
arcpy.env.workspace = r"E:\Files_E\GIS Data Documentation"
arcpy.env.overwriteOutput = True

# Define the paths to the input feature classes
input_feature_classes = ["E:\Files_E\GIS Data Documentation\BD Data\BD_districts.shp", "E:\Files_E\GIS Data Documentation\BD Data\Sylhet.shp"]

# Specify the output location and name for the intersected feature class
output_feature_class = "E:\Files_E\GIS Data Documentation\BD Data\Sylhet_Intersect.shp"

# Perform the intersect analysis
arcpy.Intersect_analysis(input_feature_classes, output_feature_class)