From “Geometries (shapely)”#

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

Exercise 07-f#

import shapely.geometry
import shapely.wkt

pol3 = shapely.wkt.loads("""
MULTIPOLYGON
(((40 40, 20 45, 45 30, 40 40)),
((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), (30 20, 20 15, 20 25, 30 20)))
""")
b3 = shapely.geometry.box(*pol3.bounds)
b3.difference(pol3)
_images/exercise_solutions_shapely_6_0.svg
pol1 = shapely.wkt.loads("POLYGON ((0 0, 0 -1, 7.5 -1, 7.5 0, 0 0))")
pol1.buffer(1).difference(pol1)
_images/exercise_solutions_shapely_8_0.svg

Exercise 07-h#

rect = shapely.wkt.loads("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))")
pnt = shapely.wkt.loads("POINT (1 1)")
circ = pnt.buffer(0.5)
rect.union(circ)
_images/exercise_solutions_shapely_13_0.svg
rect.intersection(circ)
_images/exercise_solutions_shapely_15_0.svg
rect.intersection(circ).area
0.19603428065912124
rect.union(circ).area
1.5881028419773635

Exercise 07-i#

import pandas as pd
import shapely.geometry
stops = pd.read_csv("data/gtfs/stops.txt")
stop_times = pd.read_csv("data/gtfs/stop_times.txt")
stops = stops[["stop_id", "stop_name", "stop_lon", "stop_lat"]]
stop_times = stop_times[["trip_id", "stop_id", "stop_sequence"]]
sel = stop_times["trip_id"] == "55745843_180421"
stop_times = stop_times[sel]
stops = pd.merge(stop_times, stops, on="stop_id")
stops = stops.sort_values("stop_sequence")
coords = stops.loc[:, ["stop_lon", "stop_lat"]].to_numpy().tolist()
stops_pnt = shapely.geometry.MultiPoint(coords)
stops_pnt
_images/exercise_solutions_shapely_21_0.svg
stops_line = shapely.geometry.LineString(coords)
stops_line
_images/exercise_solutions_shapely_23_0.svg
coords1 = [coords[0], coords[len(coords)-1]]
stops_line1 = shapely.geometry.LineString(coords1)
stops_line1
_images/exercise_solutions_shapely_25_0.svg
ch = stops_pnt.convex_hull
ch
_images/exercise_solutions_shapely_27_0.svg