1

This question already has an answer here:

When creating a Layer in arcpy, the syntax uses a string to denote the layer name, as shown in the Make Feature Layer sample script:

import arcpy

arcpy.env.workspace = "C:\Program Files (x86)\ArcGIS\Desktop10.2\TemplateData\TemplateData.gdb"
arcpy.MakeFeatureLayer_management("city", "citiesLyr")

print(type("citiesLyr")) # => string

As far as Python is concerned, "citiesLyr" is just a string (right?). Can I instead define the feature layer as a variable?

This is an attempt to simplify a more complicated scenario. I'm trying to use the feature layer in a script which involves multiprocessing, loops and functions, and I'm finding that the reference to the layer is being lost, and all I'm left with is a string called "citiesLyr". How can I keep the reference to the feature layer throughout my script?

The example below doesn't do anything - the point is to test how to gain access to the feature layer from within the function, which has been called within the multiprocessing environment:

import multiprocessing, arcpy

def doCity(lyr):
    #How to get access to the cities layer from this function?
    print(lyr)
    desc = arcpy.Describe(lyr) # <= this fails because the layer is just a string
    print(desc.SpatialReference)

if __name__ == '__main__':

    #Create a feature layer from the cities
    arcpy.MakeFeatureLayer_management("C:\Program Files (x86)\ArcGIS\Desktop10.2\TemplateData\TemplateData.gdb\city", "citiesLyr")

    #Create a list, so we can use the multiprocessing function
    citiesList = []
    for i in range(0,5):
        citiesList.append("citiesLyr")

    # Create a pool class and run the jobs
    pool = multiprocessing.Pool()
    pool.map(doCity, citiesList)

    # Synchronize the main process with the job processes to ensure proper cleanup.
    pool.close()
    pool.join()

(The cities list itself is also pointless - it's only there to use multiprocessing)

marked as duplicate by PolyGeo Sep 18 '14 at 3:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

I think the blocking point you are currently encountering can be illustrated in this short bit of code:

import arcpy

def doCity(lyr):
    print(lyr.name)

if __name__ == '__main__':

    arcpy.MakeFeatureLayer_management("C:\Program Files (x86)\ArcGIS\Desktop10.2\TemplateData\TemplateData.gdb\city", "citiesLyr")
    citiesLyrObject = arcpy.mapping.Layer("citiesLyr")

    doCity(citiesLyrObject)

You will see that what I am passing into the doCity function is now a layer object rather than a layer name string.

I found how to do this in an answer to Can MakeFeatureLayer Object be Passed into ExportReport Function Layer Parameter?.

  • Thanks for the tip - I've implemented that change, which has thrown light on a different error. I've rewritten the question to focus on that instead. – Stephen Lead Sep 18 '14 at 2:46
  • @StephenLead Procedurally, I would be more inclined to rollback your last edit, and to ask about "Unpickleable" in a new question. I say this because the changed question invalidates this answer whereas I think it was highly relevant to the original: "I'm finding that the reference to the layer is being lost, and all I'm left with is a string called "citiesLyr". How can I keep the reference to the feature layer throughout my script?". – PolyGeo Sep 18 '14 at 2:55
  • The original could then be made a "signpost" duplicate of gis.stackexchange.com/questions/53453/… and be of increased rather than lost value to the site. – PolyGeo Sep 18 '14 at 2:57
  • 1
    New question on multiprocessing posted at gis.stackexchange.com/q/114260/3112 – Stephen Lead Sep 18 '14 at 5:08

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