Select to view content in your preferred language

KML layer

1340
9
05-20-2010 08:22 PM
MichaelMacDonald
Emerging Contributor
Does anyone know how to use a KML layer inside of an ESRI Silverlight app?  Are there any samples of this around?

Sorry if this was repeated.
0 Kudos
9 Replies
DanielWalton
Frequent Contributor
Morten,

useCasesForKml ++;
😉
0 Kudos
BrianGustafson
Occasional Contributor
I am trying to use the sample that you provided but am having trouble getting it to work with multi geometries.  the KML that I am working with is http://rmgsc.cr.usgs.gov/outgoing/GeoMAC/2010_fire_data/KMLS/ActiveFirePerimeters.kml.  I am trying to see in the code where I need to change then recompile to get the correct output but I am having trouble finding my way around.  Can anyone point me in the right direction?

Thanks,
0 Kudos
DanielWalton
Frequent Contributor
Brian,

I seem to recall having to comment out the code underneath the "MultiGeometry" case in the KMLToFeature class. The way the XML is parsed, the inner features will get captured that way.

-Dan
0 Kudos
BrianGustafson
Occasional Contributor
I commented out the code and that did not work.  I think that I need to write a loop to cycle through the geomtry inside of the multi geometry tag but I am not sure how to get the inner elements.
0 Kudos
Sravan_KumarUddarraju
Emerging Contributor
I am having the same issue. I know we need to tweak recursive function little bit.  If you resolve this issue, please send me (if you can) updated code for this recursive function.

Regards,
Srv
0 Kudos
BrianGustafson
Occasional Contributor
In the KmlToFeatureDefinition I changed the CreateFeatureDefinition method to the code below.

private void CreateFeatureDefinition(string description, string name, KMLStyle kmlStyle, XElement placemark)
        {
            FeatureDescriptor fd = null;

            // Determine what kind of feature is present in the placemark
            XElement geomElement = GetFeatureType(placemark);
            if (geomElement != null && geomElement.Name != null)
            {
                switch (geomElement.Name.LocalName)
                {
                    case "Point":
                        fd = ExtractPoint(kmlStyle, geomElement);

                        if (fd != null)
                        {
                            if (!String.IsNullOrEmpty(description))
                                fd.Attributes.Add("description", description);
                            if (!String.IsNullOrEmpty(name))
                                fd.Attributes.Add("name", name);

                            featureDefs.AddFeature(fd);
                        }
                        break;

                    case "LineString":
                        fd = ExtractPolyLine(kmlStyle, geomElement);

                        if (fd != null)
                        {
                            if (!String.IsNullOrEmpty(description))
                                fd.Attributes.Add("description", description);
                            if (!String.IsNullOrEmpty(name))
                                fd.Attributes.Add("name", name);

                            featureDefs.AddFeature(fd);
                        }
                        break;

                    case "LinearRing":
                        fd = ExtractLinearRing(kmlStyle, geomElement);

                        if (fd != null)
                        {
                            if (!String.IsNullOrEmpty(description))
                                fd.Attributes.Add("description", description);
                            if (!String.IsNullOrEmpty(name))
                                fd.Attributes.Add("name", name);

                            featureDefs.AddFeature(fd);
                        }
                        break;

                    case "MultiGeometry":
                        // Use recursion to walk the hierarchy of embedded definitions
                        IEnumerable<XNode> x = geomElement.Nodes();

                        foreach (XNode n in x)
                        {

                            string s = n.ToString();

                            if (s.Contains("Point"))
                            {
                                XElement e = (XElement)n;
                                fd = ExtractPoint(kmlStyle, e);

                                if (fd != null)
                                {
                                    if (!String.IsNullOrEmpty(description))
                                        fd.Attributes.Add("description", description);
                                    if (!String.IsNullOrEmpty(name))
                                        fd.Attributes.Add("name", name);

                                    featureDefs.AddFeature(fd);
                                }
                            }
                            if (s.Contains("LineString"))
                            {
                                XElement e = (XElement)n;
                                fd = ExtractPolyLine(kmlStyle, e);

                                if (fd != null)
                                {
                                    if (!String.IsNullOrEmpty(description))
                                        fd.Attributes.Add("description", description);
                                    if (!String.IsNullOrEmpty(name))
                                        fd.Attributes.Add("name", name);

                                    featureDefs.AddFeature(fd);
                                }
                            }
                            if (s.Contains("Polygon"))
                            {
                                XElement e = (XElement)n;
                                fd = ExtractPolygon(kmlStyle, e);

                                if (fd != null)
                                {
                                    if (!String.IsNullOrEmpty(description))
                                        fd.Attributes.Add("description", description);
                                    if (!String.IsNullOrEmpty(name))
                                        fd.Attributes.Add("name", name);

                                    featureDefs.AddFeature(fd);
                                }
                            }
                            if (s.Contains("LinearRing"))
                            {
                                XElement e = (XElement)n;
                                fd = ExtractLinearRing(kmlStyle, e);

                                if (fd != null)
                                {
                                    if (!String.IsNullOrEmpty(description))
                                        fd.Attributes.Add("description", description);
                                    if (!String.IsNullOrEmpty(name))
                                        fd.Attributes.Add("name", name);

                                    featureDefs.AddFeature(fd);
                                }
                            }
                        }

                        break;
                        //CreateFeatureDefinition(description, name, kmlStyle, geomElement);
                        //break;

                    case "Polygon":
                        fd = ExtractPolygon(kmlStyle, geomElement);

                        if (fd != null)
                        {
                            if (!String.IsNullOrEmpty(description))
                                fd.Attributes.Add("description", description);
                            if (!String.IsNullOrEmpty(name))
                                fd.Attributes.Add("name", name);

                            featureDefs.AddFeature(fd);
                        }
                        break;
                }

                // If a feature definition was created, then assign attributes and add to collection
                //if (fd != null)
                //{
                //    if (!String.IsNullOrEmpty(description))
                //        fd.Attributes.Add("description", description);
                //    if (!String.IsNullOrEmpty(name))
                //        fd.Attributes.Add("name", name);

                //    featureDefs.AddFeature(fd);
                //}
            }
        }
