POST
|
Hello: I am trying to run the select attribute tool for a layer that I created. I can manually run the tool in ArcMap, but when I write the script the code fails after the copy feature tool was used. Not sure what I am doing wrong, I am using the ArcMap help section to get a general idea of the code that I needed. I am new to python, so I appreciate any guidance anyone has: #Import a CSV file that will show you what ASOS weather stations are in the state.
import arcpy
import os
#Lets you write over the shape file if needed
arcpy.env.overwriteOutput = True
arcpy.env.workspace = "C:\\GEOG485\\FinalProject"
#Latitude and Longitude Coordinates
xFieldName = 'LON'
yFieldName = 'LAT'
#Output folder
outFolder = "C:\\GEOG485\\FinalProject\\Output"
#ASOS stations layer
eventLayer = "ASOS"
outPutlayer = "C:\\GEOG485\\FinalProject\\FinalProject.gdb\\CurrentStations"
stateLayer = "C:\\GEOG485\\FinalProject\\FinalProject.gdb\\State_ASOS"
#Spatial Reference
spatialRef = arcpy.SpatialReference(4326)
#CSV file that will be imported into ArcMap
csvFilePath = "C:\\GEOG485\\FinalProject\\isd-history.csv"
#Select state of interest
targetState = 'NJ'
#Date of file last updated
targetEnd = '20190329'
try:
#import csv file using the xy event layer tool
arcpy.MakeXYEventLayer_management(csvFilePath, xFieldName, yFieldName, eventLayer, spatialRef)
print ("success1")
#Copy features from the XY Event Layer in order to asign Object ID's to the rows in the file
arcpy.CopyFeatures_management(eventLayer, outPutlayer)
print ("success2")
stateQuery = '"STATE" = ' + "'" + targetState + "'"
#Select Attribute management to narrow down the state of interest
arcpy.SelectLayerByAttribute_management(outPutlayer, "NEW_SELECTION", stateQuery)
print ("success4")
arcpy.SelectLayerByAttribute_management(outPutlayer, 'SUBSET_SELECTION', '"END" = ' + "'" + targetEnd + "'")
print ("success5")
arcpy.SaveToLayerFile_management(outPutlayer, stateLayer)
except:
print ("error2")
try:
mxd = arcpy.mapping.MapDocument("C:\\GEOG485\\FinalProject\\basemap.mxd")
df = arcpy.mapping.ListDataFrame(mxd, "*")[0]
newLayer = arcpy.mapping.Layer(outPutlayer)
arcpy.mapping.Layer(df, newLayer, "Bottom")
except:
print ("error4")
... View more
04-23-2019
06:19 PM
|
0
|
2
|
490
|
POST
|
Hello All: I am a little stuck on my code. I am new to python and am learning as I go, but have usually been able to figure it out till now. The code was working up till I put the dictionary part in and now this is where its failing. When I run the program, it only goes through once and doesn't appear to be storing the data. I have found people in the discussions have had the same problem, but they never said what helped them resolve the issue. Below is a breakdown of the objective of the program: The rhinos in the spreadsheet appear in no guaranteed order, and not all the rhinos appear at the beginning of the spreadsheet. As I parse each line, I must determine which rhino the reading belongs to and update that rhino's polyline track accordingly. I am not allowed to sort the Rhino column in Excel before I export to the CSV file. My script must be "smart" enough to work with an unsorted spreadsheet in the order that the records appear. I do not immediately know how many rhinos are in the file or even what their names are. Although I could visually comb the spreadsheet for this information and hard-code each rhino's name, your script is required to handle all the rhino names programmatically. The idea is that I should be able to run this script on a different file, possibly containing more rhinos, without having to make many manual adjustments. Sample Data: Observer,X,Y,Rhino,Comments Ben,26.99391,-19.10447,Bo, Ben,27.00071,-19.1089,Tulip,Bathing Ben,26.9919,-19.10511,Bo, Ben,27.00071,-19.1059,Tulip, Ben,26.96809,-19.09578,Patches, Ben,26.97808,-19.11016,Dinky, Ben,26.99213,-19.10395,Bo, Ben,27.00083,-19.10326,Tulip, Appreciate any help and guidance anyone has to offer. #Import a csv file into ArcMap and plot the points for each Rhino
import arcpy
import csv
arcpy.env.workspace = "C:\\GEOG485\\Lesson4\\Project4"
arcpy.env.overwriteOutput = True
targetFolder = "C:\\GEOG485\\Lesson4\\Project4"
rhinoShape = "C:\\GEOG485\\Lesson4\\Project4\\Rhino.shp"
rhinoFile = "C:\\GEOG485\\Lesson4\\Project4\\RhinoObservations.csv"
spatialRef = arcpy.Describe(rhinoShape).spatialReference
try:
#Open CSV file and read the headers
trackPoints = open(rhinoFile, "r")
textLine = trackPoints.readline()
headerLine = textLine.strip("\n")
pairList = headerLine.split(",")
print pairList
polylineArray = arcpy.Array()
#Get index for header fields
xIndex = pairList.index("X")
yIndex = pairList.index("Y")
rhinoIndex = pairList.index("Rhino")
print rhinoIndex
#Created an empty dictionary
dictionaryRhinoTracks = {}
for line in trackPoints.readlines():
line = line.rstrip("\n")
splitLine = line.split(",")
print splitLine
#Created variable for needed field items based on index location
pointX = splitLine[xIndex]
pointY = splitLine[yIndex]
rhinoName = splitLine[rhinoIndex]
#Create a point object
currentPoint.x = pointX
currentPoint.y = pointY
currentPoint = arcpy.Point(currentPoint.x, currentPoint.y)
print currentPoint
#Check if the dictionary does not contain name
if rhinoName not in dictionaryRhinoTracks:
rhinoTrackArray = polylineArray.Add(currentPoint)
#Add the point object to the Array dictionaryRhinoTracks[rhinoName].Add(currentPoint)
cursor = arcpy.da.InsertCursor(rhinoShape, ['SHAPE@'])
cursor.insertRow([polyline])
for rhinoName in dictionaryRhinoTracks:
row.Shape = dictionaryRhinoTracks[rhinoName]
row.Rhino_Name = rhinoName
cursor.InsertRow(row)
except: print ("error again")
... View more
04-10-2019
06:52 PM
|
0
|
1
|
750
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|