多边形重叠百分比 [英] Polygon overlapping percentage

查看:105
本文介绍了多边形重叠百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB开发地理空间网络应用程序.我在具有不同类别(COUNTRYSTATE等)的集合上有很多多边形,我想知道哪个是某个STATECOUNTRY,但是在某些情况下,邻居COUNTRY触摸STATE的边界,因此当我查询交叉路口时,我会得到2个国家/地区.

I'm working on a geo spatial web app with MongoDB. I have a lot of polygons on a collection with different categories (COUNTRY, STATE, etc.), and I want to know which is the COUNTRY of a certain STATE but in some cases the border of a neighbour COUNTRY is touching the border of the STATE so when I query the intersection I get 2 countries.

我想计算该州与这两个国家之间的重叠百分比,以知道哪个是父母.我一直在寻找,但是我没有找到具有这种操作的任何库,而且执行这种算法的效果也不好.

I want to calculate the overlapping percentage between the state and both countries to know which one is the parent. I've been looking but I didn't found any library with this kind of operation and I'm not very good doing this kind of algorithms.

添加更多上下文

这是我正在使用的模型

type GeoEntity struct {
    ID       bson.ObjectId `json:"id" bson:"_id"`
    Type     string        `json:"type" bson:"type"` // COUNTRY, STATE, etc.
    Geometry Geometry      `json:"geometry" bson:"geometry"`
}

// GeoJSON entity
type Geometry struct {
    Type        string          `json:"type" bson:"type"`
    Coordinates [][][][]float64 `json:"coordinates" bson:"coordinates"`
}

这是我现在拥有的代码块:

And this is the chunk of code I have right now:

func findParent(state *GeoEntity) GeoEntity{
    session, err := mgo.Dial("localhost")
    check(err)
    defer session.Close()

    entities := session.DB("geo").C("entity")

    query := bson.M{
        "geometry": bson.M{
            "$geoIntersects": bson.M{
                "$geometry": state.Geometry,
            },
        },
        "type": "COUNTRY",
    }

    var countries []GeoEntity
    err = entities.Find(query).All(&countries)
    check(err)

    var parent GeoEntity

    if len(countries) > 1 {
        //TODO: parent = findTheTrueParent(countries, state) 
    } else {
        parent = countries[0]
    }

    return parent 
}

这是我遇到的问题的图像示例.何时我进行查询时得到了两个国家,一个是红色国家,另一个是绿色国家,但真正的父母是绿色国家.

And here is an image example of the problem I'm having. When I make the query I get both countries, the red and green one, but the true parent is the green one.

推荐答案

如果您可以假设一个多边形始终是另一个多边形的一个区域(完全包含在其中),则可以不使用整个多边形来计算该区域它的中心点,并在该点处使用一个点或一个很小的正方形来对父母进行测试.如果您有边界框,那么应该很容易找到中心点.

If you can assume that one polygon is always a region of another (completely contained by it), you could instead of using the entire polygon for the region just calculate the centre point of it, and use either a point or a very small square at that point to test against parents. If you have a bounding box the centre point should be easy to find.

这样,您可以避免重叠两个父对象的边出现的问题,并且只要您知道该区域在其父对象之内并且不只是与之重叠,它就适用于任何区域.

That way you avoid the problems you're seeing with edges which overlap two parents, and it should work for any region as long as you know the region is within its parent and not just overlapping it.

所以您甚至不需要两个查询,只需将state减小即可,将几何图形围绕其中心点缩小为一个小正方形,然后使用该查询进行查询.

So you may not even need two queries, just reduce state.Geometry to a small square around its centre-point and query with that instead.

这篇关于多边形重叠百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