Select to view content in your preferred language

KML to Layer Tool

6117
8
08-16-2010 05:52 AM
MattMoyles
Occasional Contributor
I'm relatively new to ArcGIS and was trying to covert a KML file into a layer using the conversion toolbox so it can later be put into the SDE database.  I'm having some trouble using the tool though.  I do this:

arcpy.ImportToolbox("C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Conversion Tools.tbx");
arcpy.KMLToLayer_conversion(LocalFile, runCWD, "nws")


but when I run my script from the command ine it returns an error.  The error is

Traceback (most recent call last):
  File "kml2sde.py", line 35, in <module>
    arcpy.KMLToLayer_conversion(LocalFile, runCWD)
  File "C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Conversion
s.tbx", line 29, in KMLToLayer
    ???d[
AttributeError: 'NoneType' object has no attribute 'KMLToLayer_conversion'


If I run the tool from ArcCatalog it works fine and creates the layer and gdb folder.  I'm trying to create a script that pulls data from a KML file at regular intervals, am I going about this the right way?
0 Kudos
8 Replies
KevinHibma
Esri Regular Contributor
Matt,

You're using:
arcpy.KMLToLayer_conversion(LocalFile, runCWD, "nws")

This implies that LocalFile and runCWD are variables.
Unless you have more to your script that I'm not seeing (you've assigned values to those variables), you'll want to run it something like the following with the inputs and quotations:
arcpy.KMLToLayer_conversion(r'C:\_KMLs\eqs7day-age.kmz',r'C:\_KMLs\temp',"myEQdata")


...now if you have assigned values to the variables - everything looks correct in your code snippet.
My question would be can you run any other tools via a Python script? Something like:
arcpy.GetCount_management("aaa.shp")


Or could you just post your whole script?
0 Kudos
MattMoyles
Occasional Contributor
Thanks for your reply!  Here is my entire script

# ############################
# KML2SDE
# ############################
# Imports KML files into an SDE database
# Matt Moyles - 8/12/2010
# ############################

import sys, os, string, zipfile, urllib, arcpy, traceback

# ############################
# Edit These
# ############################

CommonName    = "Severe_WW"
FeedTitle        = "Severe Weather Watches"
FeedURL        = "http://wdssii.nssl.noaa.gov/realtime/warnings/NWS_Warnings.kml"
LocalFile     = os.getcwd() + "\\NWS_Warnings.kml"
runCWD        = os.getcwd()


# ############################
# Retrieve the file and copy it to the server (no gaurentee the file exists or is the right file)
# ############################

print "Retrieving the feed @ " + FeedURL
#urllib.urlretrieve(FeedURL, LocalFile)
#urllib.urlcleanup()
print "Saved the feed to " + LocalFile

# ############################
# Convert To Shapefile
# ############################
print "Converting to ShapeFile..."
#environment = arcpy.GetSystemEnvironment("TEMP")
#arcpy.env.scratchWorkspace = environment
# Set environment settings
arcpy.env.workspace = "C:/data"
arcpy.env.overwriteOutput = True
print LocalFile
print runCWD



arcpy.ImportToolbox("C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Conversion Tools.tbx")
arcpy.KMLToLayer_conversion(LocalFile, runCWD)


I also tried arcpy.GetCount_management() with a shapefile I had lying around and it works fine no errors.
0 Kudos
KevinHibma
Esri Regular Contributor
Matt,

On the first run through I see it hitting that same error "noneType" as well.
If you remove the arcpy.ImportToolbox from your script it should get you past this.
You don't actually need to import the system toolboxes, your reference of arcpy.KMLToLayer_conversion is enough.

Let me know if that helps or not.
0 Kudos
MattMoyles
Occasional Contributor
Now it seems that python.exe is crashing when it gets to the KMLToLayer_conversion call.  I'm trying to do it like this:

print "Converting to Layer..."
try:
    arcpy.KMLToLayer_conversion("NWS_Warnings.kml", runCWD)
except Exception as e:
    print e.message
    arcpy.AddError(e.message)


I'm running the script through the command line.

I would like to see if I can re-produce this on a clean install of ArcDesktop but I don't have that available at the moment.  Any ideas?
0 Kudos
KevinHibma
Esri Regular Contributor
Let me look into it and see what I can find. I'll let you know.
0 Kudos
KevinHibma
Esri Regular Contributor
Matt,

I've looked into this and unfortunately this appears to be a bug.
The issue is logged and for your tracking purposes you can follow:
NIM060092 -KMLtoLayer crashes python when run as a standalone script.

I looked at a variety of ways to work around the problem (such as building a script tool and calling that) - it appears there isn't a workaround.

My apologies for this inconvenience, this should fixed in SP1 due sometime in the Fall.
0 Kudos
MattMoyles
Occasional Contributor
Thanks for looking into that Kevin, I guess we'll just have to wait. 😞
0 Kudos
MattMoyles
Occasional Contributor
For anyone who is interested in this topic I was able to accomplish this task through ArcObjects using C#, here is the code enjoy!

using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessor;

namespace kml2sde
{
    class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new kml2sde.LicenseInitializer();
    
        [STAThread()]
        static void Main(string[] args)
        {
            bool bInitialized = false;
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop))
            {
                Console.WriteLine("This application could not load the correct version of ArcGIS.");
                return;
            }

            //ESRI License Initializer generated code.
            if (m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcInfo },
            new esriLicenseExtensionCode[] { }) == true)
            {
                bInitialized = true;
                Console.WriteLine("Engine initialized");
                Console.WriteLine("License type: " + esriLicenseProductCode.esriLicenseProductCodeArcInfo.ToString());
            }
            else
            {
                Console.WriteLine("Engine NOT initialized");
            }


            Geoprocessor gp = new Geoprocessor();

            IVariantArray varray = (IVariantArray)new VarArrayClass();
            varray.Add("NWS_Warnings.kml"); //input
            varray.Add("C:\\Documents and Settings\\matt.moyles\\My Documents\\Visual Studio 2008\\Projects\\kml2sde\\kml2sde\\bin\\Debug"); //output

            gp.Execute("KMLToLayer_conversion", varray, null);
            

            //Do not make any call to ArcObjects after ShutDownApplication()
            m_AOLicenseInitializer.ShutdownApplication();
        }
    }
}
0 Kudos