R 按线ID将空间点属性追加到空间线数据帧

R 按线ID将空间点属性追加到空间线数据帧,r,spatial,geo,sp,maptools,R,Spatial,Geo,Sp,Maptools,问GIS迷们一个问题。我有两个sp对象。空间线数据框(作为OS Roads矢量的剪裁版本)和空间点数据框(数据取自英国警察局数据网站)。我使用snapPointsToLines函数创建了第三个空间点数据帧,其中包含最近的线id的新属性 如何将第三个数据帧追加回空间线数据帧,以便在传单中将其用作颜色属性 总的来说,我要做的是将点捕捉到线作为计数。然后根据传单上的点数给线条上色。因此,如果有另一种解决方案,而不是上述解决方案,那么我将洗耳恭听 因为我使用的是传单,所以我使用的是特定的CRS crs

问GIS迷们一个问题。我有两个
sp
对象。空间线数据框(作为OS Roads矢量的剪裁版本)和空间点数据框(数据取自英国警察局数据网站)。我使用
snapPointsToLines
函数创建了第三个空间点数据帧,其中包含
最近的线id
的新属性

如何将第三个数据帧追加回空间线数据帧,以便在传单中将其用作颜色属性

总的来说,我要做的是将点捕捉到线作为计数。然后根据传单上的点数给线条上色。因此,如果有另一种解决方案,而不是上述解决方案,那么我将洗耳恭听

因为我使用的是传单,所以我使用的是特定的CRS

crs <- sp::CRS('+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0')
这两个数据集的结构就是我得到的。空间点数据框如下所示:

> str(Points)
Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 191 obs. of  16 variables:
  .. ..$ Crime type     : chr [1:191] "Anti-social behaviour" "Anti-social behaviour" "Anti-social behaviour" "Anti-social behaviour" ...
  .. ..$ Longitude      : num [1:191] 1.3 1.31 1.34 1.35 1.37 ...
  .. ..$ Latitude       : num [1:191] 51.4 51.3 51.4 51.4 51.3 ...
  .. ..$ n              : int [1:191] 1 1 1 1 1 3 1 3 4 1 ...
  .. ..$ objectid       : Factor w/ 380 levels "1","10","100",..: 50 50 50 50 50 50 50 50 50 50 ...
  .. ..$ lad17cd        : Factor w/ 380 levels "E06000001","E06000002",..: 143 143 143 143 143 143 143 143 143 143 ...
  .. ..$ lad17nm        : Factor w/ 380 levels "Aberdeen City",..: 329 329 329 329 329 329 329 329 329 329 ...
  .. ..$ lad17nmw       : Factor w/ 22 levels "Abertawe","Blaenau Gwent",..: NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ bng_e          : Factor w/ 380 levels "126473","199821",..: 376 376 376 376 376 376 376 376 376 376 ...
  .. ..$ bng_n          : Factor w/ 379 levels "1006584","101094",..: 63 63 63 63 63 63 63 63 63 63 ...
  .. ..$ long           : num [1:191] 1.33 1.33 1.33 1.33 1.33 ...
  .. ..$ lat            : num [1:191] 51.4 51.4 51.4 51.4 51.4 ...
  .. ..$ st_areasha     : num [1:191] 1.03e+08 1.03e+08 1.03e+08 1.03e+08 1.03e+08 ...
  .. ..$ st_lengths     : num [1:191] 56839 56839 56839 56839 56839 ...
  .. ..$ nearest_line_id: Factor w/ 98 levels "1043","1107",..: 48 94 31 80 90 11 61 50 68 4 ...
  .. ..$ snap_dist      : num [1:191] 0.0317 0.0386 0.0317 0.0964 0.0826 ...
  ..@ coords.nrs : num(0) 
  ..@ coords     : num [1:191, 1:2] -35712 -35265 -32984 -31745 -31519 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:191] "142.4" "142.10" "142.17" "142.26" ...
  .. .. ..$ : chr [1:2] "X" "Y"
  ..@ bbox       : num [1:2, 1:2] -38985 5714081 -25310 5720875
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "X" "Y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+proj=utm +zone=32 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0"
空间线数据框看起来是这样的(在这篇文章中,必须根据大小对其进行裁剪):


如何将
点$nearest_line_id
剪裁合并_Streets@Lines
。当一个是列,另一个是对象中的列表时?有没有办法把属性放在
@data
中?非常感谢您的指导。如果需要,我很乐意提供更多细节。

我想我已经破解了。关键是使用
sp::merge
确保将属性写回点。然后调用
@data
元素。然后它就像一个常规的合并

Points<-snapPointsToLines(Clipped_Points, Clipped_Street, maxDist=0.1, withAttrs = TRUE, idField="identifier")

Points_agg<-Points@data %>% group_by( nearest_line_id) %>% tally()

Clipped_Street@data<-sp::merge(Clipped_Street@data,Points_agg,by.x="identifier",by.y="nearest_line_id", all.x=TRUE )
点数%tally()

剪短_Street@data我想我弄坏了。关键是使用
sp::merge
确保将属性写回点。然后调用
@data
元素。然后它就像一个常规的合并

Points<-snapPointsToLines(Clipped_Points, Clipped_Street, maxDist=0.1, withAttrs = TRUE, idField="identifier")

Points_agg<-Points@data %>% group_by( nearest_line_id) %>% tally()

Clipped_Street@data<-sp::merge(Clipped_Street@data,Points_agg,by.x="identifier",by.y="nearest_line_id", all.x=TRUE )
点数%tally()

剪短_Street@dataI找到了两个可能的解决方案,但都不起作用,即使在将我的框架转换为rgeos时也是如此。我找到了两个可能的解决方案,但都不起作用,即使在将我的框架转换为rgeos时也是如此。
Points<-snapPointsToLines(Clipped_Points, Clipped_Street, maxDist=0.1, withAttrs = TRUE, idField="identifier")

Points_agg<-Points@data %>% group_by( nearest_line_id) %>% tally()

Clipped_Street@data<-sp::merge(Clipped_Street@data,Points_agg,by.x="identifier",by.y="nearest_line_id", all.x=TRUE )