Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop 0 is not valid: Edge 4441 crosses edge 4445 #1902

Closed
Tracked by #1771
PanfengZhang opened this issue Feb 10, 2022 · 2 comments
Closed
Tracked by #1771

Loop 0 is not valid: Edge 4441 crosses edge 4445 #1902

PanfengZhang opened this issue Feb 10, 2022 · 2 comments

Comments

@PanfengZhang
Copy link

library(sf)
cmap <- read_sf("https://geo.datav.aliyun.com/areas_v3/bound/100000.json")
cmap2 <- st_crop(cmap, xmin = 72, ymin = 17, xmax = 136, ymax = 55)

When I run the above code, the error is as follows:

Error in s2_geography_from_wkb(x, oriented = oriented, check = check) :
Evaluation error: Found 1 feature with invalid spherical geometry.
[1] Loop 0 is not valid: Edge 4441 crosses edge 4445.

How to resolve the problem?

@rsbivand
Copy link
Member

The input geometry is mis-specified:

> library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
> cmap <- read_sf("https://geo.datav.aliyun.com/areas_v3/bound/100000.json")
> cmap2 <- st_crop(cmap, xmin = 72, ymin = 17, xmax = 136, ymax = 55)
Error in s2_geography_from_wkb(x, oriented = oriented, check = check) : 
  Evaluation error: Found 1 feature with invalid spherical geometry.
[1] Loop 0 is not valid: Edge 4441 crosses edge 4445.
> st_is_longlat(cmap)
[1] TRUE
> sf_use_s2()
[1] TRUE
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> cmap2 <- st_crop(cmap, xmin = 72, ymin = 17, xmax = 136, ymax = 55)
although coordinates are longitude/latitude, st_intersection assumes that they are planar
Error in CPL_geos_op2(op, x, y) : 
  Evaluation error: TopologyException: Input geom 0 is invalid: Self-intersection at 114.320037 22.381126999999999.
> sf_use_s2(TRUE)
Spherical geometry (s2) switched on
> cmap1 <- st_make_valid(cmap)
> cmap2 <- st_crop(cmap1, xmin = 72, ymin = 17, xmax = 136, ymax = 55)
Error in s2_geography_from_wkb(x, oriented = oriented, check = check) : 
  Evaluation error: Found 1 feature with invalid spherical geometry.
[1] Loop 136 is not valid: Edge 744 crosses edge 748.
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> cmap2 <- st_crop(cmap1, xmin = 72, ymin = 17, xmax = 136, ymax = 55)
although coordinates are longitude/latitude, st_intersection assumes that they are planar
Error in CPL_geos_op2(op, x, y) : 
  Evaluation error: TopologyException: Input geom 0 is invalid: Self-intersection at 119.60970386388662 26.150518787877377.
> sf_use_s2(TRUE)
Spherical geometry (s2) switched on
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> cmap1 <- st_make_valid(cmap)
> cmap2 <- st_crop(cmap1, xmin = 72, ymin = 17, xmax = 136, ymax = 55)
although coordinates are longitude/latitude, st_intersection assumes that they are planar
Warning message:
attribute variables are assumed to be spatially constant throughout all geometries 
> sf_use_s2(TRUE)

The only remedy is to turn off spherical geometry, rebuild the geometry with st_make_valid() as planar geometry, then crop as planar geometry, even when the geometries are really spherical, and the bounding box should be curvilinear, not rectangular. See: https://r-spatial.org/r/2020/06/17/s2.html and #1771.

@PanfengZhang
Copy link
Author

Thank you very much for helping me solve this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants