Error in UseMethod("st_geometry"): no applicable method for 'st_geometry' applied to an object of class "function"

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
  set.seed(15)
  
  X=7.5
  Y=7.5
  f = function(){
    a = runif(10,1,10)
    
    return( a=as.integer(a))
    
  }
  onlineloc=data.frame(x=f(),y=f())
  store=data.frame(X,Y)
  
  plot(onlineloc,main="Online and instore customer loc",col=" red")
  points(X,Y,pch=15)

  p = st_sfc(st_point(c(7.5,7.5)))
  q=st_sfc(onlineloc)
#> Error in vapply(lst, class, rep(NA_character_, 3)): values must be length 3,
#>  but FUN(X[[1]]) result is length 1
  st_distance(p, q )
#> Error in UseMethod("st_geometry"): no applicable method for 'st_geometry' applied to an object of class "function"
  st_distance(q, p, by_element = TRUE)
#> Error in UseMethod("st_geometry"): no applicable method for 'st_geometry' applied to an object of class "function"

Created on 2019-09-12 by the reprex package (v0.3.0)
why it is occuring

Yeah, sf is a bit fiddly and confusing. What are you trying to do? To convert onlineloc to sf you need to convert it to a matrix, and then perhaps a multipoint? (depends what you are trying to do)

q = st_sfc(st_multipoint(as.matrix(onlineloc)))

This will only give you the first distance since p is only a single point.

I think sf is designed to work with dataframes. sf dataframes have a geom column which gives the spatial geometry of each item.

1 Like

yes i want to find distance of 10 points (in online loc) from single reference point (store) for that i guess i have to convert it in sf object, how i can get the distance of all 10 points from single reference point please help

I think I would probably make p a dataframe, but this works too.

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3

set.seed(15)

X <- 7.5
Y <- 7.5
f <- function(){
    as.integer(runif(10,1,10))
}
onlineloc <- data.frame(x = f(), y = f())

plot(onlineloc, main = "Online and instore customer loc", col = "red")
points(X, Y, pch = 15)


p <- st_point(c(X, Y))
onlineloc <- st_as_sf(onlineloc, coords=c("x", "y"))

st_distance(p, onlineloc)
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]    [,7]
#> [1,] 6.670832 5.700877 2.915476 1.581139 3.535534 1.581139 2.54951
#>          [,8]     [,9]     [,10]
#> [1,] 4.743416 5.522681 0.7071068

Created on 2019-09-14 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.