How to fetch current page # from Map Series for labeling map

176
2
2 weeks ago
Eliot_Reid
New Contributor II

We had label scripts in Desktop that called arcpy.mapping.MapDocument("CURRENT") and mxd.dataDrivenPages.pageRow.getValue() to populate labels in the map. I have not found any way to recreate this in Pro with Map Series. I don't want to use Dynamic Text in the layout because the location of the labels may change on every page. The labeling needs to be done in the map, but is dependent on what Map Series page is active. 

Is there a way to do this?

Below is a code snippet of one of our old label scripts from Desktop:

def FindLabel ( [PAGE_INDEX] ):
  result = None
  curIndex = [PAGE_INDEX]

  # current map page info
  mxd = arcpy.mapping.MapDocument("CURRENT")
  PageIndex = mxd.dataDrivenPages.pageRow.getValue('PAGE_INDEX')
  PageBook = long(float(PageIndex[:3]))
  PagePage = long(float(PageIndex[-2:]))

  curBook = long(float(curIndex[:3]))
  curPage = long(float(curIndex[-2:]))

  if curBook != PageBook:
    result = '<FNT scale="75">Bk.' + str(curBook) + '</FNT>\n' + str(curPage)
  else:
    if curPage != PagePage:
      result = '<FNT scale="120">' + str(curPage) + '</FNT>'
  return result
  
Tags (3)
0 Kudos
2 Replies
Brian_Wilson
Occasional Contributor III

What a weird idea, but whatever works for you.

This does not work for me but maybe it will get you further along. It validates but as soon as I try to set project (line 2) it stops working. No errors, just returns empty labels

import arcpy
project = arcpy.mp.ArcGISProject("CURRENT")
layout = project.listLayouts()[0] # Assuming you want the first layout
ms = layout.mapSeries

def FindLabel ( [PAGE_INDEX] ):
  result = ""
  curIndex = [PAGE_INDEX]
   
  # current map page info
  PageIndex = ms.pageRow.PageNumber

  # I don't know what you are doing from here down :-)

  PageBook = int(float(PageIndex[:3]))
  PagePage = int(float(PageIndex[-2:]))

  curBook = int(float(curIndex[:3]))
  curPage = int(float(curIndex[-2:]))

  if curBook != PageBook:
    result = '<FNT scale="75">Bk.' + str(curBook) + '</FNT>\n' + str(curPage)
  elif curPage != PagePage:
      result = '<FNT scale="120">' + str(curPage) + '</FNT>'
  return result

 

0 Kudos
Eliot_Reid
New Contributor II

Thanks Brian, I'm having the same problem trying to set a project and get a layout with python in Pro. For a bit more context, we are labeling adjacent assessor pages to the current assessor page - the size of the text in the label is dependent on whether the assessor page is in the same book or a different book.

I've tried running the .listLayouts() and layout.mapSeries code both within Pro and standalone in VS Code and it hasn't worked for me in either method. I get an 'OSError: CURRENT' from line 2.

Eliot_Reid_0-1714000639786.png

I guess my question is less about labels and more about if you can call "CURRENT" and use mapSeries from within Label Classes?

 

 

0 Kudos