I want to write a scale value that is changing for every Data Driven Page (since using map extent best-fit so that each polygon in the driving layer would be whole in "its" exported map) to the table of the driving layer, field "SCALE".
I found out I cannot use
mxd.dataDrivenPages.pageRow.setValue ()
(returning run time error: ERROR 999999)
because Data Driven Pages pageRow cannot be used to write a value in (but might be in the future) See pageRow - read only or can be modified?,
and so arcpy.da.UpdateCursor needs to be used instead (as arcpy.UpdateCursor is older version and way slower!).
THIS IS CURRENT CODE (you may have a look at what I tried below the two horizontal lines):
import arcpy, os
mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
rad= mxd.dataDrivenPages.pageRow.getValue("KOO") #KOO is NameField
#arcpy.mapping.ExportToJPEG(mxd, r"D:\PS" + os.sep + str(rad) + ".jpeg", resolution=400)
sc=int(mxd.dataDrivenPages.dataFrame.scale)
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE") as cursor:
for row in cursor:
row[0]=sc
cursor.updateRow(row)
This one fills all rows (field SCALE) with the scale number of the current Data Driven Page. But what I want is to have the right value in SCALE field for every row (with current scale)
--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
As suggested by artwork21 I tried to use pageNum in cursor's where-clause.
It did not worked
("por" in codes is a new field in the table that contains the number same as pageNum
- I would like to avoid adding fields in atr. table, but at least tried):
strf=mxd.dataDrivenPages.pageRow.getValue("por")
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE","""pageNum=strf""") as cursor:
for row in cursor:
row[0]=sc
cursor.updateRow(row)
-> This gave RuntimeError: A column was specified that does not exist. for row "for row in cursor:"
I tried to change the syntax in where-clause in "cursor line" but with no success
playing with str(pageNum), str(strf), """, " and ' using the code above
and also with writing where-clause outside the cursor and changing the syntax, examples:
strf=mxd.dataDrivenPages.pageRow.getValue("por")
whr= str(pageNum) + "=" + str(strf)
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE",whr) as cursor:
for row in cursor:
row[0]=sc
cursor.updateRow(row)
#RuntimeError: ERROR 999999: Error executing function.
whr= "str(pageNum) + "=" + str(strf)"
#Runtime error SyntaxError: can't assign to literal
also tried
strf=mxd.dataDrivenPages.pageRow.getValue("por")
with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE") as cursor:
for row in cursor:
if pageNum==strf:
row[0]=sc
cursor.updateRow(row)
or changing the 4th line to
if str(pageNum)==str(strf)
both ended up with: script ran, no cell in atr. table filled
I already knew those talking about query expressions/SQL in ArcGIS help: 1, 2, 3, but none of them talking about variables...
Trying to find a way searching in pages concerning variables specially with cursors at https://gis.stackexchange.com/ or https://community.esri.com/ meantioning different ways in answers, still none of them worked for me...
None
for what ever reason. Whilst looking at the help file is does actually explicitly state that pageRow is read only but then the explanation says it can be used to modify fields! Wonder if that is the issue?