TransWikia.com

Match centroids from one dataset with polygons of another

Geographic Information Systems Asked on June 2, 2021

I want to make a data frame showing which voting precincts are in each school zone. To do this, I want to see which precincts’ center points are in which school zones.

library(sf)

precincts <- st_read("voting_precincts.shp")
precincts$centroid <- st_centroid(
  x = precincts
)

elemZones <- read_sf("school_zones.shp")

So although I have the centroid coordinates for precincts, I do not know how to "join" them with the school zones data.

One Answer

One of the few limitations simple feature standard for geometries has is that it cannot have two geometry columns; you may have two or more columns with WKT definitions but you have to decide which column to use. In your case you try to add the centroid as a column, but what sf does is to add a the whole sf tbl object (the data frame) as a column, if you print the object with str(precincts) you'll see the classes of each column, if you just print str(precincts$centroid) you'll get:

 str(precincts$centroid)
 Classes ‘sf’, ‘tbl_df’, ‘tbl’ and 'data.frame'

st_centroid will return a whole sf tbl, so you may just do:

 library(sf)
 library(dplyr)
 precincts <- st_read("voting_precincts.shp")
 elemZones <- read_sf("school_zones.shp")

 st_centroid(precincts) %>% st_join(elemZones)

Correct answer by Elio Diaz on June 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP