Hi,
I have a shape file of USA imported into my layer. I want to clip the layer along the U.S. border to prevent any drawing outside of the U.S border. I think what I need to do is to create a geometry containing all the surfaceElements in my layer and set it to the Clip property of the layer. My question is how to conver the surfaceElements to a Clip geometry? Could any one gives me an example?
Thanks
Thank you very much, It works!
Note in particular that it creates a fake path element with a known color in order to help identify the canvas in the viewport that is holding the contour geometry. You also need to make sure this element is large enough that it won't get hidden by the resolution of the map. You may be able to find a cleaner way to tag this layer if you play around with it.
unfortunately the clip property does not get propagated to the actual canvas objects that are used to render the geometry, but this sample shows you how you can put together a work around to apply the clip geometry:
public partial class MainPage : UserControl
{
public MainPage()
InitializeComponent();
}
private PathGeometry clipGeo;
private PathElement CreatePathElement(double width, Point center, Color fill, Visibility vis)
PathElement se = new PathElement();
MapPolylineCollection lines = new MapPolylineCollection();
List<Point> points = new List<Point>();
MapPolyline line = new MapPolyline();
line.Add(new Point(center.X - width, center.Y - width));
line.Add(new Point(center.X + width, center.Y - width));
line.Add(new Point(center.X + width, center.Y + width));
line.Add(new Point(center.X - width, center.Y + width));
lines.Add(line);
se.Polylines = lines;
se.Fill = new SolidColorBrush(fill);
se.Visibility = vis;
se.WorldRect = lines.GetWorldRect();
se.SetProperty("countour", true);
return se;
private MapLayer CreateFakeContourLayer(Rect worldRect)
MapLayer testLayer = new MapLayer();
Point center = new Point(worldRect.Left + (worldRect.Width / 2.0),
worldRect.Top + (worldRect.Height / 2.0));
int per = (int)(Math.Min(worldRect.Width / 2.0, worldRect.Height / 2.0) / 20.0);
for (int i = 1; i < Math.Min(worldRect.Width / 2.0, worldRect.Height / 2.0); i += per)
testLayer.Elements.Add(CreatePathElement(i, center, Colors.Red, Visibility.Visible));
testLayer.Elements.Add(CreatePathElement(20000, center, Colors.Brown, Visibility.Collapsed));
testLayer.WorldRect = worldRect;
return testLayer;
private Canvas FindContourCanvas()
Canvas contourCanvas = null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(theMap.Viewport.RootCanvas); i++)
UIElement child = (UIElement)VisualTreeHelper.GetChild(this.theMap.Viewport.RootCanvas, i);
if (child is Canvas)
Canvas canvasChild = child as Canvas;
foreach (FrameworkElement fe in canvasChild.Children)
if (fe is Polyline)
Polyline poly = fe as Polyline;
if ((poly.Stroke is SolidColorBrush) &&
(poly.Stroke as SolidColorBrush).Color == Colors.Brown &&
poly.Visibility == Visibility.Collapsed)
contourCanvas = canvasChild;
break;
return contourCanvas;
private void button1_Click(object sender, RoutedEventArgs e)
MapLayer layer = this.theMap.Layers[0];
clipGeo = new PathGeometry();
clipGeo.Figures = new PathFigureCollection();
foreach (MapElement el in layer.Elements)
SurfaceElement surfaceEl = el as SurfaceElement;
for (int pl = 0; pl < surfaceEl.Polylines.Count; pl++)
MapPolyline border = surfaceEl.Polylines[pl];
PathFigure figure = new PathFigure();
figure.StartPoint = border[0];
PolyLineSegment segment = new PolyLineSegment();
for (int p = 0; p < border.Count; p++)
Point point = border[p];
segment.Points.Add(point);
figure.Segments.Add(segment);
clipGeo.Figures.Add(figure);
theMap.Layers.Add(CreateFakeContourLayer(theMap.Layers[0].WorldRect));
Canvas contourCanvas = FindContourCanvas();
if (contourCanvas != null)
contourCanvas.Clip = clipGeo;
Let me know if you have any questions about it.
-Graham
We are drawing the contour on the top of the US Map in other layer and don’t want the contour lines go
outside of the US border.
What I think is to get the Clip geometry of the US border and set up the clip region on the drawing layer.
Please advise me how to achieve it.
Thanks.
-Hongtao
Could you provide a bit more information on what you are trying to achieve? If the states for the US are the only surface elements in the layer, why are you trying to use them as the clip boundary? I'm not entirely sure it will be valid to use the Clip property on the layer in this manner, so it will help to know what you are trying for.