Differences between readOGR and st_read - r

I am starting to work with R and shapefiles, I have seen that there are two ways to load a shapefile, readOGR and st_read....could you please explain to me what is the difference between both methods and which one is more recommended to work in GIS?
thanks in advance.
PD: I am a beginner in R, usually i work with arcGis or QGis.

this depends on whether you would like to work with sf or sp (I recommend the former). Both functions read in shapefiles, but st_read reads them as class sf and readOGR from rgdal reads them as sp.
I recommend reading up on sf, and using it instead of sp, but then again, it certainly depends on what you want to do.

Related

R Social Network Analysis/Data Manipulation Question: Reading in .edges, .circles, .egofeat, .feat, and .featnames files

So I'm working with a network dataset from Stanford's SNAP Datasets and "SNAP" has wrappers for Python and C++ but not R - however, the data is still usable since I believe it's a mix of CSV files.
I can actually read in the .edges file and form an igraph object but want to read in the other files, get the attributes & add those attributes to the igraph object for analysis. I'm just confused on how to work with the .circles, .egofeat, .feat, and .featnames files since the documentation on the dataset is very scarce. Hoping someone has worked with the dataset in R or even another language and has any pointers to get started.
Thank you!

How to compute distances along a network in shapefile? in 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.

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.

Updated world map for R "maps" package?

library(maps)
1> map.where(database="world",29.392089,53.592505)
[1] "USSR"
does anyone know how I can get an updated world map database to drive this function in the maps package? I need country names only for now, not detailed sub-national administrative information such as is available in at gadm.org.
Try wrld_simpl in the maptools package.
require(maptools)
data(wrld_simpl)
plot(wrld_simpl)
## or subset based on the name
plot(wrld_simpl[wrld_simpl$NAME == "Russia", ])
## explore other attributes
summary(wrld_simpl)
I don't know how up to date it is, but ?wrld_simpl describes the source so you might find good stuff following links. Other packages that rely on sp will also be worth exploring for data.
Otherwise, there was the Rgshhs package, though I'm not sure that's still available. It came with fair detail but you can download more if you need it. It's a bit complicated, the original data is here: http://www.ngdc.noaa.gov/mgg/shorelines/gshhs.html
You can also read in data from vector formats (like MIF or SHP or PostGIS) using rgdal if you have your own data, or similar read functions in maptools for shapefile only.
Example to query the objects using points:
require(sp)
require(maptools)
data(wrld_simpl)
pts <- SpatialPoints(cbind(c(29.392089,147), c(53.592505, -35)), CRS(proj4string(wrld_simpl)))
over(pts, wrld_simpl)$NAME
For an introduction to these and other functions in sp see vignette("sp").
Also try the geonames package for more general querying of geographic names.

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