Create Kernel image results in *tif format

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

Create Kernel image results in *tif format

R-sig-geo mailing list
Dear R-sig-geo Members,

     I've like to create Kernel image results as *tif using an object of
density() function output in spatstat package. But in my example,
doesn't work when I try:

#Packages
library(spatstat)
library(raster)
library(rgdal)


#Swedishpines's data set in spatstat package
data(swedishpines)
plot(swedishpines)

#CSR with K-Ripley test
csr_pines <- envelope(swedishpines, Kest, nsim=99)
plot(csr_pines)
# r=0.75 is outside CSR

#Kernel representation using 0.75 as bandwidth
d_pines<-density(swedishpines, bw=0.75)
plot(d_pines)

#Create TIFF image
r_pines <- as(d_pines, "SpatialPixelsDataFrame")
writeGDAL(r_pines, "Pines.tif")
#

> r_pines <- as(d_pines, "SpatialPixelsDataFrame") Error in as(d_pines,
"SpatialPixelsDataFrame") : no method or default for coercing “im” to
“SpatialPixelsDataFrame”

Please any ideas for corrected this?

Thanks in advanced,

Alexandre

--
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT                      CEP: 78.200-000
Fone: (+55) 65 99686-6970 (VIVO) (+55) 65 3221-2674 (FIXO)

         [hidden email]
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: br.linkedin.com/in/alexandre-dos-santos-87961635
Mendeley:www.mendeley.com/profiles/alexandre-dos-santos6/
======================================================================


        [[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: Create Kernel image results in *tif format

Flo Betz
Dear Alexandre,

instead of converting the image to a SpatialPixelDataFrame converting to
a raster object might be an alternative.

r_pines<-raster(d_pines)
writeRaster(r_pines, "Pines.tif")

Regards,
Florian

Am 26.09.2018 um 22:25 schrieb ASANTOS via R-sig-Geo:

> Dear R-sig-geo Members,
>
>       I've like to create Kernel image results as *tif using an object of
> density() function output in spatstat package. But in my example,
> doesn't work when I try:
>
> #Packages
> library(spatstat)
> library(raster)
> library(rgdal)
>
>
> #Swedishpines's data set in spatstat package
> data(swedishpines)
> plot(swedishpines)
>
> #CSR with K-Ripley test
> csr_pines <- envelope(swedishpines, Kest, nsim=99)
> plot(csr_pines)
> # r=0.75 is outside CSR
>
> #Kernel representation using 0.75 as bandwidth
> d_pines<-density(swedishpines, bw=0.75)
> plot(d_pines)
>
> #Create TIFF image
> r_pines <- as(d_pines, "SpatialPixelsDataFrame")
> writeGDAL(r_pines, "Pines.tif")
> #
>
>> r_pines <- as(d_pines, "SpatialPixelsDataFrame") Error in as(d_pines,
> "SpatialPixelsDataFrame") : no method or default for coercing “im” to
> “SpatialPixelsDataFrame”
>
> Please any ideas for corrected this?
>
> Thanks in advanced,
>
> Alexandre
>

--
Florian Betz
Gartenstraße 13
86152 Augsburg, Deutschland

Tel.: 0176 20344096
Mail: [hidden email]

_______________________________________________
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: Create Kernel image results in *tif format

Ege Rubak
Dear Alexandre,

Indeed the solution by Florian below is very nice. If you really want to
go the other direction you can use `as.SpatialGridDataFrame.im()` from
the `maptools` package.

By the way, based on what you write, I think you mean to use the value
7.5 (=0.75m) rather than 0.75 (=0.075 m) for your kernel bandwidth.
Furthermore, the argument to set the standard deviation of the
(isotropic Gaussian) smoothing kernel in `density.ppp()` is `sigma`, so
you probably want:
     d_pines <- density(swedishpines, sigma = 7.5)

If you like to work in meters rather than decimeters you can rescale the
point pattern:
     pines <- rescale(swedishpines, s = 10, unitname = "m")
     d_pines <- density(pines, sigma = 0.75)

Regards,
Ege


On 09/27/2018 10:01 AM, Florian Betz wrote:

> Dear Alexandre,
>
> instead of converting the image to a SpatialPixelDataFrame converting to
> a raster object might be an alternative.
>
> r_pines<-raster(d_pines)
> writeRaster(r_pines, "Pines.tif")
>
> Regards,
> Florian
>
> Am 26.09.2018 um 22:25 schrieb ASANTOS via R-sig-Geo:
>> Dear R-sig-geo Members,
>>
>>       I've like to create Kernel image results as *tif using an object of
>> density() function output in spatstat package. But in my example,
>> doesn't work when I try:
>>
>> #Packages
>> library(spatstat)
>> library(raster)
>> library(rgdal)
>>
>>
>> #Swedishpines's data set in spatstat package
>> data(swedishpines)
>> plot(swedishpines)
>>
>> #CSR with K-Ripley test
>> csr_pines <- envelope(swedishpines, Kest, nsim=99)
>> plot(csr_pines)
>> # r=0.75 is outside CSR
>>
>> #Kernel representation using 0.75 as bandwidth
>> d_pines<-density(swedishpines, bw=0.75)
>> plot(d_pines)
>>
>> #Create TIFF image
>> r_pines <- as(d_pines, "SpatialPixelsDataFrame")
>> writeGDAL(r_pines, "Pines.tif")
>> #
>>
>>> r_pines <- as(d_pines, "SpatialPixelsDataFrame") Error in as(d_pines,
>> "SpatialPixelsDataFrame") : no method or default for coercing “im” to
>> “SpatialPixelsDataFrame”
>>
>> Please any ideas for corrected this?
>>
>> Thanks in advanced,
>>
>> Alexandre
>>
>

_______________________________________________
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: Create Kernel image results in *tif format

R-sig-geo mailing list
In reply to this post by Flo Betz
Thank you very much Florian,

Solve my problem!!

Best wishes,

Alexandre

--
======================================================================
Alexandre dos Santos
Proteção Florestal
IFMT - Instituto Federal de Educação, Ciência e Tecnologia de Mato Grosso
Campus Cáceres
Caixa Postal 244
Avenida dos Ramires, s/n
Bairro: Distrito Industrial
Cáceres - MT                      CEP: 78.200-000
Fone: (+55) 65 99686-6970 (VIVO) (+55) 65 3221-2674 (FIXO)

         [hidden email]
Lattes: http://lattes.cnpq.br/1360403201088680
OrcID: orcid.org/0000-0001-8232-6722
Researchgate: www.researchgate.net/profile/Alexandre_Santos10
LinkedIn: br.linkedin.com/in/alexandre-dos-santos-87961635
Mendeley:www.mendeley.com/profiles/alexandre-dos-santos6/
======================================================================

Em 27/09/2018 04:01, Florian Betz escreveu:

> Dear Alexandre,
>
> instead of converting the image to a SpatialPixelDataFrame converting
> to a raster object might be an alternative.
>
> r_pines<-raster(d_pines)
> writeRaster(r_pines, "Pines.tif")
>
> Regards,
> Florian
>
> Am 26.09.2018 um 22:25 schrieb ASANTOS via R-sig-Geo:
>> Dear R-sig-geo Members,
>>
>>       I've like to create Kernel image results as *tif using an
>> object of
>> density() function output in spatstat package. But in my example,
>> doesn't work when I try:
>>
>> #Packages
>> library(spatstat)
>> library(raster)
>> library(rgdal)
>>
>>
>> #Swedishpines's data set in spatstat package
>> data(swedishpines)
>> plot(swedishpines)
>>
>> #CSR with K-Ripley test
>> csr_pines <- envelope(swedishpines, Kest, nsim=99)
>> plot(csr_pines)
>> # r=0.75 is outside CSR
>>
>> #Kernel representation using 0.75 as bandwidth
>> d_pines<-density(swedishpines, bw=0.75)
>> plot(d_pines)
>>
>> #Create TIFF image
>> r_pines <- as(d_pines, "SpatialPixelsDataFrame")
>> writeGDAL(r_pines, "Pines.tif")
>> #
>>
>>> r_pines <- as(d_pines, "SpatialPixelsDataFrame") Error in as(d_pines,
>> "SpatialPixelsDataFrame") : no method or default for coercing “im” to
>> “SpatialPixelsDataFrame”
>>
>> Please any ideas for corrected this?
>>
>> Thanks in advanced,
>>
>> Alexandre
>>
>

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