问题
I have a data.frame object which I can easily convert to a spatialpointdataframe then convert that to a spatiallinesdataframe but then when I tried to cover to a as.linnet it does not read marks
X Y roadID
1 177321.3 3378163 1
2 177321.4 3378168 1
3 177321.4 3378168 1
4 177321.5 3378177 1
5 177321.5 3378186 1
6 177321.5 3378195 1
then I make this data.frame to a SpatialPointsDataFrame
coordinates(roaDF1) <- c("X","Y")
proj4string(roaDF1)=proj4string(trtrtt)
class : SpatialPointsDataFrame
features : 100412
extent : 143516.4, 213981, 3353367, 3399153 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
variables : 1
names : roadID
min values : 1
max values : 347
then using SpatialPointsDataFrame to convert a SpatialLinesDataFrame ( found this code online )
LineXX <- lapply(split(roaDF1, roaDF1$roadID), function(x) Lines(list(Line(coordinates(x))), x$roadID[1L]))
linesXY <- SpatialLines(LineXX)
data <- data.frame(roadID = unique(roaDF1$roadID))
rownames(data) <- data$roadID
lxy <- SpatialLinesDataFrame(linesXY, data)
proj4string(lxy)=proj4string(trtrtt)
now lxy looks like this
> lxy
class : SpatialLinesDataFrame
features : 347
extent : 143516.4, 213981, 3353367, 3399153 (xmin, xmax, ymin, ymax)
crs : +proj=utm +zone=17 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
variables : 1
names : roadID
min values : 1
max values : 347
finally I want to concert this lxy to a linnet object
> W3.orig=as.linnet(lxy)
Warning messages:
1: In spatstat::linnet(vertices = V, edges = edges, sparse = TRUE) :
edge list should not join a vertex to itself; ignored
2: In as.linnet.SpatialLines(lxy) :
Internal error: could not map data frame to lines
it is not because roaDF1 has 100421 features when I make it 20K it still gives the same error.
Any help ?
回答1:
This is occurring because the line coordinate data include repeated vertices (i.e. two successive rows of coordinate data are identical).
There is a warning message about 'joining a vertex to itself' which is an important hint.
Although the message says that this was 'ignored' (i.e. repeated vertices were deleted), the code for handling marks has failed to handle this, and it gives the second warning and gives up.
I will write a fix for as.linnet.SpatialLines
to handle repeated vertices. You will still get the warning!
PS: a fix for maptools::as.linnet.SpatialLines
has been submitted to the maintainers of maptools
and I expect it will be included in the next release.
来源:https://stackoverflow.com/questions/61286168/data-frame-object-to-use-as-as-linnet-object