azure-cosmosdb - Cosmos DB 使用 ST_Within 查询返回多边形外的点

标签 azure-cosmosdb

我使用 cosmosdb 中的查询浏览器来搜索多边形内的点,我在这个示例中的多边形是这个(可以在 geojson.io 中看到)

{
   "type": "FeatureCollection",
   "features": [
   {
       "type": "Feature",
       "geometry":{
              "type":"Polygon",
              "coordinates":   
                [[[-107.44131585954042, 24.801824950217672], 
                [-107.43361255454303, 24.801824950217672], 
                [-107.43361255454303, 24.791345071112183], 
                [-107.44131585954042, 24.791345071112183],
                [-107.44131585954042, 24.801824950217672]]]
      }, 
    "properties": {
        "name": "The Polygon"
    }
  },
  {
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [-107.437779, 24.798064]
    },
    "properties": {
        "name": "The point inside the polygon"
    }
  },
  {
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [-107.39355, 24.792837]
    },
    "properties": {
        "name": "The point offside the polygon"
    }
  }
 ]
}

但是当我在查询资源管理器中搜索时,cosmosdb 检索了我两个点 这是我的查询

SELECT * FROM root 
WHERE ST_Within(
    root["Punto"], {"type": "Polygon", "coordinates":   
    [[[-107.44131585954042, 24.801824950217672], 
      [-107.43361255454303, 24.801824950217672], 
      [-107.43361255454303, 24.791345071112183], 
      [-107.44131585954042, 24.791345071112183],
      [-107.44131585954042, 24.801824950217672]]]
      })

root["Punto"] 是一个有效的 GeoJSON 点,我用 ST_ISVALID 函数检查它,而且,如果我使用函数 ST_DISTANCE,检查点和多边形之间的距离是否大于零,数据检索到的是正确的,但不知道这种做法是否正确

这是我使用 ST_DISTANCE 的第二个查询

SELECT root.NombreUbicacion, root.Punto
from root
where ST_DISTANCE (root.Punto, {"type": "Polygon", "coordinates":   
                               [[[-107.44131585954042, 24.801824950217672], 
                               [-107.43361255454303, 24.801824950217672], 
                               [-107.43361255454303, 24.791345071112183], 
                               [-107.44131585954042, 24.791345071112183],
                               [-107.44131585954042, 24.801824950217672]]]
     }) > 0

最佳答案

我给 askcosmosdb@microsoft.com 发了邮件,他们告诉我需要重新排列多边形

The behavior that you are seeing is because the points in the polygon are in the reverse order. If you look at the documentation

"Points within a Polygon must be specified in counter-clockwise order. A Polygon specified in clockwise order represents the inverse of the region within it."

所以,我将查询更改为此,它就像一个魅力

SELECT * FROM root 
WHERE ST_Within(
root["Punto"], {"type": "Polygon", "coordinates":   
[[[-107.44131585954042, 24.801824950217672],
  [-107.44131585954042, 24.791345071112183],
  [-107.43361255454303, 24.791345071112183], 
  [-107.43361255454303, 24.801824950217672], 
  [-107.44131585954042, 24.801824950217672]]]
})

感谢 cosmosdb 团队

关于azure-cosmosdb - Cosmos DB 使用 ST_Within 查询返回多边形外的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45180570/

相关文章:

Azure Cosmos DB 要求提供存储过程的分区键

sql - Cosmos 批量读取项查询

azure - 从 Azure DocumentDB 导出数据

azure-cosmosdb - 如何通过命令行填充 CosmosDB 集合?

javascript - 如何通过 Azure Cosmos DB 中的存储过程查询数据库

Azure DocumentDB 查询在超过 1000 次调用时变慢

azure - 如果所有匹配项都被过滤掉,如何在 Azure 搜索服务查询中获取 "best match"?

azure-cosmosdb - Database.ReadAsync() 有什么用?

azure-cosmosdb - FeedOptions.EnableLowPrecisionOrderBy 属性的作用是什么

azure-cosmosdb - 在DocumentDB上使用Any进行子集合的LINQ查询问题