0

I am new to Python and using ModelBuilder as a starting point for my script. My objective is to add a new field, called LTS, and populate it with a number of 1-4 given a set of conditions based on other attributes.

SaveToLayerFile is not producing an output. Why doesn't SaveToLayerFile produce an output based on my code?

# ---------------------------------------------------------------------------
# LTS_11_9.py
# Created on: 2021-11-09 11:55:41.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

#import other modules you may want to use
import os, sys, pprint as pp

arcpy.env.workspace = 'C:\Users\kahcp\Documents\ArcGIS\Projects\ChattanoogaLTS'

#reads shapefile into arcpy layer object
arcpy.MakeFeatureLayer_management ('LTS4_11_8.shp', 'LTS4_11_8_RD')

# Local variables:
Streets_layer = "LTS4_11_8_RD"
Streets_layer__2_ = Streets_layer
Streets_layer__3_ = Streets_layer__2_
Streets_layer__4_ = Streets_layer__3_
Streets_layer__5_ = Streets_layer__4_
Streets_layer__6_ = Streets_layer__5_
Streets_layer__7_ = Streets_layer__6_
Streets_layer__8_ = Streets_layer__7_
Streets_layer__9_ = Streets_layer__8_
Streets_layer__10_ = Streets_layer__9_

# Process: Add Field
arcpy.AddField_management(Streets_layer, "LTS", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Select LTS 1 conditions
arcpy.SelectLayerByAttribute_management(Streets_layer__2_, "NEW_SELECTION", "(LanesPerDi = 1 AND sidepath =1 AND speed <= 20) OR (LanesPerDi = 1 AND sidepath =1 AND speed = 25) OR (LanesPerDi = 1 AND sidepath =1 AND speed = 30) OR (LanesPerDi = 1 AND sidepath =0 AND BL_Width >= 6 AND speed <= 20) OR (LanesPerDi = 1 AND sidepath =0 AND BL_Width >= 6 AND speed = 25) OR (LanesPerDi = 1 AND sidepath =0 AND BL_Width >= 6 AND speed = 30) OR (LanesPerDi = 1 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed <= 20) OR (LanesPerDi = 1 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 25) OR (LanesPerDi = 1 And sidepath = 0 And (FC_2 = 'Local_Private' Or FC_2 = 'Local') And BL_Width = 0 And speed <= 20) OR (LanesPerDi = 1 And sidepath = 0 And (FC_2 = 'Local_Private' Or FC_2 = 'Local') And BL_Width = 0 And speed = 25) OR (LanesPerDi = 2 AND sidepath =1 AND speed <= 20) OR (LanesPerDi = 2 AND sidepath =1 AND speed = 25) OR (LanesPerDi = 2 AND sidepath =0 AND BL_Width >= 6 AND speed <= 20) OR (LanesPerDi = 2 AND (FC_2 = 'Local_Private' Or FC_2 = 'Local') AND sidepath =0 AND BL_Width >= 6 AND speed = 25) OR (LanesPerDi = 2 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed <= 20) OR (LanesPerDi = 2 AND  (FC_2 = 'Local_Private' Or FC_2 = 'Local') AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 25) OR (LanesPerDi = 3 AND sidepath =1 AND speed <= 20)")

# Process: Calculate LTS 1
arcpy.CalculateField_management(Streets_layer__3_, "LTS", "1", "VB", "")

# Process: Select LTS 2 conditions
arcpy.SelectLayerByAttribute_management(Streets_layer__4_, "NEW_SELECTION", "(LanesPerDi = 1 AND sidepath =1 AND speed = 35) OR (LanesPerDi = 1 AND sidepath =1 AND speed >= 40) OR (LanesPerDi = 1 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 30) OR (LanesPerDi = 1 And sidepath = 0 And (FC_2 = 'Collector' Or FC_2 = 'Arterial') And BL_Width = 0 And speed <= 20) OR (LanesPerDi = 1 And sidepath = 0 And (FC_2 = 'Collector' Or FC_2 = 'Arterial') And BL_Width = 0 And speed = 25) OR (LanesPerDi = 2 AND sidepath =1 AND speed = 30) OR (LanesPerDi = 2 AND sidepath =1 AND speed = 35) OR (LanesPerDi = 2 AND sidepath =1 AND speed >= 40) OR (LanesPerDi = 2 AND (FC_2 = 'Collector' Or FC_2 = 'Arterial') AND sidepath =0 AND BL_Width >= 6 AND speed = 25) OR (LanesPerDi = 2 AND sidepath =0 AND BL_Width >= 6 AND speed = 30) OR (LanesPerDi = 2 AND  (FC_2 = 'Collector' Or FC_2 = 'Arterial') AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 25) OR (LanesPerDi = 2 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 30) OR (LanesPerDi = 2 And sidepath = 0 And BL_Width = 0 And speed <= 20) OR (LanesPerDi = 2 And sidepath = 0 And (FC_2 = 'Local_Private' Or FC_2 = 'Local') And BL_Width = 0 And speed = 25) OR (LanesPerDi = 3 AND sidepath =1 AND speed = 25) OR (LanesPerDi = 3 AND sidepath =1 AND speed = 30) OR (LanesPerDi = 3 AND sidepath =1 AND speed = 35) OR (LanesPerDi = 3 AND sidepath =1 AND speed >= 40) OR (LanesPerDi = 3 AND sidepath =0 AND BL_Width >= 6 AND speed <= 20) OR (LanesPerDi = 3 AND sidepath =0 AND BL_Width >= 6 AND speed = 25)")

# Process: Calculate LTS 2
arcpy.CalculateField_management(Streets_layer__5_, "LTS", "2", "VB", "")

# Process: Select LTS 3 conditions
arcpy.SelectLayerByAttribute_management(Streets_layer__6_, "NEW_SELECTION", "(LanesPerDi = 1 AND sidepath =0 AND BL_Width >= 6 AND speed = 35) OR (LanesPerDi = 1 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 35) OR (LanesPerDi = 1 And sidepath = 0 And BL_Width = 0 And speed = 30) OR (LanesPerDi = 1 And sidepath = 0 And BL_Width = 0 And speed = 35) OR (LanesPerDi = 2 AND sidepath =0 AND BL_Width >= 6 AND speed = 35) OR (LanesPerDi = 2 And sidepath = 0 And (FC_2 = 'Collector' Or FC_2 = 'Arterial') And BL_Width = 0 And speed = 25) OR (LanesPerDi = 3 AND sidepath =0 AND BL_Width >= 6 AND speed = 30) OR (LanesPerDi = 3 AND sidepath =0 AND BL_Width >= 6 AND speed = 35) OR (LanesPerDi = 3 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed <= 20) OR (LanesPerDi = 3 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 25) OR (LanesPerDi = 3 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 30) OR (LanesPerDi = 3 And sidepath = 0 And BL_Width = 0 And speed <= 20)")

# Process: Calculate LTS 3
arcpy.CalculateField_management(Streets_layer__7_, "LTS", "3", "VB", "")

# Process: Select LTS 4 conditions
arcpy.SelectLayerByAttribute_management(Streets_layer__8_, "NEW_SELECTION", "(LanesPerDi = 1 AND sidepath =0 AND BL_Width >= 6 AND speed >= 40) OR (LanesPerDi = 1 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 40) OR (LanesPerDi = 1 And sidepath = 0 And BL_Width = 0 And speed >= 40) OR (LanesPerDi = 2 AND sidepath =0 AND BL_Width >= 6 AND speed >= 40) OR (LanesPerDi = 2 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 35) OR (LanesPerDi = 2 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed >= 40) OR (LanesPerDi = 2 And sidepath = 0 And BL_Width = 0 And speed = 30) OR (LanesPerDi = 2 And sidepath = 0 And BL_Width = 0 And speed = 35) OR (LanesPerDi = 2 And sidepath = 0 And BL_Width = 0 And speed >= 40) OR (LanesPerDi = 3 AND sidepath =0 AND BL_Width >= 6 AND speed >= 40) OR (LanesPerDi = 3 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed = 35) OR (LanesPerDi = 3 AND sidepath =0 AND (BL_Width > 0 AND BL_Width <6) AND speed >= 40) OR (LanesPerDi = 3 And sidepath = 0 And BL_Width = 0 And speed = 25) OR (LanesPerDi = 3 And sidepath = 0 And BL_Width = 0 And speed = 30) OR (LanesPerDi = 3 And sidepath = 0 And BL_Width = 0 And speed = 35) OR (LanesPerDi = 3 And sidepath = 0 And BL_Width = 0 And speed >= 40)")

# Process: Calculate LTS 4
arcpy.CalculateField_management(Streets_layer__9_, "LTS", "4", "VB", "")

#Copy Features to new Feature Class
arcpy.CopyFeatures_management("LTS4_11_8_RD", "LTS4_11_8_updated")

#saves layer to new layer file
arcpy.SaveToLayerFile_management ("LTS4_11_8_RD", "LTS4_11_8_updated")

2
  • 2
    What do you mean by "CopyFeatures and SaveToLayerFile are not doing the trick"? What error messages are you getting? Edit your question and explain.
    – Hornbydd
    Commented Nov 10, 2021 at 14:30
  • 1
    Welcome to GIS SE. As a new user, please take the Tour. Questions here should describe the actual error encountered, vice a generic indication of failure. Please Edit the question.
    – Vince
    Commented Nov 10, 2021 at 15:06

1 Answer 1

1

Looking at Save To Layer File documentation example, I believe you need to save the new layer as a '.lyr' as follows:

#saves layer to new layer file
arcpy.SaveToLayerFile_management ("LTS4_11_8_RD", "LTS4_11_8_updated.lyr")

That should save a layer file to your current workspace.

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.