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

Bounding Box and determine if points belong to bounding box?

Topics: Algorithms, Data Access, Web Controls, WinForms Controls
Aug 15, 2008 at 3:13 AM
Dear all

I have the following question:

I have a point in lat,lon. I would like to create a buffer around this point. Lets say 10 metter and determine the bounding box for that. Is that possible throguh SharpMap?

Then based on the bounding box i would like to make some calculations:

Like for a passed lat and lon determine which would be the best fit bounding box this point would belong to.

Is that possible?

Any examples for that?

Aug 15, 2008 at 11:53 AM
Hi Dan,

I'm having a hard time trying to figure out what it is that you are asking for.  Are you asking for some way to convert Lat/Long to meters to create your 100m box?  If so, then something along the lines of this might help you:

You could convert your point to meters, then obviously just subtract and add 100m from this to get your box coordinates.

- Dirk
Aug 15, 2008 at 2:37 PM

Greetings mate,

Thank you for the response. I will try and be more clear in my explanation. I aplogise if it has been a bit unclear.

I have a point on the map. I know the lat and lon of this point. That point is the center of a photo frame taken from an airplane. Lets say the frame is rectangular and is 40 X 20. Is it possible to construct a bounding box based on that know center location? So using the distance from the center point to the rectangle edges is it possible to work out the Top left and bottom right coordinates?

That was part one of my problem.

Part two is:

Now lets say i have a point represented in lat/lon. I also have a set of bounding boxes (top left botton right lat/lon pairs). I would like to determine what bounding box does this point belong to.

Is that possible?

Any advice would be apreciated

Aug 18, 2008 at 7:44 AM
Hi Dan,

Part One:
option 1

- Use the example code that I gave you in the previous message to convert your original point from lat/lon to UTM meters.
- Create your 40 x 20 bounding box around that point by:
   x1 = lonInMeters - 20;
   y1 = latInMeters - 10;
   x2 = lonInMeters + 20;
   y2 = latInMeters + 10;
- Figure out how to convert meters back into lat/lon  (this might help but notice the accuracy is pretty iffy for your purposes.  Probably better would be to use the Corpscom library that is in the last message that I previously gave you)
- Draw bounding box in SharpMap (or just hold them in memory if you don't want to display)

option 2

- figure out how to get keep everything in lat/lon but subtract off your meters by converting that part to lat/lon (no idea of how to do this but note that this is not just a straight-forward decimal addition/subtraction.  The number of meters per degree at the equator will be vastly different than what is is when you are near the poles where it approaches zero)
- Draw bounding box in SharpMap (or just hold in memory if you don't want to display)

Part Two:
option 1 (simpler)

- just check each of the bounding box coordinates to determine if the provided coordinate falls with the box coordinates (use this if you don't want to display the bounding boxes in SharpMap but would rather just keep them in memory).

        foreach (box b in boundingboxes)
         if (b.x1 <= lat && b.x2 >= lat)
            if (b.y1 <= lon && b.y2 >= lon)
               //found it

option 2 (slightly more difficult)

- use the NTS library and pass it a coordinate and it will return the object that lies under it those coordinates.  Here's my code to do it that you will need to modify to fit your purposes:

        /// <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;

Hope this helps get you started,