How to highlight states/regions with different colors

Mar 30, 2010 at 11:20 PM

I am writing a BI Application for surface. Would be interested to know how to highlight /color code different regions in different colors on surface vE.

Coordinator
Mar 31, 2010 at 12:03 AM

Hi,

You'll have to get a data source for the shapes for the regions you want to highlight and then use Bing Maps 3D calls to create and add some geometry to the map.

This thread has some sample code. I haven't tested  it though.

http://bingmapswpf.codeplex.com/Thread/View.aspx?ThreadId=207747

There are also some other threads with code for other approaches if you search.

Josh

Mar 31, 2010 at 9:53 AM

This example is containing how to add a shape on top fo your map, you will require to add/modify the AddShape method on the VEMap class to create your object with the correct color, ehere is a quick writing of a method to use to add your Shape with a border color and transparent background color:

public void AddShape(VEShape shape, System.Drawing.Color color)
{
    if (shape.ShapeType == VEShapeType.Polyline)
    {
        List<LatLonAlt> lla = new List<LatLonAlt>();

        foreach (VELatLong item in shape.Points)
        {
            lla.Add(item.ToLatLonAlt());
        }
        PolyInfo polylineStyle = PolyInfo.DefaultPolygon;
        polylineStyle.AltitudeMode = AltitudeMode.FromGround;
        polylineStyle.LineHighlightColor = color;
        polylineStyle.FillColor = System.Drawing.Color.FromArgb(120, color);

        PolygonGeometry geom = new PolygonGeometry(
        layerId, Guid.NewGuid().ToString(), null,
        lla.ToArray(), PolygonGeometry.PolygonFormat.Polygon2D,
        polylineStyle);

        Items.Add(shape);
        this.globeControl.Host.Geometry.AddGeometry(geom);
    }
}

Hope this helps!

Ben.

 

