Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using ARCGis desktop and have written a quick conversion program to convert my layers to kml but I ran into some issues. It seems the only layers it can process correctly are group layers.

When it processes the group layers I am missing all the feature layers included in the group layer in the resulting doc.kml. It's also failing on feature layers with an error that says it's not a layer with the following message: ERROR 000840: The value is not a Layer. When I know it's a layer, I can pull it up in ARCMap and, indeed, ARCMap agrees with me that it's a layer. I can use the MapToKML function without issue but I need to control my layers individually so I was hoping I can generate KML for each individual layer on a map.

Thanks in advance for any help you can provide.

Here is my source code:

  static void Main(string[] args)
  {
   IMapDocument mapDocument = new MapDocumentClass();
   IPage page;

   string mapFilename = @"c:\maps\BaseMap_NAD83_SD_StatePlane_Plumes_Scott.mxd";

   Console.WriteLine("Initializing GEO...");
   Geoprocessor GP = new Geoprocessor();

   if (mapDocument.get_IsMapDocument(mapFilename) && mapDocument.get_IsPresent(mapFilename))
   {
    Console.WriteLine("Loading map...");
    mapDocument.Open(mapFilename, "");

    page = mapDocument.PageLayout.Page;

    IMap map;
    for (int iMapIndex = 0; iMapIndex < mapDocument.MapCount; iMapIndex++)
    {
     map = mapDocument.get_Map(iMapIndex);

     Console.WriteLine("Converting layers for map {0}...", map.Name);

     ILayer layer;
     for (int iLayerIndex = 0; iLayerIndex < map.LayerCount; iLayerIndex++)
     {
      layer = map.get_Layer(iLayerIndex);


      ConvertLayerToKML(GP, layer as ILayer2);

      Marshal.ReleaseComObject(layer);
     }

     Marshal.ReleaseComObject(map);
    }
   }

   mapDocument.Close();
   Marshal.ReleaseComObject(mapDocument);
  }

  private static string[] ConvertLayerToKML(Geoprocessor GP, ILayer2 layer)
  {
   string [] messages = new string[] {};

   string layerName = layer.Name;

   LayerToKML layerToKML = new LayerToKML();
   layerToKML.layer = layer;
   layerToKML.layer_output_scale = 10000;
   layerToKML.out_kmz_file = string.Format("{0}.kmz", layerName);

   Console.WriteLine("Converting layer {0}", layerName);

   object result = GP.Execute(layerToKML, null);

   messages = new string[GP.MessageCount];
   for (int i = 0; i < GP.MessageCount; i++)
   {
    messages[i] = GP.GetMessage(i);
   }

   return messages;
  }
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I it doesn't work with group layers, try to cast the layer to an ICompositeLayer and loop through the individual layers.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.