是否存在R函数,用于检查指定的GeoJSON对象(多边形或多多边形)是否包含指定点? [英] Is there an R function for checking if a specified GeoJSON object(polygon or multi-polygon) contains the specified point?

查看:102
本文介绍了是否存在R函数,用于检查指定的GeoJSON对象(多边形或多多边形)是否包含指定点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个点数组

{
  "Sheet1": [
    {
      "CoM ID": "1040614",
      "Genus": "Washingtonia",
      "Year Planted": "1998",
      "Latitude": "-37.81387927",
      "Longitude": "144.9817733"
    },
    {
      "CoM ID": "1663526",
      "Genus": "Banksia",
      "Year Planted": "2017",
      "Latitude": "-37.79582801",
      "Longitude": "144.9160598"
    },
    {
      "CoM ID": "1031170",
      "Genus": "Melaleuca",
      "Year Planted": "1997",
      "Latitude": "-37.82326441",
      "Longitude": "144.9305296"
    }
  ]
}

以及以下所示形式的Geojson多边形数组:

and also an array of Geojson polygon in the same form as shown below:

{"type":"FeatureCollection","features":[
{"type":"Feature","id":"01","properties":{"name":"Alabama","density":94.65},"geometry":{"type":"Polygon","coordinates":[[[-87.359296,35.00118],[-85.606675,34.984749],[-85.431413,34.124869],[-85.184951,32.859696],[-85.069935,32.580372],[-84.960397,32.421541],[-85.004212,32.322956],[-84.889196,32.262709],[-85.058981,32.13674],[-85.053504,32.01077],[-85.141136,31.840985],[-85.042551,31.539753],[-85.113751,31.27686],[-85.004212,31.003013],[-85.497137,30.997536],[-87.600282,30.997536],[-87.633143,30.86609],[-87.408589,30.674397],[-87.446927,30.510088],[-87.37025,30.427934],[-87.518128,30.280057],[-87.655051,30.247195],[-87.90699,30.411504],[-87.934375,30.657966],[-88.011052,30.685351],[-88.10416,30.499135],[-88.137022,30.318396],[-88.394438,30.367688],[-88.471115,31.895754],[-88.241084,33.796253],[-88.098683,34.891641],[-88.202745,34.995703],[-87.359296,35.00118]]]}}

我正在尝试使用R查找内部带有点的Geojson多边形. 例如,如何知道上面添加的三个点是否在多边形内部?

I'm trying to find the Geojson polygons with points inside it using R. For example how can I know if the three points I added above are inside the polygon?

我发现可能有用的功能是

The function I found may be helpful is the point.in.polygon function. but it doesn't support the Geojson format.

是否有任何R函数或任何方式对解决此问题都有用? 如果返回的是多边形的ID,将非常有帮助.

Is there any R function or any way would be useful to solve this problem? It will be really helpful if it's return is the ID of the polygon.

推荐答案

您可以使用草坪pkg,例如

you can use lawn pkg, e.g.,

x <- '{
"Sheet1": [
  {
    "CoM ID": "1040614",
    "Genus": "Washingtonia",
    "Year Planted": "1998",
    "Latitude": "-37.81387927",
    "Longitude": "144.9817733"
  },
  {
    "CoM ID": "1663526",
    "Genus": "Banksia",
    "Year Planted": "2017",
    "Latitude": "-37.79582801",
    "Longitude": "144.9160598"
  },
  {
    "CoM ID": "1031170",
    "Genus": "Melaleuca",
    "Year Planted": "1997",
    "Latitude": "-37.82326441",
    "Longitude": "144.9305296"
  }
]
}'

feature1 <- '{"type":"Feature","id":"01","properties":{"name":"Alabama","density":94.65},"geometry":{"type":"Polygon","coordinates":[[[-87.359296,35.00118],[-85.606675,34.984749],[-85.431413,34.124869],[-85.184951,32.859696],[-85.069935,32.580372],[-84.960397,32.421541],[-85.004212,32.322956],[-84.889196,32.262709],[-85.058981,32.13674],[-85.053504,32.01077],[-85.141136,31.840985],[-85.042551,31.539753],[-85.113751,31.27686],[-85.004212,31.003013],[-85.497137,30.997536],[-87.600282,30.997536],[-87.633143,30.86609],[-87.408589,30.674397],[-87.446927,30.510088],[-87.37025,30.427934],[-87.518128,30.280057],[-87.655051,30.247195],[-87.90699,30.411504],[-87.934375,30.657966],[-88.011052,30.685351],[-88.10416,30.499135],[-88.137022,30.318396],[-88.394438,30.367688],[-88.471115,31.895754],[-88.241084,33.796253],[-88.098683,34.891641],[-88.202745,34.995703],[-87.359296,35.00118]]]}}'

做一个测试:

lawn_boolean_contains(as.feature(feature1), lawn_point('[144.9817733,-37.81387927]'))
#> FALSE

一次全部

apply(jsonlite::fromJSON(x)$Sheet1, 1, function(z) {
  lawn_boolean_contains(
    as.feature(feature1), 
    lawn_point(sprintf("[%s,%s]", z['Longitude'], z['Latitude']))
)
})
#>     1     2     3
#> FALSE FALSE FALSE

这篇关于是否存在R函数,用于检查指定的GeoJSON对象(多边形或多多边形)是否包含指定点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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