GIS Development Course

ARCPY

My GIS Programming and Development coursework introduced me to python programming for Arc with ArcPy, and I enjoyed integrating these newfound ArcPy capabilities into my wildfire research. The script featured here manipulates vector data for a course lab.

_______________________________________________________________________________________________________________________________________________________________

### Sierra Raby Lab 5 Submission - Method 2

### 28 April 2022

### GEOG 181C Week 5


### This is a script for a lab assignment for the course "GIS Programming and Development"

### The lab theme is manipulating vector data.

### The goal of this assignment is to find the closest city to each big lake in North America

### by calculating the distance from each city to each point in the lake polygons.


### =================== Part 1: Setup =======================

import os

import arcpy

import math


# set working directory

folder_path = r'C:\Users\sierr\OneDrive\Documents\spring_22\geog_181C\lab5'

# set input and output folders

input_folder = os.path.join(folder_path, 'LabData')

output_folder = os.path.join(folder_path, 'output')


arcpy.env.workspace = input_folder

arcpy.env.overwriteOutput = True


os.chdir(folder_path)


### ========================== Part 2: Method 2 ========================

print('======================== Part 2: Method 2 ===========================')

# Copy shapefile

lakes_original = "NA_Big_Lakes.shp"

lakes_output2 = arcpy.management.CreateFeatureclass(output_folder, "Lakes_output2.shp", spatial_reference = lakes_original)

arcpy.CopyFeatures_management(lakes_original, lakes_output2)

print("Second output shapefile copied")


cities = "NA_Cities.shp"


# Add empty field for closest city information to output shapefile

arcpy.management.AddField(lakes_output2, "near_city", field_type="TEXT")


# Loop through lakes

with arcpy.da.SearchCursor(lakes_output2, ["SHAPE@", "FID"]) as lakecursor:

    for lakerow in lakecursor:

        FID = lakerow[1]

        distanceList = []

        cityList = []

        partnum = 1

        #loop through parts

        for part in lakerow[0]:

            # loop through points

            for point in part:

                if point:

                    x1 = point.X

                    y1 = point.Y

                    #loop though cities

                    with arcpy.da.SearchCursor(cities, ["SHAPE@XY", "CITY_NAME"]) as citycursor:

                        for cityrow in citycursor:

                            x2, y2 = cityrow[0]

                            city_name = cityrow[1]

                            # calculate distance

                            distance = ((x1 - x2)**2 + (y1 - y2)**2)**0.5

                            distanceList.append(distance)

                            cityList.append(city_name)

                else:

                    pass

            partnum += 1

        # Find the minimum distance via indexing

        min_distance = min(distanceList)

        print(min_distance)

        min_index = distanceList.index(min_distance)

        print(min_index)

        nearest_city = cityList[min_index]

        print(nearest_city)

        # Update the lake shapefile

        with arcpy.da.UpdateCursor(lakes_output2, ["near_city", "FID"]) as cursor:

            for row in cursor:

                if row[1] == lakerow[1]:

                    row[0] = nearest_city

                    cursor.updateRow(row) 

        

print("Done finding closest cities via method 2 and adding them to the new lake shapefile!")