0

Does anyone have an idea how could one get information out of shapefile attribute table (class SpatialPolygonsDataFrame in R), in my case data from column that contains species names, based on a list of coordinates that I have. Each coordinate that is within a certain polygon should be given the species name, and as a result I would like to have a dataframe with three columns: X,Y,species name. Are there any packages and their specific functions for this kind of task? Maybe sp package, aggregate function?

  • 1
    over() in sp will do this, in your case the points are the first argument, the polygons with attributes second e.g. over(points, polygons). Once you've got the result as.data.frame() drops all the class stuff and leaves you with coordinates and values. (If your layers are in different coordinate systems sp::over won't auto-transform, because life is about suffering and we are not worthy, so just ask again with more detail if you have problems) – mdsumner Nov 9 '17 at 10:59
  • Yes, that was the solution that worked for me. If you can help me further - is there a way to get these same values from atribute table, but not necessarily for the exact coordinates, but for the polygon that is closest to the given coordinates? My input coordinates are actually pixel coordinates I extracted from raster. Since I used weights in the extract function, i have extracted the values of the pixels that are located at the edge of the polygon. Thus, these pixels centroids are actually outside of the polygons, and when i use the "over" function i get the NAs. – davor korman Nov 9 '17 at 13:51
  • I created buffers with pixel diameter width arround my coordinates, and used those in the over(buffer, polygons,byid=TRUE). That way I got all the points to have their appropriate attribute table values joined. – davor korman Nov 9 '17 at 16:28

Your Answer

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

Browse other questions tagged or ask your own question.