0

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 computing plazas = sum(as.integer(st_within(properati_santa_rita, plazas_buffer))). 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. Backtrace:

  1. properati_santa_rita %>% ...
  2. 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 computing plazas = sum(as.integer(st_within(properati_santa_rita, plazas_buffer))). Caused by error: ! 'list' object cannot be coerced to type 'integer' Backtrace:

  1. properati_santa_rita %>% ...
  2. dplyr:::mutate.data.frame(...)
  3. dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
  4. 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".

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • It would be helpful if you separate the lines of code. So many of the functions are on the same line. – John Polo Jan 25 '23 at 01:37
  • How big is this `Properati` data because I'm 300MB into its download and I'm thinking about giving up...ok I gave up at 600MB. Do you really need all that data to replicate your problem? – Spacedman Jan 27 '23 at 16:42
  • Given this is a small area I would work only in a projected local coordinate system (and not 3857, either use a local UTM zone or a national/state grid system for this place) and switch of spherical geometry with `sf_use_s2(FALSE)`. – Spacedman Jan 27 '23 at 16:51

0 Answers0