Вы находитесь на странице: 1из 3

C:\Users\Dima\Desktop\School Backup\Semester2\Python\D2\KurylovichDGisc9307D2.

py

Sunday, April 26, 2015 3:35 AM

#
#
#Title:
D2 Manipulate Street Address
#Course :
GISC9307D2
#Last Edited:
19/02/2015
#Author:
Dmitry Kurylovich
#Purpose:
The purpose of this python script is to create two shapefiles for Farms and
Wineries.
#
The shapefiles are generated from two text files. The script works similarily
to how
#
shapefiles are created from XYEvent Layers manually using ArcGIS
10.2.as_integer_ratio
#
The script inputs the farm.txt and winery.txt and creates and XYEventLayer
using two specified
#
spatial coordinate columns. In this case the spatial coordinate columns are
'EastingM' and 'NorthingM'
#
#
#Note(s):
The print statements in the code are used for debugging purposes.
#---------------------------------------------------------------------------------------------------------#importing the arcpy and env modules.
import arcpy
from arcpy import env
env.overwriteOutput = True
#varible establishing the directory which user will be working out of.
directory = r'C:\temp\d2ProcKurylovichD'
#looks for the directory folder, if it exists, the folder will be deleted and a new
#directory will be created
import os
if os.path.exists (directory):
import shutil
shutil.rmtree (directory)
#if the directory does not exist a new one will be created.
if not os.path.exists (directory):
os.makedirs (directory)
#defines the workspace.
env.workspace = r'C:\temp\d2RawData'
#The two lines below refer to the columns containing the Easting and Northing in the text file
X_Coord = "EastingM"
Y_Coord = "NorthingM"
# These variables are setting the extent for the counter used in the last step of this program.
XMax = 660000
XMin = 610000
YMax = 4780000
YMin = 4760000

-1-

C:\Users\Dima\Desktop\School Backup\Semester2\Python\D2\KurylovichDGisc9307D2.py

Sunday, April 26, 2015 3:35 AM

#defined the working spatial reference


SpatialRef = "Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983 UTM Zone
17N.prj"
#output location for both Farm and Winery point feature classes and the merged shapefile
outputLocation = r"C:\temp\d2ProcKurylovichD"
outputLocationMerge = r"C:\temp\d2ProcKurylovichD\npFarm.shp"
#Creates an array of all files ending with .txt within the defined workspace
txtFiles = arcpy.ListFiles('*.txt')
#this loop will run as many times as there are .txt files.
for txtFile in txtFiles:
#this splits the name of the textfile and assigns only the name portion of to the fileName
variable
#this is used to preserve the names of the textfiles and assigns it to the generated
shapfiles.
#the name is preserved by assigning the 0 index (filename) to variable fileName.
fileName = txtFile.split('.')[0]
#allows an overwrite
env.overwriteOutput = True
#creates an XYEvent Layer for the Farm textfile, using the 'EastingM' and 'NorthingM' Fields
arcpy.MakeXYEventLayer_management(txtFile, X_Coord, Y_Coord, fileName, SpatialRef)
print "layers created"
#The generated XYEvent layer has to be converted from a featureclass to a shapefile
#these lines of code convert the created XYEventLayer into a shapefile and saves it to the
designated output location
arcpy.FeatureClassToShapefile_conversion(fileName, outputLocation)
print "Feature Class created"
continue
env.workspace = r"C:\temp\d2ProcKurylovichD"
shpList = arcpy.ListFiles('*.shp')
#Megres the two created feature classes into one feature class called WineryFarmCombined
#for shpFile in shpList:
arcpy.Merge_management(shpList, 'npFarm.shp')
print "Merge Succeeded for all .shp files"
# in order to count the amount of data that exists within the given extent a feature layer
needs to be created,
# this feature layer will be used by GetCount_management to count the amount of existing points.
arcpy.MakeFeatureLayer_management('npFarm.shp', 'layerCount')
print "New layer Created"
print "******SCRIPT SUCCEEDED********"
#the counter will count the amount of features exist within a given extent
counter = 0
-2-

C:\Users\Dima\Desktop\School Backup\Semester2\Python\D2\KurylovichDGisc9307D2.py

Sunday, April 26, 2015 3:35 AM

countedFeatureClass= r"C:\temp\d2ProcKurylovichD\npFarm.shp"
#A search cursor is used to look inside of the npFarm shapefile to count
cursor = arcpy.SearchCursor(countedFeatureClass)
for row in cursor:
if row.getValue(X_Coord) < XMax and row.getValue(X_Coord) > XMin and row.getValue(Y_Coord) <
YMax and row.getValue(Y_Coord) > YMin:
#everytime the search cursor finds a value within the defined extent, +1 will be added to
the counter.
counter += 1
#delets the cursor to prevenet hanging files.
del cursor
#prints the amount of points that fall within the extent, the counter is cast to a string for
concatenation purposes
print "The amount of points that fall within the extent is: " + str(counter)

-3-