# [R-sig-Geo] drawing a polygon from several lines

Antonio Silva @olinto@l@t @ending from gm@il@com
Wed Aug 22 23:34:39 CEST 2018

```Hello all,

I want to select the cells of a grid that in a range of latitudes and
depths.
Latitudes limits are indicated by two lines and the depths by two isobaths
from a shapefile.
https://www.dropbox.com/sh/us859uyijns36r9/AAC9N4OSwweLuW-fdsBWUy-ra?dl=0

Below are the steps I've already taken. I could not find a way to create a
polygon from the lines of depth and latitude. With this polygon I could
easily select the overlapped cells.

I would really appreciate any help. Best regards

----
library(rgeos)
library(sp)
rm(list = ls())

# import shape
isobaths <- spTransform(bath, CRS("+proj=longlat +datum=WGS84"))
proj4string(isobaths)
summary(isobaths)

# create a spatial grid and polygons
grd <-
GridTopology(cellcentre.offset=c(-47.75,-25.416667),cellsize=c(10/60,10/60),cells.dim=c(23,12))
polys <- as.SpatialPolygons.GridTopology(grd)
proj4string(polys) <- CRS("+proj=longlat +datum=WGS84")
summary(polys)
IDs <- sapply(slot(polys, "polygons"), function(x) slot(x, "ID"))

# create spatial lines
# southern limit
x <- c(-47.5,-45.5)
y <- c(-24.8,-24.8)
ls <- SpatialLines(list(Lines(Line(cbind(x,y)),
ID="b")),proj4string=CRS("+proj=longlat +datum=WGS84"))

# northern limit
x <- c(-46.2,-44.5)
y <- c(-24.02,-24.02)
ln <- SpatialLines(list(Lines(Line(cbind(x,y)),
ID="c")),proj4string=CRS("+proj=longlat +datum=WGS84"))

# plot isolines polygons and lines
plot(polys)
axis(1);axis(4)
text(coordinates(polys), labels=IDs, cex=0.7)

# select isobaths
summary(isobaths)
dmin <- subset(isobaths, ID %in% "-25")
proj4string(dmin) <- CRS("+proj=longlat +datum=WGS84")

dmax <- subset(isobaths, ID %in% "-60")
proj4string(dmax) <- CRS("+proj=longlat +datum=WGS84")

# from here I'd like to have a polygon to select the cells.
na.omit(over(polys,dmin))

--
Antonio Olinto Ávila da Silva
Instituto de Pesca (Fisheries Institute)
São Paulo, Brasil

[[alternative HTML version deleted]]

```