POST
|
I finally got this to work - though I had to create a copy of my queried feature class. Here's the code in case it helps anyone. # 1. Import modules and set environment
import arcpy, os, sys, traceback
from arcpy import env
env.workspace = r"F:\Workspace\AFPE\NRS03_AFPE.gdb"
path = 'F:\\Workspace\\AFPE\\2016\\RawMaps2\\'
# stop temp layer from being added to map
arcpy.env.addOutputsToMap = False
# 2. Open a map document - F:\Workspace\AFPE\2016\RawMaps\test.mxd
mxd = arcpy.mapping.MapDocument(r"F:\Workspace\AFPE\2016\RawMaps2\14004.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
try:
# 3. grab USAState layer
lyr = arcpy.mapping.ListLayers(mxd,"USAState",df)
stLyr = lyr[0]
print df.scale
# 4. make duplicate layer of queried USAState and zoom to extent
arcpy.CopyFeatures_management(stLyr, r"in_memory\temp")
desc = arcpy.Describe(r"in_memory\temp")
ext = desc.extent
mxd.activeDataFrame.extent = ext
arcpy.RefreshActiveView()
print df.scale
# 5. clean up
del df, mxd, lyr, stLyr, env
arcpy.Delete_management(r"in_memory\temp")
print 'Map extent complete.'
except:
print 'Program failed.'
del df, mxd, lyr, stLyr, env
arcpy.Delete_management(r"in_memory\temp")
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
arcpy.AddError(msgs)
arcpy.AddError(pymsg)
print msgs
print pymsg
arcpy.AddMessage(arcpy.GetMessages(1))
print arcpy.GetMessages(1)
... View more
09-07-2016
11:11 AM
|
1
|
0
|
914
|
POST
|
the definition queries are working. It is outputting roughly 90 maps which show the different distribution for each species. So the layer's symbology changes with each new definition query. I think it might look like it's selecting text because I use a variable name in the definition query. Anyway, that part works perfectly - its the zoom to layer that I can't get to work. It seems to only zoom to the entire US, not the states that are involved in the definition query. I thought it might have something to do with the format of the loop. All the examples I have found of zooming to a layer's via the definition query seem so simple and straightforward - though it doesn't work for me - so it could very well be that the code is malfunctioning somewhere else. I also wonder if it would just be easier to create a selection set to zoom to, but I don't want the selection highlighted on the map.
... View more
09-02-2016
11:24 AM
|
0
|
1
|
914
|
POST
|
Hi Joshua, My script uses a Look up table to grab a species code - that code is used for the definition query on two shapefiles (a state layer and a county layer). This part is working perfectly. The Select Layer by Attribute is unnecessary, I don't want to have to make a selection and I think I should be able to do this without making a selection. What I wanted to do is zoom to the extent of the state layer after the definition query is performed. Right now the extent just keeps zooming out due to the line where I multiple the scale by 1.1. If I remove that line, then the scale never changes. I thought the getExtent method is what I needed to zoom to the visible extent of the layer after the definition query. does that help clarify? Oh, I am also doing this in IDLE so it runs outside of ArcMap v. 10.3.1 - a stand alone script.
... View more
09-02-2016
07:55 AM
|
0
|
3
|
914
|
POST
|
I am using a definition query to produce species distribution maps and having issues setting the extent of the map (similar to zoom to layer in arcMap's TOC). I think it has to do with the loop, but I'm not sure. Any help or tips are greatly appreciated.
... View more
09-01-2016
11:09 AM
|
0
|
5
|
1942
|
POST
|
Arc did allow me to bring the CSV file in with numbers at the beginning of field names, but when that table is exported into a gdb then the field names are changed to include an "F" at the beginning of the numeric field name. Also, thanks Dan for links. I'll check into those and try not to create poorly constructed excel files.
... View more
12-04-2015
11:52 AM
|
0
|
0
|
530
|
POST
|
Thanks to all of your for all of your helpful comments. It ended up working when I changed the table to a csv file. I guess Arc 10.3.1 and Excel 2013 don't play nicely together. Thanks so much for your help - this forum is great for a speedy response to most all of the GIS problems I run into! Have a great weekend!
... View more
12-04-2015
11:03 AM
|
0
|
1
|
530
|
POST
|
Richard, I tried making all those values ones and it still didn't work. I do completely understand what you are saying, though. I guess I'll try a few more changes to the table and see what happens. Laura
... View more
12-04-2015
10:28 AM
|
0
|
2
|
530
|
POST
|
Hi Daren, Are you doing anything special to the table, like placing it in a geodatabase? When I try to run the tool (after changing the values to all positives) it tells me that the input table does not exist or is not supported. Just wondering what you did to make it work.
... View more
12-04-2015
10:25 AM
|
0
|
3
|
2284
|
POST
|
I altered my excel file (attached) so that the negative values were just zeros instead of a negative value and re-ran the bearing distance tool using the alt10yrSpread or the alt20yrSpread fields for the distance and it still didn't work. Here's the error that I get in case that helps to figure out the problem: Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 2360, in BearingDistanceToLine raise e ExecuteError: ERROR 999999: Error executing function. No current record. Failed to execute (BearingDistanceToLine). Any other ideas on how to draw these lines besides manually drawing them in an editing session is greatly appreciated.
... View more
12-04-2015
08:55 AM
|
0
|
9
|
2284
|
POST
|
I have three fixed points and a number of lines I would like to draw that radiate out from each point. I have tried to use the bearing distance to line tool but it keeps failing for some reason and I'm not exactly sure how to draw my lines otherwise. I am using the angle field for the bearing and this is in degrees (I think...the values fall between 0 and 360). And my distance field is the 20yrSpread field which is in KM. Any help is greatly appreciated...see the attached table.
... View more
12-03-2015
01:54 PM
|
0
|
14
|
6797
|
POST
|
Thanks for bringing this to my attention. I just changed the code to reflect the length field created w/ the geodatabase. Thanks too for all of your help! I couldn't have done this without you! Laura
... View more
06-12-2015
11:43 AM
|
1
|
1
|
661
|
POST
|
Yes, this code is only intended to be run a few times. But it sure beats doing all these steps manually. The summary statistics step took less than a second and I think that's about how long it takes to write the dictionaries. It's amazing to me how quickly it writes to my CSV file. 500 records are written to the file in only 1-2 seconds. So, thanks so much for letting me know about dictionaries. They are amazingly fast! I've already processed more data in a few minutes than I would have in 2 days before. Again, thanks for taking the time to help! It's greatly appreciated!
... View more
06-11-2015
12:18 PM
|
1
|
0
|
661
|
POST
|
Richard, many thanks for all of your insight. I think this will be a much faster process. I can complete the intersect for each layer in roughly 2 minutes, which translates to about 12 hrs to complete the entire code. I'll paste the code here so you can take a look at it. I created a dictionary to hold the summary statistics and recalled the data from that dictionary at the end of my loops. Let me know if you see any glaring errors. # --------------------Set local variables ------------------
# ---workspace for envelope layers
envelopeLyr = 'F:\\Workspace\\Sandy\\GM_costAnalysis\\analysisMay15\\Envelopes\\'
# ---workspace for route layers
routeWkspc = 'F:\\Workspace\\Sandy\\GM_costAnalysis\\analysisMay15\\Routes\\'
# ---set road layer - for use in clip & length
Road = 'F:\\Workspace\\Sandy\\GM_costAnalysis\\RoadsForSnap\\SDC Edge Source.shp'
# ---fields for CSV file
fieldNames = ['Block', 'Scale', 'BufferType', 'Sequence', 'FromPrevTr', 'FromPrevDi', 'x', 'y', 'bufferDist','totTime', \
'totDist', 'totalRdLength', '\n']
# ---List of fields from envelope layer from which to extract data
sourceFieldsList = ["Sequence", "FromPrevTr", "FromPrevDi", "POINT_X", "POINT_Y", "BUFF_DIST"]
try:
print 'Opening CSV file at: ' + strftime("%Y-%m-%d %H:%M:%S")
# Step 1 - Create table for output data and add field names
with open('F:\\Workspace\\Sandy\\GM_costAnalysis\\analysisMay15\\AllTrapsData_Speedy_June11.csv', "wb") as f:
w = csv.writer(f, delimiter=',', lineterminator='\n')
w.writerow(fieldNames)
# Calculate totalTime and totalDistance using insert cursor
# loop through each record in the table - calculate values and...
# use selected features to clip roads layer & calculate geometry
print 'Creating Dictionaries...'
envelopeList = []
for dirpath, dirnames, filenames in arcpy.da.Walk(envelopeLyr, datatype = 'FeatureClass', type = 'Polygon'):
for filename in filenames:
envelopeList.append(os.path.join(filename))
for eLayer in envelopeList:
eLayerName = eLayer.split('_')
blockEID = eLayerName[1]
scaleEID = eLayerName[2].rstrip('.shp')
bufEID = eLayerName[0]
print 'eLayer: ' + eLayer + ' Block: ' + blockEID + ' Scale: ' + scaleEID + ' BufferType: ' + bufEID
eLyrPath = envelopeLyr + eLayer
#Make a layer from the feature class - needed for selecting records to be used in the clipping
arcpy.MakeFeatureLayer_management(eLyrPath, "clipLayer")
#Use Intersect_Analysis tool to clip all of the roads by the envelopes
print "intersect begins: " + strftime("%Y-%m-%d %H:%M:%S")
inFeatures = [Road, "clipLayer"]
intersectOutput = "intersect" + blockEID + "_" + scaleEID + "_" + bufEID
arcpy.Intersect_analysis(inFeatures, intersectOutput, 'ALL','1 Meter', 'LINE')
print "intersect ends: " + strftime("%Y-%m-%d %H:%M:%S")
#Use Statistics_analysis tool to get the sum length per sequence in each envelope
statsOutput = 'stats_' + blockEID + '_' + scaleEID + '_' + bufEID
statsFields = [["length_m", "SUM"]]
caseField = "Sequence"
arcpy.Statistics_analysis(intersectOutput, statsOutput, statsFields, caseField)
#Create a dictionary to hold road length data
statsFieldsList = ["Sequence", "SUM_length_m"]
statsDict = {r[0]:(r[1:])for r in arcpy.da.SearchCursor(statsOutput, statsFieldsList)}
#-----------------------code below-Thanks to Richard Fairhurst----------------------
sourceFC = None
#select corresponding route layer file and grab sublayer: DepotVisits
print 'extracting time & distance to points. BufferType: ' + bufEID
routeLyr = arcpy.mapping.Layer(routeWkspc + "TrapRoute_" + blockEID + "_" + scaleEID + ".lyr")
if routeLyr.isGroupLayer:
for sublyrs in routeLyr:
# print sublyrs.name
if sublyrs.name == 'Depot Visits':
arcpy.MakeFeatureLayer_management(sublyrs, "depotVisits")
sourceFC = "depotVisits"
qLast = "= 2"
expression2 = arcpy.AddFieldDelimiters("depotVisits", "VisitType")+qLast
depotFieldsList = ["VisitType", "FromPrevTravelTime", "FromPrevDistance"]
depotVisitsDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, depotFieldsList, expression2)}
sourceFC = eLyrPath
# Use list comprehension to build a dictionary from a da SearchCursor
sequenceDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}
for key in sequenceDict:
dataList = []
dataList.append(str(blockEID))
dataList.append(str(scaleEID))
dataList.append(str(bufEID))
dataList.append(str(key))
dataList.append(str(list(sequenceDict[key])[0]))
dataList.append(str(list(sequenceDict[key])[1]))
dataList.append(str(list(sequenceDict[key])[2]))
dataList.append(str(list(sequenceDict[key])[3]))
dataList.append(str(list(sequenceDict[key])[4]))
n = int(key)
sN = "=" + str(n)
n2 = n +1
sel = "=" + str(n2)
prevTime = float(list(sequenceDict[key])[0])
prevDist = float(list(sequenceDict[key])[1])
#print prevTime
#print prevDist
postTime = ""
# use a nested search to get values for total time and total distance
if n2 in sequenceDict:
#values = list(sequenceDict[n2])
#for row2 in values:
postTime = float(sequenceDict[n2][0])
totTime = prevTime + postTime
dataList.append(str(totTime))
postDist = float(sequenceDict[n2][1])
totDist = prevDist + float(postDist)
dataList.append(str(totDist))
# if above for loop yeilds no results, calculate postTime and postDist differently
# grab DepotVisits layer - if VisitType = "End" then grab values for "FromPrevTravelTime" & "FromPrevDistance"
if postTime == "":
#select corresponding route layer file and grab sublayer: DepotVisits
print "extracting time & distance to Depot"
if 2 in depotVisitsDict:
row3 = list(depotVisitsDict[2])
postTime2 = row3[0]
totTime2 = prevTime + float(postTime2)
# print ("Depot Visit Time: {0}, {1}, {2}".format(prevTime, postTime2, totTime2))
dataList.append(str(totTime2))
postDist2 = row3[1]
totDist2 = prevDist + float(postDist2)
# print ("Depot Visit Distance: {0}, {1}, {2}".format(prevDist, postDist2, totDist2))
dataList.append(str(totDist2))
# Grab road length values from the statsDict
if n in statsDict:
length = float(statsDict [0])
print length
dataList.append(str(length))
# Write dataList to csv file ---- may need to dedent this one more time ---
w.writerow(dataList)
print 'Road length calculated ' + strftime("%Y-%m-%d %H:%M:%S")
# print dataList
print 'Script completed at: ' + strftime("%Y-%m-%d %H:%M:%S")
f.close()
... View more
06-11-2015
11:43 AM
|
0
|
5
|
967
|
POST
|
Thanks Darren and Dan! It's awesome to get responses so quickly. Darren you were right on. This is exactly what I was looking for, I am currently running the code to see how much time I'll save vs. clipping and I think it will be hours compared to weeks. So, my code is definitely speeding up. Dan thanks for pointing out a new tool that I never knew existed!
... View more
06-11-2015
11:14 AM
|
0
|
1
|
1059
|
POST
|
Thanks for the clarification Richard. I was thinking I needed to use the polygon intersect and couldn't find any examples of using that method. I am working on adding the intersect_analysis tool now and hope to test it out shortly. It seems to do exactly what I want when performed in ArcMap. So, now it's all a matter of speed. I agree with you about doing this part first and using all the polygons in each envelope layer at one time instead of doing the polygons individually. I am still trying to understand the dictionary, it's a bit fuzzy to me how you recall the correct field values. So, I'll have to do a bit more reading. Thanks again for all of your time! It is so incredibly helpful to be able to get such quick feedback from such a knowledgeable group.
... View more
06-11-2015
06:32 AM
|
0
|
6
|
967
|
Title | Kudos | Posted |
---|---|---|
1 | 09-07-2016 11:11 AM | |
1 | 06-12-2015 11:43 AM | |
1 | 06-11-2015 12:18 PM | |
1 | 06-10-2015 06:55 AM | |
1 | 02-05-2015 11:11 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|