Layer to KML 'Name' missing when using python

2407
11
07-08-2018 07:41 PM
MarkHansen1
New Contributor II

Hi,

I'm exporting a point features to kml via a python script but the generated kml has no 'Name'  information.  i.e. when you expand the list, every point says "[no name]".  All other data is correct.

There is a 'Name' field in the attribute table.  It is the first field after the Object ID and geometry field.

simplified code:

arcpy.management.MakeFeatureLayer(InFc, OutLyr, None, None, None)
OutKmzPath = ProjectPath + "out.kmz"
arcpy.conversion.LayerToKML(OutLyr, OutKmzPath, 0, "NO_COMPOSITE", "DEFAULT", 1024, 96, "ABSOLUTE")

but, if i do the same from within ArcGIS Pro (v2.2) the generated kml has the name field correctly populated. 

Is there another step to get the kml Name field to populate when running in a script?

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

Can you ensure that you are using Pro's arcpy?

Also... although they should be technically equal, try

arcpy.MakeFeatureLayer_management

and dump the None entries, since they aren't needed

0 Kudos
MarkHansen1
New Contributor II

Hi Dan,

Python v3.6.5.  I'm using Idle - right-click the py file, choose "Edit with IDLE (ArcGIS Pro)".

tried arcpy.MakeFeatureLayer_management but no joy.

yeah... the code still needs a lot of clean-up. 

Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

I don't see anything obvious in your code though.  Try it within Pro's IDE so that Pro is also open.. I just have a feeling I have seen this before

0 Kudos
MarkHansen1
New Contributor II

running it within the IDE works

0 Kudos
DanPatterson_Retired
MVP Emeritus

Now to find the reason... which is why I asked about Pro's arcpy... Do you have ArcMap also installed?

I know it uses python 2.7, but I used python 3.x when I used arcmap (just a matter of direction to arcpy)

Addendum

Are there any spaces in the names of the input or output files?  Pro doesn't like them (which is good) and that behaviour separates map from pro... although I haven't fully investigated yet

Addendum 2

Check your installation of arcpy …. from whatever environment

import arcpy

arcpy.__file__  # mine is installed in c:\arcgispro... not the normal install

'C:\\ArcGISPro\\Resources\\ArcPy\\arcpy\\__init__.py'

arcpy.GetInstallInfo()
 
{'InstallDir': 'c:\\arcgispro\\',
 'Installer': 'dan_p',
 'ProductName': 'ArcGISPro',
 'Version': '2.2',
 'SourceDir': 'C:\\Computer\\ArcGISPro_2_2\\ArcGISPro\\',  # ---- msi file
 'InstallType': 'N/A',
 'BuildNumber': '12813',
 'InstallDate': '2018-07-05',
 'InstallTime': '23:34:14',
 'SPNumber': 'N/A',
 'SPBuild': 'N/A'}
0 Kudos
MarkHansen1
New Contributor II

ArcMap 10.6 is installed.  I started this project using map but switched to Pro because of this problem with the surface difference tool https://community.esri.com/message/767562-using-a-las-dataset-with-the-surface-difference-tool.

No spaces in the input or output data files.  

import arcpy

arcpy.__file__
Out[2]: 'C:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcPy\\arcpy\\__init__.py'

arcpy.GetInstallInfo()
Out[3]: 
{'InstallDir': 'c:\\program files\\arcgis\\pro\\',
 'Installer': 'Mark.Hansen',
 'ProductName': 'ArcGISPro',
 'Version': '2.2',
 'SourceDir': 'C:\\Users\\mark.hansen\\AppData\\Local\\Microsoft\\Windows\\INetCache\\ArcGISUpdate\\ArcGISPro\\esrisoftware.esri.com\\',
 'InstallType': 'N/A',
 'BuildNumber': '12813',
 'InstallDate': '27/06/2018',
 'InstallTime': '9:43:07',
 'SPNumber': 'N/A',
 'SPBuild': 'N/A'}
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

My hunch is the issue revolves around a KML Placemark name being "name."  Just as a test, if you rename the "name" field to "name1", does it work in the script?

MarkHansen1
New Contributor II

Hi Joshua,

'name1' makes no difference.

I first suspected it was something to do with the 'name' field.  I saw this post (https://gis.stackexchange.com/questions/16934/how-is-the-default-label-field-chosen-by-arcmap) and started playing with variations of name/NAME/Name.  My name field is a string.

It works fine and the name is populated if you run it within the ArcPro python IDE or with the geoprocessing tools.

Because of this I suspect it has something to do with ArcPro being open and the layer is added to a map first and then exported to kml. 

I have been playing with this some more and I am guessing there is some little behind-the-scenes extra that automatically sets the default label when you add the layer to a map, then when you run the layer-to-kml it picks that default label and populates the name field in the kml.  When I do this in ArcPro and check the label, it has already selected the field called 'name'.  As a test I reordered the fields in the table and put the name field last then added to the map - the 'name' field was still selected by default.

I think that because my script doesn't run inside ArcPro and never adds to a map, the default label does not get set on the layer, therefore no name in the kml.

So, is there a way to set the default field in python?

0 Kudos
DanPatterson_Retired
MVP Emeritus

Nothing that I have found from the links, but... in desperation, perhaps you need to add 

Save To Layer File—Data Management toolbox | ArcGIS Desktop 

after the makefeaturelayer to see if it persists... and if you really need to… Delete_management it after