【问题标题】:Algorithm - Group Geographic Points in Rows算法 - 按行分组地理点
【发布时间】:2018-12-07 18:00:39
【问题描述】:

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

一棵树的数字示例如下:

5-1

5-2

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

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

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

是否有我可以使用的现有算法?如果没有,我该怎么做才能完成这项工作?一些方向将不胜感激!

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

【问题讨论】:

  • 您正在寻找line fitting 算法。首先通过应用这种拟合算法来确定哪些点在同一条线上,哪些点不在同一条线上。然后按线的方向对线的点进行排序,并给它们编号。
  • 我已经想到了,但我首先需要一条线来比较这些点。为了得到这条线,我需要一个有序点的列表。在这里解决问题?
  • 如果树之间的距离可以是 6 米,那么可以在 (0, 0) 处有一棵树,在 (0, 6) 处有一棵树,在 (0, 12) 处有一棵树... 从(0, 6) 的观点 (0, 0) 和 (0, 12) 都在同一行,但从 (0, 0) 只有 (0, 6) 是。不知道你会如何处理。也看不懂图,点是树吗?为什么有些绿色有些红色?
  • @ceds 1. 选择一个点。 2. 搜索第一个点的最近邻。如果他们的距离是
  • 包含步骤 1、2、3 和 5 中的所有点。 7. 如果可以,从所有点的初始列表中删除这些点。 8. 重复寻找下一行,直到所有点都分配给行。

标签: c# algorithm geometry


【解决方案1】:

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

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();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多