算法-按行分组地理点

时间:2018-06-28 15:22:08

标签: c# algorithm geometry

我为果园设置了一组x,y坐标(以米为单位)。我正在尝试自动对行进行分组,并从上到下对组(行)中的树编号(基于对顶部和底部的定义)。不幸的是,我无法提出解决方案。有关图片以及数据集的链接,请参见下文。

树的数字示例为:

5-1

5-2

其中5是行号,1和2是行内的树的编号。

一排树之间的距离约为6米,两排之间的距离约为12米。因此,可以使用欧几里得距离在相邻树木的距离小于7米的地方定义一行。由于行不是直线,因此无法通过y坐标来组织数据。

为了使事情变得更复杂,需要按从左到右或从右到左的顺序排列行。

是否存在可以使用的现有算法?如果没有,我该怎么做?一些方向将不胜感激!

数据: https://drive.google.com/file/d/1csLM4IpP3tMF0fqQkql6gIANHngX9A3c/view?usp=sharing

enter image description here

1 个答案:

答案 0 :(得分:1)

感谢您的所有帮助。请参阅下面的解决方案。这很混乱,但是我确信这个想法将会实现:

enter image description here

public class Group
{
    public int group;
    public int row;
    public double highestRelDistance;

    public Group(int _group)
    {
        group = _group;
    }

}

public class Tree
{
    public string name;
    public Group group = new Group(0);
    public int orderInGroup;
    public double x;
    public double y;
    public string type;
    public double relDistance;
}



public static void FitTreesToLine(List<Tree> treesList, out double m, out double c)
{
    double[] xdata = treesList.Select(x => x.x).ToArray();
    double[] ydata = treesList.Select(x => x.y).ToArray();

    Tuple<double, double> p = Fit.Line(xdata, ydata);
    double a = p.Item1; // == 10; intercept
    double b = p.Item2; // == 0.5; slope

    m = b;
    c = a;
}

public static double FindDistanceBetweenPointAndLine(double m, double c, double point_x, double point_y )
{

    double line_start_x = point_x * 0.5;
    double line_start_y = m * line_start_x + c;

    double line_end_x = point_x * 1.5;
    double line_end_y = m * line_end_x + c;

    double distance = Math.Abs((line_end_x - line_start_x) * (line_start_y - point_y) - (line_start_x - point_x) * (line_end_y - line_start_y)) /
            Math.Sqrt(Math.Pow(line_end_x - line_start_x, 2) + Math.Pow(line_end_y - line_start_y, 2));

    return (distance);

}

 public static void DoCalculations(List<Tree> treeList)
 {

    //Calculate groups
    Group curGroup = new Group(1);
    groupList.Add(curGroup);

    int searchFailures = 0;

    treeGrouping:

    List<Tree> noGroupList = treeList.Where(x => x.group.group == 0).ToList();
    List<Tree> closeTreeList = new List<Tree>();

    if (noGroupList.Count() >= 3 && searchFailures < 1000)
    {

        var refTree = noGroupList[0];
        closeTreeList.Add(refTree);
        for (int i = 1; i < noGroupList.Count(); i++)
        {

            double distance = Math.Sqrt(Math.Pow(refTree.x - noGroupList[i].x, 2) + Math.Pow(refTree.y - noGroupList[i].y, 2));

            if (distance <= 7)
            {
                closeTreeList.Add(noGroupList[i]);

                if (closeTreeList.Count() == 2)
                {
                    //Fit linear curve
                    double m = 0;
                    double c = 0;
                    FitTreesToLine(closeTreeList, out m, out c);

                    //Find all points that is close to the line in original tree list
                    for (int j = 0; j < noGroupList.Count(); j++)
                    {
                        double distanceFromLine = FindDistanceBetweenPointAndLine(m, c, noGroupList[j].x, noGroupList[j].y);

                        if (distanceFromLine <= 8)
                        {
                            noGroupList[j].group = curGroup;
                        }
                    }

                    //Iterate current group
                    curGroup = new Group(curGroup.group + 1);
                    groupList.Add(curGroup);

                    goto treeGrouping;

                }
            }
        }

        refTree.group.group = 9999999;

        //curGroup = new Group(curGroup.group + 1);
        //groupList.Add(curGroup);

        searchFailures++;
        goto treeGrouping;

    }

    //Order trees within their groups
    foreach (var group in groupList)
    {
        var groupTreeList = treeList.Where(x => x.group == group).OrderBy(x => x.y).ToList();
        for (int i = 0;i < groupTreeList.Count();i++)
        {
            groupTreeList[i].orderInGroup = i + 1;
        }
    }

    //Get max group rel distance
    foreach (var group in groupList)
    {

        var items = treeList.Where(x => x.group == group);

        if (items.Count() > 0)
        {
            group.highestRelDistance = items.OrderBy(x=>x.orderInGroup).Last().x;
        }

    }

    //Order tree groups into rows
    groupList = groupList.OrderBy(x => x.highestRelDistance).ToList();
    for (int i = 0;i < groupList.Count();i++)
    {
        var items = treeList.Where(x => x.group == groupList[i]).ToList();

        foreach (var item in items)
        {
            item.group.row = i;
        }

    }

    //Generate tree names
    foreach (var tree in treeList)
    {
        tree.name = "(" + tree.group.row.ToString().PadLeft(2,'0') + "-" + tree.orderInGroup.ToString().PadLeft(3, '0') + ")";
    }

    //Order list
    treeList = treeList.OrderBy(x => x.group.row).ThenBy(x => x.orderInGroup).ToList();

}