R中多边形内的道路长度 [英] Road Length within Polygons in R

查看:72
本文介绍了R中多边形内的道路长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个道路网络的形状文件和另一个包含区域边界的形状文件.有什么更好的代码可用来获取每个多边形内部的道路长度?之前有人问过这个问题,不同之处是我想使用R而不是QGIS.我试过了:

I have a shape file of a road network and another shape-file containing area boundaries. Is there any better code that I can use to get length of roads that lies inside each polygon? This Question was asked earlier with the difference that I want to use R instead of QGIS. I tried:

intersec=intersect(roads,Polygon)

road_length=tapply(intersec$length, intersec$polygon, sum)

这可行,但是问题是,相交不会将与多边形相交的道路的长度分开,而是在相交文件中将它们加倍,并将这条道路的全长分配给两个多边形.

This works, but the problem is that the intersection does not divide the length of the roads, that cross to Polygons, but doubles them in the intersec file and assigns the full length of those roads to both Polygons.

我如何发现该问题的:没有错误消息,但是以下证明告诉我出了点问题:

How I found out about that Problem: There is no error message, but the following proove tells me that something is wrong:

a=sum(roads$length) and b=sum(intersec$length) 

a和b的长度不同-> a小于b.

a and b do not have same length -> a is smaller than b.

推荐答案

我实际上是在大约8个月前为一个项目完成的.

I actually did this for a project about 8 months ago.

我一直在研究处理空间数据的 sf 方法,因此我的解决方案使用了该包中的类,方法和函数.首先,通过在一个上使用 sf :: st_transform ,确保我的 roads shapes 都具有相同的坐标参考系统(CRS)其中.然后,我使用 sf :: st_intersection()查找交点,并在结果上使用 sf :: st_length()获得长度.此时,您可能需要汇总长度,具体取决于您的道路是否合并为一条超多线,或者每条道路是否是其自己的对象.以下给出了我认为应该起作用的要点:

I had been getting into the sf way of dealing with spatial data, and so my solution uses Classes, Methods, and functions from that package. First, I made sure both my roads and shapes had the same coordinate-reference-system (CRS) by using sf::st_transform on one of them. Then I used sf::st_intersection() to find the intersections, and used sf::st_length() on the result to get the lengths. You may need to aggregate the lengths at this point, depending on whether your roads were combined into one super-multi-line or if each road is its own object. The following gives the gist of what I think ought to work:

sf::st_intersection(road, shape) %>% # Find the intersections, which should all be points or multilines
  dplyr::mutate(len_m = sf::st_length(geom)) %>% # Find the length of each line
  dplyr::group_by(SHAPE_COLUMNS) %>% # Here you need to insert all the columns from your shapes
  dplyr::summarize(len_m = sum(len_m))

这篇关于R中多边形内的道路长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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