Lise
Lise

Reputation: 13

Can you create multiple polygons in R from a dataframe containing the vertices' coordinates?

I have a data frame with 400 rows, each corresponding to a fishing area. Each row includes the coordinates of vertices of said fishing area, like this (NW = northwest corner, etc.):

> head(zones)
ID       NW.X     NW.Y       NE.X     NE.Y       SE.X     SE.Y       SW.X       SW.Y     NW.X.2 Effort Season  Method
1  1 -7,9961854 37.00222  -8,102379 37.05959 -8,1030245 37.01368 -7,9969111 36,9563306 -7,9961854     36 Winter Tremmel
2  2 -8,2268172 37.08076 -7,9773826 36.98974 -7,9778924 36.94389 -8,1248423 37,0183724 -8,2268172     12 Winter    Gill
3  3  -8,102379 37.05959 -7,9961854 37.00222 -7,9990974 36.75608 -8,1036475 36,8128815  -8,102379     42 Winter    Gill
4  4 -8,1024855 37.04711 -7,9963439 36.98973 -7,9994791 36.72262 -8,1036857 36,7794856 -8,1024855     42 Winter Tremmel
5  5 -8,3621785 37.02390 -7,9969701 36.93962 -7,9982745 36.83943 -8,3620979 36,9327666 -8,3621785     36 Winter    Gill
6  6 -8,2580358 37.06539 -7,9963439 36.98973 -7,9974956 36.90627 -8,2580358 36,9822685 -8,2580358     36 Winter    Gill

I want to create polygons of each individual area, and extract it as a shapefile to use in ArcGIS. I've seen questions here asking how to create a polygon from a list of coordinates, but not of multiple polygons from coordinates stored in this way, and I'm a total beginner with the sf package. I know that usually, you should only have 1 column with X and 1 column with Y, not like what I have here. I could reformat my data frame to only have 2 columns with the coordinates, but then how do I create the multiple polygons? I imagine with some sort of loop?

I think the answer to this question may help, but I don't understand it perfectly. https://gis.stackexchange.com/questions/144728/add-polygons-to-spatialpolygons-via-loop-iteration-using-r

Upvotes: 1

Views: 733

Answers (1)

Wimpel
Wimpel

Reputation: 27732

Here is a first go at it. Not sure if I accidently switches X and Y, so please (visually) check output

library(data.table)
library(sf)
library(sfheaders)
library(tidyverse)

# Sample data -----
DT <- fread("ID       NW.X     NW.Y       NE.X     NE.Y       SE.X     SE.Y       SW.X       SW.Y     NW.X.2 Effort Season  Method
  1 -7,9961854 37.00222  -8,102379 37.05959 -8,1030245 37.01368 -7,9969111 36,9563306 -7,9961854     36 Winter Tremmel
  2 -8,2268172 37.08076 -7,9773826 36.98974 -7,9778924 36.94389 -8,1248423 37,0183724 -8,2268172     12 Winter    Gill
  3  -8,102379 37.05959 -7,9961854 37.00222 -7,9990974 36.75608 -8,1036475 36,8128815  -8,102379     42 Winter    Gill
  4 -8,1024855 37.04711 -7,9963439 36.98973 -7,9994791 36.72262 -8,1036857 36,7794856 -8,1024855     42 Winter Tremmel
  5 -8,3621785 37.02390 -7,9969701 36.93962 -7,9982745 36.83943 -8,3620979 36,9327666 -8,3621785     36 Winter    Gill
  6 -8,2580358 37.06539 -7,9963439 36.98973 -7,9974956 36.90627 -8,2580358 36,9822685 -8,2580358     36 Winter    Gill")

# Code -----
# Set decimal operator correct and convert all NW-like columns to numeric
cols <- grep("^(NW|NE|SE|SW)\\.[XY]$", names(DT), value = TRUE)
DT[, (cols) := lapply(.SD, function(x) as.numeric(gsub(",", "\\.", x))), .SDcols = cols]
#set to workable format df
my.poly <- setDF(DT) %>%
  # Melt to long, beep XY paired
  pivot_longer( cols = cols,
                names_to = c("point", ".value"),
                names_pattern = "(..)\\.(.)" ) %>%
  #!! check if X and Y are correct or should be switched!!
  sfheaders::sf_polygon( x = "X", y = "Y", polygon_id = "ID" ) %>%
  sf::st_set_crs(4326)

#visual incpection
mapview::mapview(my.poly)

enter image description here

Upvotes: 3

Related Questions