LocationFinder
LocationFinder class helps in finding locations based on the given GeoDataFrame and location counts.
This class uses BallTree data structure to efficiently query nearby locations. Different types of locations (like schools, playgrounds, healthcare, etc.) can be associated with different default counts to prioritize their significance.
Source code in tripsender\location_assignment.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
__init__(gdf, location_counts=None)
Initialize the LocationFinder with a given GeoDataFrame and optional location counts.
Parameters: - gdf (GeoDataFrame): The input GeoDataFrame containing location data. - location_counts (dict, optional): A dictionary specifying the counts for different location types.
Source code in tripsender\location_assignment.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
find_closest_grocery_locations(origin_point, k=2)
Find the closest grocery locations to a given origin point based on a gravity-based scoring system.
This method first computes the euclidean distance between the origin point and each grocery location. It then calculates a gravity-based score for each grocery location using the gravity_score method. The top 'k' grocery locations with the highest gravity scores are returned.
Parameters: - origin_point (Point): The origin point from which distances and scores are calculated. - k (int, optional): The number of top-scoring grocery locations to return (default is 3).
Returns: - results (list): A list containing Location objects for each of the top 'k' grocery locations.
Source code in tripsender\location_assignment.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
find_closest_locations(origin_point, k=None)
Find the closest locations to a given origin point for each location type.
This method queries the BallTree for each location type to find the closest locations. The number of closest locations for each type is determined by the 'k' parameter or the default count associated with the location type.
Parameters: - origin_point (Point): The origin point from which distances are measured. - k (int, optional): The number of closest locations to return for each location type. If not provided, the default count for each type is used.
Returns: - results (list): A list containing Location objects for each of the closest locations.
Source code in tripsender\location_assignment.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
gravity_score(distance, area, alpha=0, beta=2)
staticmethod
Calculate the gravity-based score for a location based on its area and distance from an origin.
The gravity model, used here, is a spatial interaction model which is based on the idea that the interaction between two places can be determined by the product of the size of one (or both) and divided by their separation distance raised to a power (distance decay).
Parameters: - distance (float): The distance from the origin to the location. - area (float): The size (area) of the location. - alpha (float, optional): The exponent for the area (default is 1.5). - beta (float, optional): The exponent for the distance decay (default is 2).
Returns: - float: The gravity score for the location.
Source code in tripsender\location_assignment.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
populate_ball_trees()
Populate the BallTrees for the different location types based on the GeoDataFrame.
This method creates a BallTree for each location type listed in default_location_counts, allowing for efficient spatial queries. The BallTree, along with the associated GeoDataFrame subset and its count, is stored in the ball_trees dictionary for each location type.
Source code in tripsender\location_assignment.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
set_grocery_data()
Initialize and set the grocery-related data attributes.
This method extracts the data related to the "SHOPPING_GROCERY" activity from the main GeoDataFrame. It sets up the grocery GeoDataFrame, the coordinates of the grocery locations, and their associated areas.
Source code in tripsender\location_assignment.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
assign_jobs_to_workers(gdf_homes)
Assign job locations to workers residing in the buildings of gdf_homes.
For each building in the GeoDataFrame, the function takes the potential job locations and assigns these to every worker residing in that building.
- gdf_homes (GeoDataFrame): A GeoDataFrame containing home data. It should have 'building' and 'potential_jobs' columns, where 'building' objects have a 'worker_list' attribute, and each worker in this list has a 'work_location' attribute.
Returns: - None: The function modifies the 'work_location' attribute of each worker in-place.
Source code in tripsender\location_assignment.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
|
assign_workers_to_buildings()
Assigns workers to buildings based on their primary status.
Source code in tripsender\location_assignment.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
|
compute_job_density(file_path, radius=1000)
Computes job densities for each point in the GeoDataFrame using the given radius.
The function loads a shapefile, calculates the centroid for each geometry and then computes the job density around each centroid using a given radius. The BallTree data structure is utilized for efficient spatial queries.
Parameters: - file_path (str): Path to the shapefile. - radius (float): Radius in meters for which job density is calculated. Default is 1000 meters.
Returns: - gdf_jobs (GeoDataFrame): Updated GeoDataFrame with job densities.
Source code in tripsender\location_assignment.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 |
|
compute_preferred_locations(add_proposed_locations=False)
TODO This can be made faster by grouping buildings into chunks of 200x200m and assigning them together.
Computes and assigns preferred locations for each building instance based on their coordinates.
For each building in the list of instances, this function: 1. Determines the building's coordinate. 2. Finds the closest general locations to that coordinate. 3. Finds the closest grocery locations to that coordinate. 4. Merges the two lists of locations. 5. Initializes a PreferredLocations object using the combined locations. 6. Assigns the PreferredLocations object to the building's preferred_locations attribute.
Source code in tripsender\location_assignment.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
|
gravity_model_simulation(gdf_homes, gdf_jobs, density_weight=0.5, distance_decay=2.5, plot=True)
Simulate job attraction based on the gravity model.
The gravity model is a spatial interaction model that suggests that interaction between two places (for example, the number of people that commute from one place to another for work) is proportional to the product of the population of the two places and inversely proportional to the square of the distance between them.
Parameters: - gdf_homes (GeoDataFrame): A GeoDataFrame containing home data. It must have 'footprint' and 'workers' columns. - gdf_jobs (GeoDataFrame): A GeoDataFrame containing job location data. It must have a 'job_density' column. - density_weight (float): The weight to apply to job density in the attraction calculation. Default is 1. - distance_decay (float): The power to which distance is raised in the attraction calculation. Default is 2. - plot (bool): Whether or not to plot the results. Default is True.
Returns: - gdf_worker_jobs (GeoDataFrame): A GeoDataFrame representing the relationship between homes and potential job locations.
To skew the attraction towards job density - increase the density_weight. To skew the attraction towards distance - increase the distance_decay.
Source code in tripsender\location_assignment.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|