1

I am using ArcGIS 10.1 Standard with Python 2.7. I am trying to loop through records in a feature class using a Search cursor and for each record, run a custom ArcGIS report. I have created the report layout file (rlf) and can make this work outside a loop. The problem I have run into when I try to run within the loop is that the Search Cursor works with feature classes while the ExportReport function works with layers. I've tried using the MakeFeatureLayer function to make the conversion from feature class to feature layer within the loop, but seem to be missing something in the arguments for MakeFeatureLayer. This is the code I have used successfully to create the one-off report.

import arcpy
mxd = arcpy.mapping.MapDocument(r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Figures\MXD\AGOL\CreateReports.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
fccrossings = arcpy.mapping.ListLayers(mxd, "", df)[0]
arcpy.mapping.ExportReport(fccrossings,
                       r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Reports\ArcGIS_Report_Templates\Report_SCRField_v6.rlf",
                       r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Reports\SCR\Test\Test.pdf", "SELECTED")

del mxd

And this is what I have come to using the loop.

import arcpy

fccrossings = r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Download_AGOL\NF\SCR_04Nov2015_ReducedPhotos\366c11ade3cb4b95a3772296a1b0d8f1.gdb\StreamCrossingNF"

with arcpy.da.SearchCursor(fccrossings, ["OBJECTID"], '"OBJECTID" = 1337') as cursor:
    for row in cursor:
        report = arcpy.MakeFeatureLayer_management(row, "templayer")
        arcpy.mapping.ExportReport(report,
                    r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Reports\ArcGIS_Report_Templates\Report_SCRField_v6.rlf",
                    r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Reports\SCR\Test\Test.pdf")

del cursor

This is the error message I receive...

Traceback (most recent call last): File "G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\Python Scripts\CreateSCR_v2", line 7, in report = arcpy.MakeFeatureLayer_management(row, "templayer") File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 5748, in MakeFeatureLayer raise e RuntimeError: Object: Error in executing tool

3
  • Welcome to GIS SE. As a new user, please take the Tour. Please edit the question to specify the version of ArcGIS in use. If you receive any error messages, please be sure to include them as well.
    – Vince
    Commented Nov 9, 2015 at 16:38
  • 1
    The "row" you're passing into MakeFeatureLayer ins't a viable input. Its a tuple. Remove the cursor and add the SELECT clause into MFL itself.
    – KHibma
    Commented Nov 9, 2015 at 18:22
  • Thanks for the comment. I actually need to loop through all records using a SELECT function. I showed just a single record here ("OBJECTID" = 1337) as I wanted to focus on getting the MFL and ExportReport functions working first. To loop through all records meeting a query I will need to use a Search Cursor, won't I? Commented Nov 10, 2015 at 19:09

1 Answer 1

1

Try this:

import arcpy

fccrossings = r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Download_AGOL\NF\SCR_04Nov2015_ReducedPhotos\366c11ade3cb4b95a3772296a1b0d8f1.gdb\StreamCrossingNF"

with arcpy.da.SearchCursor(fccrossings, ["OBJECTID"]) as cursor:
    for row in cursor:
        oid = row[0]
        whereClause = 'OBJECTID = {0}'.format(oid)
        fLyr = 'tempLayer'
        report = arcpy.MakeFeatureLayer_management(fccrossings, fLyr, whereClause)
        arcpy.mapping.ExportReport(report,
                    r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Reports\ArcGIS_Report_Templates\Report_SCRField_v6.rlf",
                    r"G:\505573\MGT\40-ENG\4E-ENV\GIS\Misc\StreamCrossingReports\Reports\SCR\Test\{0}.pdf".format(oid))

        del fLyr

For one, I use a where clause in the creation of the feature layer. In this case, that takes place within the loop so that there is a feature layer created for each individual feature.

Note that row[0] gets the object ID. row would be (1337, ); you need to extract the item from the tuple.

As written, the output pdfs will be named according to the features' OIDs.

Note that when you use a with statement for a cursor, you don't need to explicitly delete the cursor.

I hope this helps.

2
  • Thanks, Tom. I tried that but it didn't work. A couple of questions: 1. For the statement fLyr = 'tempLayer' - I don't see 'tempLayer' defined anywhere previous in the code. 2. I didn't think a row object could be used as an argument in the MakeFeatureLayer statement - isn't a FeatureLayer object required here? Commented Nov 10, 2015 at 19:00
  • @KarlKeough, what was the error message? Also, 'tempLayer' is just a string; fLyr is being defined to refer to it. That string will then serve as the name for your in-memory layer when you run the MakeFeatureLayer function. Oh, sorry I overlooked the row in the MakeFeatureLayer. I'll fix it above.
    – Tom
    Commented Nov 10, 2015 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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