dowe98261 2017-09-27 19:22
浏览 65
已采纳

多边形重叠百分比

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.


EDIT: Adding more context

This is the model I'm working with

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.

  • 写回答

1条回答 默认 最新

  • donglu9896 2017-09-28 21:17
    关注

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)