Giter Club home page Giter Club logo

Comments (2)

JosiahParry avatar JosiahParry commented on June 8, 2024

from sfdep.

JoseLastra avatar JoseLastra commented on June 8, 2024

I was using the test points from PySal http://pysal.org/notebooks/explore/pointpats/centrography.html

library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(sfdep)
library(tidyverse,warn.conflicts = F)

#points creation 
points <- rbind(c(66.22, 32.54), c(22.52, 22.39), c(31.01, 81.21),
          c(9.47, 31.02),  c(30.78, 60.10), c(75.21, 58.93),
          c(79.26,  7.68), c(8.23, 39.93),  c(98.73, 77.17),
          c(89.78, 42.53), c(65.19, 92.08), c(54.46, 8.48)) %>% 
  as.data.frame() %>% rename(x = V1, y = V2) %>% mutate(weights = seq(0,11,1))

# to sf 
points_sf <- points %>% st_as_sf(coords = c('x','y'))

# Test center mean no W
mean_c <- center_mean(points_sf)

#plot results
plot(points_sf$geometry, axes = T)
plot(mean_c, col = 'red', add = T)

# Test center with W, default sfdep function
mean_cw <- center_mean(points_sf, weights = points_sf$weights)
#plot results
plot(points_sf$geometry, axes = T)
plot(mean_cw, col = 'blue', add = T) #result outside of bbox

## changing the line and possible solution
 center_mean_ps <- function (geometry, weights = NULL) {
  #geometry <- check_polygon(geometry) # removed just for this example
  crs <- sf::st_crs(geometry)
  coords <- sf::st_coordinates(geometry)
  n <- nrow(coords)
  if (!is.null(weights)) {
    res <- colSums(coords * weights)/sum(weights, na.rm = T)
  }
  else {
    res <- colSums(coords)/n
  }
  sf::st_sfc(sf::st_point(res), crs = crs)
}

#testing 
 mean_cw <- center_mean_ps(points_sf, weights = points_sf$weights)
 #plot results
 plot(points_sf$geometry, axes = T)
 plot(mean_cw, col = 'blue', add = T) #result outside of bbox

 

 ## check both mean center
 #plot results
 plot(points_sf$geometry, axes = T)
 plot(mean_c, col = 'red', add = T, pch = 13) 
 plot(mean_cw, col = 'blue', add = T,pch = 15) #result outside of bbox

Created on 2022-10-06 with reprex v2.0.2

from sfdep.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.