geojsonio::geojson_read error for a geojson file "conversion from feature type sfc_GEOMETRY to sp is not supported" - r

I downloaded a geojson file of Iraq's electrical grid from EnergyData. You can download the file yourself here:
https://development-data-hub-s3-public.s3.amazonaws.com/ddhfiles/145188/electric-network-iraq.geojson
and visit the webpage here:
https://energydata.info/dataset/iraq-electricity-transmission-network-2017/resource/4a302ef4-0d79-47db-b301-7b40293067b0
I tried to use the library geojsonio to read the file into R, but:
When I set the what argument to what = "sp" it returns the error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'addAttrToGeom': conversion from feature type sfc_GEOMETRY to sp is not supported
When I set what = "list" I get the error:
Error in rcpp_sf_to_geojson(sf, digits, factors_as_string) :
Unknown R object type
When I set what = "json" I get the error:
Error in read_json(x, parse, what, stringsAsFactors, ...) :
what='json' not supported for file and url inputs yet
Here's my code:
library(geojsonio)
obj = geojson_read("Filepath/electric-network-iraq.geojson"),
what = "sp")
I tried alternate code, which just returned an empty string:
library(spData)
obj = system.file("Filepath/electric-network-iraq.geojson",package="spData")
I know the file isn't actually empty because when I open the file up in Notepad I can see the coordinates of the electrical grid.
This is my first time ever dealing with the geojsonio package, so please be detailed in your response. I just want to read in the powerlines into R as a lines spatial object, but I'm new to these data formats.

Related

R Raster error: Cannot create a RasterLayer object from this file. (file does not exist)

Trying to pull in and scale a Landsat image but receive an error, this seems to be a somewhat common issue given the older questions and various ways of creating a RasterLayer object.
After loading the libraries I need I get to this and hit an issue:
Directory <- "D:/Geo Files/LANDSAT"
prefix <- "CU_LC08.001_"
suffix <- "_doy2020222_aid0001.tif"
## Get band 2 reflectance (blue, 0.45-0.515 micron)
sr2 <- raster(paste0(Directory,prefix,"SRB2",suffix))
Error in .local(.Object, ...) :
*Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file. (file does not exist)*
> sr2 = sr2*0.0001 #Scale
Error: object 'sr2' not found
The full name of the first (SRB2) file I want to use is:
CU_LC08.001_SRB2_doy2020222_aid0001.tif - the name of the file as it's downloaded, unchanged.
The rest of the code continues through the other files to go through each SRB and later calculate NDVI, etc
i.e. ## Get band 3 reflectance (green, 0.533-0.590 micron) sr3 <- raster(paste0(Directory,prefix,"SRB3",suffix)) sr3 = sr3*0.0001 #Scale
Edit 1: I tried using try using \ instead of forward slashes and got exactly the same error.
R version is R-4.0.3
Any help would be appreciated, this was provided to us in a hackathon and I am new to R, it should be simple to run but if the file cannot be read..?
EDIT 2: The issue was with the end of the path, it needed "\"
Directory <- "D:\\Geo Files\\LANDSAT\\"
But when trying to get another file to work from a different folder later in the code, the same issue appears, and the above fix did nothing:
Directory <- "D:\\Geo Files\\ECOSTRESS\\USCities\\"
LST_ECO <- raster(paste0(Directory,"SDS_LST_doy2020260040819_aid0001.tif"))
Error in .local(.Object, ...) :
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file. (file does not exist)
EDIT 3: Changed \Geo Files\ to "\GeoFiles\" as suggested, tried again but problem still occurs as above.
I had the same issue and the following was the process that finally worked.
# location of the file
loc_1 = "D:\\Geo Files\\ECOSTRESS\\USCities\\SDS_LST_doy2020260040819_aid0001.tif"
# check if the file can be read into r
tif1 = readr::read_file(loc_1)
It raised the following error (edited):
Error: Cannot read file "D:\\Geo Files\\ECOSTRESS\\USCities\\SDS_LST_doy2020260040819_aid0001.tif": The cloud file provider is not running.
The reason was because R could not read the file as it was in an online folder.
Confirm if the file is available offline. If it can be read by read_file function then the raster function should work.
I had the same issue
i was loading a raster file from my local storage
I added ".tif" extension to the name of the file and it worked

About my GIS question in R (from readShapePoly to sf::st_read)

I am needed to use sf package command, because readShapePoly commnad will be erased. That's neer future I know... So I want to change my code from route
thata readShapePloy to route sf::st_read. But I cannot write correct code. So I want to correct code and I am very happy, if u show correct sf package command. My now command below, thx...(I am sorry to my poor English skill,Plz over come it...)
In R, I wrote code again and again for ex, on sf::st_read command. But that show error again and again...code below nd error message below too
usa_state <- readShapePoly("usa_state.shp", IDvar = "STATE_CODE")
That is ok, but I know to change that code neer future, cuz this command is erased in neer future. So Plz show me command thata route of sf package.I tried below code but I know this is not understandable in R.
usa_state = sf::st_read("usa_state.shp", layer = "STATE_CODE")
bad code...Plz shw me correct coding!Error occured now am I...
Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
SQL execution failed, cannot open layer.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 1: SQL Expression Parsing Error: syntax error, unexpected
identifier, expecting SELECT or '('. Occurred around : "STATE_CODE"
You're almost there with usa_state = sf::st_read("usa_state.shp", layer = "STATE_CODE").
I'm guessing that STATE_CODE is a field in the usa_state.shp shapefile. You don't need to supply any field names to the st_read() function. Just use:
library(sf)
usa_state = st_read("usa_state.shp")
You'll need to make sure that the usa_state.shp file (and its associated files) are in your current working directory, or you'll need to use the full path:
usa_state = st_read("/path/to/usa_state.shp")
The sf package is well worth getting to know. It's made all of my spatial work in R much easier.

Difficulty opening a package data file of unknown type

I am trying to load the state map from the maps package into an R object. I am hoping it is a SpatialPolygonsDataFrame or something I can turn into one after I have inspected it. However I am failing at the first step – getting it into an R object. I do not know the file type.
I first tried to assign the map() output to an R object directly:
st_m <- maps::map(database = "state")
draws the map, but str(st_m) appears to do nothing, unless it is redrawing the same map.
Then I tried loading it as a dataset: st_m <- data("stateMapEnv", package="maps") but this just returns a string:
> str(stateMapEnv)
chr "R_MAP_DATA_DIR"
I opened the maps directory win-library/3.4/maps/mapdata/ and found what I think is the map file, “state.L”.
I tried reading it with scan and got an error message I do not understand:
scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
scan() expected 'a real', got '#'
I then opened the file with Notepad++. It appears to be a binary or compressed file.
So I thought it might be an R data file with an unusual extension. But my attempt to load it returned a “bad magic number” error:
st_m <- load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
bad restore file magic number (file may be corrupted) -- no data loaded
Observing that these responses have progressed from the unhelpful through the incomprehensible to the occult, I thought it best to seek assistance from the wizards of stackoverflow.
This should be able to export the 'state' or any other maps dataset for you:
library(ggplot2)
state_dataset <- map_data("state")

SparkR reading and writing dataframe issue

I have a Spark DataFrame which I want to write to my disc, I used the following code-
write.df(data_frame,"dataframe_temp.csv",source="csv",mode="overwrite",schema="true",header="true")
It got completed and I can see a new folder created with a _SUCCESS file in it.
Now when I am trying to read from the same file, using following code-
dataframe2<-read.df("dataframe_temp.csv",inferSchema="true",header="true")
I am getting following error:
ERROR RBackendHandler: loadDF on org.apache.spark.sql.api.r.SQLUtils
failed Error in invokeJava(isStatic = TRUE, className, methodName,
...) : org.apache.spark.sql.AnalysisException: Unable to infer
schema for ParquetFormat at dataframe.csv. It must be specified
manually;
I have even tried using repartition
data_frame<-repartition(data_frame,1)
Any help?
You also have to specify the source as "csv":
dataframe2<-read.df("dataframe_temp.csv", source="csv")
Regarding the header argument:
Currently there is also a bug in SparkR for Spark 2.0, where the variable arguments of the write.df function aren't passed to the options parameter (see https://issues.apache.org/jira/browse/SPARK-17442). That's why the header is not written to the csv even if you specify header="true" on write.df.
Got it solved using parquet file format, parquet file format stores the schema by default.

Saving a lattice plot

I am trying to open a device, but getting the following error:
> trellis.device(device="pdf", filename="runtime.pdf")
Error in device.call(...) : unused argument (filename = "runtime.pdf")
The same error does occur when I try to open a device with
pdf(filename="c:/R/FSM/runtime.pdf")
Is there a package that I need to load into the library?
The correct argument is file rather than filename, as in pdf(file = "myfile.pdf"). Other functions that open new devices do use the filename argument, such as jpeg(), so you need to check the help file. In general, the error that was returned Error in ...: unused argument indicates that an argument supplied in the function call is not part of the function.

Resources