You are on page 1of 3

###################################################################################

############################
# QURESHIAGISC9317D3.py
# Name: Ali Qureshi
# Date: Thursday April 12
# Description: This python code is created for Deliverable 3 of GISC9317D2. The
purpose of this
# code is to count the number of points in a polygon and return the data in a
feature class attribute table.
###################################################################################
#############################

#Import arcpy module


import arcpy
#Import the os module
import os.path
#Iport the tempfile module
import tempfile
import shutil #used for rmtree
#import the time module
import time

# Create a new folder variable name that will contain the new geodatabase.
newFolder = r'C:\temp\d3ProcDataQureshiA'
# Create a new geodatabase variable name that will contain all new feature classes.
Output_path = "C:/temp/d3ProcDataQureshiA/d3Proc.gdb"
if os.path.exists(newFolder):
# rename the folder before deleting it, this technique works around a problem
where it takes a while to delete the folder
# https://stackoverflow.com/questions/27135470/python-how-to-create-a-
directory-and-overwrite-an-existing-one-if-necessary
tmp = tempfile.mktemp(dir=os.path.dirname(newFolder))
shutil.move(newFolder, tmp)
shutil.rmtree(tmp) # delete a directory/folder and all its contents
os.makedirs(newFolder) # create a new file in the following folder
C:\temp\QureshiAD2ProcData

# Import the env settings module.


from arcpy import env
# Overwrite edited or created files everytime code is run.
env.overwriteOutput = True

# Set variable for actual time at the start of the code


startime = time.time()
# Set the arcpy environment workspace
arcpy.env.workspace = newFolder
# Set variable for the name of new geodatabase
out_name = "d3Proc.gdb"
# Create new geodatabase in newFolder with the name variable set as out_name
arcpy.CreateFileGDB_management (newFolder, out_name)

# Set the arcpy environment workspace


arcpy.env.workspace = "C:/temp/d3RawData/d3Raw.gdb"
# Copy Feature Class npCsd to the listed path and name it countyFarm, this will
ensure the same map projection
arcpy.FeatureClassToFeatureClass_conversion("npCsd", Output_path, "countyFarm")
# Copy Feature Class npFarm to the listed path and name it npFarm
arcpy.FeatureClassToFeatureClass_conversion("npFarm", Output_path, "npFarm")

# Set the arcpy environment workspace


arcpy.env.workspace = Output_path
# Set variable for spatial join path
output1 = "C:/temp/d3ProcDataQureshiA/d3Proc.gdb/SpatialJoin"
# Conduct Spatial join between npFarm and countyFarm in the path defined above
arcpy.SpatialJoin_analysis ("npFarm", "countyFarm", output1, "","")
# Set variable for frequency tool path
output2 = "C:/temp/d3ProcDataQureshiA/d3Proc.gdb/SpatialJointable"
# Conduct Frequency tool on the SpatialJoin table on the city column in the path
defined above
arcpy.Frequency_analysis("SpatialJoin", output2, "City", "")
# Join the countyFarm feature Class to the SpatialJointable using the Name and City
column, keep frequency column
arcpy.JoinField_management("countyFarm", "NAME", "SpatialJointable", "City",
"FREQUENCY")

# Delete the fields listed below from the countyFarm feature class
arcpy.DeleteField_management("countyFarm",
"PRCDCSD;POP2001;DWELL2001;AREA_SQKM;POP1996A;PROV")
# Alter the FREQUENCY column name to NumOfFarm and the NAME column name to
countyName
arcpy.AlterField_management("countyFarm", "FREQUENCY", "NumOfFarm")
arcpy.AlterField_management("countyFarm", "NAME", "CountyName", "CountyName")
# Delete the npFarm, SpatialJoin and SpatialJoinTable feature classes/tables
arcpy.Delete_management("npFarm")
arcpy.Delete_management("SpatialJointable")
arcpy.Delete_management("SpatialJoin")
# Set variable for actual time at the end of the code
endtime = time.time()

# Set variable for time it took to run the code


time = endtime - startime

# Print the time it took to run code rounded to 2 decimal places


print "Code took "+str(round(time,2))+ " seconds to complete"

# Garbage Clean Up
for name in dir():
if not name.startswith('_'):
del globals()[name]
del name

You might also like