<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def restrict_to_polygon(feed: "Feed", polygon: Polygon) -> "Feed": """ Build a new feed by restricting this one to only the trips that have at least one stop intersecting the given Shapely polygon, then restricting stops, routes, stop times, etc. to those associated with that subset of trips. Return the resulting feed. Requires GeoPandas. Assume the following feed attributes are not ``None``: - ``feed.stop_times`` - ``feed.trips`` - ``feed.stops`` - ``feed.routes`` - Those used in :func:`.stops.get_stops_in_polygon` """

    # Initialize the new feed as the old feed.
    # Restrict its DataFrames below.
    feed = feed.copy()

    # Get IDs of stops within the polygon
    stop_ids = feed.get_stops_in_polygon(polygon)["stop_id"]

    # Get all trips that stop at at least one of those stops
    st = feed.stop_times.copy()
    trip_ids = st[st["stop_id"].isin(stop_ids)]["trip_id"]
    feed.trips = feed.trips[feed.trips["trip_id"].isin(trip_ids)].copy()

    # Get stop times for trips
    feed.stop_times = st[st["trip_id"].isin(trip_ids)].copy()

    # Get stops for trips
    stop_ids = feed.stop_times["stop_id"]
    feed.stops = feed.stops[feed.stops["stop_id"].isin(stop_ids)].copy()

    # Get routes for trips
    route_ids = feed.trips["route_id"]
    feed.routes = feed.routes[feed.routes["route_id"].isin(route_ids)].copy()

    # Get calendar for trips
    service_ids = feed.trips["service_id"]
    if feed.calendar is not None:
        feed.calendar = feed.calendar[
            feed.calendar["service_id"].isin(service_ids)
        ].copy()

    # Get agency for trips
    if "agency_id" in feed.routes.columns:
        agency_ids = feed.routes["agency_id"]
        if len(agency_ids):
            feed.agency = feed.agency[
                feed.agency["agency_id"].isin(agency_ids)
            ].copy()

    # Now for the optional files.
    # Get calendar dates for trips.
    cd = feed.calendar_dates
    if cd is not None:
        feed.calendar_dates = cd[cd["service_id"].isin(service_ids)].copy()

    # Get frequencies for trips
    if feed.frequencies is not None:
        feed.frequencies = feed.frequencies[
            feed.frequencies["trip_id"].isin(trip_ids)
        ].copy()

    # Get shapes for trips
    if feed.shapes is not None:
        shape_ids = feed.trips["shape_id"]
        feed.shapes = feed.shapes[
            feed.shapes["shape_id"].isin(shape_ids)
        ].copy()

    # Get transfers for stops
    if feed.transfers is not None:
        t = feed.transfers
        feed.transfers = t[
            t["from_stop_id"].isin(stop_ids) | t["to_stop_id"].isin(stop_ids)
        ].copy()

    return feed