Cannot plot sf linestring in R: Error in CPL_geos_is_empty(st_geometry(x))
Asked Answered
S

2

5

I have hurricane track points which I converted to lines in QGIS:

https://i.sstatic.net/Gtt61.png

https://i.sstatic.net/6z8MO.png

I have both saved as shapefiles and load them into R using the sf package. The points will plot using the standard plot() function, but the lines will not.

I am encountering the error:

plot(hurricane_paths)
Error in CPL_geos_is_empty(st_geometry(x)) : 
     Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.

I have the same error when I use plot(st_geometry(hurricane_paths))

R definitely loads in the geometry, though:

> hurricane_paths
Simple feature collection with 1410 features and 5 fields
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: -179.9 ymin: -4.9 xmax: 8 ymax: 70.7
epsg (SRID):    4269
proj4string:    +proj=longlat +datum=NAD83 +no_defs
First 10 features:
             N.A begin  end Year           N.A_1                       geometry
1  1976143N24271  <NA> <NA> 1976 SUBTROP:UNNAMED LINESTRING (-89 24, -89.6 2...
2  1976155N11265  <NA> <NA> 1976         ANNETTE LINESTRING (-95 11.4, -95.2...
3  1976159N27281  <NA> <NA> 1976         UNNAMED LINESTRING (-79 26.8, -78.5...

And st_geometry(hurricane_paths) returns

Geometry set for 1410 features 
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: -179.9 ymin: -4.9 xmax: 8 ymax: 70.7
epsg (SRID):    4269
proj4string:    +proj=longlat +datum=NAD83 +no_defs
First 5 geometries:
LINESTRING (-89 24, -89.6 24.8, -90 25.4, -90.5...
LINESTRING (-95 11.4, -95.2 11.7, -95.3 12.1, -...
LINESTRING (-79 26.8, -78.5 28, -78.1 29.2, -77...
LINESTRING (-81 26.5, -78.5 28.2, -76.2 30, -73...
LINESTRING (-103 16, -104.1 15.8, -105.1 15.8, ...

I already checked for NAs in my geometry column:

> which(is.na(hurricane_paths$geometry)==T)
integer(0)

plot_sf() does not return any error message, but it returns a blank screen in the plots pane, so that's a no-go either.

But ggplot2 is great, and returns a graphic, no problem:

> ggplot() + 
+   geom_sf(data = hurricane_paths)

Mapview also is fine: > mapview::mapview(hurricane_paths)

But the basic plot() function remains stubborn. What could the problem be?

Shirr answered 15/10, 2019 at 18:12 Comment(0)
C
5

I was having a problem just like yours and it seems that the problem is in the conversion from points to linestring. Following a solution found in the thread Create Multilines from Points, grouped by ID with sf package, you may have to use

summarise(do_union = FALSE) %>%
st_cast("LINESTRING")

before transforming your point dataset to a linestring.

I hope it helps.

Consultant answered 26/10, 2019 at 3:51 Comment(0)
G
1

Hard to help without the data. Does st_length(hurricane_paths) show any zero-length geometries? What if you plot a subset, eg plot(hurricane_paths[1:10,]) to plot the first ten?

I can duplicate your error by creating a line with only one point in it:

> Line = st_sfc(st_linestring(x = matrix(1, ncol=2, nrow=1), dim = "XYZ"))
> g = st_sf(data.frame(x=1, geom=Line))
> plot(g)
Error in CPL_geos_is_empty(st_geometry(x)) : 
  Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.

I suspect that a single-coordinate "line" is invalid under the Simple Features specification, and should really be cast to a POINT geometry, or possibly fixed by adding a second identical coordinate so that it is still a line but has two points to it.

You can use st_is_valid to detect things like this (and it even talks about LINESTRINGs with one point) and then cast them to POINT, or remove them, or deal with them appropriately to your analysis.

Gunter answered 17/10, 2019 at 10:26 Comment(3)
Hi, thanks for the reply. That was a good suggestion, but st_is_valid(hurricane_paths) returns only TRUE values for my dataset. Data can be found here: ncdc.noaa.gov/ibtracs/index.php?name=wmo-dataShirr
Can you be more specific about which data set? And what did you do in QGIS? Can you make your output from QGIS that you have read into R available via a sharing site?Gunter
and did you try narrowing it down by subsetting?Gunter

© 2022 - 2024 — McMap. All rights reserved.