Help with standalone script from model builder model

683
6
Jump to solution
12-01-2020 08:54 AM
JoshBlack
New Contributor II

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")

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor
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")

View solution in original post

6 Replies
BlakeTerhune
MVP Regular Contributor

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"
0 Kudos
JoshBlack
New Contributor II

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).

0 Kudos
DavidPike
MVP Frequent Contributor
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")
JoshBlack
New Contributor II

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?

0 Kudos
DavidPike
MVP Frequent Contributor

sure, just set an environment, best to put it under your import arcpy.

arcpy.env.overwriteOutput = True

 

enjoy.

JoshBlack
New Contributor II

perfect. Thank you for your help.

0 Kudos