1

I have a large number of lines in a shapefile representing sites on a map. Each has a unique 2- or 3-digit Id, but they are not consecutive (i.e. 6, 27, 53,... not 1,2,3,...). I want to zoom to each feature in turn and export the map as a PNG file.I want to save each one as the name of the Id.

I have tried the loop suggested by @Hornbydd but this creates many blank/useless maps for the numbers which are not allocated to sites (i.e. 1,2,3 etc are unneeded), which I had to go through and delete. Note that I cleared the selection before exporting as .png because I don't want the line to show up as highlighted blue.

mxd = arcpy.mapping.MapDocument("CURRENT")
for x in range(6,200):
   arcpy.SelectLayerByAttribute_management("sites","NEW_SELECTION",' "Id" = x ')
   df = arcpy.mapping.ListDataFrames(mxd)[0]
   df.zoomToSelectedFeatures()
   df.scale=5000
   arcpy.RefreshActiveView
   arcpy.SelectLayerByAttribute_management("sites","CLEAR_SELECTION")
   arcpy.mapping.ExportToPNG(mxd, r"X:\site maps\site_" + str(x), "PAGE_LAYOUT", 1280, 960, 160)

This worked for the sites I needed but took too long and saved too many useless files.

So then I tried Data Driven Pages:

mxd = arcpy.mapping.MapDocument("CURRENT")
for pagenum in range(1, mxd.dataDrivenPages.pageCount + 1):
...    mxd.dataDrivenPages.currentPageID = pagenum
...    ID = <-- what goes here?
...    print "Exporting site {0}, page {1} of {2}".format(str(ID), str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
...    arcpy.mapping.ExportToPNG(mxd, r"U:\Mapping\site_" + str(ID), "PAGE_LAYOUT", 1280, 960, 160)

I can't work out what to allocate to ID to get it to be the value in the Id field. I have allocated the Id field as the Name field, but I haven't figured out how to get those values out.

Using

...    ID = mxd.dataDrivenPages.pageNameField.name

sets ID to "Id"

2

Taking your code you need to wrap it up in a loop. The code below assumes you have an ID starting from 1 going to 100.

mxd = arcpy.mapping.MapDocument("CURRENT")
for x in range(1,101):
   arcpy.SelectLayerByAttribute_management("sites","NEW_SELECTION",' "Id = ' + str(x))
   df = arcpy.mapping.ListDataFrames(mxd)[0]
   df.zoomToSelectedFeatures()
   df.scale=5000
   arcpy.RefreshActiveView
   arcpy.SelectLayerByAttributeManagement("sites","CLEAR_SELECTION")
   arcpy.mapping.ExportToPNG(mxd, r"X:\site maps\img_" + str(x), df, 1280, 960, 160)

As a side note, avoid starting file names with numbers.

  • Thanks for this! It has worked for the files within the range. The problem is I don't have all the numbers, consecutively. The first Id is 6, then 27, then 53, so I have ended up with a load of arbitrary files I don't need. Is there a faster way of doing it that creates a loop only with the Ids I have? – holnic Jul 27 '16 at 9:47
  • You could use the get count tool to return the number of selected, if zero then skip – Hornbydd Jul 29 '16 at 18:27

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.