Tuesday, May 17, 2022

Arcpy and python scripts for Arcmap desktop GIS software

 Listed below are some scripts in python/acrpy for performing various useful tasks in Arcmap/ArcGIS desktop software.


Selected and save shapefile based on attribute

# Code to save selected shp based on attribute...

import arcpy

# Path to input shp...
nig_shp = r"C:\Users\Yusuf_08039508010\Desktop\...\Data\New Data from Scratch\data\NIG_ADM.shp"

# Path to output shp...
out_put_path = r"C:\Users\Yusuf_08039508010\Desktop\...\Data\New Data from Scratch\data\out\nez_3.shp"

# Select and save polygons where "Weight"=5...
arcpy.Select_analysis(nig_shp, out_put_path, '"Weight"=5')

# Select and save polygons where "geographic"='NEZ'...
arcpy.Select_analysis(nig_shp, out_put_path, '"geographic"=\'NEZ\'')


Create new attribute field in bulk

# Create new attribute field

import arcpy


shp = r"C:\Users\Yusuf_08039508010\Desktop\...\Data\New Data from Scratch\data\NIG_ADM.shp"

field_name = 'Yr_2025'
field_type = 'Integer'

arcpy.AddField_management(shp, field_name, field_type)

print('New field has been added...')



#Exercise: Modify the arcpy code snippet above to create attribute fields from years from 1980 to 2020.

# Solution....
for year in range(1980, 2021):
        print('Processing....', year)

        field_name = 'Yr_{}'.format(year)
        field_type = 'Integer'

        arcpy.AddField_management(shp, field_name, field_type)
# Another version of bulk creation of attribute fields...
import arcpy

for x in range(1980, 1990):
    print('Creating field...', x)

    field_name = 'AA_{}'.format(x)
    field_type = 'Integer'

    arcpy.AddField_management('NIG_ADM', field_name, field_type)


Buffer at multiple distances

# Buffer at multiple distance

import arcpy

my_buffer_dist = [5000, 8000, 11000, 14000, 17000, 20000, 23000, 26000, 29000, 32000]

for distance in my_buffer_dist:
    print('Processing...', distance)

    out_file_name = "Buffer_{}m".format(distance)

    arcpy.Buffer_analysis("Lagos_to_Kano", out_file_name, distance)


Select features from provided list

import arcpy
from arcpy import env


# Get number of features in shp
feature_count = int( arcpy.GetCount_management(nig_shp).getOutput(0) )

# Define custome workspace directory...
env.workspace

states = ['Abia', 'Adamawa', 'Akwa Ibom', 'Anambra', 'Bauchi', 'Bayelsa', 'Benue', 'Borno', 'Cross River', 'Delta', 'Ebonyi', 'Edo', 'Ekiti', 'Enugu', 'Abuja', 'Gombe', 'Imo', 'Jigawa', 'Kaduna', 'Kano', 'Katsina', 'Kebbi', 'Kogi', 'Kwara', 'Lagos', 'Nasarawa', 'Niger', 'Ogun', 'Ondo', 'Osun', 'Oyo', 'Plateau', 'Rivers', 'Sokoto', 'Taraba', 'Yobe', 'Zamfara']

out_folder = r"C:\Users\Yusuf_08039508010\Desktop\...\Data\New Data from Scratch\data\out"

for f in states:
    print('Processing object...', f)
    out_put_path = out_folder + '\{}.shp'.format(f)
    arcpy.Select_analysis(nig_shp, out_put_path, '"state_name"=\'{}.format(f)\'')


Label based on column with varying font size and color

# Label based on column with varying font size and color (to be used in label EXPRESSION)
def FindLabel ( [state_name] ): len_of_name = len( [state_name] ) if len_of_name < 6: return " <CLR red='255' green='0' blue='0'> <FNT size = '{}'> {} </FNT> </CLR> ".format( len_of_name, [state_name] ) elif len_of_name >= 6: return " <CLR red='0' green='0' blue='255'> <FNT size = '{}'> {} </FNT> </CLR> ".format( len_of_name, [state_name] )


Multiline label with varying font size

# Multiline label with varying font size (to be used in label EXPRESSION)

def FindLabel ( [state_name], [Weight] ):
  return [state_name] +  "\n"  +  "<FNT size = '14'> {} </FNT>".format([Weight])



Return random value from a list into cell of an attribute field
# Return random value from a list (to be used in field Calculator)

import random
meters_per_hr = [30000, 35000, 40000]

def random_km():
    return random.choice(meters_per_hr)




Hope you find it useful for you next project.