基于geojson多边形限制简单特征图

时间:2020-03-07 11:01:27

标签: r ggplot2 geospatial sf

我想限制基于geojson中定义的多边形的图,以便它仅显示阴影为蓝色here的区域。

即只需画出包括环路在内的要素即可。

geonjson可用here

在边缘周围添加缓冲区以包括环城公路也很棒。

我绘制所有功能的代码(下面是geojson的限制)。

User.aggregate([
  {
    $match: {
      _id: mongoose.Types.ObjectId(req.user._id)
    }
  },
  {
    $sort: {
      "finance.expenditure.currentdate": -1
    }
  },
  {
    $unwind: "$finance.expenditure"
  },
  {
    $unwind: "$finance.customcategories"
  },
  {
    $match: {
      "finance.expenditure.status": true
    }
  },
  {
    $group: {
      _id: "$finance.expenditure.category",
      amount: {
        $addToSet: "$finance.expenditure"
      },
      customcategories: {
        $addToSet: "$finance.customcategories"
      }
    }
  },
  {
    $project: {
      _id: 0,
      "amount": {
        $sum: "$amount.amount"
      },
      "category": "$_id",
      "budget": {
        $sum: {
          $let: {
            vars: {
              budget: {
                $filter: {
                  input: "$customcategories",
                  cond: {
                    $eq: [
                      "$_id",
                      "$$this.title"
                    ]
                  }
                }
              }
            },
            in: "$$budget.budget"
          }
        }
      }
    }
  }
])
//.exec(function(err, result){})

1 个答案:

答案 0 :(得分:1)

以下,我假定已经通过运行问题中代码的前几行定义了对象streets。然后,下一步是使用read_sf()包中的sf读取多边形。下一行转换为更合适的坐标系(OSGB 1936 /英国国家网格),因为不可能在经度/纬度坐标中添加以米为单位的缓冲区。使用st_buffer()添加40米的缓冲区,最后将坐标转换回WGS84:

library(sf)
area <- read_sf("~/Birmingham CAZ 2020.GeoJSON") %>%
        st_transform(27700) %>%
        st_buffer(units::set_units(40, m)) %>%
        st_transform(4326)

当然,您需要使路径适应您实际存储文件的位置。然后,我使用st_intersection()提取streets$osm_lines中位于多边形内部的部分:

streets_area <- st_intersection(poly, streets$osm_lines)

最后,我使用您问题中的代码制作了情节。请注意,我在第二行中添加了带有多边形的图层,以证明街道确实位于多边形内:

ggplot() +
  geom_sf(data = area) +
  geom_sf(data = streets_area,
          inherit.aes = FALSE,
          color = "grey",
          size = 1) +
  theme_void() +
  theme(
    plot.background = element_rect(fill = "white"),
    legend.position = "none"
  ) +
  coord_sf(xlim = c(-1.933, -1.869),
       ylim = c(52.46,  52.496),
       expand = FALSE)

enter image description here