Dipu
Dipu

Reputation: 133

Converting Raster to Point Shapefile

I am trying to convert raster to point shapefiles

> library (raster)
> ras <- raster("dem.tif")
> pts = rasterToPoints(ras)
> pts
               x         y       Z
   [1,] 70.87500 38.375007  5.035302e+14
   [2,] 71.12500 38.375007  4.563356e+14
   [3,] 70.62500 38.125007  4.987732e+14
   [4,] 70.87500 38.125007  4.108542e+14
   [5,] 71.12500 38.125007  4.114788e+14
   [6,] 70.37500 37.875006  6.538783e+14
   [7,] 70.62500 37.875006  5.798219e+14
   [8,] 70.87500 37.875006  5.045395e+14
   [9,] 71.12500 37.875006  4.315951e+14
> writeOGR(p, ".", "output_name, driver="ESRI Shapefile")
Error in writeOGR(p, ".", "filename", driver = "ESRI Shapefile") : 
  inherits(obj, "Spatial") is not TRUE
> raster::shapefile(pts, "output_name.shp")
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘shapefile’ for signature ‘"matrix"’
> shapefile(p, outfile, overwrite=TRUE)
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘shapefile’ for signature ‘"matrix"’
>plot(pts)

enter image description here Where am i wrong in converting these files into shapefile?

Upvotes: 1

Views: 3919

Answers (1)

caldwellst
caldwellst

Reputation: 5956

You need to use pts = rasterToPoints(ras, spatial = TRUE) to ensure that pts is a SpatialPointsDataFrame. As is, it is just a matrix (as stated in the error message), so can't be exported as a shapefile.

Upvotes: 2

Related Questions