3

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...

  • Ideas: is scale an integer field as scale returned is double? Does the field SCALE exist? Do you have a table join? Does the space between setValue and () cause a problem?, is sc definitely set? – Hornbydd Oct 31 '17 at 23:42
  • if I write print sc intead of the last row (with setValue) it gives me integer value, as it was meant... SCALE is integer and it really exists, but tried also double, no change, still the same error as I thought; no table join, deleting space between setValue and () did not solve that; what do you mean by "is sc definitely set"? – Abc1 Nov 1 '17 at 9:06
  • The fact you were able to print it out means it was set, just thought it might be returning a 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? – Hornbydd Nov 1 '17 at 9:46
  • edited - UPDATED with arcpy cursor example with setValue; the link "row" for datatype in <a href="desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…"> help </a> is referring to row in cursor; tried the code above in the Q box, did not work – Abc1 Nov 1 '17 at 10:48
  • With all your edits it is hard to tell what you are currently asking. I think you may be better to accept the answer (assuming that it was some help) and start a new question that only asks about where you are currently stuck. – PolyGeo Nov 18 '17 at 21:46
2

You may use the pageNum value to create a where clause expression within the for pageNum loop to filter the update cursor to only update the current page scale field attribute:

UpdateCursor (in_table, field_names, {where_clause})

where_clause

An optional expression that limits the records returned. For more information on WHERE clauses and SQL statements, see Building a query expression.

  • have not found a way how to make it work, more specific answer? – Abc1 Nov 1 '17 at 19:03
  • If you tried this method please update the question including code you used and provide any error info or where you are stuck. – artwork21 Nov 1 '17 at 19:07
  • edited the code + question (with where clause it finally helped but still not working the way it should) – Abc1 Nov 18 '17 at 12:11
2

So after a lot of trying and searching... finally I found a way that should work
(assumming that the fact this gives numbers not being the current scale but some numbers close to it, may be connected with bugs appeared recently and being in proccess of solving with ESRI Tech. support, knowing that before that appeared the numbers where ok geting sc value the same way)

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")
     #arcpy.mapping.ExportToJPEG(mxd, r"D:\PS" + os.sep + str(rad) + ".jpeg", resolution=400)
     sc=int(mxd.dataDrivenPages.dataFrame.scale)
     pork=arcpy.AddFieldDelimiters(mxd.dataDrivenPages.indexLayer, "KOO")
     with arcpy.da.UpdateCursor(mxd.dataDrivenPages.indexLayer,"SCALE","{0} = '{1}'".format(pork, rad)) as cursor:
         for row in cursor:
             row[0]=sc
             cursor.updateRow(row)
  • any way(syntax/formatting) of trying pageNum=strf could not help as those are equal when it gets to the with cursor line )

  • using fields it is helpful and recommended to use arcpy.AddFieldDelimiters (see arcpy.AddFieldDelimiters in ArcGIS help)

  • using this "{0} = {1}".format(x, y)) way of syntax writing appears to be useful and working, do not forget to bound the strings with '

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.