How to compute distances along a network in shapefile? in R - r

I have a river network in a shapefile (class: "SpatialLinesDataFrame"), with some points on it (see picture below).
I would like to compute the distances between points, but along the rivers. I have been searching a lot and I am not able to find any function that allows directly that.
The closest thing I have found is the function "networkdistance" in the package "secrlinear", however I don't manage to transform my shapefile into the format required to use the function (a "linearmask" object).
Any help with this would be extremely appreciated.
Thanks in advance,
Tina.

I know this is an old thread, but just in case someone runs across this in the future: I just released an R package (riverdist) that deals with this issue, and also provides some tools for network editing and data summaries & visualization. It was written with fisheries work in mind, but could probably be applied to what you're working on, or at least that's the hope!
https://cran.r-project.org/web/packages/riverdist/vignettes/riverdist_vignette.html
Sorry this wasn't more timely -

I think we resolved this problem offline: the geographic coordinates (lat/long) of the shapefile needed to be projected before they could be used in secrlinear. That package approximates the linear network and uses igraph functions for distances.

Related

Write igraph clustering to file

I am currently testing various community detection algorithms in the igraph package to compare against my implementation.
I am able to run the algorithms on different graphs but I was wondering if there was a way for me to write the clustering to a file, where all nodes in one community are written to one line and so on. I am able to obtain the membership of each node using membership(communities_object) and write that to a file using dput() but I don't know how to write it the way I want.
This is the first time I am working with R as well. I apologize if this has been asked before.
This does not have to do much with igraph, the clustering is given by a simple numeric vector. See ?write.
write(membership(communities_object), file="myfile", ncolumns=1)
write(communities_object$membership, file="myfile", ncolumns=1) also work

Exporting GIS line vector data (roads) to R with intention to use it for network K-function

I am still a beginner to R and I have the following problem. Any tips will be highly appreciated - it will be a big help to start off.
I have a road shapefile for one country in ArcGIS, that I would like to use as a network variable in R to apply network K-function (look for point randomness on the network using spatstat package). I have never worked with this type of problem before in R and I can't figure out how to convert the vector line dataset into network variable in R. I tried to used the shapefile package, but after reading the .shp, .shx and .dbf files, I don't know how it can be used any further.
The comments above are not correct.
In the spatstat package, first convert your shapefile to an object of class "psp" (planar segment pattern) as explained in the accompanying vignette on shapefiles.
Then use the function as.linnet to convert the "psp" object to a linear network object of class "linnet".
For more explanation, see chapter 17 of the spatstat book.

Can R be used for GIS? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'd like to create some GIS plots, and I'm wondering if R can be used for this. Here are some examples of plots I'd similar in concept to those I'd like to make:
A temperature plot (or contour plot) of the United States, with color (or height) determined by state GDP. Thus, state boundaries would give discontinuities in the resulting plot.
A temperature plot of the United States where altitude is used for data. In this case, the resulting plot should vary smoothly across state boundaries.
The sum of the above 2 plots (with some scaling applied).
I'm just starting to learn R, and want to know if it would be the right tool for this kind of job. Looking at the coord_map of ggplot2, it looks like superimposing data onto the US is possible. But getting data to respect state boundaries could be very difficult.
Any advice?
First, you have the maps, mapproj and maptools packages, that give you a wide variety of map functions, projections, and so on to create about any map you can think of.
Then there is the sp package, which -among other things- allows you to plot any kind of data you load from the GADM database.
But most of all, there is the spatial projects page of R which gives you a whole lot more information, including links to mailing lists, to get going with R and spatial data. And if that's not enough, you have the CRAN Task View page for spatial data, listing 100+ packages to do what you want to do.
Think you had it now? There is more! Both books for sale and free blogs can help you finding out how to do what you want to do. And if you have some specific question, you can always come to StackOverflow , or use any of the mailing lists to get some more help.
So you see : This is R. There is no if. Only how. (Simon Blomberg)
powered by googling.
it still at alpha stage but the Rgis (composed of R packages terrain, RemoteSensing, gdistance ..) project look very promising. You can test the package on r-forge.
For raster data (DEM, altitude,...) handling there is the excellent raster package, and for other task like polygon clipping and more complicated stuff you can use rgeos (bidding of GEOS libs), maptools (for format exchange) or PBSmapping, and of course the sp package and the companion book Applied Spatial Analysis with R (Bivand, Pebsema and Rubio 2008) is a must.
On the other way, you can also link R to GIS like grass (spgrass6), saga (RSAGA), even QGIS and arcGIS but i don't use them.
finally you have to take a look at http://cran.r-project.org/web/views/Spatial.html
You might also want to look at this.
Integrating External programs with Modelbuilder
Using R in ArcGIS 10
and a thread
from Roger Bivand with useful links, advice, and some code for raster import.

Finding Large Example Graphs

I am doing a project that involves processing large, sparse graphs. Does anyone know of any publicly available data sets that can be processed into large graphs for testing? I'm looking for something like a Facebook friend network, or something a little smaller with the same flavor.
I found the Stanford Large Network Dataset Collection pretty useful.
If you asked nicely, you might be able to get Brian O'Meara's data set for treetapper. It's a pretty nice example of real-world data in that genre. Particularly, you'd probably be interested in the coauthorship data.
http://www.treetapper.org/
http://www.brianomeara.info/
Github's API is nice for building out graphs. I've messed around using the python lib networkx to generate graphs of that network. Here's some sample code if you're interested.
Apologies for the double post, evidently I can only post two links at a time since I have <10 reputation...
DIMACS also has some data sets from their cluser challenge and there's always the Graph500. The Boost Graph Library has a number of graph generators as well.
Depending on what you consider "large", there's the University of Florida Sparse Matrix Collection as well as some DIMACS Road Networks (mostly planar of course).
A few other ones:
Newman's page
Barabasi's page
Pajek software
Arena's page
Network Science

Map Data with R World Regions

Lately I have seen some cool examples of mapping in R and wanted to give this a shot. I currently have ArcView at work, but my spatial join is not working correctly (most likely user error).
Objective: I need a list of countries and what World Region they belong to. I have two layers (one country detail, the other region detail) and wanted to join the world region assignment onto each country. The join isn't working, so i figured I would come to the R community.
What are my options? This is my first attempt at doing any mapping in R and maybe there is an easier/better solution. Eventually I want to take lat/long data and map it as well.
Any insight will be much appreciated.
Brock
See the Spatial task view on CRAN, and packages like maps/mapdata, sp, rgdal, raster, blighty, rworldmap, RgoogleMaps, etc.
Do you have shapefiles you want to read? First get rgdal installed, or look at other options like maptools and shapefiles if that is difficult on your platform. Read functions in these packages will provide Spatial*DataFrame objects.
For information on the Spatial classes:
library(sp)
vignette("sp")
spatstat also has a lot of support for spatial data, and another vignette for converting to / from sp:
library(spatstat)
vignette("shapefiles")
The PBSmapping package is another good place to start. They have pretty extensive documentation and a great reference manual as well.

Resources