[R-sig-Geo] st_intersection produce Geometry type: GEOMETRY instead of Geometry type: POLYGON
Josiah Parry
jo@|@h@p@rry @end|ng |rom gm@||@com
Fri Feb 10 19:09:25 CET 2023
Manuel, I think you're looking for an intersect*s* rather than the
intersection. Is the following what you're after?
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc <- st_transform(nc, 3857)
g_50000 <- st_make_grid(nc, cellsize = 50000) |> st_as_sf()
g_50000 <- g_50000[nc, ]
g_50000_d <- st_union(g_50000)
g_25000 = st_make_grid(g_50000_d, cellsize = 25000) |> st_as_sf()
g_25000 # Geometry type: POLYGON
index <- which(lengths(st_intersects(g_50000, nc)) > 0)
plot(st_union(g_50000[index,]))
On Fri, Feb 10, 2023 at 12:53 PM Bede-Fazekas Ákos <bfalevlist using gmail.com>
wrote:
> Dear Manuel,
>
> technically, the result of st_intersection(x, y), where both x and y are
> POLYGONs, can be POINT, LINESTRING, POLGYON and GEOMETRY as well. The
> result is GEOMETRY if the type of the different features is not the same
> (e.g. POLYGON+POINT).
> You can subset the result in this way:
> g_25000_c_polygons_only <- g_25000_c[st_is(x = g_25000_c, type =
> "POLYGON")]
>
> HTH,
> Ákos
> ___________
> Ákos Bede-Fazekas
> Centre for Ecological Research, Hungary
>
> 2023.02.10. 18:32 keltezéssel, Manuel Spínola írta:
> > Dear list members,
> >
> > I am trying to "crop" a polygon (grid) with a polygon, but the result is
> an
> > sf object Geometry type: GEOMETRY, instead of an sf object Geometry type:
> > POLYGON.
> >
> > How can I obtain an sf POLYGON?
> >
> > nc = st_read(system.file("shape/nc.shp", package="sf"))
> >
> >
> >
> > nc <- st_transform(nc, 3857)
> >
> >
> >
> > g_50000 <- st_make_grid(nc, cellsize = 50000) |> st_as_sf()
> >
> >
> >
> > g_50000 <- g_50000[nc, ]
> >
> >
> >
> > g_50000_d <- st_union(g_50000)
> >
> >
> >
> > g_25000 = st_make_grid(g_50000_d, cellsize = 25000) |> st_as_sf()
> >
> >
> >
> > g_25000 # Geometry type: POLYGON
> >
> >
> >
> > g_25000_c <- st_intersection(g_25000, g_50000_d)
> >
> >
> >
> > g_25000_c # Geometry type: GEOMETRY
> >
> >
> >
> >
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list