2

I'm working with shapefile in R and I need to calculate the polygons' area that I created by Gdiference function, So ,now I need to create a column with the size of freshly polygons created, because after that I need take out the small polygons which are not important to my academic research

I didn't use any function yet.

  • Take a look at ?rgeos::gArea – Jeffrey Evans Jun 27 '18 at 1:20
6

Can you provide data for an example? Anyways, try this:

library(sf)
f <- st_read("shapefile.shp")
f$area <- st_area(f) #Take care of units

Best

  • It worked, but when I tried to use the shapefile function, returns me a error. Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘shapefile’ for signature ‘"sf"’ – Kledson Lemes Jun 27 '18 at 1:43
  • 1
    The raster::shapefile function is for reading data from on-disk files and returns a polygon object using the sp package's methods for representing spatial data in R (in this case, a SpatialPolygonsDataFrame). If you want to convert the sf polygon dataframe back to an SPDF, use as(f, 'Spatial'). To go from sp to sf, you can use sf::st_as_sf(f). If all you want to do is write your result back to disk, you don't have to convert to sp first - just use sf::st_write(). – obrl_soil Jun 27 '18 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.