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

Marcelino de la Cruz Rot m@rce||no@de|@cruz @end|ng |rom urjc@e@
Mon Jan 23 21:59:39 CET 2023


Hi Miguel,
Maybe this way:


xy<-gridcentres(madrid_sqr_m,150,150)
xy.df <- as.data.frame(xy)
xy_sf <- st_as_sf(xy.df, coords=1:2)
xy_crs <- st_sfc(xy_sf, crs=25830)
xy_joined <- st_join(xy_crs, Catfiscal)
join.ppp <- ppp(st_coordinates(xy_joined)[,"X"], 
st_coordinates(xy_joined)[,"Y"], window=madrid_sqr_m, marks 
=xy_joined$categoria)

Cheers,
Marcelino




El 23/01/2023 a las 20:11, MIGUEL GOMEZ DE ANTONIO escribió:
> [No suele recibir correo electr?nico de mga using ccee.ucm.es. Descubra por qu? esto es importante en https://aka.ms/LearnAboutSenderIdentification ]
>
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


-- 
Marcelino de la Cruz Rot
Coordinador funcional de Biología
Depto. de Biología y Geología
Física y Química Inorgánica
Universidad Rey Juan Carlos
Móstoles España



More information about the R-sig-Geo mailing list