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...
No comments:
Post a Comment