I have a shapefile that I extracted form a compilation of species distributions taken from IUCN.
> foo
Simple feature collection with 1 feature and 27 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -60.62262 ymin: -27.12379 xmax: -58.33061 ymax: -25.42827
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
id_no binomial presence origin seasonal compiler year
I loaded the data with this
AR <- st_read("Mammals.shp")
foo <- AR %>% filter(binomial == "Foo bar")
I need to export these polygons, as points (lat and long) a .csv file to use the GeoCAT tools.
I tried this way but it didn't work,
foo_sf = st_as_sf(foo, coords = c("x", "y")) st_write(foo_sf,
"foo.csv")
I am newbie to some of these packages so I don't know any workaround. I think the problem is the CSV needs lat and long points instead. I don't know if using SpatialPointsDataFrame would work here.
Best Answer
The help for st_write shows how to get the geometry for points into a CSV either as X,Y or as WKT, by using a specific
layer_options
parameter:I have an
sf
object:then:
This produces the following CSV for my one-row data:
Now this is a for a POINT feature object - you can save a POLYGON feature object using WKT format, but is that what you want?
Normally to put a POLYGON of species occurrence into a model that needs points you'd take the centroid of the polygon, but if you really want to take the vertices of the polygon, then cast it into points before writing it as a CSV of points (as above):
Get one feature from the NC dataset:
This is a polygon with some attributes:
Casting to POINT results in a number of features with repeated attributes:
Then:
will get you:
Now I think the tool you are using needs columns called "Latitude" and "Longitude" instead of X an Y (read the docs in detail for the file format). I'm not sure if this can be specified in st_write or the CSV OGR driver, so you may have to edit the file.