0

I've written a script that converts all shapefiles contained in a folder into lyr and then kmz.

import arcpy

arcpy.env.workspace = "path"
arcpy.env.overwriteOutput = True

allfeatures = arcpy.ListFeatureClasses()
print allfeatures
for feature in allfeatures:
    desc = arcpy.Describe(feature)
    shape = desc.shapeType
    if shape == "Point":
        name = feature[:-4]
        lyrname = name +"_lyr"
        arcpy.MakeFeatureLayer_management(feature, lyrname)
        outputlyr = lyrname + ".lyr"
        lyr=arcpy.SaveToLayerFile_management(lyrname, outputlyr)
        outputkmz = name + ".kmz"
        arcpy.LayerToKML_conversion(lyr,outputkmz)

I'm now trying to handle labeling to make sure the kmz are actually displaying a defined label. Should I do this on the lyr files before exporting them as kmz?

I've been writing another script that turns on labels on lyr files in a .mxd file (it's working) but I wanted to know if there's a way to do this directly in a folder? (So I wouldn't have to open an .mxd and drop all of my lyr in the ToC).

import arcpy
mxd = arcpy.mapping.MapDocument("path")
layers = arcpy.mapping.ListLayers(mxd)
for layer in layers:
    print layer.name
    if layer.supports("LABELCLASSES"):
        print "yeeeah"
        #layer.labelClasses
        for lblclass in layer.labelClasses:
            lblclass.expression = "[ORIG_FID]"
            layer.showLabels = True
            #lblclass.showClassLabels
        layer.showLabels = True
mxd.saveACopy("path")

Basically, my global process would be "In a folder, convert all shp to kmz with a label turned on".

(I'm still quite a beginner with Python).

My attempt to have a global script would be to "list all files" but the Labelling properties don't seem to work if they aren't used inside a mxd.

import arcpy

Set workspace
arcpy.env.workspace = "path"
arcpy.env.overwriteOutput = True

allfeatures = arcpy.ListFeatureClasses()
print allfeatures
for feature in allfeatures:
    desc = arcpy.Describe(feature)
    shape = desc.shapeType
    if shape == "Point":
        name = feature[:-4]
        lyrname = name +"_lyr"
        arcpy.MakeFeatureLayer_management(feature, lyrname)
        outputlyr = lyrname + ".lyr"
        lyr=arcpy.SaveToLayerFile_management(lyrname, outputlyr)

allfiles = arcpy.ListFiles()
for file in allfiles:
    if file.endswith(".lyr"):
        if file.supports("LABELCLASSES"):
            for lblclass in file.labelClasses:
                lblclass.expression= "[ORIG_FID]"
                file.showLabels = True
            file.showLabels = True

        outputkmz = file[:-8] + ".kmz"
        arcpy.LayerToKML_conversion(file,outputkmz)

So I have been working on a global script and here I am:

import arcpy
import arcpy.mapping

arcpy.env.workspace = path
pathfolder = path
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument(path)
dataFrame = arcpy.mapping.ListDataFrames(mxd) #list of data frames inside MXD
currentdataFrame = dataFrame[0] #specifies we want to work in the first dataframe


allfeatures = arcpy.ListFeatureClasses() 
for feature in allfeatures:
    desc = arcpy.Describe(feature)
    shape = desc.shapeType
    if shape == "Point":
        name = feature[:-4]
        lyrname = name + "_lyr"
        arcpy.MakeFeatureLayer_management(feature, lyrname)
        outputlyr = lyrname + ".lyr"
        lyr=arcpy.SaveToLayerFile_management(lyrname, outputlyr)
        pathlyr = str(pathfolder) + "/" + str(outputlyr)
        layertoadd = arcpy.mapping.Layer(pathlyr)
        arcpy.mapping.AddLayer(currentdataFrame, layertoadd, "BOTTOM")

mxd.saveACopy(path)
mxd = arcpy.mapping.MapDocument(path)
layers = arcpy.mapping.ListLayers(mxd)
for layer in layers:
    classes = layer.labelClasses
    classdefault = classes[0]
    classdefault.expression = "[ORIG_FID]" 
    layer.showLabels = "True"
    classdefault.showClassLabels = "True"
    outputkmz = layer.name[:-4] + ".kmz"
    arcpy.LayerToKML_conversion(layer,outputkmz,"0","NO_COMPOSITE","","","","CLAMPED_TO_GROUND")
mxd.saveACopy(path)

It doesn't crash, i got .mxd where the label i want is being displayed and kmz are being created. But when I open them in google earth, I don't see anything. And if I try to read them in a .mxd, i've got an error message saying the extent doesn't match. Even when i try to set an output extent in the function arcpy.LayerToKML_conversion, it doesn't work. On the opposite, if I convert one of the .lyr directly in ArcGis, it's working, it's giving me the kmz I want. I really don't get what's wrong in my script...

  • If you want to solve a problem in a programmatic way, you are excepted to show the effort you made (no matter how much or less it is) – LaughU Jan 22 '18 at 10:25
1

If I follow correctly your posted answer, you are adding the layers to an mxd and setting the labels. To get the labels to show for all layers in the mxd make sure you save the mxd after adding all the layers and setting their label and then use the Map to KML method to create the kmz, see code example below:

for layer in layers:
    classes = layer.labelClasses
    classdefault = classes[0]
    classdefault.expression = "[ORIG_FID]" 
    layer.showLabels = "True"
    classdefault.showClassLabels = "True"
mxd.saveACopy(path)
arcpy.MapToKML_3d(path, "Layers", "C:/Temp/mykmz.kmz", "1")

Map to KML reference.

  • Hi artwork21, thanks a lot for your help. I just tried, a kmz file is created but still doesn't contain anything. And with that tool, I would have only kmz, right? While I'd still like to have one kmz per layer. Actually, I'm working through an .mxd only because i couldn't find a way to do it directly in a folder (I mean, I couldn't find a way to play with the labels if I'm not working in a .mxd). – romanceline Jan 23 '18 at 5:34
  • So your adding layers to a mxd, saving it, and then running the Map to KML on that mxd and it creates an empty kmz? Either the code is not adding layers to mxd or the Map to KML method is not pointing to the correct saved mxd. Where are you defining the path variable? – artwork21 Jan 23 '18 at 13:50
  • The code is correctly adding the layers to the mxd (that part is easy to check). But I don't think Map to KML can solve my problems as I want a kmz for every layer in this .mxd. – romanceline Jan 23 '18 at 14:02
  • The solution would be a logic re-ordering, add layer to map, set label, save map, run Map to KML, remove layer, add next layer to map, set label.....etc. – artwork21 Jan 23 '18 at 17:09
  • But why would I use run Map to kml while there's a function Layer to kml? – romanceline Jan 23 '18 at 17:18

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.