[R-sig-Geo] Problem with writing a function that attaches atributes from polygons of a shapefile to the points of a grid

MIGUEL GOMEZ DE ANTONIO mg@ @end|ng |rom ccee@ucm@e@
Mon Jan 23 20:11:44 CET 2023


Hi all,

I need to create a function that georreference covariates that are not
available at the point level but at a census tract or any other
administrative polygon level (cities, regions, etc.).

My last goal is to create my own function that attaches the values of a
census tract shapefile to the points of the grid in an observation window.
Then I would like to kernel smooth it to obtain a covariate that is
continouos in the observation window that contains the grid points.

I am guessing that my problem is either in creating an object of the class
sf when adding the dataframe to the sfg object or when using the function
st_join () that is supposed to add the attributes of the polygon to the
points located in it.



#I first created the coordinates of the points of the grid in the
observation window "madrid_sqr_m" xy<-gridcentres(madrid_sqr_m,150,150)#
"madrid_sqr_m" is just the boundary of the grid (class owin)

#Then I created a sfg object multipolygon

x=as.numeric(xy$x)

y=as.numeric(xy$y)

XY=data.frame(x,y)

xy=as.matrix(XY)

xy=st_multipoint(xy)#creo el objeto sfg que es un conjunto de puntos
(multypoligon)

xy_crs=st_sfc(xy, crs=25830)#add the proyected reference system

xy_crs

#add the dataframe to have an object of class sf. NOT SURE IF I AM DOING
THIS RIGHT. THE GRID POINTS HAVE NOT ATTRIBUTES SO THE DATAFRAME SHOULD BE
EMPTY, ISN`T?

df <- data.frame(nombre = c("grid"))#

grid_sf=st_sf(df, xy_crs)#

class(grid_sf)# #Add the covariates of a shapefile named "Catfiscal", which
is a census tract polygon, to the points of the grid

join<-st_join(grid_sf,Catfiscal)# PROBLEM MIGHT BE HERE BECAUSE IS NOT
ASIGNING TO EACH GRID POINT THE COVARIATES OF THE POLIGONS

x=join$categoria# "categoria" is the covariate I would like to add to the
points of the grid once I convert the sf object into a "ppp" object of the
Spatstat

join.ppp<-as.ppp(st_coordinates(join), madrid_sqr_m)#"madrid_sqr_m" is just
the boundary of the grid (class owin)

grid_con_cat_f.ppp<-setmarks(join.ppp,x) **

*#Error in `marks<-.ppp`(`*tmp*`, value = value) : # number of points !=
number of marks ** *

density.CATF<-Smooth.ppp(grid_con_cat_f.ppp)

I have been stuck on this several weeks os any advice with be very much
appreciated
Thanks for your help

Miguel

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list