Skip to content

Improve search_radius speed #33

Description

@StefanMusch

The search_radius function is incredibly slow because it's created in a loop instead of being vectorized. A quick and dirty speed up could be:


search_radius_fast <- function(lat, lng, radius = 1) {
  
  # Filter out invalid coordinates
  zip_data <- zip_code_db %>%
    filter(!is.na(lat) & !is.na(lng))
  
  # Vectorized distance calculation (meters → miles)
  zip_data$distance <-
    raster::pointDistance(
      c(lng, lat),
      zip_data[, c("lng", "lat")],
      lonlat = TRUE
    ) * 0.000621371
  
  # Filter and arrange by distance
  
  result <- zip_data %>% dplyr::filter(.data$distance <= radius) %>% 
    dplyr::select(.data$zipcode, .data$distance) %>% dplyr::as_tibble() %>% 
    dplyr::arrange(.data$distance)
  
  if (nrow(result) == 0) {
    warning(paste("No ZIP codes found for coordinates", 
                  paste0(lat, ",", lng), "with radius", radius, "mi"))
  }
  return(result)
  }

This already speeds up the function by a margin of about 40.
Image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions