6

I just want to find a straightforward, C# class that takes in a starting latitude and longitude and a distance and finds the bounding box (max lat, min lat, max lon, min lon). There are other, similar, questions here on SO but none of them really answer this and the ones that do are not in C#.

Help.

Unknown Coder
  • 6,625
  • 20
  • 79
  • 129
  • 5
    Looking through a few related posts, there appears to be more than enough information and references to piece together your own implementation in C#. If you're asking for someone to write the code for you so that you don't have to do that, you may not have much luck here. –  Jul 16 '10 at 22:27
  • Without specific links to posts I'll happily ignore your comment. I've done my search, it's not here in C# – Unknown Coder Jul 16 '10 at 22:35
  • 4
    Well, let me copy the first of the Related Links on the right side of this page for you. http://stackoverflow.com/questions/1648917/given-a-latitude-and-longitude-and-distance-i-want-to-find-a-bounding-box Poke through that and you'll find at least on reference to an implementation in Java, as well as a couple of articles on mathematical approaches to the problem. You may not find the exact implementation you want in the exact language you prefer. The idea is to get help thinking about it, not have people do the thinking and coding for you, then just use the first class that compiles. –  Jul 16 '10 at 22:54

3 Answers3

16

Here's what you are asking for. Kudos to Federico A. Ramponi who wrote the original in Python here.

public class MapPoint
{
    public double Longitude { get; set; } // In Degrees
    public double Latitude { get; set; } // In Degrees
}

public class BoundingBox
{
    public MapPoint MinPoint { get; set; }
    public MapPoint MaxPoint { get; set; }
}        

// Semi-axes of WGS-84 geoidal reference
private const double WGS84_a = 6378137.0; // Major semiaxis [m]
private const double WGS84_b = 6356752.3; // Minor semiaxis [m]

// 'halfSideInKm' is the half length of the bounding box you want in kilometers.
public static BoundingBox GetBoundingBox(MapPoint point, double halfSideInKm)
{            
    // Bounding box surrounding the point at given coordinates,
    // assuming local approximation of Earth surface as a sphere
    // of radius given by WGS84
    var lat = Deg2rad(point.Latitude);
    var lon = Deg2rad(point.Longitude);
    var halfSide = 1000 * halfSideInKm;

    // Radius of Earth at given latitude
    var radius = WGS84EarthRadius(lat);
    // Radius of the parallel at given latitude
    var pradius = radius * Math.Cos(lat);

    var latMin = lat - halfSide / radius;
    var latMax = lat + halfSide / radius;
    var lonMin = lon - halfSide / pradius;
    var lonMax = lon + halfSide / pradius;

    return new BoundingBox { 
        MinPoint = new MapPoint { Latitude = Rad2deg(latMin), Longitude = Rad2deg(lonMin) },
        MaxPoint = new MapPoint { Latitude = Rad2deg(latMax), Longitude = Rad2deg(lonMax) }
    };            
}

// degrees to radians
private static double Deg2rad(double degrees)
{
    return Math.PI * degrees / 180.0;
}

// radians to degrees
private static double Rad2deg(double radians)
{
    return 180.0 * radians / Math.PI;
}

// Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
private static double WGS84EarthRadius(double lat)
{
    // http://en.wikipedia.org/wiki/Earth_radius
    var An = WGS84_a * WGS84_a * Math.Cos(lat);
    var Bn = WGS84_b * WGS84_b * Math.Sin(lat);
    var Ad = WGS84_a * Math.Cos(lat);
    var Bd = WGS84_b * Math.Sin(lat);
    return Math.Sqrt((An*An + Bn*Bn) / (Ad*Ad + Bd*Bd));
}
Community
  • 1
  • 1
Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
5

I know I'm resurrecting a two year old question, but in the event that anyone gets here from Google, I've written a C# class library that does exactly this and a little more. I've created a Github repository to host the code and will be writing a blog post covering it shortly.

Geolocation repository

Scott
  • 13,735
  • 20
  • 94
  • 152
  • Might want to check out another project also which wraps the entire Google Maps API and not just the geocoding: https://github.com/maximnovak/google-maps. Might be worth forking – Pete Jun 04 '12 at 02:54
  • Actually this repo doesn't leverage the Google API at all. It simply uses mathematical formulas to determine distance, direction, bounding boxes, etc. However, there is a related Geocoding API I wrote (which is also on Github) that does handle geocoding locations. I'll check out the repo you mentioned though, interesting. Thanks! – Scott Jun 04 '12 at 03:00
-1

If you only have a starting latitude, longitude and altitude then there is no bounding box as you have only defined a single point in space. Any rectangle/bounding box would require at least two points to define it!

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • yeah but that's what the distance is for... to generate the bounding box around that point with distance as radius to the point – samiq Jul 28 '10 at 10:23
  • Without the information 'with distance as radius to the point' the question is meaningless as I point out. Distance when using geographic points would usually refer to the altitude (distance from the earth). If all you have are the points lat and lng and no altitude then how can you possibly compute the box to fit it!? – Fraser Jul 28 '10 at 19:57