Error when saving an sf (data) object to file as a shapefile

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Error when saving an sf (data) object to file as a shapefile

Lom Navanyo
Hello,

I have had to merge a shapefile that I read into R as an sf object with a
.csv data containing  some variables. Now I want to save the merged data to
a file (a folder on my pc). I am however getting following error:

Error in CPL_write_ogr(obj, dsn, layer, driver,
as.character(dataset_options),  :
  Write error

Below is a snippet of code used:
library(sf)
library(dplyr)
library(ggplot2)
library(stringr)
library(rgdal)
library(sp)

parcel1 <- st_read("parcels_all.shp")
balance5 <- read.csv("Balanced_5.csv")

mergedparcel <- merge(parcel1, balance5, by=c('PARCEL_ID','CAL_YEAR'),
all.x = FALSE, all.y=TRUE)

st_write(mergedparcel,"mergedparcel.shp")

I also used the shapefile function thus:

shapefile(mergedparcel , "D:/Documents/mergedparcel.shp")
 This also gives me:
Error in shapefile(mergedparcel, "D:/Documents/
Documents/mergedparcel.shp") :
  could not find function "shapefile"

Am I doing this right?
Any suggestion to resolve this issue would be appreciated.

-----------------
Lom

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error when saving an sf (data) object to file as a shapefile

Bede-Fazekas Ákos
Dear Lom,

Since you did not provide reproducible example, I can only guess...
You used merge(, all.y = TRUE). This might produce an sf object of type
GEOMETRY and some "GEOMETRYCOLLECTION EMPTY" in the geometry column, if
some of the key values of y (balance5) are not found in the key values
of x (parcel1). ESRI shapefile cannot accept mixed geometries, this
might be the reason that you cannot write the dataset to shp format.

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences


2020.06.20. 10:43 keltezéssel, Lom Navanyo írta:

> Hello,
>
> I have had to merge a shapefile that I read into R as an sf object with a
> .csv data containing  some variables. Now I want to save the merged data to
> a file (a folder on my pc). I am however getting following error:
>
> Error in CPL_write_ogr(obj, dsn, layer, driver,
> as.character(dataset_options),  :
>    Write error
>
> Below is a snippet of code used:
> library(sf)
> library(dplyr)
> library(ggplot2)
> library(stringr)
> library(rgdal)
> library(sp)
>
> parcel1 <- st_read("parcels_all.shp")
> balance5 <- read.csv("Balanced_5.csv")
>
> mergedparcel <- merge(parcel1, balance5, by=c('PARCEL_ID','CAL_YEAR'),
> all.x = FALSE, all.y=TRUE)
>
> st_write(mergedparcel,"mergedparcel.shp")
>
> I also used the shapefile function thus:
>
> shapefile(mergedparcel , "D:/Documents/mergedparcel.shp")
>   This also gives me:
> Error in shapefile(mergedparcel, "D:/Documents/
> Documents/mergedparcel.shp") :
>    could not find function "shapefile"
>
> Am I doing this right?
> Any suggestion to resolve this issue would be appreciated.
>
> -----------------
> Lom
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error when saving an sf (data) object to file as a shapefile

Lom Navanyo
Dear Bede-Fazekas,

Thank you for the swift response.
I confirm that all key values in y (balance5) are in x (parcel1). I used
all.y=TRUE because y the IDs (key values in y) are actually a subset of x.
However, what I cannot confirm is if the key variables combination
c('PARCEL_ID','CAL_YEAR') match perfectly from the two data sets.
I therefore changed the merging as:
mergedparcel2 <- merge(parcel1, balance5, by=c('PARCEL_ID','CAL_YEAR'))

I still get the following error:
> st_write(mergedparcel2,"mergedparcel2.shp")
Writing layer `mergedparcel2' to data source `mergedparcel2.shp' using
driver `ESRI Shapefile'
Writing 4682 features with 204 fields and geometry type Multi Polygon.
Unknown field name `Act_Depthtwtr': updating a layer with improper field
name(s)?
Error in CPL_write_ogr(obj, dsn, layer, driver,
as.character(dataset_options),  :
  Write error
In addition: There were 21 warnings (use warnings() to see them)

-----------------
Lom

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error when saving an sf (data) object to file as a shapefile

Jose Ramon Martinez Batlle
In reply to this post by Lom Navanyo
Hello.

The "Write error" seems a permissions issue. Do you have write
permission to the folder you are trying to save the file? You can see
the default path by typing getwd() in the console.

Regarding the error message you got while trying to write the shape
using the shapefile function, I guess you mean the function belonging
to the raster package, which you may don't have installed in your
computer. That may be the reason why R can't find the function after
searching in your libraries.

If you type ?raster::shapefile in the console, with the raster package
installed, you will find the documentation of the function, where the
argument x (the source) is defined as "character (a file name, when
reading a shapefile) or Spatial* object (when writing a shapefile)".
So if you provide an object (which you did), it must be an object of
class Spatial*, but the one you provided seems to be an sf object. You
can try to coerce the Spatial to an sf object, but I advise against
that, because it is an unnecessary workaround when using the sf
workflow.

BTW, try to avoid using shapefile as the default format.

Best regards.



El sáb., 20 jun. 2020 a las 4:44, Lom Navanyo (<[hidden email]>) escribió:

>
> Hello,
>
> I have had to merge a shapefile that I read into R as an sf object with a
> .csv data containing  some variables. Now I want to save the merged data to
> a file (a folder on my pc). I am however getting following error:
>
> Error in CPL_write_ogr(obj, dsn, layer, driver,
> as.character(dataset_options),  :
>   Write error
>
> Below is a snippet of code used:
> library(sf)
> library(dplyr)
> library(ggplot2)
> library(stringr)
> library(rgdal)
> library(sp)
>
> parcel1 <- st_read("parcels_all.shp")
> balance5 <- read.csv("Balanced_5.csv")
>
> mergedparcel <- merge(parcel1, balance5, by=c('PARCEL_ID','CAL_YEAR'),
> all.x = FALSE, all.y=TRUE)
>
> st_write(mergedparcel,"mergedparcel.shp")
>
> I also used the shapefile function thus:
>
> shapefile(mergedparcel , "D:/Documents/mergedparcel.shp")
>  This also gives me:
> Error in shapefile(mergedparcel, "D:/Documents/
> Documents/mergedparcel.shp") :
>   could not find function "shapefile"
>
> Am I doing this right?
> Any suggestion to resolve this issue would be appreciated.
>
> -----------------
> Lom
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
José Ramón Martínez Batlle
Investigador/Profesor Universidad Autónoma de Santo Domingo (UASD)
Correo electrónico: [hidden email]
Página web: http://geografiafisica.org

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error when saving an sf (data) object to file as a shapefile

edzer
In reply to this post by Lom Navanyo


On 6/20/20 12:28 PM, Lom Navanyo wrote:
> Unknown field name `Act_Depthtwtr': updating a layer with improper field
> name(s)?

Shapefiles have funny restrictions as to the length and form of variable
names, you probably ran into that, as the message suggests.

A general advice is to not use shapefiles for writing; there are better
alternatives, geopackage comes to mind. Shapefiles make life unnecessary
complicated, you've now run into two of the many problems they have. The
days of creating shapefiles are over. If someone asks you to create
shapfiles, explain them it is a bad idea.
--
Edzer Pebesma
Institute for Geoinformatics
Heisenbergstrasse 2, 48149 Muenster, Germany
Phone: +49 251 8333081

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

pEpkey.asc (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Error when saving an sf (data) object to file as a shapefile

Tim Salabim
And for anyone looking for arguments:

http://switchfromshapefile.org/


On Sat, Jun 20, 2020, 18:20 Edzer Pebesma <[hidden email]>
wrote:

>
>
> On 6/20/20 12:28 PM, Lom Navanyo wrote:
> > Unknown field name `Act_Depthtwtr': updating a layer with improper field
> > name(s)?
>
> Shapefiles have funny restrictions as to the length and form of variable
> names, you probably ran into that, as the message suggests.
>
> A general advice is to not use shapefiles for writing; there are better
> alternatives, geopackage comes to mind. Shapefiles make life unnecessary
> complicated, you've now run into two of the many problems they have. The
> days of creating shapefiles are over. If someone asks you to create
> shapfiles, explain them it is a bad idea.
> --
> Edzer Pebesma
> Institute for Geoinformatics
> Heisenbergstrasse 2, 48149 Muenster, Germany
> Phone: +49 251 8333081
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error when saving an sf (data) object to file as a shapefile

Lom Navanyo
In reply to this post by Jose Ramon Martinez Batlle
I have write permission to the folder. I have also now called the raster
package, coerced the sf object to Spatial and could save as a shapefile.
But just as you hinted and was further explained by Edzer and Tim, the
shapefile could not be used elsewhere.

Thank you all for the support.

--------
Lom

On Sat, Jun 20, 2020 at 8:46 AM Jose Ramon Martinez Batlle <
[hidden email]> wrote:

> Hello.
>
> The "Write error" seems a permissions issue. Do you have write
> permission to the folder you are trying to save the file? You can see
> the default path by typing getwd() in the console.
>
> Regarding the error message you got while trying to write the shape
> using the shapefile function, I guess you mean the function belonging
> to the raster package, which you may don't have installed in your
> computer. That may be the reason why R can't find the function after
> searching in your libraries.
>
> If you type ?raster::shapefile in the console, with the raster package
> installed, you will find the documentation of the function, where the
> argument x (the source) is defined as "character (a file name, when
> reading a shapefile) or Spatial* object (when writing a shapefile)".
> So if you provide an object (which you did), it must be an object of
> class Spatial*, but the one you provided seems to be an sf object. You
> can try to coerce the Spatial to an sf object, but I advise against
> that, because it is an unnecessary workaround when using the sf
> workflow.
>
> BTW, try to avoid using shapefile as the default format.
>
> Best regards.
>
>
>
> El sáb., 20 jun. 2020 a las 4:44, Lom Navanyo (<[hidden email]>)
> escribió:
> >
> > Hello,
> >
> > I have had to merge a shapefile that I read into R as an sf object with a
> > .csv data containing  some variables. Now I want to save the merged data
> to
> > a file (a folder on my pc). I am however getting following error:
> >
> > Error in CPL_write_ogr(obj, dsn, layer, driver,
> > as.character(dataset_options),  :
> >   Write error
> >
> > Below is a snippet of code used:
> > library(sf)
> > library(dplyr)
> > library(ggplot2)
> > library(stringr)
> > library(rgdal)
> > library(sp)
> >
> > parcel1 <- st_read("parcels_all.shp")
> > balance5 <- read.csv("Balanced_5.csv")
> >
> > mergedparcel <- merge(parcel1, balance5, by=c('PARCEL_ID','CAL_YEAR'),
> > all.x = FALSE, all.y=TRUE)
> >
> > st_write(mergedparcel,"mergedparcel.shp")
> >
> > I also used the shapefile function thus:
> >
> > shapefile(mergedparcel , "D:/Documents/mergedparcel.shp")
> >  This also gives me:
> > Error in shapefile(mergedparcel, "D:/Documents/
> > Documents/mergedparcel.shp") :
> >   could not find function "shapefile"
> >
> > Am I doing this right?
> > Any suggestion to resolve this issue would be appreciated.
> >
> > -----------------
> > Lom
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
> --
> José Ramón Martínez Batlle
> Investigador/Profesor Universidad Autónoma de Santo Domingo (UASD)
> Correo electrónico: [hidden email]
> Página web: http://geografiafisica.org
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Error trying to stack two raster bricks with different extension and resolution

Macropipus
In reply to this post by Tim Salabim
Hi All,


I�m starting to work with the package raster and although I�ve been looking for solutions, I can�t find the way to solve my problem.


I�ve two ncdf files downloaded from Copernicus with near-bottom sea temperature data. They correspond to two different periods of time (1998-2018 & 2019) and they have both different extent and resolution. The files can be downloaded from https://www.dropbox.com/sh/orr567sjdvxjycn/AAB2zEbo5hMwRhm0YEvXY5dma?dl=0


I want to stack both files in one raster brick with a final resolution of 3km and a LAEA projection. I�ve tried several approaches but none has worked.


Any help will be very appreciated! Thanks in advance.


Juan Carlos



This is the code I have been working with
-----------------------------------------------------------------------


library(ncdf4)
library(raster)
library(proj4)

temp_9818 <-brick('temp_9818.nc')
temp_19 <-brick('temp_2019.nc')
LAEA <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"

temp_9818
#class      : RasterBrick
#dimensions : 61, 145, 8845, 252  (nrow, ncol, ncell, nlayers)
#resolution : 0.08333333, 0.08333333  (x, y)
#extent     : -11.04167, 1.041666, 40.95833, 46.04167  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

temp_19
#class      : RasterBrick
#dimensions : 181, 433, 78373, 12  (nrow, ncol, ncell, nlayers)
#resolution : 0.02777778, 0.02777778  (x, y)
#extent     : -11.01389, 1.013889, 40.98611, 46.01389  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

# Different extents
extent(temp_9818)
#xmin       : -11.04167
#xmax       : 1.041666
#ymin       : 40.95833
#ymax       : 46.04167

extent(temp_19)
#xmin       : -11.01389
#xmax       : 1.013889
#ymin       : 40.98611
#ymax       : 46.01389

##  Projecting 3km & LAEA ---
temp_9818_project <-projectRaster(temp_9818, crs=LAEA, res=3000, method="bilinear")
temp_19_project <-projectRaster(temp_19, crs=LAEA, res=3000, method="bilinear")

extent(temp_9818_project)
#xmin       : 2552554
#xmax       : 3644554
#ymin       : 2015246
#ymax       : 2792246

extent(temp_19_project)
#xmin       : 2555581
#xmax       : 3641581
#ymin       : 2017655
#ymax       : 2788655


##  Obviously, error!! ----
temp_9819_final <- stack(temp_9818_project,temp_19_project)
#Error in compareRaster(x) : different extent

## Defining new extension for cropping
newextend <- extent(c(2395000, 3400000, 2281000, 2600000))

temp_9818_project_crop <-crop(temp_9818_project, newextend)
temp_19_project_crop <-crop(temp_19_project, newextend)

temp_9819_final <- stack(temp_9818_project_crop, temp_19_project_crop) #Error again!!
#Error in compareRaster(x) : different extent

extent(temp_9818_project_crop)
#xmin       : 2552554
#xmax       : 3398554
#ymin       : 2282246
#ymax       : 2600246

extent(temp_19_project_crop)
#xmin       : 2555581
#xmax       : 3398581
#ymin       : 2281655
#ymax       : 2599655

[[elided Hotmail spam]]

-------------------------------------------------------------

________________________________
De: R-sig-Geo <[hidden email]> en nombre de Tim Salabim <[hidden email]>
Enviado: s�bado, 20 de junio de 2020 18:32
Para: Edzer Pebesma <[hidden email]>
Cc: r-sig-geo <[hidden email]>
Asunto: Re: [R-sig-Geo] Error when saving an sf (data) object to file as a shapefile

And for anyone looking for arguments:

http://switchfromshapefile.org/


On Sat, Jun 20, 2020, 18:20 Edzer Pebesma <[hidden email]>
wrote:

>
>
> On 6/20/20 12:28 PM, Lom Navanyo wrote:
> > Unknown field name `Act_Depthtwtr': updating a layer with improper field
> > name(s)?
>
> Shapefiles have funny restrictions as to the length and form of variable
> names, you probably ran into that, as the message suggests.
>
> A general advice is to not use shapefiles for writing; there are better
> alternatives, geopackage comes to mind. Shapefiles make life unnecessary
> complicated, you've now run into two of the many problems they have. The
> days of creating shapefiles are over. If someone asks you to create
> shapfiles, explain them it is a bad idea.
> --
> Edzer Pebesma
> Institute for Geoinformatics
> Heisenbergstrasse 2, 48149 Muenster, Germany
> Phone: +49 251 8333081
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

        [[alternative HTML version deleted]]


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error trying to stack two raster bricks with different extension and resolution

Bede-Fazekas Ákos
Dear Juan Carlos,

maybe the simplest solution is
1) creating an empty raster at 3 km resolution and LAEA projection,
using the desired extent,
2) then reprojecting both rasters to the projection/resolution/extent of
this empty one using projectRaster(from, to)

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2020.07.03. 19:26 keltezéssel, JC Arronte írta:

> Hi All,
>
>
> I�m starting to work with the package raster and although I�ve been looking for solutions, I can�t find the way to solve my problem.
>
>
> I�ve two ncdf files downloaded from Copernicus with near-bottom sea temperature data. They correspond to two different periods of time (1998-2018 & 2019) and they have both different extent and resolution. The files can be downloaded from https://www.dropbox.com/sh/orr567sjdvxjycn/AAB2zEbo5hMwRhm0YEvXY5dma?dl=0
>
>
> I want to stack both files in one raster brick with a final resolution of 3km and a LAEA projection. I�ve tried several approaches but none has worked.
>
>
> Any help will be very appreciated! Thanks in advance.
>
>
> Juan Carlos
>
>
>
> This is the code I have been working with
> -----------------------------------------------------------------------
>
>
> library(ncdf4)
> library(raster)
> library(proj4)
>
> temp_9818 <-brick('temp_9818.nc')
> temp_19 <-brick('temp_2019.nc')
> LAEA <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
>
> temp_9818
> #class      : RasterBrick
> #dimensions : 61, 145, 8845, 252  (nrow, ncol, ncell, nlayers)
> #resolution : 0.08333333, 0.08333333  (x, y)
> #extent     : -11.04167, 1.041666, 40.95833, 46.04167  (xmin, xmax, ymin, ymax)
> #crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
>
> temp_19
> #class      : RasterBrick
> #dimensions : 181, 433, 78373, 12  (nrow, ncol, ncell, nlayers)
> #resolution : 0.02777778, 0.02777778  (x, y)
> #extent     : -11.01389, 1.013889, 40.98611, 46.01389  (xmin, xmax, ymin, ymax)
> #crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
>
> # Different extents
> extent(temp_9818)
> #xmin       : -11.04167
> #xmax       : 1.041666
> #ymin       : 40.95833
> #ymax       : 46.04167
>
> extent(temp_19)
> #xmin       : -11.01389
> #xmax       : 1.013889
> #ymin       : 40.98611
> #ymax       : 46.01389
>
> ##  Projecting 3km & LAEA ---
> temp_9818_project <-projectRaster(temp_9818, crs=LAEA, res=3000, method="bilinear")
> temp_19_project <-projectRaster(temp_19, crs=LAEA, res=3000, method="bilinear")
>
> extent(temp_9818_project)
> #xmin       : 2552554
> #xmax       : 3644554
> #ymin       : 2015246
> #ymax       : 2792246
>
> extent(temp_19_project)
> #xmin       : 2555581
> #xmax       : 3641581
> #ymin       : 2017655
> #ymax       : 2788655
>
>
> ##  Obviously, error!! ----
> temp_9819_final <- stack(temp_9818_project,temp_19_project)
> #Error in compareRaster(x) : different extent
>
> ## Defining new extension for cropping
> newextend <- extent(c(2395000, 3400000, 2281000, 2600000))
>
> temp_9818_project_crop <-crop(temp_9818_project, newextend)
> temp_19_project_crop <-crop(temp_19_project, newextend)
>
> temp_9819_final <- stack(temp_9818_project_crop, temp_19_project_crop) #Error again!!
> #Error in compareRaster(x) : different extent
>
> extent(temp_9818_project_crop)
> #xmin       : 2552554
> #xmax       : 3398554
> #ymin       : 2282246
> #ymax       : 2600246
>
> extent(temp_19_project_crop)
> #xmin       : 2555581
> #xmax       : 3398581
> #ymin       : 2281655
> #ymax       : 2599655
>
> [[elided Hotmail spam]]
>
> -------------------------------------------------------------
>
> ________________________________
> De: R-sig-Geo <[hidden email]> en nombre de Tim Salabim <[hidden email]>
> Enviado: s�bado, 20 de junio de 2020 18:32
> Para: Edzer Pebesma <[hidden email]>
> Cc: r-sig-geo <[hidden email]>
> Asunto: Re: [R-sig-Geo] Error when saving an sf (data) object to file as a shapefile
>
> And for anyone looking for arguments:
>
> http://switchfromshapefile.org/
>
>
> On Sat, Jun 20, 2020, 18:20 Edzer Pebesma <[hidden email]>
> wrote:
>
>>
>> On 6/20/20 12:28 PM, Lom Navanyo wrote:
>>> Unknown field name `Act_Depthtwtr': updating a layer with improper field
>>> name(s)?
>> Shapefiles have funny restrictions as to the length and form of variable
>> names, you probably ran into that, as the message suggests.
>>
>> A general advice is to not use shapefiles for writing; there are better
>> alternatives, geopackage comes to mind. Shapefiles make life unnecessary
>> complicated, you've now run into two of the many problems they have. The
>> days of creating shapefiles are over. If someone asks you to create
>> shapfiles, explain them it is a bad idea.
>> --
>> Edzer Pebesma
>> Institute for Geoinformatics
>> Heisenbergstrasse 2, 48149 Muenster, Germany
>> Phone: +49 251 8333081
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>          [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
> [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error trying to stack two raster bricks with different extension and resolution

Macropipus
Dear Ákos,

It worked!!. Thank you very much for your help.

Best regards,

Juan Carlos

________________________________
De: R-sig-Geo <[hidden email]> en nombre de Bede-Fazekas Ákos <[hidden email]>
Enviado: viernes, 3 de julio de 2020 19:37
Para: [hidden email] <[hidden email]>
Asunto: Re: [R-sig-Geo] Error trying to stack two raster bricks with different extension and resolution

Dear Juan Carlos,

maybe the simplest solution is
1) creating an empty raster at 3 km resolution and LAEA projection,
using the desired extent,
2) then reprojecting both rasters to the projection/resolution/extent of
this empty one using projectRaster(from, to)

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences

2020.07.03. 19:26 keltezéssel, JC Arronte írta:

> Hi All,
>
>
> I�m starting to work with the package raster and although I�ve been looking for solutions, I can�t find the way to solve my problem.
>
>
> I�ve two ncdf files downloaded from Copernicus with near-bottom sea temperature data. They correspond to two different periods of time (1998-2018 & 2019) and they have both different extent and resolution. The files can be downloaded from https://www.dropbox.com/sh/orr567sjdvxjycn/AAB2zEbo5hMwRhm0YEvXY5dma?dl=0
>
>
> I want to stack both files in one raster brick with a final resolution of 3km and a LAEA projection. I�ve tried several approaches but none has worked.
>
>
> Any help will be very appreciated! Thanks in advance.
>
>
> Juan Carlos
>
>
>
> This is the code I have been working with
> -----------------------------------------------------------------------
>
>
> library(ncdf4)
> library(raster)
> library(proj4)
>
> temp_9818 <-brick('temp_9818.nc')
> temp_19 <-brick('temp_2019.nc')
> LAEA <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
>
> temp_9818
> #class      : RasterBrick
> #dimensions : 61, 145, 8845, 252  (nrow, ncol, ncell, nlayers)
> #resolution : 0.08333333, 0.08333333  (x, y)
> #extent     : -11.04167, 1.041666, 40.95833, 46.04167  (xmin, xmax, ymin, ymax)
> #crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
>
> temp_19
> #class      : RasterBrick
> #dimensions : 181, 433, 78373, 12  (nrow, ncol, ncell, nlayers)
> #resolution : 0.02777778, 0.02777778  (x, y)
> #extent     : -11.01389, 1.013889, 40.98611, 46.01389  (xmin, xmax, ymin, ymax)
> #crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
>
> # Different extents
> extent(temp_9818)
> #xmin       : -11.04167
> #xmax       : 1.041666
> #ymin       : 40.95833
> #ymax       : 46.04167
>
> extent(temp_19)
> #xmin       : -11.01389
> #xmax       : 1.013889
> #ymin       : 40.98611
> #ymax       : 46.01389
>
> ##  Projecting 3km & LAEA ---
> temp_9818_project <-projectRaster(temp_9818, crs=LAEA, res=3000, method="bilinear")
> temp_19_project <-projectRaster(temp_19, crs=LAEA, res=3000, method="bilinear")
>
> extent(temp_9818_project)
> #xmin       : 2552554
> #xmax       : 3644554
> #ymin       : 2015246
> #ymax       : 2792246
>
> extent(temp_19_project)
> #xmin       : 2555581
> #xmax       : 3641581
> #ymin       : 2017655
> #ymax       : 2788655
>
>
> ##  Obviously, error!! ----
> temp_9819_final <- stack(temp_9818_project,temp_19_project)
> #Error in compareRaster(x) : different extent
>
> ## Defining new extension for cropping
> newextend <- extent(c(2395000, 3400000, 2281000, 2600000))
>
> temp_9818_project_crop <-crop(temp_9818_project, newextend)
> temp_19_project_crop <-crop(temp_19_project, newextend)
>
> temp_9819_final <- stack(temp_9818_project_crop, temp_19_project_crop) #Error again!!
> #Error in compareRaster(x) : different extent
>
> extent(temp_9818_project_crop)
> #xmin       : 2552554
> #xmax       : 3398554
> #ymin       : 2282246
> #ymax       : 2600246
>
> extent(temp_19_project_crop)
> #xmin       : 2555581
> #xmax       : 3398581
> #ymin       : 2281655
> #ymax       : 2599655
>
> [[elided Hotmail spam]]
>
> -------------------------------------------------------------
>
> ________________________________
> De: R-sig-Geo <[hidden email]> en nombre de Tim Salabim <[hidden email]>
> Enviado: s�bado, 20 de junio de 2020 18:32
> Para: Edzer Pebesma <[hidden email]>
> Cc: r-sig-geo <[hidden email]>
> Asunto: Re: [R-sig-Geo] Error when saving an sf (data) object to file as a shapefile
>
> And for anyone looking for arguments:
>
> http://switchfromshapefile.org/
>
>
> On Sat, Jun 20, 2020, 18:20 Edzer Pebesma <[hidden email]>
> wrote:
>
>>
>> On 6/20/20 12:28 PM, Lom Navanyo wrote:
>>> Unknown field name `Act_Depthtwtr': updating a layer with improper field
>>> name(s)?
>> Shapefiles have funny restrictions as to the length and form of variable
>> names, you probably ran into that, as the message suggests.
>>
>> A general advice is to not use shapefiles for writing; there are better
>> alternatives, geopackage comes to mind. Shapefiles make life unnecessary
>> complicated, you've now run into two of the many problems they have. The
>> days of creating shapefiles are over. If someone asks you to create
>> shapfiles, explain them it is a bad idea.
>> --
>> Edzer Pebesma
>> Institute for Geoinformatics
>> Heisenbergstrasse 2, 48149 Muenster, Germany
>> Phone: +49 251 8333081
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>          [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo