Reading geojson file in R - r

I have converted .shp file into geojson file using rgdal, httr, leafletR packages
file <- "province" # shp file name
arg_file <- readOGR(dsn = ".", "province") # destination require . to load from current directory
q.dat <- toGeoJSON(data = arg_file, name = "argentina")
Now I have an argentina.geojson file
How can I read the geojson file directly next time without converting the .shp file to geojson again as it is taking a lot of time.
Thanks in advance

Related

Error opening .nc files in R

I am new to R and NetCDF files. I am trying to open data on surface sea temperatures that are in a .nc file from here. My code is as follows:
rm(list=ls())
#install.packages(c("netcdf", "chron", "RColorBrewer", "lattice"))
library(chron)
library(RColorBrewer)
library(lattice)
library(ncdf4)
# set path and filename
ncpath <- "C:/Users/Mihir Sharma/Dropbox/data/"
ncname <- "20140101023458-NCEI-L3C_GHRSST-SSTskin-AVHRR_Pathfinder-PFV5.3_NOAA19_G_2014001_night-v02.0-fv01.0"
ncfname <- paste(ncpath, ncname, ".nc", sep="")
dname <- "tmp" # note: tmp means temperature (not temporary)
# open a NetCDF file
ncin <- nc_open(ncfname)
But I am getting the following error:
Error in nc_open(ncfname) :
Error in nc_open trying to open file C:/Users/Mihir Sharma/Dropbox/1 EPIC/MPA/data/20140101023458-NCEI-L3C_GHRSST-SSTskin-AVHRR_Pathfinder-PFV5.3_NOAA19_G_2014001_night-v02.0-fv01.0.nc
I have followed the code from here and here. what am I doing wrong?
Many thanks,
Mihir
Path problems when using someone else's code:
# set path and filename
ncpath <- "C:/Users/Mihir Sharma/Dropbox/data/"
When you're following code on a blog or tutorial, if it's written well, they'll use a platform independent way to describe, but often they won't.
The platform independent way to write a path is:
file.path("Users", "Mihir Sharma", "Dropbox", "data"). This will always use the correct file separator for your Platform, a value stored in .Platform$file.sep

Read in all shapefiles in a directory in R into memory

I would like to read all shapefiles in a directory into the global environment However, when I get a list of files in my working directory, the list includes both .shp and .shp.xml files, the latter of which I do not want. My draft code reads both .shp and .shp.xml files. How can I prevent it from doing so?
Draft code follows:
library(maptools)
# get all files with the .shp extension from working directory
setwd("N:/Dropbox/_BonesFirst/139_Transit_Metros_Points_Subset_by_R")
shps <- dir(getwd(), "*.shp")
# the assign function will take the string representing shp
# and turn it into a variable which holds the spatial points data
for (shp in shps) {
cat("now loading", shp, "...", '\n\r')
assign(shp, readOGR(shp))
}
EDIT: Problems seems to be in the readShapePoints. Either readOGR (from rgdal) or shapefile (from raster) work better.
Get all the files:
# replace with your folder name:
dir <- "c:/files/shpfiles"
ff <- list.files(dir, pattern="\\.shp$", full.names=TRUE)
Now read them. Easiest with raster::shapefile. Do not use readShapefile (obsolete and incomplete)
library(raster)
# first file
shapefile(ff[1])
# all of them into a list
x <- lapply(ff, shapefile)
These days, you could use "terra" and do
library(terra)
v <- vect( lapply(ff, vect) )
Going by the Title of your post, I believe you want to list all shape files from your current directory.You may want to use below.
list.files(, pattern="*.shp", full.names=TRUE)

R: Use unzip to extract raster that is downloaded to a temporary file

I am trying to download worldclim gridded climate data to a temporary file and then open it as a raster. I do not want to save the files to my computer. I can do this with zipfiles that contain only one gridded climate dataset but I can not seem to get this to work when there are many.
Thanks in advance.
temp <- tempfile()
#download worldclim climate data
download.file("http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/tmean_10m_esri.zip",temp, mode="wb")
unzip(temp,list=TRUE)#list files
#unzip and make raster, may need this as seperate steps
meanT<- raster(unzip(paste0(temp,"/tmean/tmean_9")))
unlink(temp)
Here is a solution; unzip the downloaded temporary file to a temporary directory.
temp <- tempfile()
tempd <- tempdir()
#download worldclim climate data
download.file("http://biogeo.ucdavis.edu/data/climate/worldclim/1_4/grid/cur/tmean_10m_esri.zip",temp, mode="wb")
unzip(temp, exdir=tempd)
tmean_raster <- raster(file.path(tempd, "tmean/tmean_9"))
unlink(tempd)
A direct way to get these data is:
library(raster)
wc <- getData('worldclim', var='tmean', res=10)