Apr 1, 2010 at 4:22 AM
Hi ,First of thanks for the reply. Josh: your mix session was good.
I was able to color the states ,but the colors were kind of patchy when i move the globe. Looks like i was missing something.. Please find the below code..
///Load shape..

   private void LoadShape()
        {
            try
            {

                ShapeFile shp = new ShapeFile(@"C:\SharpMap\st99_d00.shp"); // Shape files from infragistics samples..
                shp.Open();
                FeatureDataTable dt = new FeatureDataTable();
                Collection<uint> GeomIds = new Collection<uint>();
                GeomIds = shp.GetObjectIDsInView(shp.GetExtents());


                Dictionary<string, FeatureDataRow> featureList = new Dictionary<string, FeatureDataRow>();
                foreach (uint id in GeomIds)
                {
                    FeatureDataRow dr = shp.GetFeature(id);
                    featureList.Add(dr.ItemArray[0].ToString(), dr);
                }

                AddToMap(featureList);
            }
            catch (Exception e)
            {
                string s = e.StackTrace;
            }
        }

 private void AddToMap(Dictionary<string, FeatureDataRow> featureList)
        {
            //if (row.ItemArray[0].ToString() == "SWITZERLAND")
            //{
                foreach (FeatureDataRow row in featureList.Values)
                {
                    List<PointF> points = new List<PointF>();

                    if (row.Geometry is SharpMap.Geometries.Polygon)
                    {
                        SharpMap.Geometries.Polygon polygon = row.Geometry as SharpMap.Geometries.Polygon;
                      
                        if (polygon.ExteriorRing.Vertices.Count == 0) { return; }
                       
                        VEMapShape push = new VEMapShape();
                        push.ShapeType = VEShapeType.Polyline;

                      
                      
                        foreach (var vertex in polygon.ExteriorRing.Vertices)
                        {
                            push.Points.Add(new VELatLong(vertex.Y, vertex.X));
                        }
                      
                       
                        map.AddShape(push, row.ItemArray[0].ToString());

                    }
                    else if (row.Geometry is SharpMap.Geometries.MultiPolygon)
                    {
                        MultiPolygon multiPolygon = row.Geometry as SharpMap.Geometries.MultiPolygon;
                        foreach (SharpMap.Geometries.Polygon nestedGeometry in multiPolygon.Polygons)
                        {
                            if (nestedGeometry.ExteriorRing.Vertices.Count == 0) { return; }

                            VEMapShape push = new VEMapShape();
                            push.ShapeType = VEShapeType.Polyline;
                           
                          
                            foreach (var vertex in nestedGeometry.ExteriorRing.Vertices)
                            {
                                push.Points.Add(new VELatLong(vertex.Y, vertex.X));
                            }
                            map.AddShape(push, row.ItemArray[0].ToString());
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(String.Format("shape type {0} is not supported yet.", row.Geometry.GetType().Name));
                    }
                }
         //   }

        }

   public void AddShape(VEShape shape, string layerId)
        {
            if (shape.ShapeType == VEShapeType.Polyline)
            {
                List<LatLonAlt> lla = new List<LatLonAlt>();

                foreach (VELatLong item in shape.Points)
                {
                    lla.Add(item.ToLatLonAlt());
                }

                PolyInfo polylineStyle = PolyInfo.DefaultPolygon;
                polylineStyle.AltitudeMode = AltitudeMode.FromDatum;
                Random random = new Random();
                System.Drawing.Color randomColour =System.Drawing.Color.FromArgb(75, random.Next(255), random.Next(255), random.Next(255));
                polylineStyle.FillColor = System.Drawing.Color.FromArgb(120, randomColour);
                polylineStyle.FillHighlightColor = System.Drawing.Color.FromArgb(120, randomColour);
               // polylineStyle.Filled = true;
                polylineStyle.LineStyle = LineStyle.Single;
                
                this.globeControl.Host.Geometry.AddGeometry(new PolygonGeometry(layerId, Guid.NewGuid().ToString(), lla.ToArray(), polylineStyle));

                //this.globeControl.Host.Geometry.AddGeometry(
                //    new PolylineGeometry(
                //        layerId, Guid.NewGuid().ToString(),
                //        lla.ToArray(),
                //        polylineStyle));
            }
        }
Coordinator
Apr 2, 2010 at 8:54 PM

SurfaceBI,

Thanks about my session.

Your colors may be patchy because you are drawing a flat 2D polygon in a 3D world with terrain. Where the terrain intersects the polygon, it will obscure it.

Take a look at the sample code I posted in this thread to see if it helps you. http://bingmapswpf.codeplex.com/Thread/View.aspx?ThreadId=58327

Thanks,

Josh

Apr 4, 2010 at 4:30 AM

Rocking !

One line modification to above code, it works like a charm.

this.globeControl.Host.Geometry.AddGeometry(new PolygonGeometry(layerId, Guid.NewGuid().ToString(),this, lla.ToArray(),PolygonGeometry.PolygonFormat.Polygon2D, polylineStyle));

 Thanks, joshb & JaYmZ666

 

May 20, 2010 at 12:51 PM
Edited May 20, 2010 at 12:52 PM
Hello, I was trying this out, but i'm getting some errors like this:
'Point' is an ambiguous reference between 'System.Windows.Point' and 'SharpMap.Geometries.Point'
And if i don't use that namespace i get namespace errors.

Sorry, i'm new in C#. Can you please tell me what i'm doing wrong? thanks~
Jun 4, 2010 at 2:45 PM
HistoT - In your code, you can specify which type of Point you want, by using the full class name (including the namespace of the class), like this: using System.Windows; using SharpMap.Geometries; ... System.Windows.Point myPoint = new System.Windows.Point(); SharpMap.Geometries.Point myGeoPoint = new SharpMap.Geometries.Point(); (note that I haven't looked to verify that there is a constructor that has no arguments, but that's unimportant)