You are on page 1of 3

Author: Neela Bariya

#Subject: Advanced Programming for ArcGIS - Deliverable 2

#Goal: To write a python script to create a point feature class from an (x, y) text file and assign the
appropriate spatial reference system to it.

#Date of Creation: 15th March, 2019

#import Operating system and arcpy module

import os

import shutil

import arcpy

from arcpy import env

#Define a raw data and output data to a variable

inputFolder = r"C:\temp\d2RawData"

outputFolder = r"C:\temp\bariyaND2ProcData"

#Delete the output data if it alredy exists

if os.path.exists (outputFolder):

shutil.rmtree(outputFolder)

if not os.path.exists(outputFolder):

os.makedirs(outputFolder)

#set the workspace environment

arcpy.env.workspace = inputFolder

arcpy.env.overwriteOutput = True

#all existing text files are assigned to variable named list

list = arcpy.ListFiles("*.txt")
#set a for loop for textfile in list

#a spatial refernce is defined

#a new point layer is created using x and y data as defined in raw data

#Then new variables created to use in new feature class

for textfile in list:

layer = "tempLayer"

spRef = arcpy.SpatialReference(26917)

arcpy.MakeXYEventLayer_management(textfile, "EastingM", "NorthingM", layer, spRef)

shapeFile = os.path.join(outputFolder, textfile.strip(".*"))

arcpy.CopyFeatures_management(layer, shapeFile)

#set the env workspace

#listed the feature classes that are in shapefile format

#a new variable defined that will combine all shapefiles

#same data type merged

arcpy.env.workspace = outputFolder

shapeList = arcpy.ListFeatureClasses("*.shp")

newShape = "npFarm.shp"

arcpy.Merge_management(shapeList, newShape)

#New feature layer created

#on the basis of extent, data selected

arcpy.MakeFeatureLayer_management("npFarm.shp", "newLayer")

arcpy.SelectLayerByAttribute_management("newLayer", "NEW_SELECTION", '"EastingM" > 610000 AND


"EastingM" < 660000 AND "NorthingM" > 4760000 AND "NorthingM" < 4780000')

#a total number of farms selected on the basis of given extent


output = arcpy.GetCount_management("newLayer")

count = int(output.getOutput(0))

print "There are " + str(count) + " farms in the vicinity of the extent"

arcpy.Delete_management("newLayer")

You might also like