0

Below is the code I wrote in ArcPy in ArcGIS Pro. The layer from "arcpy.MakeFeatureLayer_management" is created (name changes in each iteration as "Layer_%s" %(FIPS)). But "arcpy.SaveToLayerFile_management" is not accepting that created layer and giving the error-000732. Ultimately I want to apply symbology from one layer to many layers generated from "SaveToLayerFile". Is there a work around it?

import arcpy

arcpy.env.addOutputsToMap = 0
arcpy.env.overwriteOutput = True


arcpy.env.workspace = "C:/Documents/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties"
PM_Shapefiles = arcpy.ListFeatureClasses()

in_symbology_layer = "C:/Documents/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties/Source_Symbology_Layer/Size2000_MaxTotalPM25_Layer.lyrx"

for shp in PM_Shapefiles: 
    FIPS = shp.replace("PM25Monitors_JoinedInMAPPM_2000Size_", "")
    FIPS = FIPS.replace(".shp","")
    FIPS = int(FIPS)
    print ("FIPS=%s" %(FIPS))

    arcpy.Delete_management("Layer_%s" %(FIPS))
    where_clause = '"TotalPM25">0.0001'
    arcpy.MakeFeatureLayer_management("C:/Users/mthind/Documents/EMRE_Project/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties/%s" %(shp), "Layer_%s" %(FIPS), where_clause)
    arcpy.SaveToLayerFile_management("Layer_%s" %(FIPS), "Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS))

    symbologyFields = [["VALUE_FIELD", "PM_MTInMAP", "PM_MTInMAP"], 
                   ["COLOR_EXPRESSION_FIELD", "TotalPM25", "TotalPM25"]]

    arcpy.ApplySymbologyFromLayer_management("Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS), in_symbology_layer, symbologyFields)

enter image description here

Edit code to response @ 8:31 ET

import arcpy
import os

arcpy.env.addOutputsToMap = 0
arcpy.env.overwriteOutput = True


arcpy.env.workspace = "C:/Users/mthind/Documents/EMRE_Project/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties"
PM_Shapefiles = arcpy.ListFeatureClasses()

in_symbology_layer = "C:/Users/mthind/Documents/EMRE_Project/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties/Source_Symbology_Layer/Size2000_MaxTotalPM25_Layer.lyrx"

#workspace = "C:/Users/mthind/Documents/EMRE Project/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties/OK"

for shp in PM_Shapefiles: 
    FIPS = shp.replace("PM25Monitors_JoinedInMAPPM_2000Size_", "")
    FIPS = FIPS.replace(".shp","")
    FIPS = int(FIPS)
    print ("FIPS=%s" %(FIPS))

    arcpy.Delete_management("Layer_%s" %(FIPS))
    where_clause = '"TotalPM25">0.0001'
    arcpy.MakeFeatureLayer_management("C:/Users/mthind/Documents/EMRE_Project/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties/%s" %(shp), "Layer_%s" %(FIPS), where_clause)

    if arcpy.Exists("Layer_%s" %(FIPS)):
        print("layer exists")
    else:
        print("Does not exist")

    if os.path.exists("Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS)):
        arcpy.Delete_management("Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS))

    arcpy.SaveToLayerFile_management("Layer_%s" %(FIPS), "Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS))

    symbologyFields = [["VALUE_FIELD", "PM_MTInMAP", "PM_MTInMAP"], 
                   ["COLOR_EXPRESSION_FIELD", "TotalPM25", "TotalPM25"]]

    arcpy.ApplySymbologyFromLayer_management("Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS), in_symbology_layer, symbologyFields)

enter image description here

  • 1
    Try print("C:/Users/mthind/Documents/EMRE_Project/Data/ArcGIS_WorkFiles/Metric_1/Monitors_Joined_With_PM_2000Size_AllCounties/%s" %(shp)), does it look like a valid path to the shapefile? – BERA Dec 30 '18 at 8:18
  • Yes, used the "print" command you suggested and it gives a valid path. But it gives same error as before. – Mony Dec 30 '18 at 22:56
1

Your problem is identified in the 1st parameter of SaveToLayerFile as ERROR 000732, meaning the input layer (Layer_17011) "does not exist or is not supported".

Not sure, but think the key is in that SaveToLayerFile expects a Layer data type - MakeFeatureLayer outputs a Feature Layer data type. According to the documentation, you can use getOutput(0) to return the result object (or you may be able to simply use [0] as I've seen in practice). See http://pro.arcgis.com/en/pro-app/arcpy/classes/result.htm, and under the method overview is this quote:

If the output of the tool, such as MakeFeatureLayer is a layer, getOutput will return a Layer object.

Provided your MakeFeatureLayer provides valid output, then you should be able to do something roughly like this:

layer_to_save = arcpy.MakeFeatureLayer_management(<my shapefile>,
      "Layer_%s" % (FIPS), where_clause).getOutput(0)

arcpy.SaveToLayerFile_management(layer_to_save, <my lyrx filename>)

Otherwise, advise you do as BERA says in his above comment and test your string paths to make sure they're valid.

EDIT (7:45 PM Eastern):

According to your code, this is the problem line, the one that throws the error:

arcpy.SaveToLayerFile_management("Layer_%s" %(FIPS), "Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS))

There are 2 parameters you need to check - one is the input feature layer, "Layer_%s" % (FIPS), and the output layer file,"Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS).

I now suspect the problem could be your lyrx file - you could check if it exists, delete it if it does, then run the save to layer process, as so:

if os.path.exists("Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS)):
    arcpy.Delete_management("Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS))
# then you should be free to write the file (if not locked)
arcpy.SaveToLayerFile_management("Layer_%s" %(FIPS), "Layer_Files_2000Size/Symbology_%s.lyrx" %(FIPS))

...of course, if that does not work, by process of elimination you should check that your layer obj you're trying to write-to-file exists and is valid....you can use something like:

if arcpy.Exists("Layer_%s" %(FIPS)):
    print("layer exists")
    # carry on with processing...

Not to forget, you need to add an import for os at the beginning of your script...you can do that alongside your import arcpy statement:

import arcpy
import os
  • I tried your suggestion but it gives same error "ERROR 000732: Input Layer: Dataset Layer_17011 does not exist or is not supported". One thing I wanted to mention: The script ran successfully one time before (initially before I posted my question on StackExchange), but when I ran again, it never successfully ran. Is there something in the settings to change or any parameter in the environment to change? The generated layer from "MakeFeatureLayer_management" is stored in in-memory. Is it something to do with that? – Mony Dec 30 '18 at 23:07
  • Yes definitely may be key....in that case you should set the overwrite output env variable to true, else if you can also delete a feature layer before proceeding with an attempt to essentially replace it. [arcpy.env.overwriteOutput = True] – T. Wayne Whitley Dec 30 '18 at 23:49
  • Yes, if you see my script above, I have already included that: arcpy.env.overwriteOutput = True. Still there is same error. – Mony Dec 30 '18 at 23:59
  • Looks like you already have the overwrite setting turned on, so what you should try is an If statement to check for the lyrx file in the dir you’re writing to and explicitly delete the file before rewriting it. The management toolbox has a Delete tool, otherwise you can delete files easily via strictly Python. ...first put in a statement if arcpy.Exists(<your layer file>)... – T. Wayne Whitley Dec 31 '18 at 0:00
  • Yup, I included that (delete any previous in memory file) in my original script as well: arcpy.Delete_management("Layer_%s" %(FIPS)) – Mony Dec 31 '18 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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