Last active
June 1, 2016 22:02
-
-
Save CodeWithCory/1fb346adf5fb11ac242a0877d1a2cdbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cory Rahman | |
# This script must be added to an ArcGIS Toolbox (.tbx) in order to run | |
# Python 2.7.8 | |
print "Initializing...", | |
import os | |
import arcpy | |
from arcpy.sa import * | |
print " Done!" | |
print "Reading text file and creating Feature Class...", | |
# Step 1: Reads UTF and displays XY data as a feature class | |
directory=arcpy.GetParameterAsText(0)#VARIABLE----- | |
arcpy.env.overwriteOutput = True | |
arcpy.env.workspace = directory#sets the arcpy workspace to the GDB path from above | |
utf8_file = arcpy.GetParameterAsText(1)#VARIABLE----- | |
inputName = arcpy.GetParameterAsText(2)#VARIABLE----- | |
sr = arcpy.SpatialReference('WGS 1984')#sets sr variable to coordinate system | |
arcpy.CreateFeatureclass_management(directory, inputName, "POINT", spatial_reference = sr)#creates the Feature Class | |
arcpy.AddField_management(inputName, "Species_Name", "TEXT")#Populates the feature class | |
arcpy.AddField_management(inputName, "Count", "FLOAT")#Populates the feature class | |
in_file = open(utf8_file, "r") | |
in_file.readline() | |
#these lists get apended upon in the while loop below | |
species_list = [] | |
count_list = [] | |
point_list = [] | |
while 1: | |
raw_line = in_file.readline() | |
if not raw_line: | |
break | |
values = raw_line.split("\t") | |
Species_Name = values[3] | |
species_list.append(Species_Name) | |
Count = values[7] | |
count_list.append(Count) | |
latitude = float(values[21]) | |
longitude = float(values[20]) | |
point_1 = arcpy.Point(latitude,longitude)#sets long and lat for below | |
point_list.append(point_1) | |
with arcpy.da.InsertCursor(inputName, ["SHAPE@", "Species_Name", "Count"]) as ic: | |
for lcv_row in range (0, len(count_list)): | |
if count_list[lcv_row] == "X": | |
pass | |
else: | |
ic.insertRow([point_list[lcv_row], species_list[lcv_row], count_list[lcv_row]]) | |
# ^Creates each point | |
print " Done!" | |
print "Projecting XY Data...", | |
# Step 2: Projects the XY data to USA Contiguous Albers Equal Area Conic | |
input_features=directory+"/"+inputName | |
output_features_class = input_features+"_reprojected" | |
install_dir = arcpy.GetInstallInfo()['InstallDir'] | |
out_coordinate_system = arcpy.SpatialReference('USA Contiguous Albers Equal Area Conic') | |
arcpy.Project_management(input_features, output_features_class, out_coordinate_system) | |
print " Done!" | |
arcpy.CheckOutExtension("Spatial") | |
print "Creating Kernel Density...", | |
# Step 3: Perform Kernel Density analysis on projected XY data | |
outKDens = KernelDensity(output_features_class, "Count") | |
outKDens.save(input_features+"_Kernel") | |
print " Done!" | |
print "Creating Contours...", | |
# Step 4: Create Contours from Kernel Density | |
Contour(inputName+"_Kernel", input_features+"_Contours",.01) | |
print "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment