Exporting ESRI Silverlight Graphic Layer to Google Earth, Part 2

A while ago Sky wrote a blog post about exporting an ESRI Silverlight graphic layer to Google Earth. The post explained how to export point data to KML but recently someone inquired on how to also do line and polygon layers. In this post I will explain how to do so.

We will expand on the same code from the earlier post and will be concentrating on the KMLGraphicsConverter class. First, we will get the geometry for lines and polygons by adding the following two methods.

private static geGeometry ToKMLPolyline(Polyline geometry) 
{ 
geLineString kmlGeometry; 
List<geCoordinates>; points = new List<geCoordinates>;(); 
foreach(ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection in geometry.Paths) 
{ 
foreach (MapPoint point in pointCollection) 
{ 
points.Add(PointToCoordinate(point)); 
} 
} 
kmlGeometry = new geLineString(points); 
return kmlGeometry; 
} 
private static geGeometry ToKMLPolygon(Polygon geometry) 
{ 
geLineString kmlGeometry; 
List<geCoordinates>; points = new List<geCoordinates>;(); 
foreach (ESRI.ArcGIS.Client.Geometry.PointCollection ring in geometry.Rings) 
{ 
foreach (MapPoint point in ring) 
{ 
points.Add(PointToCoordinate(point)); 
} 
} 
kmlGeometry = new geLineString(points); 
return kmlGeometry; 
}

The method ToKMLPolyLine(PolyLine geometry) loops though the PolyLine’s point collection to create a geLineString object. For the polygon method we need to loop through each of the rings and their points to build our points list. Then we have to alter the ToKMLGeometry() method as follows:

private static geGeometry ToKMLGeometry(ESRI.ArcGIS.Client.Geometry.Geometry geometry) 
{ 
if(geometry is MapPoint) 
{ 
return ToKMLPoint(geometry as MapPoint); 
} 
else if (geometry is Polyline) 
{ 
return ToKMLPolyline(geometry as Polyline); 
} 
else if (geometry is Polygon) 
{ 
return ToKMLPolygon(geometry as Polygon); 
} 
return null; 
}

And as simple as that we can export the geometry for lines and polygons to KML but what about the symbology? In order to do that we add these following two methods.

private static geStyle ConvertToLineStyle(LineSymbol lineSym) 
{ 
geLineStyle lineStyle; 
geStyle style; 
string hashId; 
hashId = lineSym.GetHashCode().ToString(); 
style = new geStyle(hashId); 
lineStyle = new geLineStyle(); 
geColor color = new geColor(); 
// Convert Brush to Color 
color.SysColor = ((SolidColorBrush)lineSym.Color).Color; 
lineStyle.Color = color; 
lineStyle.Width = (float)lineSym.Width; 
style.LineStyle = lineStyle; 
return style; 
} 
private static geStyle ConvertToPolyStyle(FillSymbol fillSym) 
{ 
gePolyStyle polyStyle; 
geLineStyle border; 
geStyle style; 
string hashId; 
hashId = fillSym.GetHashCode().ToString(); 
style = new geStyle(hashId); 
polyStyle = new gePolyStyle(); 
geColor color = new geColor(); 
// Convert Brush to Color 
color.SysColor = ((SolidColorBrush)fillSym.Fill).Color; 
polyStyle.Color = color; 
border = new geLineStyle(); 
geColor borderColor = new geColor(); 
borderColor.SysColor = ((SolidColorBrush)fillSym.BorderBrush).Color; 
border.Color = borderColor; 
style.LineStyle = border; 
style.PolyStyle = polyStyle; 
return style; 
}  

In this case, since we do not have to deal with special marker symbol images, it is a simple task of getting the color and width for the polyline and the fill color and border for the polygon. The only thing we have left to do is change the ConvertToKMLStyle() method like so:

public static geStyleSelector ConvertToKMLStyle(Symbol symbol) 
{ 
geStyle style; 
style = null; 
if(symbol is MarkerSymbol) 
{ 
style = ConvertToPointStyle((MarkerSymbol)symbol); 
} 
else if (symbol is LineSymbol) 
{ 
style = ConvertToLineStyle((LineSymbol)symbol); 
} 
else if (symbol is FillSymbol) 
{ 
style = ConvertToPolyStyle((FillSymbol)symbol); 
} 
return style; 
}

Now we are exporting points, lines and polygons to KML along with their symbology. You can find the revised code here.

This post was written by:

Dan

Senior Associate

For more information on this post, the technologies discussed here, or Zekiah’s geospatial technology services, please e-mail us at contact@zekiah.com