Skip to content
New issue

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

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

Already on GitHub? # to your account

vect(wkt) with strange behavior for MULTIPOINT #1376

Closed
silasprincipe opened this issue Dec 18, 2023 · 3 comments
Closed

vect(wkt) with strange behavior for MULTIPOINT #1376

silasprincipe opened this issue Dec 18, 2023 · 3 comments

Comments

@silasprincipe
Copy link

silasprincipe commented Dec 18, 2023

Hi, I'm using terra to get the convex hull of multiple types of geometries. It worked for all cases that I tested (polygon, multipolygon, linestring, multilinestring, point), except one: multipoint. Interestingly, it correctly reads the wkt, but then the convHull function does not returns what is expected (a linestring, in this particular test). Using the package sf or a mix of terra and sf works. Is that a bug or is there any option I'm missing? Thanks in advance!

See examples:

wkt <- "MULTIPOINT ((0 1), (0 -1))"

# sf version, working properly
s <- sf::st_as_sfc(wkt)
p <- sf::st_cast(s, "POINT")
mp <- sf::st_combine(p)
h <- sf::st_convex_hull(mp)
h

# Geometry set for 1 feature 
# Geometry type: LINESTRING
# Dimension:     XY
# Bounding box:  xmin: 0 ymin: -1 xmax: 0 ymax: 1
# CRS:           NA
# LINESTRING (0 1, 0 -1)

# terra + sf version, working properly
s <- terra::vect(sf::st_as_sfc(wkt))
h <- terra::convHull(s)
h

# class       : SpatVector 
# geometry    : lines 
# dimensions  : 1, 0  (geometries, attributes)
# extent      : 0, 0, -1, 1  (xmin, xmax, ymin, ymax)
# coord. ref. :  

# terra only version, not working
s <- terra::vect(wkt)
h <- terra::convHull(s)
h

# Returns points, not a line as expected

# class       : SpatVector 
# geometry    : points 
# dimensions  : 1, 0  (geometries, attributes)
# extent      : 0, 0, 1, 1  (xmin, xmax, ymin, ymax)
# coord. ref. :  

terra version: 1.7.65
sf version: 1.0.14
gdal version: 3.5.3
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.4.1

@silasprincipe
Copy link
Author

Just to complement the issue, I think that it can be because terra is considering a MULTIPOINTS wkt as a single geometry. So see for example:

wkt <- "MULTIPOINT (-64.02832 26.39187, -65.961914 26.627818)"
s <- terra::vect(wkt)
s

# class       : SpatVector 
# geometry    : points 
# dimensions  : 1, 0  (geometries, attributes)
# extent      : -65.96191, -64.02832, 26.39187, 26.62782  (xmin, xmax, ymin, ymax)
# coord. ref. :  

# If extract coordinates and then convert again
crds_s <- terra::vect(terra::crds(s))

# class       : SpatVector 
# geometry    : points 
# dimensions  : 2, 0  (geometries, attributes)
# extent      : -65.96191, -64.02832, 26.39187, 26.62782  (xmin, xmax, ymin, ymax)
# coord. ref. :  

Probably this is the expected result (sf::st_as_sfc(wkt) will also return a MULTIPOINT object instead of POINT), but is there a quick and more elegant way to ensure terra uses it as individual points when using convHull?

@htopazian
Copy link

Thanks for suggesting the sf::st_as_sfc workaround, I am having this issue too with v. 1.7-39. Running code from a year ago which used to work with vect with the same SpatVector object

@rhijmans
Copy link
Member

Thanks, fixed now. I am sorry that it took so long.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jan 5, 2025
# version 1.8-5

## bug fixes

- `Spatsample(method='stratified', ext=e)` returned the wrong sampling
  coordinates [#1628](rspatial/terra#1628)
  by Barnabas Harris

- `spatSample(method='stratified')` could fail with small sample sizes
  [#1503](rspatial/terra#1503) by karluf

- transparency (alpha) did not work with RGB
  plotting. [#1642](rspatial/terra#1642) by
  Timothée Giraud

- rasterization failed on very large rasters
  [#1636](rspatial/terra#1636) by Mary
  Fisher, [#1463](rspatial/terra#1463) by
  Nic Spono and [#1281](rspatial/terra#1281)
  by Sebastian Dunnett

- `tmpFiles` only looked in the default temp files folder
  [#1630](rspatial/terra#1630) by
  smckenzie1986

- `where.min` did not work well if there were negative values
  [#1634](rspatial/terra#1634) by Michael
  Sumner

- `plet<SpatRaster>` now works for RGB rasters and rasters with a
  color table [#1596](rspatial/terra#1596)
  by Agustin Lobo

- `vect<MULTIPOINT WKT>` did not work properly
  [#1376](rspatial/terra#1376) by
  silasprincipe

- `compareGeom<SpatVector>` did not work
  [#1654](rspatial/terra#1654) by Jason
  Flower

- `buffer<SpatVector>` is now more accurate buffers for lonlat
  polygons [#1616](rspatial/terra#1616) by
  Roberto Amaral-Santos

- `terra:interpNear` used square windows, not circles, beyond 100
  points [#1509](rspatial/terra#1509) by
  Jean-Luc Dupouey

- `vect` read INT64 fields as integers, sometimes leading to
  overflows. [#1666](rspatial/terra#1666) by
  bengannon-fc

- `plot` showed a legend title even if none was requestd if title
  parameters were specified
  . [#1664](rspatial/terra#1664) by Márcia
  Barbosa



## enhancements

n- improved documentation of `writeVector` overwrite when using
  layers. [#1573](rspatial/terra#1573) by
  Todd West

- improved treatment of (supposedly) flipped rasters by Timothée
  Giraud [#1627](rspatial/terra#1627) and
  fchianucci [#1646](rspatial/terra#1646)

- added `map.pal("random")`
  [#1631](rspatial/terra#1631) by Agustin
  Lobo

- expressions can now be used in legend titles
  [#1626](rspatial/terra#1626) by Noah
  Goodkind

- `app` and `tapp` now emit a warning when factors are coerced to
  numeric [#1566](rspatial/terra#1566) by
  shuysman

- `plet<SpatRaster>` now has argument "stretch" for RGB rasters
  [#1596](rspatial/terra#1596) by Agustin

- `%%` and `%/%` now behave the same for SpatRaster as for (base R)
  numbers [#1661](rspatial/terra#1661) by
  Klaus Huebert

## new

- `patches` with option `valus=TRUE` can now distinguish regions based
  on their cell values (instead of only NA vs not-NA)
  [#495](rspatial/terra#495) by Jakub
  Nowosad and [#1632](rspatial/terra#1632)
  by Agustin Lobo

- `rowSums`, `rowMeans`, `colSums` and `colMeans` for SpatRaster

- `metags` for SpatRasterDataset
  [#1624](rspatial/terra#1624) by Andrea
  Manica

- `metags` for layers (bands) of SpatRaster are now saved to and read
  from GTiff files
  [#1071](rspatial/terra#1071) by Mike
  Koontz

- `global` has new effcient functions "anyNA" and "anynotNA"
  [#1540](rspatial/terra#1540) by Kevin J
  Wolz

- `wrap`, `saveRDS` and `serialize` for
  SpatExtent. [#1430](rspatial/terra#1430)
  by BastienFR

- `vect<SpatGraticule>` method suggested in relation to [tidyterra
  #155](dieghernan/tidyterra#155) by Diego
  Hernangómez

- `toMemory<SpatRaster>` and `<SpatRasterDataset>` methods
  [#1660](rspatial/terra#1660) by Derek Friend
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants