0

I have a large SpatialPolygonsDataFrame that describes a species distribution. It has four main features for different parts of the species range (e.g. breeding area, wintering area etc). Each of the four features is made up of many polygons that together cover the (sometimes disconnected) region(s) that particular feature covers. When I do sp::coordinates(my_data) I get four sets of coordinates:

      [,1]     [,2]
0 58.03994 43.18015
1 73.65654 29.37278
2 52.59073 37.45488
3 68.26932 36.04832

Presumably, each one relates to one of the four features but I am unsure exactly what these coordinates are. Are they a centroid value of some kind? I notice they are identical to the labeling coordinates within the SpatialPolygonsDataFrame (labpt). Has sp taken the values from there? Or are they calculated from the polygons within the SpatialPolygonsDataFrame? Will these values always fall within a polygon for each feature, or could they fall outside the polygons themselves (as I think could happen if a centroid was calculated just on the bounding box)?

Thank you for any help or insight provided. Sorry if my questions seem confused - I am new to working with this kind of spatial data!

camille
  • 16,432
  • 18
  • 38
  • 60
  • not an answer but maybe you want to try using the `sf` package. it displays coordinates with the rest of the data in a data frame format. that makes it easier to work with geodata – D.J Mar 01 '23 at 14:19
  • Ahh, yes the other post is very helpful, thank you camille. But I haven't totally solved my problem as if the centroid value may not actually be within a polygon (in the case of a multiple polygon object), then I need another way as I need a point that defintely falls within a polygon. The sf package was a nice suggestion thank you D.J, as st_centroid(x, of_largest_polygon = TRUE) could solve this. But this doesn't seem to work on a SpatialPolygonsDataFrame. I guess I must need to somehow convert to an sf object of some kind? – Emily O'Connor Mar 01 '23 at 16:33
  • The function `sf::st_as_sf` might help you with that – L-- Mar 01 '23 at 16:46
  • Thank you L. That works to convert my SpatialPolygonsDataFrame to a sf object, which gave me hope! But then when I tried to run st_centroid(x, of_largest_polygon = TRUE) on this I got an error message "Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented, : Loop 0 is not valid: Edge 3224 has duplicate vertex with edge 3230 In addition: Warning message: In st_centroid.sf(Acme_sf) : st_centroid assumes attributes are constant over geometries of x". I'm not sure what that means, but it seems the data format isn't working for some reason? – Emily O'Connor Mar 01 '23 at 20:01
  • This is getting a bit far from what the initial question is about. You might actually find the answer to this new issue in other posts (e.g., https://stackoverflow.com/questions/68808238) and if you can't find an answer I suggest you to open a new question with a title that reflects the new issue so that everyone can see it and potentially help – L-- Mar 01 '23 at 23:21
  • Ok, will do. Thanks everyone for your help. – Emily O'Connor Mar 02 '23 at 12:38

0 Answers0