This project has moved and is read-only. For the latest updates, please go here.

how to give a specified color for a region?

Topics: SharpMap v0.9 / v1.x
Aug 10, 2008 at 4:12 PM
Hi, Fortunately, I'm making progress with SharpMap and I want your help with this:

I have a map of a country, I want the map to be highlighted when the user clicks on a region

(ploygon), i.e. I want to make the polygon be colored in different (special) color because

I did, I could get the region that the clicked point belongs to. and now I want your help to move to the next step.


using SharpMap V0.9;
Aug 11, 2008 at 10:15 AM
You need to do a combination of figuring out what was clicked:

And then, figure out how to render it:

See the How To's for more information:

Aug 12, 2008 at 11:01 AM
Hi dirkkaiser, Ok. I succeeded in coloring the full map each province with a different color,

but I want to disable themeing for a moment, and it will be all black of course

and I want to use the MouseDown event to color a specified province but the function (GetCountryStyle)

cannot be invoked if we invoke it from MouseDown event (for the map image control), it is just could not be invoked,

I tried to put a breakpoint, but it does not stop executing. It just goes on...

what is the solution?
Aug 13, 2008 at 5:06 AM
I would suggest keeping track of your mouse coordinates over the map via the MouseMove event like:

        private void mapImage1_MouseMove(SharpMap.Geometries.Point WorldPos, MouseEventArgs ImagePos)
            //use to get the coordinates and put to form level variable
            ptX = WorldPos.X;
            ptY = WorldPos.Y;

Then on your MouseDown event, do a mapImage1.Refresh(); which will trigger your GetCountryStyle event.  You can then use the x/y coordinates that you saved above to figure out which shape is below the mouse so you can render it like you want.  To do the figuring out of what is underneath those coordinates, I use the following function which uses the NTS library (be sure to reference the library in your project).   You will need to modify the below code for your shapefile attributes obviously:

        /// <summary>
        /// Returns the HASC code for a given lat/lon
        /// </summary>
        /// <param name="x">lon</param>
        /// <param name="y">lat</param>
        /// <returns></returns>
        private string GetUnderlyingHASC(double x, double y)
            if (x != 0 || y != 0)
                ptX = x;
                ptY = y;
            string sHASC = "";
            SharpMap.Data.Providers.ShapeFile oShape = new SharpMap.Data.Providers.ShapeFile(layerColl[_iMap].folderpath + layerColl[_iMap].base_filename);
            uint i = 0;
            uint iFeature = (uint)oShape.GetFeatureCount();

            SharpMap.Geometries.Point oPoint = new SharpMap.Geometries.Point(ptX, ptY);
            GisSharpBlog.NetTopologySuite.Geometries.Geometry oPt = SharpMap.Converters.NTS.GeometryConverter.ToNTSGeometry(oPoint, new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory());

            // find a record
            for (i = 0; i < iFeature; i++)
                GisSharpBlog.NetTopologySuite.Geometries.Geometry oPoly = SharpMap.Converters.NTS.GeometryConverter.ToNTSGeometry(oShape.GetFeature(i).Geometry, new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory());
                if (oPt.Within(oPoly))
                    sHASC = oShape.GetFeature(i)["HASC"].ToString();
            return sHASC;