English 中文(简体)
发现最接近两倍
原标题:find nearest match to array of doubles
  • 时间:2010-04-15 19:49:55
  •  标签:
  • c#
  • asp.net

鉴于以下法典,我如何将物体价值清单与测试价值进行比较?

I m 建造一个地段应用。 我的病情是经过大度和自由度的,希望得到最接近这些价值观的地点的服务答案。

我开始走上将价值观转化成一种扼杀和化的道路,将价值分成两处,但这似乎是一个太小的贫民窟,我渴望找到一个更加棘手的解决办法。

public class Location : IEnumerable
{
    public string label { get; set; }
    public double lat { get; set; }
    public double lon { get; set; }

    //Implement IEnumerable
    public IEnumerator GetEnumerator()
    {
        return (IEnumerator)this;
    }

}
[HandleError]
public class HomeController : Controller
{
    private List<Location> myList = new List<Location>
 {             
    new Location {
        label="Atlanta Midtown", 
        lon=33.657674, 
        lat=-84.423130},
    new Location {
        label="Atlanta Airport", 
        lon=33.794151, 
        lat=-84.387228},
    new Location {
        label="Stamford, CT", 
        lon=41.053758, 
        lat=-73.530979}, ...
}

 public static int Main(String[] args)
 {
     string inLat = "-80.987654";
     double dblInLat = double.Parse(inLat);

     // here s where I would like to find the closest location to the inLat
     // once I figure out this, I ll implement the Longitude, and I ll be set
 }
最佳回答

我发现this,其中一人使用几种不同方法计算了全球两个距离之间的距离。 我不得不改写。 NET项目与更新的VS2008项目,但似乎行得通。 然后,我又在我的解决方案中加上该项目,并提到了该项目。

我的法典后来成为:

string inLat = "-80.987654";
string inLon = "33.521478";
var miles = GetNearestLocation(inLat, inLon);

public double GetNearestLocation(string lat, string lon)
{
    double dblInLat = double.Parse(lat);
    double dblInLon = double.Parse(lon);

    // instantiate the calculator
    GeodeticCalculator geoCalc = new GeodeticCalculator();

    // select a reference elllipsoid
    Ellipsoid reference = Ellipsoid.WGS84;

    // set user s current coordinates
    GlobalCoordinates userLocation;
    userLocation = new GlobalCoordinates(
        new Angle(dblInLon), new Angle(dblInLat)
    );

    // set example coordinates- when fully fleshed out, 
    //    this would be passed into this method
    GlobalCoordinates testLocation;
    testLocation= new GlobalCoordinates(
        new Angle(41.88253), new Angle(-87.624207) // lon, then lat
    );

    // calculate the geodetic curve
    GeodeticCurve geoCurve = geoCalc.CalculateGeodeticCurve(reference, userLocation, testLocation);
    double ellipseKilometers = geoCurve.EllipsoidalDistance / 1000.0;
    double ellipseMiles = ellipseKilometers * 0.621371192;
    /*
    Console.WriteLine("2-D path from input location to test location using WGS84");
    Console.WriteLine("   Ellipsoidal Distance: {0:0.00} kilometers ({1:0.00} miles)", ellipseKilometers, ellipseMiles);
    Console.WriteLine("   Azimuth:              {0:0.00} degrees", geoCurve.Azimuth.Degrees);
    Console.WriteLine("   Reverse Azimuth:      {0:0.00} degrees", geoCurve.ReverseAzimuth.Degrees);
    */
    return ellipseMiles;
}
问题回答

如果你不希望最终取得令人厌恶的结果,你就希望为此采用正确的距离方案:

double CalculateDistance(double lat1, double lon1, double lat2, double lon2)
{
    const double R = 6371;
    return Math.Acos(
        Math.Sin(lat1) * Math.Sin(lat2) +
        Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1)) * R;
}

我希望,按照正确的公式,我的话语在这里可能只是略微的鲁.。 所有的参数都需要在斜线上,这样,如果你重新投入学位,也写出实用方法:

double DegToRad(double deg)
{
    return deg * Math.PI / 180.0;
}

不管怎么说,在这样之后,你可以走到最短的距离:

Location GetClosestLocation(Location origin)
{
    double olatr = DegToRad(origin.Lat);
    double olonr = DegToRad(origin.Lon);
    return
        (from l in locations
         let latr = DegToRad(l.Lat)
         let lonr = DegToRad(l.Lon)
         orderby CalculateDistance(latr, lonr, olatr, olonr))
        .FirstOrDefault();
}

这从技术上说是最具成效的解决办法,因为它必须做某种工作,但是没有一种冰色的林克延伸方法可以与预测相混合。 如果你想要的话,你必须写一下自己的<条码>。 住宿:

Location GetClosestLocation(Location origin)
{
    double olatr = DegToRad(origin.Lat);
    double olonr = DegToRad(origin.Lon);
    Location closest = null;
    double minDistance = double.MaxValue;
    foreach (Location l in locations)
    {
        double latr = DegToRad(l.Lat);
        double lonr = DegToRad(l.Lon);
        double dist = CalculateDistance(latr, lonr, olatr, olonr));
        if (dist < minDistance)
        {
            minDistance = dist;
            closest = l;
        }
    }
    return closest;
}

你们需要怎样准确? 它称作大圆环。

See for example 。 http://www.movable-type.co.uk/scripts/gis-faq-5.1.html

我认为最容易的是做以下工作。 但并非大多数业绩者:

通过名单计算每个地点与贵点之间的距离。 在每一步骤中,检查一下这是否是你迄今为止所看到的最短距离,并储存。 一旦名单结束,你将存放在你储存的变量中的位置最接近。

如果你谈论大量地点,并且你计划进行许多关于这种性质的快速询问,你可能会考虑建立数据四方指数。

我希望,这是我在迅速做事之后发现的一个环节,它应当有助于远距离计算。 请参见这一链接:

rel=“nofollow noreferer” http://www.delphiforfun.org/Programs/Math_Topics/Lat-Long%20Distance.htm





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签