|
| 1 | +--- |
| 2 | +jupytext: |
| 3 | + text_representation: |
| 4 | + extension: .md |
| 5 | + format_name: myst |
| 6 | +kernelspec: |
| 7 | + display_name: Python 3 |
| 8 | + name: python3 |
| 9 | +--- |
| 10 | + |
| 11 | +# The amenity basket |
| 12 | + |
| 13 | +A city planner wants every resident to be able to walk to a basket of six everyday |
| 14 | +amenities: a grocery store, a library, a park, a frequent-transit stop, a restaurant, |
| 15 | +and a cafe. This tutorial measures how many residents already have that, and shows |
| 16 | +where the gaps are. The idea follows |
| 17 | +[this analysis](https://nathenry.com/writing/2023-02-07-seattle-walkability.html), |
| 18 | +here applied to Richmond, Virginia. |
| 19 | + |
| 20 | +```{code-cell} python |
| 21 | +:tags: [remove-cell] |
| 22 | +import os |
| 23 | +from closecity import Client |
| 24 | +close = Client(os.environ.get("CLOSECITY_KEY")) |
| 25 | +``` |
| 26 | + |
| 27 | +## Set up |
| 28 | + |
| 29 | +Read the six category ids from the free catalog, and turn the city name into a centre |
| 30 | +point. |
| 31 | + |
| 32 | +```python |
| 33 | +from closecity import Client |
| 34 | + |
| 35 | +close = Client("ck_live_your_key") # use your own key here |
| 36 | +``` |
| 37 | + |
| 38 | +```{code-cell} python |
| 39 | +types = close.destination_types().data["destination_types"] |
| 40 | +ids = {t["label"]: t["dest_type_id"] for t in types} |
| 41 | +basket = { |
| 42 | + "grocery": ids["grocery_stores"], |
| 43 | + "library": ids["libraries"], |
| 44 | + "park": ids["parks"], |
| 45 | + "transit": ids["frequent_transit"], |
| 46 | + "restaurant": ids["restaurants"], |
| 47 | + "cafe": ids["cafes"], |
| 48 | +} |
| 49 | +
|
| 50 | +city = close.places("Richmond").data["places"][0] |
| 51 | +``` |
| 52 | + |
| 53 | +## Pull the blocks, with population |
| 54 | + |
| 55 | +One call gets the walk time from every block near downtown to each of the six |
| 56 | +categories, along with each block's population. |
| 57 | + |
| 58 | +```{code-cell} python |
| 59 | +blocks = close.blocks_query( |
| 60 | + center = {"lon": city["lon"], "lat": city["lat"]}, radius_m = 2500, |
| 61 | + mode = "walk", type = list(basket.values()), include_population = True) |
| 62 | +
|
| 63 | +one_per_block = blocks.drop_duplicates("geoid") |
| 64 | +total_pop = one_per_block["population"].sum() |
| 65 | +``` |
| 66 | + |
| 67 | +## Coverage, one amenity at a time |
| 68 | + |
| 69 | +For each amenity, a block counts as covered when it is within a 15-minute walk. |
| 70 | + |
| 71 | +```{code-cell} python |
| 72 | +for name, type_id in basket.items(): |
| 73 | + covered = set(blocks.loc[(blocks.dest_type_id == type_id) & |
| 74 | + (blocks.travel_time <= 15), "geoid"]) |
| 75 | + pop = one_per_block.loc[one_per_block.geoid.isin(covered), "population"].sum() |
| 76 | + print(f"{name:11} {100 * pop / total_pop:3.0f}%") |
| 77 | +``` |
| 78 | + |
| 79 | +Map one amenity to see the pattern. |
| 80 | + |
| 81 | +```{code-cell} python |
| 82 | +near_transit = set(blocks.loc[(blocks.dest_type_id == basket["transit"]) & |
| 83 | + (blocks.travel_time <= 15), "geoid"]) |
| 84 | +one_per_block = one_per_block.assign( |
| 85 | + has_transit = one_per_block.geoid.isin(near_transit)) |
| 86 | +one_per_block.plot(column = "has_transit", cmap = "Greens") |
| 87 | +``` |
| 88 | + |
| 89 | +## Who can reach all six |
| 90 | + |
| 91 | +A block is fully covered only if all six amenities are within 15 minutes. |
| 92 | + |
| 93 | +```{code-cell} python |
| 94 | +covered_all = set(one_per_block.geoid) |
| 95 | +for type_id in basket.values(): |
| 96 | + covered = set(blocks.loc[(blocks.dest_type_id == type_id) & |
| 97 | + (blocks.travel_time <= 15), "geoid"]) |
| 98 | + covered_all &= covered |
| 99 | +
|
| 100 | +basket_pop = one_per_block.loc[one_per_block.geoid.isin(covered_all), |
| 101 | + "population"].sum() |
| 102 | +print(f"All six amenities: {100 * basket_pop / total_pop:.0f}% of residents") |
| 103 | +
|
| 104 | +one_per_block = one_per_block.assign( |
| 105 | + full_basket = one_per_block.geoid.isin(covered_all)) |
| 106 | +one_per_block.plot(column = "full_basket", cmap = "Oranges") |
| 107 | +``` |
| 108 | + |
| 109 | +## Which amenity to add first |
| 110 | + |
| 111 | +Count how many not-yet-covered residents are missing each amenity. The amenity the |
| 112 | +most people lack is the one to add first. |
| 113 | + |
| 114 | +```{code-cell} python |
| 115 | +uncovered = set(one_per_block.geoid) - covered_all |
| 116 | +for name, type_id in basket.items(): |
| 117 | + covered = set(blocks.loc[(blocks.dest_type_id == type_id) & |
| 118 | + (blocks.travel_time <= 15), "geoid"]) |
| 119 | + lacking = uncovered - covered |
| 120 | + pop = one_per_block.loc[one_per_block.geoid.isin(lacking), "population"].sum() |
| 121 | + print(f"{name:11} {pop:6.0f} residents would gain access") |
| 122 | +``` |
| 123 | + |
| 124 | +Map the uncovered blocks to see where new amenities would do the most good. |
| 125 | + |
| 126 | +```{code-cell} python |
| 127 | +one_per_block = one_per_block.assign( |
| 128 | + uncovered = one_per_block.geoid.isin(uncovered)) |
| 129 | +one_per_block.plot(column = "uncovered", cmap = "Blues") |
| 130 | +``` |
0 commit comments