This project has moved. For the latest updates, please go here.

ESRI Shapefiles and XAML Map Control

Sep 21, 2015 at 1:44 PM
Hi,

Is it possible to add ESRI shapefiles as an overlay to BingMapsTileLayer? And is it possible to apply opacity property to it once added?

I need to show shapefile boundaries to an area of study with Bing Maps as my map.

Appreciate your help.
Coordinator
Sep 22, 2015 at 6:45 AM
Edited Sep 22, 2015 at 6:45 AM
You can add arbitrary overlays to the map. However, you have to load the shapefile and create a geometry from its content yourself. It is not supported out of the box.
Sep 24, 2015 at 9:11 PM
I've researched some ways to load a shapefile into XAML Map Control but I still can't quite figure it. Steps taken:
  1. Open the shapefile specify the path and name (I used OpenFileDialog to navigate to the file).
  2. Then I added a method "AddShapefile" to the Mapbase. I simply wanted to get the Extent of the shapefile.
  3. Then it should load the shapefile!
Unfortunately, opening the shapefile is more complex than I thought. I created a ShapeFile "library" (sort of) the shapefile. Below is my OpenShapefile code that I'm struggling with:

private ShapeFile OpenShapeFile(string path, string name, string renderFieldName)
    {
        if (path.EndsWith(".shp", StringComparison.OrdinalIgnoreCase))
        {
            path = path.Substring(0, path.Length - 4);
        }
        else
        {
            throw new ArgumentException("path does not end in \".shp\"");
        }

        ShapeFile sf = new ShapeFile();

        sf.LoadFromFile(path);
        sf.Name = name;
        sf.MapProjectionType = this.projectionType;
        if (sf.RenderSettings != null) sf.RenderSettings.Dispose();
        sf.RenderSettings = new ShapefileLib.RenderSettings(path, renderFieldName, new System.Drawing.Font("Segoe UI", 6f));

        LoadOptimalRenderSettings(sf);
        myShapefiles.Add(sf);


        return sf;
    }

     /// <summary>
    /// Load optimal render settings
    /// </summary>
    /// <param name="sf"></param>
    protected static void LoadOptimalRenderSettings(ShapeFile sf)
    {
        System.Drawing.RectangleF r = sf.Extent;
        if (r.Top > 90 || r.Bottom < -90)
        {
            //assume projected coordinates
            sf.RenderSettings.PenWidthScale = 5;
        }
        else
        {
            System.Drawing.PointF pt = new System.Drawing.PointF(r.Left + r.Width / 2, r.Top + r.Height / 2);
            ShapefileLib.UtmCoordinate utm1 = ShapefileLib.ConversionFunctions.LLToUtm(ShapefileLib.ConversionFunctions.RefEllipse, pt.Y, pt.X);
            ShapefileLib.UtmCoordinate utm2 = utm1;
            utm2.Northing += 15;
            ShapefileLib.LatLongCoordinate ll = ShapefileLib.ConversionFunctions.UtmToLL(ShapefileLib.ConversionFunctions.RefEllipse, utm2);
            sf.RenderSettings.PenWidthScale = (float)Math.Abs(ll.Latitude - pt.Y);
        }
    }
private void RenderShapefiles()
    {
        if (this.ActualWidth <= 0 || this.ActualHeight <= 0) return;
        if (screenBuf == null || (screenBuf.Size.Width != this.ActualWidth && screenBuf.Size.Height != this.ActualHeight))
        {
            if (screenBuf != null)
            {
                screenBuf.Dispose();
            }

            int newWidth = (int)this.ActualWidth;
            int newHeight = (int)this.ActualHeight;

            var newScreenBuf = GetBitmap();

            screenBuf = new System.Drawing.Bitmap(newWidth, newHeight);
        }
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(screenBuf);

        try
        {
            int width = (int)this.ActualWidth;
            int height = (int)this.ActualHeight;

            g.Clear(MapBackColor);

            foreach (ShapeFile sf in myShapefiles)
            {
                sf.Render(g, screenBuf.Size, this._centrePoint, this._zoomLevel, this.projectionType);
            }
        }
        finally
        {
            g.Dispose();
        }
    }
Coordinator
Sep 24, 2015 at 10:02 PM
Edited Sep 24, 2015 at 10:02 PM
I'm sorry, but reading the content of an ESRI shape file hasn't got anything to do with the map control.

Please ask that question somewhere else.
Sep 25, 2015 at 4:40 PM
Hi,

Got it. Now, I'm able to read and overlay the shapefile into MapControl. However, it's not "projecting" into the map. It just sits on top of it and doesn't scale when I zoom-in or zoom-out.

Is this still a shapefile issue? Or, is there any more settings or methods I need to do to make it align with the boundaries of the map?
Coordinator
Sep 25, 2015 at 4:45 PM
Edited Sep 25, 2015 at 4:46 PM
I don't know, as you haven't shown what exactly your shapefile object is. But of course it will only transform from world to view coordinates if you use it in conjuction with e.g. a MapPolyline, or a MapOverlay or something like that. Take a look at the sample application that comes with the map control download to get an idea about which map elements are available and how they are added to a map.
Sep 25, 2015 at 10:03 PM
Hi,

I'm almost there. Is there an equivalent code for the following in MapControl:

//Set the map view for the data set
MyMap.SetView(RectangleDToLocationRect(shapefile.BoundingBox));

The above snippet is taken from: http://blogs.msdn.com/b/rbrundritt/archive/2013/11/08/esri-shapefiles-and-bing-maps-wpf.aspx
Coordinator
Sep 25, 2015 at 11:20 PM
Edited Sep 25, 2015 at 11:22 PM
In order to transform the bounding box from view to world coordinates, you would use the Map's ViewportPointToLocation method with two corners of the bounding box. An equivalent of SetView might be the ZoomMap method. Not sure if that's what you need. Again, please take a look at the sample application and the source code of the map control to get an idea of how it works.
Sep 29, 2015 at 2:52 PM
Hi,

Finally figured it out. Somehow, the shapefile projection has to be in geographic coordinate system and WGS84. I was using NAD83 shapefiles and they were not showing in the map. With GCS-WGS84, they are showing. I think both Bing and Google maps are using WGS84 (World Mercator) projection. Thought others may find this helpful.
Marked as answer by DamaDeNoche on 9/30/2015 at 11:45 AM