Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am having issues with my scrip, because it makes my computer very slow after 15 loops arcgis 10.1 and explorer.exe crashes. But instead of this, i can see my 15 pdf files saved correctly and i need to move the cursor to 16 for begin the loop.

I am using ddp, dynamic tables (with script), GDB, everything is loaded in memory and then i delete memory every time the cicle begins.

Here is my Scrip, can anyone tell me what i am doing wrong?.

import arcpy, os, sys

    relpath = os.path.dirname(sys.argv[0])

#Create final output PDF
pdfPath = relpath + "\FinalOutput.pdf"
if os.path.exists(pdfPath):
    os.remove(pdfPath)
finalResult = arcpy.mapping.PDFDocumentCreate(relpath + "\FinalOutput.pdf")

#Reference MXD and layers
mxd = arcpy.mapping.MapDocument(relpath + r"\MAPABASE.mxd")
LiderLayer = arcpy.mapping.ListLayers(mxd, "LS2")[0]
queryLayer = arcpy.mapping.ListLayers(mxd, r"SUBSECC_VIC")[0]
BFLayer = arcpy.mapping.ListLayers(mxd, "BF1")[0]
BFquery = arcpy.mapping.ListLayers(mxd, r"BF1")[0]

#Reference page layout elements
for elm in arcpy.mapping.ListLayoutElements(mxd):
  if elm.name == "bar1":  bar1 = elm
  if elm.name == "bar2":  bar2 = elm
  if elm.name == "bar1txt":  bar1txt = elm
  if elm.name == "bar2txt":  bar2txt = elm
  if elm.name == "NoGrowth": noGrowth = elm
  if elm.name == "horzLine": horzLine = elm
  if elm.name == "vertLine": vertLine = elm
  if elm.name == "cellTxt":  cellTxt = elm
  if elm.name == "headerTxt": headerTxt = elm
  if elm.name == "bar1b":  bar1b = elm
  if elm.name == "bar2b":  bar2b = elm
  if elm.name == "bar1txtb":  bar1txtb = elm
  if elm.name == "bar2txtb":  bar2txtb = elm
  if elm.name == "NoGrowthb": noGrowthb = elm
  if elm.name == "horzLineb": horzLineb = elm
  if elm.name == "vertLineb": vertLineb = elm
  if elm.name == "cellTxtb":  cellTxtb = elm
  if elm.name == "headerTxtb": headerTxtb = elm

#Reference DDP object
ddp = mxd.dataDrivenPages
#Loop through each DDP page
for pageNum in range(44, mxd.dataDrivenPages.pageCount+1):
  mxd.dataDrivenPages.currentPageID = pageNum

  #Graphic table variable values for LS and BF(b)
  tableHeight = 3.0
  tableWidth = 4.32
  headerHeight = 0.16
  rowHeight = 0.10
  upperX = 11.18
  upperY = 7.60

  tablebHeight = 3.0
  tablebWidth = 4.32
  headerbHeight = 0.16
  rowbHeight = 0.10
  upperbX = 11.18
  upperbY = 6.63

  #Build selection set
  seccnum = ddp.pageRow.seccioncom
  arcpy.AddMessage("Processing: " + seccnum)

  arcpy.SelectLayerByAttribute_management(LiderLayer, "NEW_SELECTION",  
                                        "\"seccioncom\" = " + "'" + seccnum + "'")


  #selection
  arcpy.Select_analysis(LiderLayer, "in_memory\select", "\"seccioncom\" = " + "'" + seccnum + "'")
  numRecords = int(arcpy.GetCount_management(LiderLayer).getOutput(0))
  arcpy.Select_analysis(BFLayer, "in_memory\select2", "\"seccioncom\" = " + "'" + seccnum + "'")
  numRecordsb = int(arcpy.GetCount_management("in_memory\select2").getOutput(0))

  #Show selected features


