0

I'm trying to run a very simple python script to create a feature layer from a shapefile.

The original script read as follows (I added the two print statements at the end to verify upon completion that:

1) the output I was trying to create was created (Exists(the layer I was trying to create)) and

2) that the script was recognizing the path name by asking if a known folder existed

# Import system modules
import arcpy
from arcpy import env

# Set overwrite option
arcpy.env.overwriteOutput = True


# Set workspace
# arcpy.env.workspace = r"H:\Python\Python_Cookbook\data\TravisCounty"



# Make feature layer to hold selection.
Bldgs = r"H:\Python\Python_Cookbook\data\TravisCounty\BuildingPermits.shp"
Bldg_Lyr = r"H:\Python\Python_Cookbook\data\TravisCounty\Building.lyr"
arcpy.MakeFeatureLayer_management(Bldgs, Bldg_Lyr)


print arcpy.Exists(
    r"H:\Python\Python_Cookbook\data\TravisCounty\Building.lyr")

print arcpy.Exists(
    r"H:\Python\Python_Cookbook\data\TravisCounty")

The result from this first script was that no layer appeared in Catalog, and the print statements returned False and True. So the script recognized the existing folder, but did not find the layer it was supposed to create.

My second attempt was to comment out the workspace and to assign variables to the full paths instead. (see below).

# Import system modules
import arcpy
from arcpy import env

# Set overwrite option
arcpy.env.overwriteOutput = True


# Set workspace
# arcpy.env.workspace = r"H:\Python\Python_Cookbook\data\TravisCounty"



# Make feature layer to hold selection.
Bldgs =     r"H:\Python\Python_Cookbook\data\TravisCounty\BuildingPermits.shp"
Bldg_Lyr = r"H:\Python\Python_Cookbook\data\TravisCounty\Building.lyr"
arcpy.MakeFeatureLayer_management(Bldgs, Bldg_Lyr)


print arcpy.Exists(
r"H:\Python\Python_Cookbook\data\TravisCounty\Building.lyr")

print arcpy.Exists(
r"H:\Python\Python_Cookbook\data\TravisCounty")

This seemed to work better: My print statements returned True and True this time around.

HOWEVER: When I opened the ArcCatalog, I still couldn't find the new layer that was supposed to have been created. Copying the full path (that just returned 'True' to the exists() call) into the Location bar of ArcCatalog returned a message that it was "...and invalid or non-existent directory."

ps. Is there a way to apply the codewrapper without manually entering four spaces before each line of code?

3
  • A word of advice on posting your question. Whenever you're using code samples, please use the code sample wrapper so it shows up formatted. It makes it a lot easier on others to read and understand what you're asking. Read the How To Format section on the right side of the Ask a Question page. You can also use the buttons on the top of the input form as a shortcut to format your question properly.
    – Branco
    Commented Jul 22, 2015 at 13:22
  • Yeah, apologies for the messiness; I did try to figure that out before posting, to no avail. I couldn't figure out how to use the buttons on the input form -- highlighting my pasted text(script) and clicking the "code" button wasn't working. What am I doing wrong?
    – Raquel
    Commented Jul 22, 2015 at 13:46
  • Worse comes to worst, you can also format it using the symbols/spacing that the guidelines have if for some reason the buttons won't work for you (might be a good question for the Meta site). I only mention it so your questions get more attention, and they don't get down-voted.
    – Branco
    Commented Jul 22, 2015 at 13:53

2 Answers 2

1

If you really need your layer physically (e.g., saved on your disk), you should follow @Tangnar's advice, but if you want to play with the layer (basically what they are for), there is no such need (since you are testing layer behaviour by a script). Professionally I do not use (never, ever) MakeFeatureLayer_management to create layer in Python scripts but mapping sub-package's Layer method which returns a layer OBJECT. The former generates a layer at the run-time and it names it with your specification (in this case r"H:\Python\Python_Cookbook\data\TravisCounty\Building.lyr", which is a simple STRING object referring to your run-time layer). More clearly, if you change your Bldg_Lyr to "how_are_you Travis" (i.e., Bldg_Lyr = "how_are_you Travis") and use MakeFeatureLayer_management and use arcpy.Exists("how_are_you Travis"), it will return True again. arcpy.Exists looks for not only file locations but also memory items. In this case "how_are_you Travis" exists as a String object as well as reference to a layer object. Confused? I am not surprised. If you are planning to create layers, try this:

Bldgs =     r"H:\Python\Python_Cookbook\data\TravisCounty\BuildingPermits.shp"
my_lyr=arcpy.mapping.Layer(Bldgs)

Now you have a layer OBJECT and simply use my_lyr.save(r"H:\Python\Python_Cookbook\data\TravisCounty\Building.lyr") to have it physically.

1
  • Hm. At the end of the day, I'm trying to automate adding features (in this case, shapefiles) to an existing map. I would prefer to just automate adding a shapefile, but since I can only find arcpy.mapping functions that add layers, I've been trying to use arcpy to convert my shapefiles to layers, save the layers, and then add the layers. Kind of annoying, and feels like extra work, but this may just be my newbie-ness adding a layer of misunderstanding to the process. I appreciate your explanation above, I'm going to play around with the examples you gave.
    – Raquel
    Commented Jul 22, 2015 at 16:11
0

Feature layers are temporary... From Arc Help "The layer that is created by the tool is temporary and will not persist after the session ends unless the layer is saved to disk or the map document is saved."

If you are trying to save it, you need to save it as a layer file, or save it as a feature class.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000

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.