how to read .shp files in R Tool

link for the .shp file dataset
I want to read a .shp file from R tool, am using the following script:
>scot_LL<-readOGR("C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot/scot.shp","scot"),
but when I run the script, am getting the below errors:
>Error in file(paste(DSN, .Platform$file.sep, layer, ".dbf", sep = ""), <br/>:
cannot open the connection<br/>:
In addition: Warning message:<br/>:
In file(paste(DSN, .Platform$file.sep, layer, ".dbf", sep = ""), :<br/>:
cannot open file 'C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot/scot.dbf': No such file or directory
Can you please help me.
Here is a solution using the maptools package:
library(maptools)
scot_mp <- readShapeSpatial('C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot/scot.shp')
proj4string(scot_mp) <- "+proj=longlat +datum=WGS84" # specify projection
plot(scot_mp)
and the result:
EDIT:
Solution using the rgdal package:
scot_mp <- readOGR('C:/Documents and Settings/Admin/Desktop/scotlip/lip cancer/New Scot', 'scot')
The first argument should not necessary contain the name of shape file, but the main problem seems to be that at least the dbf file is not in your folder, all of these are needed and should be in the same folder: scot.shp, scot.dbf, scot.shx. After that it proceeds as the other solution:
proj4string(scot_mp) <- "+proj=longlat +datum=WGS84"
plot(scot_mp)

Read shape file with readOGR verses readShapePoly

I have read a shapefile using readShapePoly in the maptools package, but cannot read that same file with readOGR. I am hoping someone may be able to help me read the shapefile with readOGR.
I downloaded the file orcounty.shp from here: http://geography.uoregon.edu/geogr/topics/maps.htm
I also downloaded the associated files: orcounty.shx, orcounty.sbx, orcounty.sbn, and orcounty.dbf and put all five files in the folder: c:/users/mark w miller/gis_in_R/shapefile_example/
The following code reads the shapefile and displays some attributes:
library(maptools)
setwd('c:/users/mark w miller/gis_in_R/shapefile_example/')
# Oregon county census data (polygons)
orcounty.poly <- readShapePoly('orcounty.shp', proj4string=CRS("+proj=longlat"))
orcounty.line <- readShapeLines('orcounty.shp', proj4string=CRS("+proj=longlat"))
# see projection
summary(orcounty.poly)
Object of class SpatialPolygonsDataFrame
Coordinates:
min max
x -124.55840 -116.46944
y 41.98779 46.23626
Is projected: FALSE
proj4string : [+proj=longlat]
Data attributes:
However, when I try to read that same shapefile using the following code I receive an error:
library(rgdal)
# read shapefile
oregon.map <- readOGR(dsn="c:/users/mark w miller/gis_in_R/shapefile_example/", layer="orcounty")
# convert to dataframe
oregon.map_df <- fortify(oregon.map)
The error message says:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) :
Cannot open file
I can read Natural Earth http://www.naturalearthdata.com/ shapefiles using:
library(rgdal)
setwd("c:/users/mark w miller/gis_in_R/")
# read shapefile
wmap <- readOGR(dsn="ne_110m_physical", layer="ne_110m_land")
So, apparently there is a difference between the Natural Earth shapefiles and the Oregon shapefile orcounty.shp.
Thank you for any advice on how to read orcounty.shp with readOGR. My question is similar to the question here: rgdal / readOGR - unable to read shapefile from .zip
Try to remove your last '/' from file path.
readOGR(dsn = 'c:/users/mark w miller/gis_in_R/shapefile_example',
layer = 'orcounty')
For anyone ending up here with this error on a Linux box, I found the problem was using a home path shortcut. i.e.
# Works
readOGR(dsn="/home/user/dir", layer="file")
# Doesn't work
readOGR(dsn="~/dir", layer="file")
I have no idea why.
I used the file ne_110m_land
Try with this:
setwd('D:/JMSR/codes.R/mapas')
unzip("ne_110m_land.zip")
ogrInfo(".", "ne_110m_land")
wmap <- readOGR(".", "ne_110m_land")
raster::shapefile wraps around readOGR to take care of paths and tildes; just pass the full file name.
library(raster)
x <- shapefile("c:/users/orcounty.shp')
or
y <- shapefile("~/users/orcounty.shp")

Resources