rspatialgeor-spr-maptools

Appending Spatial Points Attributes to Spatial Lines Data Frame by Line ID


question for GIS fans out there. I have two sp objects. A spatial lines data frame (Taken as a clipped version of the OS Roads Vector) and a spatial points data frame (Data taken from the Police UK Data website). I used the snapPointsToLines function to create a third spatial point data frame with the new attribute for nearest_line_id.

How can I append the third dataframe back to my spatial lines data frame so I can use it as a color attribute in leaflet?

What I am trying to do overall is snap points to lines as a count. Then colour the lines based on the quantity of points in leaflet. So if there is another solution other than the one above to achieve that I am all ears.

Because I am using leaflet I am using a specific CRS

crs <- sp::CRS('+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0')

However to do the snapPointsToLines function I need to temporally convert the crs to cartesian coordinates and then back (I know, its a pain).

temp_crs <- sp::CRS("+proj=utm +zone=32 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

The structure is what is getting me for the two data sets. The Spatial Points Dataframe looks like this:

> 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"

And the Spatial Lines DataFrame looks like this (had to cut it for size in this post):

> str(Clipped_Street)
Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
  ..@ data       :'data.frame': 6271 obs. of  29 variables:
  .. ..$ fictitious: Factor w/ 1 level "false": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ identifier: Factor w/ 33642 levels "0002D55A-A91E-41D7-B8ED-973F7562D903",..: 6313 22851 21972 20186 15807 23558 25474 25983 26498 25490 ...
  .. ..$ class     : Factor w/ 7 levels "A Road","B Road",..: 2 3 2 6 3 6 3 3 6 6 ...
  .. ..$ roadNumber: Factor w/ 52 levels "A2","A20","A2033",..: 32 NA 36 NA NA NA NA NA NA NA ...
  .. ..$ name1     : Factor w/ 6320 levels "Abbey Close",..: 5586 2955 5720 1695 2955 99 2955 2955 2082 5720 ...
  .. ..$ name1_lang: Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ name2     : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ name2_lang: Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ formOfWay : Factor w/ 6 levels "Collapsed Dual Carriageway",..: 5 5 5 5 5 5 5 5 5 5 ...
  .. ..$ length    : int [1:6271] 111 86 66 63 19 41 8 46 45 23 ...
  .. ..$ primary   : Factor w/ 2 levels "false","true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ trunkRoad : Factor w/ 2 levels "false","true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ loop      : Factor w/ 2 levels "false","true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ startNode : Factor w/ 21676 levels "00010DB2-DF39-455B-AA62-C3404539F18A",..: 1194 3350 8606 17475 6530 6530 5161 11442 3186 6852 ...
  .. ..$ endNode   : Factor w/ 24871 levels "00010DB2-DF39-455B-AA62-C3404539F18A",..: 5437 7429 10034 15538 5848 4003 13102 20036 13102 9098 ...
  .. ..$ structure : Factor w/ 1 level "Road In Tunnel": NA NA NA NA NA NA NA NA NA NA ...
  .. ..$ nameTOID  : Factor w/ 7826 levels "osgb4000000029278481",..: 4067 4079 4071 6680 4079 7623 4079 4079 4084 4071 ...
  .. ..$ numberTOID: Factor w/ 52 levels "osgb4000000030880859",..: 29 NA 43 NA NA NA NA NA NA NA ...
  .. ..$ function. : Factor w/ 8 levels "A Road","B Road",..: 2 5 2 4 5 4 5 5 4 5 ...
  .. ..$ 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:6271] 1.33 1.33 1.33 1.33 1.33 ...
  .. ..$ lat       : num [1:6271] 51.4 51.4 51.4 51.4 51.4 ...
  .. ..$ st_areasha: num [1:6271] 1.03e+08 1.03e+08 1.03e+08 1.03e+08 1.03e+08 ...
  .. ..$ st_lengths: num [1:6271] 56839 56839 56839 56839 56839 ...
  ..@ lines      :List of 6271
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:8, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "1"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:5, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "2"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:5, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "3"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "4"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "5"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "6"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "7"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "8"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "9"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "10"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "11"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "12"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "13"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "14"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "15"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:4, 1:2] 1.38 1.38 1.38 1.38 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "16"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "17"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:7, 1:2] 1.39 1.39 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "18"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:4, 1:2] 1.38 1.38 1.38 1.38 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "19"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:6, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "20"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "21"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "22"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:6, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "23"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:4, 1:2] 1.38 1.38 1.38 1.38 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "24"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:5, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "25"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:9, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "26"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:7, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "27"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.39 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "28"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "29"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:2, 1:2] 1.38 1.38 51.39 51.39
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "30"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:8, 1:2] 1.39 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "31"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:5, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "32"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:14, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "33"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:5, 1:2] 1.38 1.38 1.38 1.38 1.38 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "34"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "35"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1.38 1.38 1.38 51.39 51.39 ...
  .. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. .. .. .. .. ..$ : NULL
  .. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
  .. .. .. ..@ ID   : chr "36"
  .. .. [list output truncated]
  ..@ bbox       : num [1:2, 1:2] 1.21 51.31 1.45 51.39
  .. ..- 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=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

How do I merge Points$nearest_line_id with Clipped_Streets@Lines. When one is a column and the other is a list within an object? Is there any way I can put the attributes in @data instead? Very grateful for any direction. Happy to provide more detail if necessary.


Solution

  • I think I cracked it. The key is to use sp::merge to ensure that attributes get written back to points. Then invoke the @data element. then it is just like a regular merge.

    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 )
    

    The next problem is merging and splitting the dataset before or after a merge and keeping the spatial attributes, but I may put that in a new question.