Sf: wt.centroid ?

Created on 17 Feb 2019  Â·  6Comments  Â·  Source: r-spatial/sf

is there something like the wt.centroid function in sf?

https://www.rdocumentation.org/packages/spatialEco/versions/1.1-1/topics/wt.centroid

right now, i'm converting back and forth to sp in order to use that function. i'd be happy to learn that's not necessary :-)

thanks

Most helpful comment

so this is enough for my use case. I am pretty sure it's not tight enough for you in terms of which sf classes are acceptable etc, I know too little about it I am afraid. If you can give some hints i can try a pull request.

wtc <- function(g,w){
  if (!(is(g,"sf")) | !(w %in% colnames(g))){
    stop(paste("requires an sf object with at a column",w))
  }
  centers = st_coordinates(st_centroid(st_geometry(g)))
  # crsx = st_crs(g) how could i reuse the CRS of g? do i need that?
  out = st_point(c(weighted.mean(centers[,"X"],g[[w]]), weighted.mean(centers[,"Y"],g[[w]])))
  return(out)
}

nc = st_read(system.file("shape/nc.shp", package="sf"))
nc$weights = c(rep(1,5),rep(0,95))
plot(st_geometry(nc))
plot(st_centroid(st_union(nc)),col="red",pch=3,add=TRUE)
plot(wtc(nc,"weights"),col="blue",pch=3,add=TRUE)

rplot

All 6 comments

No there isn't, and there wasn't one in sp. Maybe you can raise an issue with the spatialEco maintainers and/or write the function yourself - I'd be happy to help if you get stuck.

Ok. Is there some API docs that I could look at? I couldn’t even get x and
y separately from a POINT object...

On Sun 17 Feb 2019 at 19:38, Edzer Pebesma notifications@github.com wrote:

No there isn't, and there wasn't one in sp. Maybe you can raise an issue
with the spatialEco maintainers and/or write the function yourself - I'd
be happy to help if you get stuck.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/r-spatial/sf/issues/977#issuecomment-464492845, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AA-WdkXwNW8lLNqkF8os_tQMbiASeUgiks5vOaGtgaJpZM4a_nyi
.

skip that. not sure what i was doing...

so i would just extract the x and y coordinates from all POINT's like that one by one or is there anohter method (say, for a vector of POINT)?

> g = st_point(1:2)
> g
POINT (1 2)
> g[1]
[1] 1
> g[2]
[1] 2
> 

st_coordinates; see also here.

so this is enough for my use case. I am pretty sure it's not tight enough for you in terms of which sf classes are acceptable etc, I know too little about it I am afraid. If you can give some hints i can try a pull request.

wtc <- function(g,w){
  if (!(is(g,"sf")) | !(w %in% colnames(g))){
    stop(paste("requires an sf object with at a column",w))
  }
  centers = st_coordinates(st_centroid(st_geometry(g)))
  # crsx = st_crs(g) how could i reuse the CRS of g? do i need that?
  out = st_point(c(weighted.mean(centers[,"X"],g[[w]]), weighted.mean(centers[,"Y"],g[[w]])))
  return(out)
}

nc = st_read(system.file("shape/nc.shp", package="sf"))
nc$weights = c(rep(1,5),rep(0,95))
plot(st_geometry(nc))
plot(st_centroid(st_union(nc)),col="red",pch=3,add=TRUE)
plot(wtc(nc,"weights"),col="blue",pch=3,add=TRUE)

rplot

Thanks! closing here..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nowosad picture Nowosad  Â·  3Comments

dpprdan picture dpprdan  Â·  4Comments

tiernanmartin picture tiernanmartin  Â·  3Comments

jsta picture jsta  Â·  4Comments

Nosferican picture Nosferican  Â·  3Comments