Generate KML/KMZ

8235
3
Jump to solution
12-10-2012 03:24 AM
Labels (1)
De_WetEllis
New Contributor III
Hi

Is there a way to Generate a KML/KMZ file using the ArcGIS Runtime SDK for WPF.
I tried using the local server but it doesnt support the GenerateKML operation. Or the f=kmz in the querystring.

Our Client just wants a simple Map application using a map package and then find a parcel and export to kml.

Any help would be appreciated.

Thanks
De Wet Ellis
1 Solution

Accepted Solutions
De_WetEllis
New Contributor III
Hi all

I have found a free Library called sharpKml

http://sharpkml.codeplex.com/

"SharpKML is an implementation of the Open Geospatial Consortium (OGC) KML 2.2 standard developed in C#, able to read/write both KML files and KMZ files."

This is a great tool that makes writing KML files very easy.

Sample Code

            Document document = new Document();
            document.Name = "Polygon.kml";
            document.Open = false;
            int i = 1;
            GraphicsLayer graphicsLayer = _map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            foreach (Graphic grp in graphicsLayer.Graphics)
            {
                OuterBoundary outerBoundary = new OuterBoundary();
                outerBoundary.LinearRing = new LinearRing();
                outerBoundary.LinearRing.Coordinates = new CoordinateCollection();

                ESRI.ArcGIS.Client.Geometry.Polygon pg = grp.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
                foreach (ESRI.ArcGIS.Client.Geometry.MapPoint itn in pg.Rings[0])
                {
                    outerBoundary.LinearRing.Coordinates.Add(new Vector(itn.Y, itn.X, 30));
                }

                Polygon polygon = new Polygon();
                polygon.Extrude = true;
                polygon.AltitudeMode = AltitudeMode.ClampToGround;
                polygon.OuterBoundary = outerBoundary;
                //Color Style Setting:
                byte byte_Color_R = 150, byte_Color_G = 150, byte_Color_B = 150, byte_Color_A = 100; //you may get your own color by other method
                var style = new Style();
                style.Polygon = new PolygonStyle();
                style.Polygon.ColorMode = SharpKml.Dom.ColorMode.Normal;
                style.Polygon.Color = new Color32(byte_Color_A, byte_Color_B, byte_Color_G, byte_Color_R);
                //Set the polygon and style to the Placemark:
                Placemark placemark = new Placemark();
                placemark.Name = "place" + i.ToString();
                placemark.Geometry = polygon;
                placemark.StyleSelector = style;
                //Finally to the document and save it
                document.AddFeature(placemark);
                i++;
            }
            var Mykml = new Kml();
            Mykml.Feature = document;
            KmlFile MykmlFile = KmlFile.Create(Mykml, true);
            MykmlFile.Save("C:\\MyPolygon.kml");

Hope this can help someone out in the future.

Regards ,

De Wet Ellis

View solution in original post

0 Kudos
3 Replies
KevinHibma
Esri Regular Contributor
As you've probably already determined, the (from/to) KML tools arent supported in the Runtime.
In core ArcGIS, either the tools or using the output from Server with type of KML is the only way to create KML. (or extensions like data interop)

I havent personally used any of the C# KML packages listed here, but one might provide a useful option?

Or there might be a script tool out there which writes KML. If you can find a script that someone has already written you can wrap that in a geoprocessing task.
0 Kudos
De_WetEllis
New Contributor III
Hi Kevin

Thanks for the reply. I will look into the suggested options.
If I find a solution I will post it here.

In the mean time .. If someone has a solution already. Dont hesitate to reply 😛

Thanks again .

De Wet Ellis
0 Kudos
De_WetEllis
New Contributor III
Hi all

I have found a free Library called sharpKml

http://sharpkml.codeplex.com/

"SharpKML is an implementation of the Open Geospatial Consortium (OGC) KML 2.2 standard developed in C#, able to read/write both KML files and KMZ files."

This is a great tool that makes writing KML files very easy.

Sample Code

            Document document = new Document();
            document.Name = "Polygon.kml";
            document.Open = false;
            int i = 1;
            GraphicsLayer graphicsLayer = _map.Layers["MySelectionGraphicsLayer"] as GraphicsLayer;
            foreach (Graphic grp in graphicsLayer.Graphics)
            {
                OuterBoundary outerBoundary = new OuterBoundary();
                outerBoundary.LinearRing = new LinearRing();
                outerBoundary.LinearRing.Coordinates = new CoordinateCollection();

                ESRI.ArcGIS.Client.Geometry.Polygon pg = grp.Geometry as ESRI.ArcGIS.Client.Geometry.Polygon;
                foreach (ESRI.ArcGIS.Client.Geometry.MapPoint itn in pg.Rings[0])
                {
                    outerBoundary.LinearRing.Coordinates.Add(new Vector(itn.Y, itn.X, 30));
                }

                Polygon polygon = new Polygon();
                polygon.Extrude = true;
                polygon.AltitudeMode = AltitudeMode.ClampToGround;
                polygon.OuterBoundary = outerBoundary;
                //Color Style Setting:
                byte byte_Color_R = 150, byte_Color_G = 150, byte_Color_B = 150, byte_Color_A = 100; //you may get your own color by other method
                var style = new Style();
                style.Polygon = new PolygonStyle();
                style.Polygon.ColorMode = SharpKml.Dom.ColorMode.Normal;
                style.Polygon.Color = new Color32(byte_Color_A, byte_Color_B, byte_Color_G, byte_Color_R);
                //Set the polygon and style to the Placemark:
                Placemark placemark = new Placemark();
                placemark.Name = "place" + i.ToString();
                placemark.Geometry = polygon;
                placemark.StyleSelector = style;
                //Finally to the document and save it
                document.AddFeature(placemark);
                i++;
            }
            var Mykml = new Kml();
            Mykml.Feature = document;
            KmlFile MykmlFile = KmlFile.Create(Mykml, true);
            MykmlFile.Save("C:\\MyPolygon.kml");

Hope this can help someone out in the future.

Regards ,

De Wet Ellis
0 Kudos