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'm trying to write a script that uses a search cursor to loop through a field in a .shp file (Incident_Z) find every value that meets a specific condition (zip code=10004) and then at a scale of 1000, the layout at that scale to a pdf.

With advice from various folks I've tried two approaches:

  1. Using a search cursor. However I've run into problems with it: The problem is that there is no output, and interestingly, no errors. The script runs, and when I include a print "Done" statement at the end, prints correctly.

    import arcpy

    mxd = arcpy.mapping.MapDocument("C:\temp\Graffiti_DM1.mxd") df = arcpy.mapping.ListDataFrames(mxd, "*")[0] expdir= "C:\temp"

    cursor=arcpy.SearchCursor("C:\temp\itscomplicated.shp",'"Incident_Z" = ' + "'10004'", "", "", "")

    for cur in cursor: cur.rotation = 0 cur.scale = 1000 outFile = expdir + "\" + cur.name + ".tif" cur.panToExtent(lyr.getSelectedExtent()) arcpy.mapping.ExportToTIFF(mxd, outFile, df)

    del cursor

  2. Additionally, I've tried as per @PolyGeo's suggestion, the definition query method, below, which runs and produces an output, but not exactly what.I'm looking for.Definition Query code. The problem is that the output is a pdf (I'm actually preferential to pdf than tiff) of the current extent of the layout, and only the current layout. What I'm looking to do is to have a page for each instance of the where_clause, i.e. each record where '"Incident_Z" = ' + "'10033'", at a specified scale of 1000. Is it possible to modify the existing definition_query script to achieve that? As always, any advice is greatly apprecaited.

share|improve this question

closed as not a real question by PolyGeo, Fezter, R.K., Devdatta Tengshe, Chad Cooper May 1 '13 at 13:38

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
This should all really be merged with your previous question. You were asking why you had empty output, and that problem has obviously not been solved. No need to open an identical question. –  Get Spatial Apr 21 '13 at 3:23
    
possible duplicate of Arcpy: SQL error in select_analysis produces empty feature –  Get Spatial Apr 21 '13 at 3:25
    
Although worded similarly I think this actually a distinct issue. The advice on how to resolve the original question has been incorporated into this one. I would recommend rewording this Question to try and hone in more on the non-working part(s). I think the choice to use an SQL expression on a Search Cursor rather than a Definition Query overly complicates the problem. –  PolyGeo Apr 21 '13 at 4:17
    
@PolyGeo - This does not take away from the fact that one issue was solved in the previous question. The point, like you alluded to is that the new information he put here should have been out in the other question instead. It would mean that was not solved, which takes away from you. Unfortunately, even though you solved one part, the overall problem of an empty result still remains and should be one question. –  Get Spatial Apr 21 '13 at 5:16
    
@PolyGeo - Let me add, not solved until your answer here, which may well do the trick. –  Get Spatial Apr 21 '13 at 5:18

2 Answers 2

Rather than use a Search Cursor to find a particular feature using its field value I think a much better way is to set a Definition Query on the layer. The block of code below, which is from a presentation I did at Esri APUC in Auckland last November, should give you the essence of this alternative approach. You will just need to export to TIFF instead of PDF. Also, my example uses a File Geodatabase Feature Class rather than a shapefile so the SQL syntax is slightly different.

import arcpy

inputMap = r"C:\Conferences\ESRI APUC\APUC 2012\GenericMap.mxd"
inputLotNo = "70"
inputPlanNo = "WV733"
outputPDF = r"C:\Conferences\ESRI APUC\APUC 2012\GenericMap.pdf"

mxd = arcpy.mapping.MapDocument(inputMap)
df = arcpy.mapping.ListDataFrames(mxd,"Main Map")[0]
subjectLotLayer = arcpy.mapping.ListLayers(mxd,"Subject Lot",df)[0]

where_clause = "LOT = '" + inputLotNo + "' AND PLAN = '" + inputPlanNo + "'"
subjectLotLayer.definitionQuery = where_clause
df.extent = subjectLotLayer.getSelectedExtent(False)

if arcpy.Exists(outputPDF):
    arcpy.Delete_management(outputPDF)
arcpy.mapping.ExportToPDF(mxd,outputPDF)
share|improve this answer
1  
What if there were an unknown number of lot70 and planWV733's? Would it be best to set up data driven pages? As to the posted question: would this give David the one page at 1:1000 of each incidentZ = 10033? –  gm70560 Apr 21 '13 at 14:24
1  
Wrapping the code from the line setting the where_clause variable into a Search Cursor (or using DDP) would be the way to do that but he asked for any advice to try to meet his requirement so I have not kept strictly to the Question because I think this is a better technique. –  PolyGeo Apr 21 '13 at 21:35
    
@PolyGeo, updated the original question above, as this is a key part of the problem. –  David Meek Apr 21 '13 at 21:53
1  
My recommendation is that if a question has two parts then it is usually two questions and should be split rather than appended to. To keep the Q&A format "clean" I think the Title should always be a succinct question so that what is being asked is immediately obvious. The body of the question serves to provide background and more details about the original question. –  PolyGeo Apr 21 '13 at 22:12
    
@PolyGeo, The reason I asked was that I felt that was where the poster was heading. I agree in regards to the two questions, but it would be better to have a clear and precise question on the front end. –  gm70560 Apr 21 '13 at 22:40

I think the problem is in this line:

cur.panToExtent(lyr.getSelectedExtent())

You have no reference in your code to the lyr variable. After you find your value in your cursor you then push that value into a select by attribute method, then pan to the selected feature.

share|improve this answer

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