I have a simple model that I've built in model builder and I would like to be able to run in it as a standalone script. I think the issue is that the script doesn't know where to find the layer from the mxd file, but I don't know the syntax to tell it where to find it. Can someone take a look and help please?
# Import arcpy module
import arcpy
# Local variables:
oms_span = "Blink Outages\\oms_span"
oms_span__2_ = oms_span
test_kmz = "F:\\test\\Project\\test.kmz"
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(oms_span, "NEW_SELECTION", "gs_outg_cd = 31 OR gs_outg_cd = 32 OR gs_outg_cd = 35")
# Process: Layer To KML
arcpy.LayerToKML_conversion(oms_span__2_, test_kmz, "0", "NO_COMPOSITE", "DEFAULT", "1024", "96", "CLAMPED_TO_GROUND")
Solved! Go to Solution.
import arcpy
oms_span = r"D:\pathtoyour.gdb\Blink Outages\oms_span"
test_kmz = r"F:\test\Project\test.kmz"
arcpy.MakeFeatureLayer_management(oms_span, "oms_span_lyr")
arcpy.SelectLayerByAttribute_management("oms_span_lyr", "NEW_SELECTION", "gs_outg_cd = 31 OR gs_outg_cd = 32 OR gs_outg_cd = 35")
arcpy.LayerToKML_conversion("oms_span_lyr", test_kmz, "0", "NO_COMPOSITE", "DEFAULT", "1024", "96", "CLAMPED_TO_GROUND")
I'm assuming it's the path to the oms_span feature class that isn't working? Try changing
oms_span = "Blink Outages\\oms_span"
to
oms_span = "your_geodatabase_path\\oms_span"
When I change to the feature class path I get this error:
ExecuteError: Failed to execute. Parameters are not valid.
The value cannot be a feature class
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).
import arcpy
oms_span = r"D:\pathtoyour.gdb\Blink Outages\oms_span"
test_kmz = r"F:\test\Project\test.kmz"
arcpy.MakeFeatureLayer_management(oms_span, "oms_span_lyr")
arcpy.SelectLayerByAttribute_management("oms_span_lyr", "NEW_SELECTION", "gs_outg_cd = 31 OR gs_outg_cd = 32 OR gs_outg_cd = 35")
arcpy.LayerToKML_conversion("oms_span_lyr", test_kmz, "0", "NO_COMPOSITE", "DEFAULT", "1024", "96", "CLAMPED_TO_GROUND")
I think this will work. Thank you so much. The only problem now is that the output kmz already exists and I want to overwrite it each time i run the script. Is that possible?
sure, just set an environment, best to put it under your import arcpy.
arcpy.env.overwriteOutput = True
enjoy.
perfect. Thank you for your help.