|
POST
|
I also found this from a thread a few years old, using an update cursor to be able to do the same thing but include an if statement so features get treated differently. Thanks to Chris Snyder for his example snippet! updateRows = arcpy.UpdateCursor("pythontesting", "", "", "FIELD3")
updateRow = updateRows.next()
while updateRow:
if updateRow.FIELD1 == 1:
updateRow.FIELD3 = (updateRow.FIELD1 + 1) * updateRow.FIELD2
else:
pass
updateRows.updaterow(updateRow)
updateRow = updateRows.next()
... View more
09-18-2013
11:58 AM
|
0
|
0
|
861
|
|
POST
|
You're right!! In the field calculator that's how the field names get labeled... It does work now, just I have to be in edit mode because it gives me a cannot acquire a lock error, can this be avoided?
... View more
09-18-2013
11:24 AM
|
0
|
0
|
861
|
|
POST
|
I'm trying understand the CalculateField_management tool and I guess I just don't understand how to properly define a field to be a variable. I'm just working in the Python Window for now, attempting to get my expression to work. I'm just using a test shapefile until I understand how the function works, so I just have "FIELD1", "FIELD2", "FIELD3", AND "FIELD4" and when I run my expression I receive an error saying it can't concatenate 'str' and 'int' objects. Obviously this is a problem because all of my attribute fields are set up as number fields, 1 and 2 are short and 3 and 4 are double. So do I need a line to define each column as a number since it seems to be defaulting to strings? Here is my line: arcpy.CalculateField_management("pythontesting", "FIELD3", ("FIELD1" + 1) * "FIELD2", "PYTHON") That doesn't work, but if I replace my expression with 8 * 4, then I get the calculated value of 32. What am I doing wrong?
... View more
09-18-2013
10:11 AM
|
0
|
4
|
1780
|
|
POST
|
When you say the tool won't run (after correcting the path issue), is it that you get the same error, or the tool dialog itself shows an issue (red X)? If it's the tool dialog, then you might need to adjust how the parameter is set up. If it's set up as a 'file', validation does check if the file exists. In the dialog box I get a red "X", why does the file need to exist beforehand? Okay, I exported a page so I had a pdf file to replace and it worked just fine. Does this seem odd? And I have to browse to the folder and select the file. For some reason, even though the path and file a specified, it won't go unless I manually choose that file.
... View more
08-20-2013
11:51 AM
|
0
|
0
|
2680
|
|
POST
|
I noticed it was just a folder location listed, I set the tool parameter wrong. But when I do specify a file name, the tool won't run because it says the file doesn't exist. I don't have the problem when I won't run a multi-page pdf export, but it doesn't create documents either. I don't know what changed to keep it from the file creation, but it says it completes the script on single page PDFs. import arcpy, os, sys
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr = arcpy.mapping.ListLayers(mxd, "Detail_2013")[0]
#Get input parameters
pageNumList = arcpy.GetParameterAsText(0).split(";")
multiPage = arcpy.GetParameterAsText(1)
outputPDF = arcpy.GetParameterAsText(2)
if multiPage == "true":
if os.path.exists(outputPDF):
os.remove(outputPDF)
#Create new FinalMapBook that results will be appeneded into
finalMapBook = arcpy.mapping.PDFDocumentCreate(outputPDF)
#Listing the text elements on the page
concatElem1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat1")[0]
concatElem2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat2")[0]
#Get page number from data driven page - specified in the tool parameter dialogue box
ddp = mxd.dataDrivenPages
for pageNum in pageNumList:
arcpy.AddMessage(pageNum)
pageID = mxd.dataDrivenPages.getPageIDFromName(str(pageNum))
mxd.dataDrivenPages.currentPageID = pageID
#Set layer definition query, this contols the rowcount variable
pageFieldValue = pageNum
mapLyr.definitionQuery = '"pageNum" = %s' % pageNum
#Finds the number of features in the map and sets up the lists to evenly distribute into the two text elements/columns
rowcount = int(arcpy.GetCount_management("Detail_2013").getOutput(0))
percolumn = round(rowcount / 2.0)
count1 = 1
count2 = rowcount
#Specifies the features being used for the SearchCursor
rows = arcpy.SearchCursor(mapLyr, "", "", "CONCAT")
fieldrow = arcpy.SearchCursor(mapLyr, "", "", "pageNum")
currentpage = ""
text_var1 = str()
text_var2 = str()
for row in fieldrow:
if currentpage != row.pageNum:
currentpage = row.pageNum
for row in rows:
if count1 <= percolumn:
text_var1 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)
concatElem1.text = text_var1
count1 += 1
elif count2 > percolumn:
text_var2 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)
concatElem2.text = text_var2
count2 - 1
else:
pass
else:
pass
#Generate multi-page PDF file or single PDF files
if multiPage == "true": #Appends all PDFs into single, multi-page PDF
arcpy.mapping.ExportToPDF(mxd, outputPDF[:-4] + pageNum + ".pdf") #strip away .pdf, add platnum value and add ".pdf"
finalMapBook.appendPages(outputPDF[:-4] + pageNum + ".pdf") #append page to final PDF
os.remove(outputPDF[:-4] + pageNum + ".pdf") #no longer needed once appended
else: #Create individual PDFs for each page
arcpy.mapping.ExportToPDF(mxd, outputPDF[:-4] + pageNum + ".pdf")
#Add PDF properties for multi-page PDF
if multiPage == "true":
finalMapBook.updateDocProperties("2010-2011 Crash Report", "2010-2011 Crash Report", "map sheets, map book", "USE_THUMBS")
finalMapBook.saveAndClose()
arcpy.RefreshActiveView()
arcpy.AddMessage("PROCESS COMPLETED")
#Removed the definition query so all page numbers appear when the script is run next and refresh he layout view
mapLyr.definitionQuery = ""
arcpy.RefreshActiveView()
del mxd, row, rows, rowcount, percolumn, count1, count2
... View more
08-20-2013
09:38 AM
|
0
|
0
|
2680
|
|
POST
|
I'm trying to create a DDP export to PDF script that uses the ESRI Example from South Kingstown, RI, and when I run my script I get this error: Runtime error Traceback (most recent call last): File "<string>", line 17, in <module> WindowsError: [Error 5] Access is denied: 'R:\\GIS\\...\\PDF' I thinks due to this bit of code: if multiPage == "true": if os.path.exists(outputPDF): os.remove(outputPDF) #Create new FinalMapBook that results will be appeneded into finalMapBook = arcpy.mapping.PDFDocumentCreate(outputPDF) I have full access to the drive and the folder. I tried my local drive and I get the same error. Any ideas?
... View more
08-20-2013
08:57 AM
|
0
|
7
|
4370
|
|
POST
|
@rzufelt I received the subscription email for your post, but I'm not seeing it in the thread. Yes, I did use "pageNum" as my field name in both my index layer and my feature layer.
... View more
08-20-2013
08:29 AM
|
0
|
0
|
648
|
|
POST
|
I have a full working script! Mzcoyle and Wayne, I couldn't have done this without you guys, so thank you so much for your patience and help! I have to also thank South Kingstown, RI for their ESRI example, I wouldn't have been able to figure this out without their template. Only one parameter was created and that was for the page number selection. The tool takes the features in the data driven page map extent and lists the "CONCAT" field into two text elements on the screen. The "CONCAT" field is a concatenated field of the feature's number on the map and the feature description. Each feature's string is listed on a new line and the lines are divided so it appears as though there are two columns of text. If there are an uneven number of lines, then the first column will have one more line than the second. Here's the python code: import arcpy, os #set map doc and the layer to be used mxd = arcpy.mapping.MapDocument("Current") mapLyr = arcpy.mapping.ListLayers(mxd, "Detail_2013")[0] #Get page number from data driven page - specified in the tool parameter dialogue box pageNum = arcpy.GetParameterAsText(0) ddp = mxd.dataDrivenPages arcpy.AddMessage(pageNum) pageID = mxd.dataDrivenPages.getPageIDFromName(pageNum) mxd.dataDrivenPages.currentPageID = pageID #Set layer definition query, this contols the rowcount variable pageFieldValue = pageNum mapLyr.definitionQuery = '"pageNum" = %s' % pageNum #Listing the text elements on the page concatElem1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat1")[0] concatElem2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat2")[0] #Finds the number of features in the map and sets up the lists to evenly distribute into the two text elements/columns rowcount = int(arcpy.GetCount_management("Detail_2013").getOutput(0)) percolumn = round(rowcount / 2.0) count1 = 1 count2 = rowcount #Specifies the features being used for the SearchCursor rows = arcpy.SearchCursor(mapLyr, "", "", "CONCAT") fieldrow = arcpy.SearchCursor(mapLyr, "", "", "pageNum") currentpage = "" text_var1 = str() text_var2 = str() #The first for and if block limits the searched rows to the definition query #The seconded/indented for and if block adds the text fields to the text elements for row in fieldrow: if currentpage != row.pageNum: currentpage = row.pageNum for row in rows: if count1 <= percolumn: text_var1 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep) concatElem1.text = text_var1 count1 += 1 elif count2 > percolumn: text_var2 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep) concatElem2.text = text_var2 count2 - 1 else: pass else: pass #Removed the definition query so all page numbers appear when the script is run next and refresh he layout view mapLyr.definitionQuery = "" arcpy.RefreshActiveView() del mxd, row, rows, rowcount, percolumn, count1, count2 And this part is what is in the validation field of the script tool: class ToolValidator: """Class for validating a tool's parameter values and controlling the behavior of the tool's dialog.""" def __init__(self): """Setup the Geoprocessor and the list of tool parameters.""" import arcpy self.params = arcpy.GetParameterInfo() def initializeParameters(self): """Refine the properties of a tool's parameters. This method is called when the tool is opened.""" import arcpy, os, sys #Reference Plat Index Layer to get unique Plat Numbers mxd = arcpy.mapping.MapDocument("CURRENT") mapLyr = arcpy.mapping.ListLayers(mxd, "Detail_2013")[0] #Iterate rows in Plat Index Layer to generate unique name list rows = arcpy.SearchCursor(mapLyr) row = rows.next() #Create and populate list uniqueList = [] while row: #If the value is not already in the list, append it if row.getValue("pageNum") not in uniqueList: uniqueList.append(row.getValue("pageNum")) row = rows.next() #Sort the list alphanumerically uniqueList.sort() self.params[0].filter.list = uniqueList self.params[0].value = uniqueList[0] return def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parmater has been changed.""" return def updateMessages(self): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return
... View more
08-20-2013
07:13 AM
|
0
|
0
|
2010
|
|
POST
|
Okay, so I had an idea of how to try this script and I have it working with a definition query. The only problem is that I can't get python to make the definition query for me. I've tried to get the page number from the ddp but it sends the map book back to page 1 with with a query of FIELDNAME = "" mxd = arcpy.mapping.MapDocument("current")
pageNum = arcpy.GetParameterAsText(0)
ddp = mxd.dataDrivenPages
arcpy.AddMessage(pageNum)
pageID = mxd.dataDrivenPages.getPageIDFromName(pageNum)
mxd.dataDrivenPages.currentPageID = pageID Would pageRow be a better way of getting the page number from the index layer?
... View more
08-20-2013
05:10 AM
|
0
|
0
|
648
|
|
POST
|
Then I guess this just got more complicated. So I'll explain my map book. It's a collection of intersections with crash data. Each page is a tile of an intersection and within each tile are the point features. What I'm trying to label in the layout is the description of each point. Originally I was expecting the definition queries of the point layer would control which points were being labelled. I didn't know what the python script would disregard that control. My basic script works great for a single page if all the features are in the page extent. As you can tell, I still have lot to learn about Python and I keep getting into more advanced programming of this script.I tried using the ESRI example from South Kingston, RI with their plat map book and it's really helped me learn a lot of getting a script set up. I think for now I will work with what I have to limit the amount of time I'm spending on this. Once I work through my "Python Scripting for ArcGIS" book I'll look at this again and hopefully have a better understanding of how to use more of the tools and functions of arcpy. I won't call this thread dead because this is a tool I would like to have working for future maps, but for now I'll have to work with what I got. Thank you for all your help, I've really learned a lot and feel like I've gotten a good start into Python with ArcGIS.
... View more
08-19-2013
09:47 AM
|
0
|
0
|
1052
|
|
POST
|
That has me thinking of two questions. Does the layer being searched have to be the index layer? I imagine that it doesn't. Also, is pageName only a number? I haven't seen anywhere that getPageIDFromName actually returns the string of the field attribute but returns a number based on the sort order of the DDP.
... View more
08-19-2013
07:49 AM
|
0
|
0
|
1052
|
|
POST
|
Wow, that is really useful information. Thanks mzcoyle! In the line is PageID supposed to be the field being searched and is Intersec supposed to be the value of that field in the row in a wildcard format?
... View more
08-19-2013
05:29 AM
|
0
|
0
|
1052
|
|
POST
|
I think that's beyond what I can figure our right now. I'm happy with what I've accomplished. I might use the split layer by attributes and create separate shapefiles for each map page, then the script can be run on each layer. I would just need to change the layer name each for each page, but it would be a workaround. Thanks for all your help, have a great weekend.
... View more
08-16-2013
11:26 AM
|
0
|
0
|
1052
|
|
POST
|
Yeah, I knew I was using DDP from the beginning, but I expected the definition and page query to limit the results to the queried features. I'll look at the link and see what I can do. Thanks for all the help so far.
... View more
08-16-2013
10:31 AM
|
0
|
0
|
1052
|
|
POST
|
I added this to get the page name:
pageNum = arcpy.GetParameterAsText(0)
ddp = mxd.dataDrivenPages
arcpy.AddMessage(pageNum)
pageID = mxd.dataDrivenPages.getPageIDFromName(str(pageNum))
mxd.dataDrivenPages.currentPageID = pageID and I changed the SearchCursor to this: rows = arcpy.SearchCursor(mapLyr.dataSource, "PageID = \'Intersec\'") but I get an error saying the fields in the search cursor do not exits. I'm trying to set the field value to the page name. The other thing that happens is the page name seems to only be stored as a page number and not the specified index layer field.
... View more
08-16-2013
09:44 AM
|
0
|
0
|
994
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 09-24-2014 08:42 AM | |
| 1 | 01-30-2015 09:21 AM | |
| 1 | 01-30-2015 09:25 AM |
| Online Status |
Offline
|
| Date Last Visited |
11-11-2020
02:24 AM
|