0 Kudos
Sravan_KumarUddarraju
Emerging Contributor
Excellent... I really appreciated your quick response.
You saved my day.

Regards,
Srv
0 Kudos
Sravan_KumarUddarraju
Emerging Contributor
There is another issue which I noticed in the sample, code will create only one inner ring even though polygon has multiple rings with in it. Here need to be loop though the each �??innerBoundaryIs�?� node and add the inner rings to the polygon collection.

Regards,
Srv


private FeatureDescriptor ExtractPolygon(KMLStyle kmlStyle, XElement geomElement)
        {
            ESRI.ArcGIS.Client.Geometry.Polygon polygon = new Polygon();
            XElement boundary;

            boundary = geomElement.Element(kmlNS + "outerBoundaryIs");
            if (boundary != null)
            {
                ESRI.ArcGIS.Client.Geometry.PointCollection pts = ExtractRing(boundary);
                if (pts != null && pts.Count > 0)
                {
                    polygon.Rings.Add(pts);
                }
            }

            // create the inner rings to the polygons
            foreach (XElement item in geomElement.Nodes())
            {
                if (item.Name.LocalName == "innerBoundaryIs")
                {
                    if (item != null)
                    {
                        ESRI.ArcGIS.Client.Geometry.PointCollection pts = ExtractRing(item);
                        if (pts != null && pts.Count > 0)
                        {
                            polygon.Rings.Add(pts);
                        }
                    }
                }

               
            }

            //boundary = geomElement.Element(kmlNS + "innerBoundaryIs");
            //if (boundary != null)
            //{
            //    ESRI.ArcGIS.Client.Geometry.PointCollection pts = ExtractRing(boundary);
            //    if (pts != null && pts.Count > 0)
            //    {
            //        polygon.Rings.Add(pts);
            //    }
            //}

            // Create symbol and use style information
            PolygonSymbolDescriptor sym = new PolygonSymbolDescriptor();
            sym.PolyFill = kmlStyle.PolyFill;
            sym.PolyOutline = kmlStyle.PolyOutline;

            if (kmlStyle.PolyFillColor != null)
            {
                sym.PolyFillColor = kmlStyle.PolyFillColor;
            }

            if (kmlStyle.PolyOutline)
            {
                if (kmlStyle.LineColor != null)
                {
                    sym.LineColor = kmlStyle.LineColor;
                }
                if (kmlStyle.LineWidth != 0.0)
                {
                    sym.LineWidth = kmlStyle.LineWidth;
                }
            }

            if (polygon.Rings.Count > 0)
            {
                // Create feature descriptor from geometry and other information
                return new FeatureDescriptor()
                {
                    Geometry = polygon,
                    Symbol = sym
                };
            }

            return null;
        }
0 Kudos