Python Script Error for KML conversion

1991
5
05-23-2016 10:50 PM
MANESK
by
New Contributor III

I tried to run this script for converting a batch of shp files in a folder... But i receive error msg posted below... Kindly suggest...

import arcpy

# Set environment settings

arcpy.env.workspace = r"D:\Work\Test"

#Output Path

outKML = r"D:\Work\Test\new"

# Variables Defined

composite = 'NO_COMPOSITE'

pixels = 2048

dpi = 96

clamped = 'CLAMPED_TO_GROUND'

scale = 10000

for layer in arcpy.ListFiles():

        arcpy.MakeFeatureLayer_management(layer, Flayer)

        arcpy.LayerToKML_conversion(Flayer, outKML,scale, composite,

                                        '', pixels, dpi, clamped)

else:

        arcpy.AddMessage('There are no layer files in '+arcpy.env.workspace+'.')

Error :

Traceback (most recent call last):

  File "D:\Work\Test\Test.py", line 13, in <module>

    arcpy.MakeFeatureLayer_management(layer, Flayer)

NameError: name 'Flayer' is not defined

0 Kudos
5 Replies
NeilAyres
MVP Frequent Contributor

May I suggest that you use python syntax highlighting (in the advanced editor) to post code.

But, your problem seems obvious. Where are you defining what the variable Flayer is.

Or do you want to call the output feature layer "Flayer".

0 Kudos
MANESK
by
New Contributor III

Script.png

I'm trying to convert the Shapefile to layer and then to kml....I'm not sure whether the flow is correct .... Pls Suggest

0 Kudos
NeilAyres
MVP Frequent Contributor

In that script you are not assigning Flayer to be anything.

Therefore, when it appear in the MakeFeatureLayer tool, it is not assigned (does not exist).

0 Kudos
NeilAyres
MVP Frequent Contributor

And, what is arcpy.ListFiles() returning? A list of what?. Shapefiles?

0 Kudos
DarrenWiens2
MVP Honored Contributor

You should use ListFeatureClasses rather than ListFiles, because ListFiles will return all individual files from a shapefile (e.g. shp, and shx, and dbf, etc.).

Also, as your code is now, you will overwrite your output KML over and over because you don't change the name on each iteration.