#Add note if there are no Lider .................................................................
  if numRecords == 0:
    noGrowth.elementPositionX = 11.20
    noGrowth.elementPositionY = 7.50
  else:

    #if number of rows exceeds page space, resize row height
    if ((tableHeight - headerHeight) / numRecords) < rowHeight:
      headerHeight = headerHeight * ((tableHeight - headerHeight) / numRecords) / rowHeight
      rowHeight = (tableHeight - headerHeight) / numRecords

    #Set and clone vertical line work
    vertLine.elementHeight = (headerHeight) + (rowHeight * numRecords)
    vertLine.elementPositionX = upperX
    vertLine.elementPositionY = upperY

    temp_vert = vertLine.clone("_clone")
    temp_vert.elementPositionX = upperX + 2.00
    temp_vert = vertLine.clone("_clone")
    temp_vert.elementPositionX = upperX + 4.32

    #Set and clone horizontal line work
    horzLine.elementWidth = tableWidth
    horzLine.elementPositionX = upperX
    horzLine.elementPositionY = upperY

    horzLine.clone("_clone")
    horzLine.elementPositionY = upperY - (headerHeight - 0.01)

    y=upperY - headerHeight
    for horz in range(1, numRecords+1):
      y = y - rowHeight
      temp_horz = horzLine.clone("_clone")
      temp_horz.elementPositionY = y

    #Set header text elements
    headerTxt.fontSize = headerHeight / 0.0200
    headerTxt.text = "NAME SUPERVISOR"
    headerTxt.elementPositionX = upperX + 0.80
    headerTxt.elementPositionY = upperY - (headerHeight / 2)

    newFieldTxt = headerTxt.clone("_clone")
    newFieldTxt.text = "ADDRESS"
    newFieldTxt.elementPositionX = upperX + 2.70

    #Set and clone cell text elements
    cellTxt.fontSize = rowHeight / 0.0200

    y = upperY - headerHeight 
    rows = arcpy.SearchCursor("in_memory\select")
    for row in rows:
      x = upperX + 0.02
      col1CellTxt = cellTxt.clone("_clone")
      col1CellTxt.text = row.getValue("NOMBRE_LS")
      col1CellTxt.elementPositionX = x
      col1CellTxt.elementPositionY = y
      col2CellTxt = cellTxt.clone("_clone")
      col2CellTxt.text = row.getValue("DOMICILIO")
      col2CellTxt.elementPositionX = x + 2.10
      col2CellTxt.elementPositionY = y
      y = y - rowHeight
