Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add funtions to cal angle to prevent sideroads getting values of main road #524

Conversation

wangzhao0217
Copy link
Collaborator

  • update funtions get_vector and calculate_angle
  • corr_line_geometry is not exist

@wangzhao0217
Copy link
Collaborator Author

wangzhao0217 commented Sep 4, 2023

Hi @Robinlovelace
Currently, I couldn't find a more efficient method to prevent sideroads from getting values from the main road, except to use the angle calculation method. I did the following update on file rnet_join.R

  • add line in function rnet_join 109 rnet_y_centroids$corr_line_geometry = rnet_y$geometry
  • add line in function rnet_merge 256 res_sf$angle = sapply(1:nrow(res_sf), function(i) {
    calculate_angle(get_vector(res_sf$corr_line_geometry[i,]), get_vector(res_sf$geometry[i,]))
    })
  • updating
    for(i in sum_cols) {
      # TODO: deduplicate
      length_y = as.numeric(sf::st_length(rnet_y))
      mask <- (res_sf$angle < 15) | (res_sf$angle > 160)
      # i = sum_cols[1]
      res_sf[mask, i] <- res_sf[mask, i] / res_sf[mask, "length_x"]
      over_estimate <- sum(res_sf[mask, i] * res_sf[mask, "length_x"], na.rm = TRUE) /
        sum(rnet_y[mask, i] * length_y, na.rm = TRUE)
      
      res_sf[mask, i] <- res_sf[mask, i] / over_estimate
    }
get_vector <- function(line) {
  if (class(line) == "LINESTRING") {
    coords <- st_coordinates(line)
    start <- coords[1, 1:2]
    end <- coords[2, 1:2]
  } else {  # For MultiLineStrings, just use the first line
    first_line <- st_cast(line, "LINESTRING")[[1]]
    coords <- st_coordinates(first_line)
    start <- coords[1, 1:2]
    end <- coords[2, 1:2]
  }
  return(c(end[1] - start[1], end[2] - start[2]))
}

calculate_angle <- function(vector1, vector2) {
  dot_product <- sum(vector1 * vector2)
  magnitude_product <- sqrt(sum(vector1^2)) * sqrt(sum(vector2^2))
  cos_angle <- dot_product / magnitude_product
  angle <- acos(cos_angle) * (180 / pi)
  return(angle)
}

However, it is still not working, will keep fixing it.

@Robinlovelace
Copy link
Member

Thanks Zhao, will take a look.

Copy link
Member

@Robinlovelace Robinlovelace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good Zhao. Do you have any results showing the removal of high values on side roads?

R/rnet_join.R Outdated
for(i in sum_cols) {
# TODO: deduplicate
length_y = as.numeric(sf::st_length(rnet_y))
mask <- (res_sf$angle < 15) | (res_sf$angle > 160)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are hard coded. At some point it would be good to make these available as arguments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are hard coded. At some point it would be good to make these available as arguments.

Got it, Robin. Sorry at the moment there is a bug in the code, I am working on it to solve it. (Switching from Python to R for programming is tough that I thought)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's tricky. Keep up the good work and no worries about making these hardcoded values available as arguments, can add later..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Robinlovelace got some results, catch up with you tomorrow
image

Copy link
Member

@Robinlovelace Robinlovelace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Let's discuss and hopefully merge this today @wangzhao0217

@wangzhao0217
Copy link
Collaborator Author

will make new pr

@Robinlovelace
Copy link
Member

Sounds good, looking forward to reviewing it and seeing the results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants