You are on page 1of 2

# QURESHIAGISC9317D2.

py
# Name: Create Point Data Feature Class From XY data
# Date: Tuesday March 27th 2018
# Description: This python code is created for Deliverable 2 of GISC9317D2. The
purpose of this
# code is to convert text files (XY data) into shape files. This code also merges
2 shapefiles together to create
# a third shapefile and finally count the number of features in a provided extent.

#Import arcpy module


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

# Create a new folder to hold the shapefiles


newFolder = r'C:\temp\QureshiAD2ProcData'
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 workspace environment from arcpy module


from arcpy import env

#Set the workspace to the following folder


env.workspace = r"C:\temp\d2RawData"

# Create a list of all files that end with .txt and name the list TextFiles
TextFiles = arcpy.ListFiles("*.txt")
# For all the txtfiles in the TextFiles list perform the following
for txtFile in TextFiles:
# Split the names from the "." and use the first word for the name and call
this variable File
File = txtFile.split (".")[0]
# Allow the output to be overwritten if previous files exist
env.overwriteOutput = True
# Set the coordinate system as NAD83 UTM Zone 17N and name it spRef
spRef = "Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983
UTM Zone 11N.prj"
# Create an XY event layer using the txtFiles, the x column is EastingM, the y
column is NorthingM, the output name is based on
# file variable and the projection is based on the spRef variable
arcpy.MakeXYEventLayer_management(txtFile, "EastingM" , "NorthingM" , File,
spRef)
# Convert XY event layer to shapefile by using the File variable for the input
and newFolder as the output location
arcpy.FeatureClassToShapefile_conversion(File, newFolder)

# Set workspace to the following location


env.workspace =newFolder
# Create a list of all files that end with .shp and call it shpfiles
shpfiles = arcpy.ListFiles("*.shp")
# Merge all files in the shpfiles list and name is npfarm.shp
arcpy.Merge_management (shpfiles, "npfarm.shp")

# Set workspace to the following location


arcpy.env.workspace = newFolder

# Set the extent to the following Xmax, Xmin, Ymax, Ymin values
arcpy.env.extent = arcpy.Extent (610000,4760000,660000,4780000)

#Count all features in the npfarm.shp file in the extent above and name it results
results = arcpy.GetCount_management("npfarm.shp")

# print results of the above command


print results

# Clean up by deleting all variable names


del newFolder
del TextFiles
del File
del shpfiles
del results

You might also like