I have a large polyline shapefile that needs to be dissolved. However, the examples online only relate to polygons not polylines for example gUnaryUnion. I am reading in my shapefile using st_read from the sf package. Any help would be appreciated.
If I understand your question, one option is to use dplyr.
library(sf)
library(dplyr)
# get polyline road data
temp_shapefile <- tempfile()
download.file("https://www2.census.gov/geo/tiger/TIGER2017//ROADS/tl_2017_06075_roads.zip", temp_shapefile)
temp_dir <- tempdir()
unzip(temp_shapefile, exdir = temp_dir)
sf_roads <- read_sf(file.path(temp_dir,'tl_2017_06075_roads.shp'))
Use the RTTYP field to reduce the polyline from ~4000 unique segments to 6 segments.
sf_roads_summarized <- sf_roads %>%
group_by(RTTYP) %>%
summarize()
I manged to achieve this by using st_combine.
Related
I am trying to crop a shapefile containing rivers and streams (sf.streams) by the extent of an AOI shapefile (shp.AOI) I already read in prior. I am not finding a tutorial on the web explaining this. Any help would be appreciated. I attached some code below that did not work due to me being new to R and to the sf package. I don't have any formal R training and I am learning as I am doing so sorry if this is a simple question or if I am wayyy off. I also don't know if I am supposed to be cropping and masking, I am confused on the two. What I want to do is remove any data outside the AOI to save on computing power and time because the datasets I am using are very large. THANKS!!!
shp.AOI <- readOGR(dsn="InputData/GIS/AOI", layer="AOI") %>%
spTransform(., crs.NAD83.UTM.Z10) %>%
tidy(.)
sf.streams <-
sf::st_read(file.path("InputData", "GIS", "Streams","Preprocessed","Rivers.shp"),
stringsAsFactors=F, crs=crs.NAD83.UTM.Z10) %>%
st_transform(.,aoi=shp.AOI)
I also tried........
sf.streams <-
sf::st_read(file.path("InputData", "GIS", "Streams","Preprocessed","Rivers.shp"),
stringsAsFactors=F, crs=crs.NAD83.UTM.Z10) %>%
st_crop(.,aoi=shp.AOI)
I need to add some points to the map using simple points function. The issue is that points don't add to the map. It's simple command, I follow some tutorial where adding points to the map works this way but not in my case. Plot function plots Texas choropleth properly but next line (points) doesn't add points to the map at all:
library(rgdal)
library(rgeos)
library(sp)
companies <- read.csv('geoloc_data_comp.csv', header = T, dec = ',', sep = ';')
states <- readOGR('.', 'states')
plot(states[states#data$stat_name == 'texas',])
points(companies$coords.x1, companies$coords.x2, pch = 21)
First you shoud start to avoid rgeos/rgdal because they will stop being maintains. See : https://github.com/r-spatial/evolution
sf is replacing them:
library(sp)
library(sf)
library(spData) #used because I wanted US states
# list of data in spData you have one with US states
data(package = "spData")
if you want to read shapefile or other GIS format check sf::st_read() (instead of readOGR())
# one way with sf
plot(us_states$geometry[us_states$NAME == "Texas"])
# if you want do use the sp way
us_sp <- as(us_states, "Spatial") # convert to sp
plot(us_sp[us_sp#data$NAME == "Texas",])
with sf you have the geometry in one column (see "geometry") instead of having an R S4 with nested lists (see #data and #polygones).
Before getting some points we need to check in which CRS our data are. If you do not know CRS I like this website : https://ihatecoordinatesystems.com/
You also have information in the us_states documentation: https://www.rdocumentation.org/packages/spData/versions/2.0.1/topics/us_states
Then you can use:
sp::proj4string(us_sp)
sf::st_crs(us_states)
# This is EPSG 4269 or NAD83
If you want to use points() they need to be in this coordinates system (I suspect this explain your trouble ie different CRS).
You didn't provide data points so I produced some:
library(osmdata)
#this will just download node matching the key/value place=city
some_city_in_texas <- osmdata::opq(osmdata::getbb("Texas US"),
nodes_only = TRUE) %>%
osmdata::add_osm_feature(key = "place", value = "city") %>%
osmdata::osmdata_sf() #keep them in sf format
# osmdata_sp() also exist
The class osmdata is a bit complicated but here you just need to know that some_city_in_texas$osm_points provide us with points (to test points()). Now we can check their CRS:
sf::st_crs(some_city_in_texas$osm_points)
As you can see we are in an other CRS so we need to transform it. (you will probably need to do it).
city_in_texas <- sf::st_transform(some_city_in_texas$osm_points,
4269)
sf use simple feature standard to store localization and points() want two vectors x&y. You should also check that (common cause of error): R use x/y (long/lat) and not lat/long.
Here we convert city_in_texas to just coords. (if you need to do the reverse, ie converting data frame with X/Y, into an sf object look at sf::st_as_sf())
coords_city <- sf::st_coordinates(city_in_texas)
Finally this works fine now:
plot(us_states$geometry[us_states$NAME == "Texas"])
points(coords_city, pch = 21)
Good ressources are https://r-spatial.org/ and https://geocompr.robinlovelace.net/
I have a folder with about 100 point shapefiles that are locations obtained while scat sampling of an ungulate species. I would like to merge all these point shapefiles into one shapefile in R. All the point data were in .gpx format initially which I then changed to shapefiles.
I am fairly new to R,so I am very confused as on how to do it and could not find codes that merged or combined more than a few shapefiles. Any suggestions would be much appreciated. Thanks!
Building on #M_Merciless ..
for long lists you can use
all_schools <- do.call(rbind, shapefile_list)
Or, alternatively, the very fast:
all_schools <- sf::st_as_sf(data.table::rbindlist(x))
library(sf)
list all the shapefiles within the folder location
file_list <- list.files("shapefile/folder/location", pattern = "*shp", full.names = TRUE)
read all shapefiles, store as a list
shapefile_list <- lapply(file_list, read_sf)
append the separate shapefiles, in my example there are 4 shapefiles, this can probably be improved by using a for loop or apply function for a longer list.
all_schools <- rbind(shapefile_list[[1]], shapefile_list[[2]], shapefile_list[[3]], shapefile_list[[4]])
Adding a solution that I think is "tidier"
library(fs)
library(tidyverse)
# Getting all file paths
shapefiles <- 'my/data/folder' |>
dir_ls(recurse = TRUE) |>
str_subset('.shp$')
# Loading all files
sfdf <- shapefiles |>
map(st_read) |>
bind_rows()
Admittedly, more lines of code but personally I think the code is much easier to read and comprehend this way.
I would like to load the following geospatial file in R: ftp://ftp.nodc.noaa.gov/pub/data.nodc/icoads/1930s/1930s/ICOADS_R3.0.0_1930-10.nc. The problem is that using the subsequent code I only obtain one dimension, even though I should obtain three:
require("raster")
require("ncdf4")
nc_data <- nc_open("ICOADS_R3.0.0_1930-10.nc")
id.array <- ncvar_get(nc_data, "ID")
dim(id.array)
How do I fix this?
Thank you for any comments and suggestions.
Does this give you what you expect?
library(tidync)
library(magrittr)
tfile <- tempfile(fileext = ".nc")
download.file("ftp://ftp.nodc.noaa.gov/pub/data.nodc/icoads/1930s/1930s/ICOADS_R3.0.0_1930-10.nc", tfile)
id <- tidync(tfile) %>% activate("ID") %>% hyper_tibble()
dim(id)
[1] 69779 3
tidync is only on Github: https://github.com/hypertidy/tidync
I'm wondering if there is an easy way to write a CSV of a point sf object (sf R package) that includes the coordinates.
You can use st_write(input, "output.csv") and it will write a CSV without coordinates. My hack for writing a file with coordinates is:
coords <- st_coordinates(input)
input_dat <- input %>% st_set_geometry(., NULL)
input_dat <- cbind(input_dat, coords)
But it seems there must be a simpler way.
As requested, here is the setup for the code above:
input <- data.frame(ID = 1:10, longitude = rnorm(10), latitude = rnorm(10))
input <- st_as_sf(input, coords = c("longitude", "latitude"))
I was sent to the solution by Jakub Nowosad. He pointed me to this github issue which was solved by Etienne B. Racine.
Apparently GDAL has a flag that allows you to include the coordinates. So:
st_write(input, "output.csv", layer_options = "GEOMETRY=AS_XY")
You want a different treatment for POINT simple feature geometries from all other geometry types for something as basic as as.data.frame; I consider that feature creep. I think
cbind(as.data.frame(input), st_coordinates(input))
is easy enough, for this particular case.
st_write is meant to be your portal to all GDAL drivers and nothing more, it will do nothing with the data first, or manipulate GDAL settings by itself.
There should be an easier way, I agree. The as.data.frame() method for sp objects appends the coordinates and I feel like there should be one for sf objects too. But for now, how about:
input %>%
cbind(., st_coordinates(.)) %>%
st_set_geometry(NULL)