#End of Lider Loop.................................................................................
#Add note if there are no Beneficiarios............................................................
  if numRecordsb == 0:
    noGrowthb.elementPositionX = 11.20
    noGrowthb.elementPositionY = 6.30
  else:

    #if number of rows exceeds page space, resize row height
    #if ((tablebHeight - headerbHeight) / numRecordsb) < rowbHeight:
    #  headerbHeight = headerbHeight * ((tablebHeight - headerbHeight) / numRecordsb) / rowbHeight
    #  rowbHeight = (tablebHeight - headerbHeight) / numRecordsb

    #Set and clone vertical line work
    vertLineb.elementHeight = (headerbHeight) + (rowbHeight * numRecordsb)
    vertLineb.elementPositionX = upperbX
    vertLineb.elementPositionY = upperbY

    temp_vertb = vertLineb.clone("_clone")
    temp_vertb.elementPositionX = upperbX + 2
    temp_vertb = vertLineb.clone("_clone")
    temp_vertb.elementPositionX = upperbX + 4.32

    #Set and clone horizontal line work
    horzLineb.elementWidth = tablebWidth
    horzLineb.elementPositionX = upperbX
    horzLineb.elementPositionY = upperbY

    horzLineb.clone("_clone")
    horzLineb.elementPositionY = upperbY - (headerbHeight - 0.01)

    yb=upperbY - headerbHeight
    for horzb in range(1, numRecordsb+1):
      yb = yb - rowbHeight
      temp_horzb = horzLineb.clone("_clone")
      temp_horzb.elementPositionY = yb

    #Set header text elements
    headerTxtb.fontSize = headerbHeight / 0.0200
    headerTxtb.text = "NAME OF BENEFIT"
    headerTxtb.elementPositionX = upperbX + 0.80
    headerTxtb.elementPositionY = upperbY - (headerbHeight / 2)

    newFieldTxtb = headerTxtb.clone("_clone")
    newFieldTxtb.text = "ADDRESS"
    newFieldTxtb.elementPositionX = upperbX + 2.70

    #Set and clone cell text elements
    cellTxtb.fontSize = rowbHeight / 0.0200

    yb = upperbY - headerbHeight
    rowsb = arcpy.SearchCursor("in_memory\select2")
    for row in rowsb:
      xb = upperbX + 0.02
      col1CellTxtb = cellTxtb.clone("_clone")
      col1CellTxtb.text = row.getValue("NOMBRE_BF")
      col1CellTxtb.elementPositionX = xb
      col1CellTxtb.elementPositionY = yb
      col2CellTxtb = cellTxtb.clone("_clone")
      col2CellTxtb.text = row.getValue("DOMICILIO")
      col2CellTxtb.elementPositionX = xb + 2.10
      col2CellTxtb.elementPositionY = yb
      yb = yb - rowbHeight

  #Export to PDF Append Disabled
  #ddp.exportToPDF(relpath + r"\temp.pdf", "CURRENT")
  #finalResult.appendPages(relpath + r"\temp.pdf")
  #os.remove(relpath + r"\temp.pdf")
  #Export to Single PDF Document Each
  arcpy.mapping.ExportToPDF(mxd, relpath + "\\" + seccnum + ".pdf") 

  #Clean-up before next page
  for elm in arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "*clone*"):
    elm.delete()
  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*clone*"):
    elm.delete()
  noGrowth.elementPositionX = -6
  cellTxt.elementPositionX = -6
  headerTxt.elementPositionX = -6
  horzLine.elementPositionX = -6
  vertLine.elementPositionX = -6
  noGrowthb.elementPositionX = -6
  cellTxtb.elementPositionX = -6
  headerTxtb.elementPositionX = -6
  horzLineb.elementPositionX = -6
  vertLineb.elementPositionX = -6
  del row
  del rows
  del rowsb
  del horz
  del horzb
  arcpy.SelectLayerByAttribute_management(LiderLayer, "CLEAR_SELECTION")
  arcpy.Delete_management("in_memory\select")
  arcpy.Delete_management("in_memory\select2")

finalResult.updateDocProperties("MAPABASE", "AREAS", "Esri", "map sheets" , "map book", "USE_THUMBS")
finalResult.saveAndClose()
share|improve this question

1 Answer 1

My gut feeling for anything that relates to Arc which works fine initially and then slows down and crashes is that it is one of the well known memory leaks that ESRI claim on a regular basis to have fixed.

The standard work-around for this is to put all the main part of your process into a subprocess and call the subprocess with subprocess.call as opposed to subprocess.popen to avoid over-running your memory. This works because the memory is released properly as each subprocess closes. If you want speed have a look at my reply to a similar question here.

share|improve this answer
    
Hi, Thank you for your time and Reply, I had ran the script with IDLE and the same thing happens, I Have an Intel Core i5 With 4 GB DDR3 and 500 GB HDD complety of free space, The thing here is why the first time I ran the script it finish 38 loops and now only 15 .. I'm not a master in Python, Do you Recommend me install an Service Pack for Arcgis Desktop?. –  user15574 Feb 28 '13 at 17:38
    
Did you separate the main body of the script (the bit you do each loop) as a separate module and then call that as a subprocess from inside a loop? If you just run the entire script from Idle, you should expect the same problem. As for the service pack, I'd always install it if there is one available from ESRI. –  MappaGnosis Mar 1 '13 at 7:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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