So I have two datasets. The first properati_santa_rita which is created in this way:
barrios <- st_read('https://bitsandbricks.github.io/data/CABA_barrios.geojson')
Properati = vroom::vroom("https://storage.googleapis.com/properati-data-public/ar_properties.csv.gz")
properati_CABA_Propiedades<- Properati %>%
filter(lat !="na",
lon != "na")
properati_CABA_Propiedades <- st_as_sf(x = properati_CABA_Propiedades,
coords = c("lon", "lat"),
crs = 4326)
properati_CABA_Propiedades <- properati_CABA_Propiedades[!duplicated(properati_CABA_Propiedades$title),]
santa_rita = barrios %>%
filter(BARRIO =="VILLA SANTA RITA")
properati_santa_rita = st_intersection(properati_CABA_Propiedades, santa_rita)
The second object "plazas" is created like this
# edit - read data before using it
espaciosverdes <- st_read('http://cdn.buenosaires.gob.ar/datosabiertos/datasets/espacios-verdes/espacio-verde-publico.geojson')
plazas = espaciosverdes %>%
filter(clasificac == "PLAZA")
I want to know how many properties are in a 500 meter radius of plazas. I have tried these.
plazas_clean <- st_transform(plazas, crs = 4326)
plazas_clean <- st_make_valid(plazas)
plazas_buffer = st_buffer(plazas_clean,500)
properati_santa_rita <- st_transform(plazas, crs = 4326)
plazas_clean <- st_transform(plazas_clean, crs = 4326)
properati_santa_rita = properati_santa_rita %>%
mutate(plazas = sum(as.integer(st_within(properati_santa_rita, plazas_buffer))))
Error in
stopifnot()
: ! Problem while computingplazas = sum(as.integer(st_within(properati_santa_rita, plazas_buffer)))
. Caused by error ins2_geography_from_wkb()
: ! Evaluation error: Found 4 features with invalid spherical geometry. [21] Loop 16 is not valid: Edge 40 has duplicate vertex with edge 44 [25] Loop 30 is not valid: Edge 45 has duplicate vertex with edge 49 [56] Loop 15 is not valid: Edge 47 has duplicate vertex with edge 51 [230] Loop 5 is not valid: Edge 280 has duplicate vertex with edge 300. Backtrace:
- properati_santa_rita %>% ...
- base::stop(
<Rcpp::v_>
)
And then
properati_santa_rita <- st_transform(plazas, crs = 3857)
plazas_buffer <- st_transform(plazas_buffer, crs = 3857)
properati_santa_rita = properati_santa_rita %>%
mutate(plazas = sum(as.integer(st_within(properati_santa_rita, plazas_buffer))))
Error in
stopifnot()
: ! Problem while computingplazas = sum(as.integer(st_within(properati_santa_rita, plazas_buffer)))
. Caused by error: ! 'list' object cannot be coerced to type 'integer' Backtrace:
- properati_santa_rita %>% ...
- dplyr:::mutate.data.frame(...)
- dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
- mask$eval_all_mutate(quo) Error in stopifnot(!inherits(x, "sf"), !missing(sf_column_name), !missing(agr)) :
Caused by error: ! 'list' object cannot be coerced to type 'integer'
I have also tried:
properati_santa_rita$plaza_entorno = lengths(st_intersects(st_buffer(properati_santa_rita, 500), plazas_clean))
But instead of returning the dataset of properati_santa_rita it gives me the plazas dataset back with the extra column.
On the other hand
properati_santa_rita <- st_transform(plazas, crs = 4326)
plazas_clean <- st_transform(plazas_clean, crs = 4326)
properati_santa_rita = properati_santa_rita %>% mutate(plaza_entorno = lengths(st_intersects(st_buffer(plazas_clean, 500), properati_santa_rita)))
throws
"Error in stopifnot(!inherits(x, "sf"), !missing(sf_column_name), !missing(agr)) : Caused by error in
s2_geography_from_wkb()
: ! Evaluation error: Found 4 features with invalid spherical geometry. [21] Loop 16 is not valid: Edge 40 has duplicate vertex with edge 44 [25] Loop 30 is not valid: Edge 45 has duplicate vertex with edge 49 [56] Loop 15 is not valid: Edge 47 has duplicate vertex with edge 51 [230] Loop 5 is not valid: Edge 280 has duplicate vertex with edge 300."
Finally
properati_santa_rita <- st_transform(plazas, crs = 3857)
plazas_clean <- st_transform(plazas_clean, crs = 3857)
properati_santa_rita = properati_santa_rita %>%
mutate(plaza_entorno = lengths(st_intersects(st_buffer(plazas_clean, 500), properati_santa_rita)))
gives me the wrong result.
Any ideas?
I am trying to get in object A a new column called entorno_plazas that is the result of an intersection between object A points and object B polygons. There are points that intersect with more than one polygon. If for example a point is within 500 meters of 3 plazas I want the new column to show "3".