简体   繁体   English

在R中的shapefile到神经网络

[英]shapefile to neural network in R

I need to convert a shapefile (ESRI) of roads type SpatialLinesDataFrame in a neural network in R. 我需要在R中的神经网络中转换道路类型SpatialLinesDataFrame的shapefile(ESRI)。

I do not know how to remove nodes or vertices of the shape. 我不知道如何删除形状的节点或顶点。 Determine the length of each edge between nodes. 确定节点之间每条边的长度。 With these parameters I can create the network using the packet (network). 通过这些参数,我可以使用数据包(网络)创建网络。

Summary: Input shapefile for the igraph network in R. 摘要:输入R中igraph网络的shapefile。

Thank you from the South of Chile. 谢谢智利南部。

Here is a try -- 这是一个尝试 -

library(rgdal)
library(igraph)

dsn <- system.file("vectors", package = "rgdal")[1]
sl <- readOGR(dsn=dsn, layer="kiritimati_primary_roads")
lines2xcoord <- function(lns) sapply(lns@Lines, function(l) l@coords[,1])
lines2ycoord <- function(lns) sapply(lns@Lines, function(l) l@coords[,2])

x <- unlist(sapply(sl@lines, lines2xcoord))
y <- unlist(sapply(sl@lines, lines2ycoord))

g <- graph.empty(n=length(x), directed=FALSE)
V(g)$lat <- x
V(g)$lng <- y
e <- c(t(matrix(c(head(V(g),-1),tail(V(g),-1)), ncol=2)))
add.edges(g,e)

Now g is an igraph with the lines. 现在g是带有线条的igraph。 It assumes incorrectly the lines in the shapefile to be connected, though. 但它假定shapefile中的行不正确。 Also, it does not store lat/lon in this example, but the projected coordinates. 此外,它不会在此示例中存储lat / lon,而是存储投影坐标。

The shp2graph package can convert SpatialLinesDataFrame objects into igraph objects. shp2graph包可以将SpatialLinesDataFrame对象转换为igraph对象。 Have a look at the nel2igraph function. 看看nel2igraph函数。 Here is an example taken from the help file: 以下是从帮助文件中获取的示例:

data(ORN)
rtNEL<-readshpnw(rn, ELComputed=TRUE)
#Add the edge length as the weight for graph
igr<-nel2igraph(rtNEL[[2]],rtNEL[[3]],weight=rtNEL[[4]])
plot(igr, vertex.label=NA, vertex.size=2,vertex.size2=2)
#plot(rn)

rn is a SpatialLinesDataFrame object that is first converted into a list object, and then converted into an igraph object. rn是一个SpatialLinesDataFrame对象,首先转换为列表对象,然后转换为igraph对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM