You can try fiddling a bit with:
pol2 <- rasterToPolygons(r, fun=function(x){x>6}, dissolve = TRUE)
pol3 <- gBuffer(pol2,width=1e-5, byid=FALSE)
Where the width is sufficiently small as to not affect the general shape. However, it yields very jagged polygons, which I do not know if it is what you intend.
Best wishes,
Jos? M. Blanco
El 02/05/2015 a las 12:00, r-sig-geo-request at r-project.org<mailto:r-sig-geo-request at r-project.org> escribi?:
Subject: Re: [R-sig-Geo] Delimit a polygon for the region which is>
1000 m from my raster altitude
Message-ID: <1430485336885-7588149.post at n2.nabble.com><mailto:1430485336885-7588149.post at n2.nabble.com>
Content-Type: text/plain; charset=us-ascii
Thank you very much "MacQueen, Don" for your answer.
I have followed your instructions from this script :
library(raster)
library(sp)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
# add contour
x <- rasterToContour(r,levels=500)
class(x)
plot(r)
plot(x, add=TRUE)
#export contour to shapefile
library(rgdal)
writeOGR(x, ".", "contour", driver="ESRI Shapefile")
but when I open the shapefile, is a lines not a polygons? My goal is to
have the polygon areas for altitude> 500m.
I thought to "rasterToPolygons"
library(raster)
library(sp)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
# add polygon
library(rgeos)
pol <- rasterToPolygons(r, fun=function(x){x>500})
plot(pol, add=T, col='red')
#export polygon to shapefile
library(rgdal)
writeOGR(pol, ".", "polygon", driver="ESRI Shapefile")
but the result, it displays a shapefile with polygons for each pixel
I just want a polygons to areas where are> 500m, not for each pixel.
Thank you again
--
---------------------------------------
Jos? M. Blanco-Moreno
Dept. de Biologia Vegetal (Bot?nica)
Facultat de Biologia
Universitat de Barcelona
Av. Diagonal 643
08028 Barcelona
SPAIN
---------------------------------------
phone: (+34) 934 039 871
fax: (+34) 934 112 842
---------------------------------------
L'amistat ?s com una novel?la, s?n
paraules ben dites. -Montserrat Roig
(Barcelona 1946-1991)
Aquest correu electr?nic i els annexos poden contenir informaci? confidencial o protegida legalment i est? adre?at exclusivament a la persona o entitat destinat?ria. Si no sou el destinatari final o la persona encarregada de rebre?l, no esteu autoritzat a llegir-lo, retenir-lo, modificar-lo, distribuir-lo, copiar-lo ni a revelar-ne el contingut. Si heu rebut aquest correu electr?nic per error, us preguem que n?informeu al remitent i que elimineu del sistema el missatge i el material annex que pugui contenir. Gr?cies per la vostra col?laboraci?.
Este correo electr?nico y sus anexos pueden contener informaci?n confidencial o legalmente protegida y est? exclusivamente dirigido a la persona o entidad destinataria. Si usted no es el destinatario final o la persona encargada de recibirlo, no est? autorizado a leerlo, retenerlo, modificarlo, distribuirlo, copiarlo ni a revelar su contenido. Si ha recibido este mensaje electr?nico por error, le rogamos que informe al remitente y elimine del sistema el mensaje y el material anexo que pueda contener. Gracias por su colaboraci?n.
This email message and any documents attached to it may contain confidential or legally protected material and are intended solely for the use of the individual or organization to whom they are addressed. We remind you that if you are not the intended recipient of this email message or the person responsible for processing it, then you are not authorized to read, save, modify, send, copy or disclose any of its contents. If you have received this email message by mistake, we kindly ask you to inform the sender of this and to eliminate both the message and any attachments it carries from your account. Thank you for your collaboration.
Delimit a polygon for the region which is>, 1000 m from my raster altitude
6 messages · "José M. Blanco Moreno", sadaoui, Robert J. Hijmans
Thank you very much "Jos? M. Blanco Moreno" for your answer.
I have followed your instructions from this script :
library(raster)
library(sp)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
# add polygon
library(rgeos)
pol2 <- rasterToPolygons(r, fun=function(x){x>500}, dissolve = TRUE)
pol3 <- gBuffer(pol2,width=0, byid=FALSE)
plot(r)
plot(pol3,add=TRUE,border='blue',lwd=2,col="red",axes=TRUE)
title("area > 500m")
I have had the result as I want a polygon for the area> 500m.
But,
I found a problem to export this polygon (pol3)
#export polygon to shapefile
library(rgdal)
writeOGR(pol3, ".", "polygon3", driver="ESRI Shapefile")
Error in writeOGR(pol3, ".", "polygon3", driver = "ESRI Shapefile") :
obj must be a SpatialPointsDataFrame, SpatialLinesDataFrame or
SpatialPolygonsDataFrame
it has not recognized as a polygon!!! I don't know why?
thank you for helping me to the last key
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Re-Delimit-a-polygon-for-the-region-which-is-1000-m-from-my-raster-altitude-tp7588152p7588153.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
" it has not recognized as a polygon!!! I don't know why? " That is not true:
class(pol3)
[1] "SpatialPolygons" attr(,"package") [1] "sp" It is not a 'SpatialPolygonsDataFrame' You can write it with: shapefile(pol3, "polygon3.shp") or use the SpatialPolygons object to create a SpatialPolygonsDataFrame and then use writeOGR (that is what the shapefile function does for you). Robert
On Sat, May 2, 2015 at 12:16 PM, sadaoui <sadaouimahrez at outlook.com> wrote:
Thank you very much "Jos? M. Blanco Moreno" for your answer.
I have followed your instructions from this script :
library(raster)
library(sp)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
# add polygon
library(rgeos)
pol2 <- rasterToPolygons(r, fun=function(x){x>500}, dissolve = TRUE)
pol3 <- gBuffer(pol2,width=0, byid=FALSE)
plot(r)
plot(pol3,add=TRUE,border='blue',lwd=2,col="red",axes=TRUE)
title("area > 500m")
I have had the result as I want a polygon for the area> 500m.
But,
I found a problem to export this polygon (pol3)
#export polygon to shapefile
library(rgdal)
writeOGR(pol3, ".", "polygon3", driver="ESRI Shapefile")
Error in writeOGR(pol3, ".", "polygon3", driver = "ESRI Shapefile") :
obj must be a SpatialPointsDataFrame, SpatialLinesDataFrame or
SpatialPolygonsDataFrame
it has not recognized as a polygon!!! I don't know why?
thank you for helping me to the last key
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Re-Delimit-a-polygon-for-the-region-which-is-1000-m-from-my-raster-altitude-tp7588152p7588153.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Thank you "Robert Hijmans", it works with this function ; shapefile(pol3, "polygon3.shp") but I want to have the corresponding data to this shapefile. I tried, as did you say : "or use the SpatialPolygons object to create a SpatialPolygonsDataFrame and then use writeOGR (that is what the shapefile function does for you). " I tried to follow this solution : http://gis.stackexchange.com/questions/61633/r-convert-a-spatial-polygon-objet-to-spatial-polygon-data-frame but I can not convert the spatial polygon to SpatialPolygonsDataFrame. sorry i am a beginner in this field. thank you -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Re-Delimit-a-polygon-for-the-region-which-is-1000-m-from-my-raster-altitude-tp7588152p7588155.html Sent from the R-sig-geo mailing list archive at Nabble.com.
If you want to keep the raster cell values, you could do shapefile(pol2, "polygon2.shp") and use that file.
On Sat, May 2, 2015 at 2:10 PM, sadaoui <sadaouimahrez at outlook.com> wrote:
Thank you "Robert Hijmans", it works with this function ; shapefile(pol3, "polygon3.shp") but I want to have the corresponding data to this shapefile. I tried, as did you say : "or use the SpatialPolygons object to create a SpatialPolygonsDataFrame and then use writeOGR (that is what the shapefile function does for you). " I tried to follow this solution : http://gis.stackexchange.com/questions/61633/r-convert-a-spatial-polygon-objet-to-spatial-polygon-data-frame but I can not convert the spatial polygon to SpatialPolygonsDataFrame. sorry i am a beginner in this field. thank you -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Re-Delimit-a-polygon-for-the-region-which-is-1000-m-from-my-raster-altitude-tp7588152p7588155.html Sent from the R-sig-geo mailing list archive at Nabble.com.
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Ok thank you Robert Hijmans If you want to keep the raster cell values, you could do shapefile(pol2, "polygon2.shp") and use that file.
On Sat, May 2, 2015 at 2:10 PM, sadaoui <sadaouimahrez@> wrote:
Thank you "Robert Hijmans", it works with this function ; shapefile(pol3, "polygon3.shp") but I want to have the corresponding data to this shapefile. I tried, as did you say : "or use the SpatialPolygons object to create a SpatialPolygonsDataFrame and then use writeOGR (that is what the shapefile function does for you). " I tried to follow this solution : http://gis.stackexchange.com/questions/61633/r-convert-a-spatial-polygon-objet-to-spatial-polygon-data-frame but I can not convert the spatial polygon to SpatialPolygonsDataFrame. sorry i am a beginner in this field. thank you -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Re-Delimit-a-polygon-for-the-region-which-is-1000-m-from-my-raster-altitude-tp7588152p7588155.html Sent from the R-sig-geo mailing list archive at Nabble.com.
_______________________________________________ R-sig-Geo mailing list R-sig-Geo@ https://stat.ethz.ch/mailman/listinfo/r-sig-geo
_______________________________________________ R-sig-Geo mailing list R-sig-Geo@ https://stat.ethz.ch/mailman/listinfo/r-sig-geo -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Re-Delimit-a-polygon-for-the-region-which-is-1000-m-from-my-raster-altitude-tp7588152p7588157.html Sent from the R-sig-geo mailing list archive at Nabble.com.