Home assignment 4#

Last updated: 2023-02-25 13:41:24


Question 1#

• Read the towns (muni_il.shp) layer (see Sample data).

• Aggregate the towns layer according to the 'Muni_Eng' column, to dissolve the separate polygons per town (see Aggregation (.dissolve)).

• Choose a city that starts with the same English letter as your first name (e.g., "Metula" if your first name is Michael).

• Create a subset of the towns layer which includes the neighbors of (i.e., intersecting with) the town you selected.

• Plot the resulting layer, using different colors for the selected town and for the neighbors.

• Note that you cannot specify any specific town names or indices other than the string with the name of the selected town (e.g., "Metula").

Question 2#

• Calculate and plot the density (stops per $$km^2$$) of public transport stops (stops.txt) per town (muni_il.shp). Go through the following steps.

• Read the towns (muni_il.shp) layer (see Sample data).

• Aggregate the towns layer according to the 'Muni_Heb' column, to dissolve the separate polygons per town (see Aggregation (.dissolve)).

• Read the stops.txt table and convert it to a point layer (see Table to point layer).

• Transform the stops layer to the CRS of towns (ITM).

• Calculate the number of stops per town. Hint: use spatial join to join stops to the towns layer, then use aggregation to get a table of stop counts per town, and, finally, join the counts table back to the towns layer.

• Calculate town area in $$km^2$$, then divide the stop count by area to get stops density.

• Plot the towns layer using using the 'Reds' colormap (cmap='Reds') with a legend (legend=True).

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/tmp/ipykernel_13174/1173015350.py in <module>
14 stops = stops.to_crs(towns.crs)
15 # Calculate count per town
---> 16 stops = gpd.sjoin(stops, towns, how='left')
17 stops = stops[['Muni_Heb', 'stop_id']].groupby('Muni_Heb').nunique().reset_index()
18 towns = pd.merge(towns, stops, on='Muni_Heb', how='left')

~/.local/lib/python3.10/site-packages/geopandas/tools/sjoin.py in sjoin(left_df, right_df, how, predicate, lsuffix, rsuffix, **kwargs)
122     _basic_checks(left_df, right_df, how, lsuffix, rsuffix)
123
--> 124     indices = _geom_predicate_query(left_df, right_df, predicate)
125
126     joined = _frame_join(indices, left_df, right_df, how, lsuffix, rsuffix)

~/.local/lib/python3.10/site-packages/geopandas/tools/sjoin.py in _geom_predicate_query(left_df, right_df, predicate)
214             # all other predicates are symmetric
215             # keep them the same
--> 216             sindex = right_df.sindex
217             input_geoms = left_df.geometry
218

~/.local/lib/python3.10/site-packages/geopandas/base.py in sindex(self)
2704                [2]])
2705         """
-> 2706         return self.geometry.values.sindex
2707
2708     @property

~/.local/lib/python3.10/site-packages/geopandas/array.py in sindex(self)
289     def sindex(self):
290         if self._sindex is None:
--> 291             self._sindex = _get_sindex_class()(self.data)
292         return self._sindex
293

~/.local/lib/python3.10/site-packages/geopandas/sindex.py in _get_sindex_class()
19     if compat.HAS_RTREE:
20         return RTreeIndex
---> 21     raise ImportError(
22         "Spatial indexes require either rtree or pygeos. "
23         "See installation instructions at https://geopandas.org/install.html"

ImportError: Spatial indexes require either rtree or pygeos. See installation instructions at https://geopandas.org/install.html