示例#1
0
def runTool(output_stop_file, SQLDbase, time_window_value_table,
            snap_to_nearest_5_minutes):
    def RetrieveFrequencyStatsForStop(stop_id, stoptimedict, start_sec,
                                      end_sec):
        '''For a given stop, query the dictionary
        and return the NumTrips, NumTripsPerHr, MaxWaitTime, and AvgHeadway given a
        specific route_id and direction. If snap to nearest five minutes is true, then
        this function will return headways snapped to the closest 5 minute interval.'''
        # Make a list of stop_times
        StopTimesAtThisPoint = []
        try:
            for trip in stoptimedict[stop_id]:
                StopTimesAtThisPoint.append(trip[1])
        except KeyError:
            pass
        StopTimesAtThisPoint.sort()

        # Calculate the number of trips
        NumTrips = len(StopTimesAtThisPoint)
        NumTripsPerHr = round(
            float(NumTrips) / ((end_sec - start_sec) / 3600), 2)
        # Get the max wait time and the average headway
        MaxWaitTime = BBB_SharedFunctions.CalculateMaxWaitTime(
            StopTimesAtThisPoint, start_sec, end_sec)
        if snap_to_nearest_5_minutes:
            round_to = 5
        else:
            round_to = None
        AvgHeadway = BBB_SharedFunctions.CalculateAvgHeadway(
            StopTimesAtThisPoint, round_to)
        return NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway

    # ----- Get input parameters and set things up. -----
    # Check software version and fail out quickly if it's not sufficient.
    BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2")

    arcpy.AddMessage("Reading data...")

    # Connect to SQL database of preprocessed GTFS from Step 1
    conn = BBB_SharedFunctions.conn = sqlite3.connect(SQLDbase)
    c = BBB_SharedFunctions.c = conn.cursor()

    # Store frequencies if relevant
    frequencies_dict = BBB_SharedFunctions.MakeFrequenciesDict()

    # Get unique route_id/direction_id pairs and calculate the trips used in each
    # Some GTFS datasets use the same route_id to identify trips traveling in
    # either direction along a route. Others identify it as a different route.
    # We will consider each direction separately if there is more than one.
    trip_route_dict = {
    }  # {(route_id, direction_id): [(trip_id, service_id),..]}
    triproutefetch = '''SELECT DISTINCT route_id,direction_id FROM trips;'''
    c.execute(triproutefetch)
    for rtpair in c.fetchall():
        key = tuple(rtpair)
        route_id = rtpair[0]
        direction_id = rtpair[1]
        # Get list of trips
        # Ignore direction if this route doesn't have a direction
        if direction_id is not None and str(direction_id).strip():
            triproutefetch = '''
                    SELECT trip_id, service_id FROM trips
                    WHERE route_id = '{0}' AND direction_id = {1};'''.format(
                route_id, direction_id)
        else:
            triproutefetch = '''
                    SELECT trip_id, service_id FROM trips
                    WHERE route_id = '{0}';'''.format(route_id)
        c.execute(triproutefetch)
        triproutelist = c.fetchall()
        trip_route_dict[key] = triproutelist

    # ----- For each time window, calculate the stop frequency -----
    final_stop_freq_dict = {
    }  # {(stop_id, route_id, direction_id): {prefix: (NumTrips, NumTripsPerHour, MaxWaitTimeSec, AvgHeadwayMin)}}
    # The time_window_value_table will be a list of nested lists of strings like:
    # [[Weekday name or YYYYMMDD date, HH: MM, HH: MM, Departures / Arrivals, Prefix], [], ...]
    for time_window in time_window_value_table:
        # Prefix/identifier associated with this time window
        prefix = time_window[4]
        arcpy.AddMessage("Calculating statistics for time window %s..." %
                         prefix)
        # Clean up date and determine whether it's a date or a weekday
        Specific, day = BBB_SharedFunctions.CheckSpecificDate(time_window[0])
        # Convert times to seconds
        start_time = time_window[1]
        end_time = time_window[2]
        if not start_time:
            start_time = "00:00"
        if not end_time:
            end_time = "23:59"
        start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(
            start_time, end_time)
        # Clean up arrival/departure time choice
        DepOrArr = BBB_SharedFunctions.CleanUpDepOrArr(time_window[3])

        # Get the trips running in this time window for each route/direction pair
        # Get the service_ids serving the correct days
        serviceidlist, serviceidlist_yest, serviceidlist_tom = \
            BBB_SharedFunctions.GetServiceIDListsAndNonOverlaps(day, start_sec, end_sec, DepOrArr, Specific)

        # Retrieve the stop_times for the time window broken out by route/direction
        stoproutedir_dict = {
        }  # {(stop_id, route_id, direction_id): [NumTrips, NumTripsPerHour, MaxWaitTimeSec, AvgHeadwayMin]}
        for rtdirpair in trip_route_dict:
            # Get trips running with these service_ids
            trip_serv_list = trip_route_dict[rtdirpair]
            triplist = []
            for tripserv in trip_serv_list:
                # Only keep trips running on the correct day
                if tripserv[1] in serviceidlist or tripserv[1] in serviceidlist_tom or \
                    tripserv[1] in serviceidlist_yest:
                    triplist.append(tripserv[0])

            # Get the stop_times that occur during this time window for these trips
            try:
                stoptimedict = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(
                    start_sec, end_sec, DepOrArr, triplist, "today",
                    frequencies_dict)
            except KeyError:  # No trips
                pass
            try:
                stoptimedict_yest = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(
                    start_sec, end_sec, DepOrArr, triplist, "yesterday",
                    frequencies_dict)
            except KeyError:  # No trips
                pass
            try:
                stoptimedict_tom = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(
                    start_sec, end_sec, DepOrArr, triplist, "tomorrow",
                    frequencies_dict)
            except KeyError:  # No trips
                pass

            # Combine the three dictionaries into one master
            for stop in stoptimedict_yest:
                stoptimedict[stop] = stoptimedict.setdefault(
                    stop, []) + stoptimedict_yest[stop]
            for stop in stoptimedict_tom:
                stoptimedict[stop] = stoptimedict.setdefault(
                    stop, []) + stoptimedict_tom[stop]

            for stop in stoptimedict.keys():
                # Get Stop-Route-Dir Frequencies by time period
                vals = RetrieveFrequencyStatsForStop(stop, stoptimedict,
                                                     start_sec, end_sec)
                key = (
                    stop,
                    rtdirpair[0],
                    rtdirpair[1],
                )
                if key not in final_stop_freq_dict:
                    final_stop_freq_dict[key] = {prefix: vals}
                else:
                    final_stop_freq_dict[key][prefix] = vals

    # ----- Write the stops and stats to the output feature class -----
    arcpy.AddMessage("Writing outputs...")
    # Make the basic feature class for stops with correct gtfs fields
    with arcpy.EnvManager(overwriteOutput=True):
        output_coords = BBB_SharedFunctions.CreateStopsFeatureClass(
            output_stop_file)

    # Add fields specific to this tool's outputs
    arcpy.management.AddField(output_stop_file, 'route_id', "TEXT")
    arcpy.management.AddField(output_stop_file, 'direction_id', "SHORT")
    # Create fields for stats for each time window using prefix
    base_field_names = [
        '_NumTrips', '_NumTripsPerHr', '_MaxWaitTime', '_AvgHeadway'
    ]
    new_fields = []
    for time_window in time_window_value_table:
        for base_field in base_field_names:
            new_field = time_window[4] + base_field
            new_fields.append(new_field)
            arcpy.management.AddField(output_stop_file, new_field, "DOUBLE")

    # Get the stop info from the GTFS SQL file
    StopTable = BBB_SharedFunctions.GetStopsData()
    stop_dict = {stop[0]: stop for stop in StopTable}

    # Make a dictionary to track whether we have inserted all stops at least once into the output
    used_stops = {stop[0]: False for stop in StopTable}
    # Store stop geometries in dictionary so they can be inserted multiple times without recalculating
    stop_geoms = {
        stop[0]: BBB_SharedFunctions.MakeStopGeometry(stop[4], stop[5],
                                                      output_coords)
        for stop in StopTable
    }

    # Add the stops with stats to the feature class
    fields = [
        "SHAPE@", "stop_id", "stop_code", "stop_name", "stop_desc", "zone_id",
        "stop_url", "location_type", "parent_station", "route_id",
        "direction_id"
    ] + new_fields
    with arcpy.da.InsertCursor(output_stop_file, fields) as cur3:
        # Iterate over all unique stop, route_id, direction_id groups and insert values
        for key in sorted(final_stop_freq_dict.keys()):
            stop_id = key[0]
            used_stops[stop_id] = True
            route_id = key[1]
            direction_id = key[2]
            stop_data = stop_dict[stop_id]
            # Schema of StopTable
            ##   0 - stop_id
            ##   1 - stop_code
            ##   2 - stop_name
            ##   3 - stop_desc
            ##   4 - stop_lat
            ##   5 - stop_lon
            ##   6 - zone_id
            ##   7 - stop_url
            ##   8 - location_type
            ##   9 - parent_station
            row = [
                stop_geoms[stop_id],  # Geometry
                stop_data[0],
                stop_data[1],
                stop_data[2],
                stop_data[3],
                stop_data[6],
                stop_data[7],
                stop_data[8],
                stop_data[9],  # GTFS data
                route_id,
                direction_id  # route and direction IDs
            ]
            # Populate stats fields for each prefix
            for time_window in time_window_value_table:
                prefix = time_window[4]
                try:
                    vals = final_stop_freq_dict[key][prefix]
                except KeyError:
                    # This stop/route/direction group had no service for this time window
                    vals = [0, 0, None, None]
                row += vals

            # Insert the row
            cur3.insertRow(row)

        # Insert row for any remaining stops that were not used at all
        for stop_id in used_stops:
            if used_stops[stop_id]:
                # This one was already inserted
                continue
            stop_data = stop_dict[stop_id]
            row = [
                stop_geoms[stop_id],  # Geometry
                stop_data[0],
                stop_data[1],
                stop_data[2],
                stop_data[3],
                stop_data[6],
                stop_data[7],
                stop_data[8],
                stop_data[9],  # GTFS data
                None,
                None  # route and direction IDs - None because not used
            ]
            # Populate stats fields for each prefix
            for time_window in time_window_value_table:
                row += [0, 0, None, None]
            # Insert the row
            cur3.insertRow(row)

    # Close Connection
    conn.close()
    arcpy.AddMessage("Finished!")
    arcpy.AddMessage("Calculated trip counts, frequency, max wait time, and \
headway were written to an output stops file by route-direction pairs.")
示例#2
0
def runTool(FCs, SQLDbase, dayString, start_time, end_time, DepOrArrChoice):

    def RetrieveStatsForStop(stop_id, rtdirtuple):
        '''For a given stop, query the stoptimedict {stop_id: [[trip_id, stop_time]]}
        and return the NumTrips, NumTripsPerHr, MaxWaitTime, and AvgHeadway given a
        specific route_id and direction'''

        try:
            stoptimedict = stoptimedict_rtdirpair[rtdirtuple]
        except KeyError:
            # We will get a KeyError if there were no trips found for the route/direction
            # pair, which usually happens if the wrong SQL database was selected.
            stoptimedict = {}

        # Make a list of stop_times
        StopTimesAtThisPoint = []
        try:
            for trip in stoptimedict[stop_id]:
                StopTimesAtThisPoint.append(trip[1])
        except KeyError:
            pass
        StopTimesAtThisPoint.sort()

        # Calculate the number of trips
        NumTrips = len(StopTimesAtThisPoint)
        NumTripsPerHr = float(NumTrips) / TimeWindowLength

        # Get the max wait time and the average headway
        MaxWaitTime = BBB_SharedFunctions.CalculateMaxWaitTime(StopTimesAtThisPoint, start_sec, end_sec)
        AvgHeadway = BBB_SharedFunctions.CalculateAvgHeadway(StopTimesAtThisPoint)

        return NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway

    try:
        # ------ Get input parameters and set things up. -----
        try:
            OverwriteOutput = arcpy.env.overwriteOutput # Get the orignal value so we can reset it.
            arcpy.env.overwriteOutput = True

            BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2")

            # Stops and Polygons from Step 1 (any number and route combo)
            FCList = FCs.split(";")
            # Remove single quotes ArcGIS puts in if there are spaces in the filename.
            for d in FCList:
                if d[0] == "'" and d[-1] == "'":
                    loc = FCList.index(d)
                    FCList[loc] = d[1:-1]

            # Get list of field names from the input data and check that the required ones are there
            FieldNames = {}
            RequiredFields = ["stop_id", "route_id", "direction_id"]
            for FC in FCList:
                Fields = arcpy.ListFields(FC)
                FieldNames[FC] = [f.name for f in Fields]
                for field in RequiredFields:
                    if not field in FieldNames[FC]:
                        arcpy.AddError("Feature class %s does not have the required \
fields %s. Please choose a valid feature class." % (FC, str(RequiredFields)))
                        raise BBB_SharedFunctions.CustomError

            # SQL database of preprocessed GTFS from Step 1
            conn = BBB_SharedFunctions.conn = sqlite3.connect(SQLDbase)
            c = BBB_SharedFunctions.c = conn.cursor()

            Specific, day = BBB_SharedFunctions.CheckSpecificDate(dayString)
            # For field names in the output file
            if Specific:
                dayshort = BBB_SharedFunctions.days[day.weekday()][0:3] 
            else:
                dayshort = dayString[0:3]
            
            if start_time == "":
                start_time = "00:00"
            start_time_pretty = start_time.replace(":", "") # For field names in the output file
            if end_time == "":
                end_time = "23:59"
            end_time_pretty = end_time.replace(":", "") # For field names in the output file
            start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(start_time, end_time)
            TimeWindowLength = (end_sec - start_sec) / 3600

            # Does the user want to count arrivals or departures at the stops?
            DepOrArr = BBB_SharedFunctions.CleanUpDepOrArr(DepOrArrChoice)

        except:
            arcpy.AddError("Error getting inputs.")
            raise


        # ----- Get list of route_ids and direction_ids to analyze from input files -----
        try:
            # We just check the first line in each file for this information.
            FC_route_dir_dict = {} # {FC: [route_id, direction_id]}
            route_dir_list = [] # [[route_id, direction_id], ...]
            for FC in FCList:
                with arcpy.da.SearchCursor(FC, ["route_id", "direction_id"]) as cur:
                    rt_dir = cur.next()
                route_dir_pair = [rt_dir[0], rt_dir[1]]
                FC_route_dir_dict[FC] = route_dir_pair
                if not route_dir_pair in route_dir_list:
                    route_dir_list.append(route_dir_pair)

        except:
            arcpy.AddError("Error getting route_id and direction_id values from input feature classes.")
            raise


        # ----- Get trips associated with route and direction -----

        try:
            arcpy.AddMessage("Getting list of trips...")

            # Get the service_ids serving the correct days
            serviceidlist, serviceidlist_yest, serviceidlist_tom = \
                BBB_SharedFunctions.GetServiceIDListsAndNonOverlaps(day, start_sec, end_sec, DepOrArr, Specific)

            trip_route_dict = {} #{(route_id, direction_id): [trip_id, trip_id,..]}
            trip_route_dict_yest = {}
            trip_route_dict_tom = {}
            for rtpair in route_dir_list:
                key = tuple(rtpair)
                route_id = rtpair[0]
                direction_id = rtpair[1]

                # Get list of trips
                # Ignore direction if this route doesn't have a direction
                if direction_id:
                    triproutefetch = '''
                        SELECT trip_id, service_id FROM trips
                        WHERE route_id='%s'
                        AND direction_id=%s
                        ;''' % (route_id, direction_id)
                else:
                    triproutefetch = '''
                        SELECT trip_id, service_id FROM trips
                        WHERE route_id='%s'
                        ;''' % route_id
                c.execute(triproutefetch)
                triproutelist = c.fetchall()

                if not triproutelist:
                    arcpy.AddWarning("Your GTFS dataset does not contain any trips \
corresponding to Route %s and Direction %s. Please ensure that \
you have selected the correct GTFS SQL file for this input file or that your \
GTFS data is good. Output fields will be generated, but \
the values will be 0 or <Null>." % (route_id, str(direction_id)))

                for triproute in triproutelist:
                    # Only keep trips running on the correct day
                    if triproute[1] in serviceidlist:
                        trip_route_dict.setdefault(key, []).append(triproute[0])
                    if triproute[1] in serviceidlist_tom:
                        trip_route_dict_tom.setdefault(key, []).append(triproute[0])
                    if triproute[1] in serviceidlist_yest:
                        trip_route_dict_yest.setdefault(key, []).append(triproute[0])

                if not trip_route_dict and not trip_route_dict_tom and not trip_route_dict_yest:
                    arcpy.AddWarning("There is no service for route %s in direction %s \
on %s during the time window you selected. Output fields will be generated, but \
the values will be 0 or <Null>." % (route_id, str(direction_id), str(day)))

        except:
            arcpy.AddError("Error getting trips associated with route.")
            raise


        #----- Query the GTFS data to count the trips at each stop -----
        try:
            arcpy.AddMessage("Calculating the number of transit trips available during the time window...")

            frequencies_dict = BBB_SharedFunctions.MakeFrequenciesDict()

            stoptimedict_rtdirpair = {}
            for rtdirpair in list(set([rt for rt in list(trip_route_dict.keys()) + list(trip_route_dict_yest.keys()) + list(trip_route_dict_tom.keys())])):

                # Get the stop_times that occur during this time window
                stoptimedict = {}
                stoptimedict_yest = {}
                stoptimedict_tom = {}
                try:
                    triplist = trip_route_dict[rtdirpair]
                    stoptimedict = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(start_sec, end_sec, DepOrArr, triplist, "today", frequencies_dict)
                except KeyError: # No trips
                    pass
                try:
                    triplist_yest = trip_route_dict_yest[rtdirpair]
                    stoptimedict_yest = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(start_sec, end_sec, DepOrArr, triplist_yest, "yesterday", frequencies_dict)
                except KeyError: # No trips
                    pass
                try:
                    triplist_tom = trip_route_dict_tom[rtdirpair]
                    stoptimedict_tom = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(start_sec, end_sec, DepOrArr, triplist_tom, "tomorrow", frequencies_dict)
                except KeyError: # No trips
                    pass

                # Combine the three dictionaries into one master
                for stop in stoptimedict_yest:
                    stoptimedict[stop] = stoptimedict.setdefault(stop, []) + stoptimedict_yest[stop]
                for stop in stoptimedict_tom:
                    stoptimedict[stop] = stoptimedict.setdefault(stop, []) + stoptimedict_tom[stop]

                stoptimedict_rtdirpair[rtdirpair] = stoptimedict

                # Add a warning if there is no service.
                if not stoptimedict:
                    arcpy.AddWarning("There is no service for route %s in direction %s \
on %s during the time window you selected. Output fields will be generated, but \
the values will be 0 or <Null>." % (rtdirpair[0], str(rtdirpair[1]), dayString))

        except:
            arcpy.AddError("Error counting arrivals or departures at stop during time window.")
            raise


        #----- Write to output -----

        arcpy.AddMessage("Writing output...")

        try:
            # Prepare the fields we're going to add to the feature classes
            ending = "_" + dayshort + "_" + start_time_pretty + "_" + end_time_pretty
            fields_to_fill = ["NumTrips" + ending, "NumTripsPerHr" + ending, "MaxWaitTime" + ending, "AvgHeadway" + ending]
            fields_to_read = ["stop_id", "route_id", "direction_id"] + fields_to_fill
            field_type_dict = {"NumTrips" + ending: "Short", "NumTripsPerHr" + ending: "Double", "MaxWaitTime" + ending: "Short", "AvgHeadway" + ending: "Short"}

            for FC in FCList:
                # We probably need to add new fields for our calculations, but if the field
                # is already there, don't add it because we'll overwrite it.
                for field in fields_to_fill:
                    if field not in FieldNames[FC]:
                        arcpy.management.AddField(FC, field, field_type_dict[field])
                with arcpy.da.UpdateCursor(FC, fields_to_read) as cur2:
                    for row in cur2:
                        rtpairtuple = (row[1], row[2]) # (route_id, direction_id)
                        stop = row[0]
                        NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway = RetrieveStatsForStop(stop, rtpairtuple)
                        row[3] = NumTrips
                        row[4] = NumTripsPerHr
                        row[5] = MaxWaitTime
                        row[6] = AvgHeadway
                        cur2.updateRow(row)

        except:
            arcpy.AddError("Error writing output to feature class(es).")
            raise

        arcpy.AddMessage("Finished!")
        arcpy.AddMessage("Calculated trip counts, frequency, max wait time, and \
headway were written to the following fields in your input feature class(es):")
        for field in fields_to_fill:
            arcpy.AddMessage("- " + field)

        # Tell the tool that this is output. This will add the output to the map.
        arcpy.SetParameterAsText(6, FCs)

    except BBB_SharedFunctions.CustomError:
        arcpy.AddError("Failed to calculate transit statistics for this route and time window.")
        pass

    except:
        arcpy.AddError("Failed to calculate transit statistics for this route and time window.")
        raise

    finally:
        arcpy.env.overwriteOutput = OverwriteOutput
def runTool(step1LinesFC, SQLDbase, linesFC, day, start_time, end_time):
    try:
        # ------ Get input parameters and set things up. -----

        BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2")

        try:
            # If it was a feature layer, it will have a data source property
            step1LinesFC = step1LinesFC.dataSource
        except:
            # Otherwise, assume it was a catalog path and use as is
            pass

        # GTFS SQL dbase - must be created ahead of time.
        BBB_SharedFunctions.ConnectToSQLDatabase(SQLDbase)

        Specific, day = BBB_SharedFunctions.CheckSpecificDate(day)
        start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(
            start_time, end_time)

        # Does the user want to count arrivals or departures at the stops?
        DepOrArr = "departure_time"

        # ----- Prepare output file -----

        try:
            arcpy.management.Copy(step1LinesFC, linesFC)
        except:
            arcpy.AddError(
                "Error copying template lines feature class to output %s," %
                linesFC)
            raise

        # ----- Query the GTFS data to count the trips on each line segment -----
        try:
            arcpy.AddMessage(
                "Calculating the number of transit trips available during the time window..."
            )

            # Get a dictionary of {line_key: [[trip_id, start_time, end_time]]} for our time window
            linetimedict = BBB_SharedFunctions.CountTripsOnLines(
                day, start_sec, end_sec, DepOrArr, Specific)

        except:
            arcpy.AddError(
                "Error counting arrivals or departures at during time window.")
            raise

        # ----- Write to output -----
        try:
            arcpy.AddMessage("Writing output data...")

            combine_corridors = "route_id" not in [
                f.name for f in arcpy.ListFields(linesFC)
            ]

            triproute_dict = None
            if not combine_corridors:
                triproute_dict = BBB_SharedFunctions.MakeTripRouteDict()

            arcpy.management.AddField(linesFC, "NumTrips", "SHORT")
            arcpy.management.AddField(linesFC, "NumTripsPerHr", "DOUBLE")
            arcpy.management.AddField(linesFC, "MaxWaitTime", "SHORT")
            arcpy.management.AddField(linesFC, "AvgHeadway", "SHORT")

            with arcpy.da.UpdateCursor(linesFC, [
                    "pair_id", "NumTrips", "NumTripsPerHr", "MaxWaitTime",
                    "AvgHeadway"
            ]) as ucursor:
                for row in ucursor:
                    NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway = \
                                BBB_SharedFunctions.RetrieveStatsForLines(
                                    str(row[0]), linetimedict,
                                    start_sec, end_sec, combine_corridors, triproute_dict)
                    row[1] = NumTrips
                    row[2] = NumTripsPerHr
                    row[3] = MaxWaitTime
                    row[4] = AvgHeadway
                    ucursor.updateRow(row)

        except:
            arcpy.AddError("Error writing to output.")
            raise

        arcpy.AddMessage("Finished!")
        arcpy.AddMessage("Your output is located at " + linesFC)

    except BBB_SharedFunctions.CustomError:
        arcpy.AddError("Failed to count trips on lines.")
        pass

    except:
        arcpy.AddError("Failed to count trips on lines.")
        raise
示例#4
0
def runTool(outStops, SQLDbase, day, start_time, end_time, DepOrArrChoice,
            FrequencyThreshold, SnapToNearest5MinuteBool):
    def RetrieveFrequencyStatsForStop(stop_id,
                                      rtdirtuple,
                                      snap_to_nearest_5_minutes=False):
        '''For a given stop, query the stop_time_dictionaries {stop_id: [[trip_id, stop_time]]}
        and return the NumTrips, NumTripsPerHr, MaxWaitTime, and AvgHeadway given a
        specific route_id and direction. If snap to nearest five minutes is true, then
        this function will return headways snapped to the closest 5 minute interval.'''
        try:
            stop_time_dictionaries = stoptimedict_rtedirpair[rtdirtuple]
        except KeyError:
            # We will get a KeyError if there were no trips found for the route/direction
            # pair, which usually happens if the wrong SQL database was selected.
            stop_time_dictionaries = {}

        # Make a list of stop_times
        StopTimesAtThisPoint = []
        try:
            for trip in stop_time_dictionaries[stop_id]:
                StopTimesAtThisPoint.append(trip[1])
        except KeyError:
            pass
        StopTimesAtThisPoint.sort()

        # Calculate the number of trips
        NumTrips = len(StopTimesAtThisPoint)
        NumTripsPerHr = float(NumTrips) / TimeWindowLength
        # Get the max wait time and the average headway
        MaxWaitTime = BBB_SharedFunctions.CalculateMaxWaitTime(
            StopTimesAtThisPoint, start_sec, end_sec)
        AvgHeadway = None
        if NumTrips > 1:
            AvgHeadway = max(
                1,
                int(
                    round(
                        float(
                            sum(
                                abs(x - y)
                                for (x, y) in zip(StopTimesAtThisPoint[1:],
                                                  StopTimesAtThisPoint[:-1])) /
                            (len(StopTimesAtThisPoint) - 1)) / 60,
                        0)))  # minutes
            if snap_to_nearest_5_minutes:
                AvgHeadway = round(AvgHeadway / 5.0) * 5
        return NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway

    try:
        # ------ Get input parameters and set things up. -----
        try:
            arcpy.env.overwriteOutput = True

            BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2",
                                                min_version_10x="10.4")

            try:
                import pandas as pd
            except:
                # Pandas is shipped with ArcGIS Pro and ArcGIS 10.4 and higher.  The previous logic should hopefully prevent users from ever hitting this error.
                arcpy.AddError(
                    "This BetterBusBuffers tool requires the python library pandas, but the tool was unable to import the library."
                )
                raise BBB_SharedFunctions.CustomError

            # GTFS SQL dbase - must be created ahead of time.
            conn = BBB_SharedFunctions.conn = sqlite3.connect(SQLDbase)
            c = BBB_SharedFunctions.c = conn.cursor()

            Specific, day = BBB_SharedFunctions.CheckSpecificDate(day)
            start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(
                start_time, end_time)
            # Window of Time In Hours
            TimeWindowLength = (end_sec - start_sec) / 3600

            # threshold for headways to be counted
            FrequencyThreshold = float(FrequencyThreshold)
            # boolean will snap headways to nearest 5 minute increment (ie 11.5 minutes snaps to 10, but 13 snaps to 15)
            SnapToNearest5MinuteBool = bool(SnapToNearest5MinuteBool)
            # Does the user want to count arrivals or departures at the stops?
            DepOrArr = BBB_SharedFunctions.CleanUpDepOrArr(DepOrArrChoice)
            time_period = start_time + ":" + end_time

        except:
            arcpy.AddError("Error getting user inputs.")
            raise

        # ----- Create a feature class of stops and add fields for transit trip counts ------
        try:
            arcpy.AddMessage("Creating feature class of GTFS stops...")
            # Create a feature class of transit stops
            outStops, StopIDList = BBB_SharedFunctions.MakeStopsFeatureClass(
                outStops)
        except:
            arcpy.AddError("Error creating feature class of GTFS stops.")
            raise

        # ----- Query the GTFS data to count the trips at each stop -----
        try:
            arcpy.AddMessage(
                "Calculating the determining trips for route-direction pairs..."
            )

            # Get the service_ids serving the correct days
            serviceidlist, serviceidlist_yest, serviceidlist_tom = \
                BBB_SharedFunctions.GetServiceIDListsAndNonOverlaps(day, start_sec, end_sec, DepOrArr, Specific)

            # Assemble Route and Direction IDS
            triproutefetch = '''SELECT DISTINCT route_id,direction_id FROM trips;'''
            c.execute(triproutefetch)
            #route_dir_list = c.fetchall()

            # Some GTFS datasets use the same route_id to identify trips traveling in
            # either direction along a route. Others identify it as a different route.
            # We will consider each direction separately if there is more than one.
            trip_route_warning_counter = 0
            trip_route_dict = {
            }  # {(route_id, direction_id): [trip_id, trip_id,..]}
            trip_route_dict_yest = {}
            trip_route_dict_tom = {}
            triproutelist = []
            c2 = conn.cursor()
            for rtpair in c:
                key = tuple(rtpair)
                route_id = rtpair[0]
                direction_id = rtpair[1]
                # Get list of trips
                # Ignore direction if this route doesn't have a direction
                if direction_id not in [
                        None, ""
                ]:  # GTFS can have direction IDs of zero
                    triproutefetch = '''
                            SELECT trip_id, service_id FROM trips
                            WHERE route_id='%s'
                            AND direction_id=%s
                            ;''' % (route_id, direction_id)
                else:
                    triproutefetch = '''
                            SELECT trip_id, service_id FROM trips
                            WHERE route_id='%s'
                            ;''' % route_id
                c2.execute(triproutefetch)
                triproutelist = c2.fetchall()
                if not triproutelist:
                    arcpy.AddWarning(
                        "Your GTFS dataset does not contain any trips \
corresponding to Route %s and Direction %s. Please ensure that \
you have selected the correct GTFS SQL file for this input file or that your \
GTFS data is good. Output fields will be generated, but \
the values will be 0 or <Null>." % (route_id, str(direction_id)))

                for triproute in triproutelist:
                    # Only keep trips running on the correct day
                    if triproute[1] in serviceidlist:
                        trip_route_dict.setdefault(key,
                                                   []).append(triproute[0])
                    if triproute[1] in serviceidlist_tom:
                        trip_route_dict_tom.setdefault(key,
                                                       []).append(triproute[0])
                    if triproute[1] in serviceidlist_yest:
                        trip_route_dict_yest.setdefault(key, []).append(
                            triproute[0])

                if not trip_route_dict and not trip_route_dict_tom and not trip_route_dict_yest:
                    arcpy.AddWarning(
                        "There is no service for route %s in direction %s \
on %s during the time window you selected. Output fields will be generated, but \
the values will be 0 or <Null>." % (route_id, str(direction_id), str(day)))

        except:
            arcpy.AddError("Error getting trips associated with route.")
            raise

        # ----- Query the GTFS data to count the trips at each stop for this time period -----
        try:
            arcpy.AddMessage(
                "Calculating the number of transit trips available during the time window of time period ID {0}..."
                .format(str(time_period)))

            frequencies_dict = BBB_SharedFunctions.MakeFrequenciesDict()

            stoptimedict_rtedirpair = {}  # #{rtdir tuple:stoptimedict}}
            stoptimedict_service_check_counter = 0
            for rtdirpair in list(
                    set([
                        rt for rt in list(trip_route_dict.keys()) +
                        list(trip_route_dict_yest.keys()) +
                        list(trip_route_dict_tom.keys())
                    ])):

                # Get the stop_times that occur during this time window
                stoptimedict = {}
                stoptimedict_yest = {}
                stoptimedict_tom = {}
                try:
                    triplist = trip_route_dict[rtdirpair]
                    stoptimedict = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(
                        start_sec, end_sec, DepOrArr, triplist, "today",
                        frequencies_dict)
                except KeyError:  # No trips
                    pass
                try:
                    triplist_yest = trip_route_dict_yest[rtdirpair]
                    stoptimedict_yest = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(
                        start_sec, end_sec, DepOrArr, triplist_yest,
                        "yesterday", frequencies_dict)
                except KeyError:  # No trips
                    pass
                try:
                    triplist_tom = trip_route_dict_tom[rtdirpair]
                    stoptimedict_tom = BBB_SharedFunctions.GetStopTimesForStopsInTimeWindow(
                        start_sec, end_sec, DepOrArr, triplist_tom, "tomorrow",
                        frequencies_dict)
                except KeyError:  # No trips
                    pass

                # Combine the three dictionaries into one master
                for stop in stoptimedict_yest:  # Update Dictionaries based on setdefault returns values.
                    stoptimedict[stop] = stoptimedict.setdefault(
                        stop, []) + stoptimedict_yest[stop]
                for stop in stoptimedict_tom:
                    stoptimedict[stop] = stoptimedict.setdefault(
                        stop, []) + stoptimedict_tom[stop]
                # PD here
                stoptimedict_rtedirpair[
                    rtdirpair] = stoptimedict  # {rtdir tuple:{stoptimedict}}
                # Add a minor warning if there is no service for at least one route-direction combination.
                if not stoptimedict:
                    stoptimedict_service_check_counter += 1
            if stoptimedict_service_check_counter > 0:
                arcpy.AddWarning(
                    "There is no service for %s route-direction pair(s) \
on %s during the time window you selected. Output fields will be generated, but \
the values will be 0 or <Null>." %
                    (str(stoptimedict_service_check_counter), str(day)))

        except:
            arcpy.AddError(
                "Error counting arrivals or departures at stop during time window."
            )
            raise

        # ----- Write to output -----

        try:
            arcpy.AddMessage(
                "Calculating frequency statistics from route direction pairs..."
            )
            frequency_record_table = [
            ]  #[(rtedirpair_id,route_id,direction_id,stop_id,NumTripsPerHr,MaxWaitTime,AvgHeadway)]
            labels = [
                "rtedir_id", "rte_count", "stop_id", "NumTrips",
                "NumTripsPerHr", "MaxWaitTime", "AvgHeadway"
            ]
            for rtedirpair in stoptimedict_rtedirpair:
                route_id = rtedirpair[0]
                stops = stoptimedict_rtedirpair[rtedirpair].keys()
                for stop_id in stops:
                    NumTrips, NumTripsPerHr, MaxWaitTime, AvgHeadway = RetrieveFrequencyStatsForStop(
                        stop_id,
                        rtedirpair,
                        snap_to_nearest_5_minutes=SnapToNearest5MinuteBool)
                    AvgHeadway = post_process_headways(AvgHeadway,
                                                       NumTripsPerHr)
                    frequency_record_table.append(
                        (rtedirpair, route_id, stop_id, NumTrips,
                         NumTripsPerHr, MaxWaitTime, AvgHeadway))
            frequency_dataframe = pd.DataFrame.from_records(
                frequency_record_table, columns=labels)
            #Count the number of routes that meet threshold
            frequency_dataframe["MetHdWyLim"] = 1
            frequency_dataframe["MetHdWyLim"].where(
                frequency_dataframe["AvgHeadway"] <= FrequencyThreshold,
                np.nan,
                inplace=True)
            #Add Fields for frequency aggregation
            frequency_dataframe["MinHeadway"] = frequency_dataframe[
                "AvgHeadway"]
            frequency_dataframe["MaxHeadway"] = frequency_dataframe[
                "AvgHeadway"]
            output_stats = collections.OrderedDict([("NumTrips", ("sum")),
                                                    ("NumTripsPerHr", ("sum")),
                                                    ("MaxWaitTime", ("max")),
                                                    ("rte_count", ("count")),
                                                    ("AvgHeadway", ("mean")),
                                                    ("MinHeadway", ("min")),
                                                    ("MaxHeadway", ("max")),
                                                    ("MetHdWyLim", ("sum"))])
            stop_groups = frequency_dataframe.groupby("stop_id")
            stop_frequency_statistics = stop_groups.agg(output_stats)
            if ".shp" in outStops:
                # Set up shapefile accommodations for long fields (>10 chars) & null values
                stop_frequency_statistics.rename(columns={
                    "NumTripsPerHr": "TripsPerHr",
                    "MaxWaitTime": "MxWtTime"
                },
                                                 inplace=True)
                stop_frequency_statistics = stop_frequency_statistics.fillna(
                    value=-1)

        except:
            arcpy.AddError("Error calculating frequency statistics...")
            raise
        try:
            arcpy.AddMessage("Writing output data...")
            # Create an update cursor to add numtrips, trips/hr, maxwaittime, and headway stats to stops
            frequency_records = stop_frequency_statistics.to_records()
            arcpy.da.ExtendTable(outStops,
                                 "stop_id",
                                 frequency_records,
                                 "stop_id",
                                 append_only=False)
            arcpy.AddMessage("Script complete!")
        except:
            arcpy.AddError("Error writing to output.")
            raise

        arcpy.AddMessage("Finished!")
        arcpy.AddMessage("Your output is located at " + outStops)

    except BBB_SharedFunctions.CustomError:
        arcpy.AddError("Failed to count high frequency routes at stops.")
        pass

    except:
        arcpy.AddError("Failed to count high frequency routes at stops.")
        raise
示例#5
0
def runTool(inStep1GDB, outFile, day, start_time, end_time, DepOrArrChoice):
    try:

        # ----- Set up the run -----
        try:
            BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2")

            # Get the files from Step 1 to work with.
            # Step1_GTFS.sql and Step1_FlatPolys must exist in order for the tool to run.
            # Their existence is checked in the GUI validation logic.
            FlatPolys = os.path.join(inStep1GDB, "Step1_FlatPolys")
            SQLDbase = os.path.join(inStep1GDB, "Step1_GTFS.sql")
            # Connect to the SQL database
            conn = BBB_SharedFunctions.conn = sqlite3.connect(SQLDbase)
            c = BBB_SharedFunctions.c = conn.cursor()

            # Output file designated by user
            outDir = os.path.dirname(outFile)
            outFilename = os.path.basename(outFile)

            Specific, day = BBB_SharedFunctions.CheckSpecificDate(day)
            start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(
                start_time, end_time)

            # Will we calculate the max wait time? This slows down the calculation, so leave it optional.
            CalcWaitTime = True

            # It's okay to overwrite stuff.
            OverwriteOutput = arcpy.env.overwriteOutput  # Get the orignal value so we can reset it.
            arcpy.env.overwriteOutput = True

        except:
            arcpy.AddError("Error setting up run.")
            raise

        #----- Query the GTFS data to count the trips at each stop -----
        try:
            arcpy.AddMessage(
                "Counting transit trips during the time window...")

            # Get a dictionary of stop times in our time window {stop_id: [[trip_id, stop_time]]}
            stoptimedict = BBB_SharedFunctions.CountTripsAtStops(
                day, start_sec, end_sec,
                BBB_SharedFunctions.CleanUpDepOrArr(DepOrArrChoice), Specific)

        except:
            arcpy.AddError(
                "Failed to count transit trips during the time window.")
            raise

        #----- Find which stops serve each polygon -----
        try:
            arcpy.AddMessage(
                "Retrieving list of stops associated with each polygon...")
            # Find the stop_ids associated with each flattened polygon and put them in
            # a dictionary. {ORIG_FID: [stop_id, stop_id,...]}
            stackedpointdict = {}
            GetStackedPtsStmt = "SELECT * FROM StackedPoints"
            c.execute(GetStackedPtsStmt)
            for PolyFID in c:
                stackedpointdict.setdefault(PolyFID[0],
                                            []).append(str(PolyFID[1]))
        except:
            arcpy.AddError(
                "Error retrieving list of stops associated with each polygon.")
            raise

        # ----- Generate output data -----
        try:
            arcpy.AddMessage("Writing output data...")

            # Create the output file from FlatPolys.  We don't want to overwrite the
            # original Step 1 template file.
            arcpy.management.CopyFeatures(FlatPolys, outFile)
            badpolys = []

            if ".shp" in outFilename:
                ucursor = arcpy.da.UpdateCursor(outFile, [
                    "PolyID", "NumTrips", "NumTripsPe", "NumStopsIn",
                    "MaxWaitTim"
                ])
            else:
                ucursor = arcpy.da.UpdateCursor(outFile, [
                    "PolyID", "NumTrips", "NumTripsPerHr", "NumStopsInRange",
                    "MaxWaitTime"
                ])
            for row in ucursor:
                try:
                    ImportantStops = stackedpointdict[int(row[0])]
                except KeyError:
                    # If we got a KeyError here, then an output polygon never
                    # got a point associated with it, probably the result of a
                    # geometry problem because of the large cluster tolerance
                    # used to generate the polygons in Step 1. Just skip this
                    # polygon and alert the user.
                    badpolys.append(row[0])
                    continue
                NumTrips, NumTripsPerHr, NumStopsInRange, MaxWaitTime = \
                                BBB_SharedFunctions.RetrieveStatsForSetOfStops(
                                    ImportantStops, stoptimedict, CalcWaitTime,
                                    start_sec, end_sec)
                row[1] = NumTrips
                row[2] = NumTripsPerHr
                row[3] = NumStopsInRange
                if ".shp" in outFilename and MaxWaitTime == None:
                    row[4] = -1
                else:
                    row[4] = MaxWaitTime
                ucursor.updateRow(row)

            if badpolys:
                arcpy.AddWarning(
                    "Warning! BetterBusBuffers could not calculate trip \
statistics for one or more polygons due to a geometry issue. These polygons will \
appear in your output data, but all output values will be null. Bad polygon \
PolyID values: " + str(badpolys))

        except:
            arcpy.AddMessage("Error writing output.")
            raise

        arcpy.AddMessage("Finished!")
        arcpy.AddMessage("Your output is located at " + outFile)

    except CustomError:
        arcpy.AddError("Error counting transit trips in polygons.")
        pass

    except:
        arcpy.AddError("Error counting transit trips in polygons.")
        raise

    finally:
        # Reset overwriteOutput to what it was originally.
        arcpy.env.overwriteOutput = OverwriteOutput
def runTool(outFile, SQLDbase, inPointsLayer, inLocUniqueID, day, start_time,
            end_time, BufferSize, BufferUnits, DepOrArrChoice, username,
            password):
    def runOD(Points, Stops):
        # Call the OD Cost Matrix service for this set of chunks
        result = ODservice.GenerateOriginDestinationCostMatrix(
            Points,
            Stops,
            TravelMode,
            Distance_Units=BufferUnits,
            Cutoff=BufferSize,
            Origin_Destination_Line_Shape=PathShape)

        # Check the status of the result object every 0.5 seconds
        # until it has a value of 4(succeeded) or greater
        while result.status < 4:
            time.sleep(0.5)

        # Print any warning or error messages returned from the tool
        result_severity = result.maxSeverity
        if result_severity == 2:
            errors = result.getMessages(2)
            if "No solution found." in errors:
                # No destinations were found for the origins, which probably just means they were too far away.
                pass
            else:
                arcpy.AddError("An error occured when running the tool")
                arcpy.AddError(result.getMessages(2))
                raise BBB_SharedFunctions.CustomError
        elif result_severity == 1:
            arcpy.AddWarning("Warnings were returned when running the tool")
            arcpy.AddWarning(result.getMessages(1))

        # Get the resulting OD Lines and store the stops that are reachable from points.
        if result_severity != 2:
            linesSubLayer = result.getOutput(1)
            with arcpy.da.SearchCursor(
                    linesSubLayer,
                ["OriginOID", "DestinationOID"]) as ODCursor:
                for row in ODCursor:
                    UID = pointsOIDdict[row[0]]
                    SID = stopOIDdict[row[1]]
                    PointsAndStops.setdefault(str(UID), []).append(str(SID))

    try:
        # Source FC names are not prepended to field names.
        arcpy.env.qualifiedFieldNames = False
        # It's okay to overwrite in-memory stuff.
        OverwriteOutput = arcpy.env.overwriteOutput  # Get the orignal value so we can reset it.
        arcpy.env.overwriteOutput = True

        BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2")
        BBB_SharedFunctions.ConnectToSQLDatabase(SQLDbase)

        Specific, day = BBB_SharedFunctions.CheckSpecificDate(day)
        start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(
            start_time, end_time)

        # Distance between stops and points
        BufferSize_padded = BufferSize + (.2 * BufferSize)
        BufferLinearUnit = str(BufferSize_padded) + " " + BufferUnits

        # Will we calculate the max wait time?
        CalcWaitTime = True

        # Output file designated by user
        outDir = os.path.dirname(outFile)
        outFilename = os.path.basename(outFile)
        ispgdb = "esriDataSourcesGDB.AccessWorkspaceFactory" in arcpy.Describe(
            outDir).workspaceFactoryProgID

        inLocUniqueID = BBB_SharedFunctions.HandleOIDUniqueID(
            inPointsLayer, inLocUniqueID)

        # ----- Prepare OD service -----
        try:
            arcpy.AddMessage(
                "Obtaining credentials for and information about OD Cost Matrix service..."
            )

            # Hard-wired OD variables
            TravelMode = "Walking Distance"
            PathShape = "None"

            OD_service_name = "World/OriginDestinationCostMatrix"
            Utility_service_name = "World/Utilities"
            # Get the credentials from the signed in user and import the service
            if username and password:
                ODservice = BBB_SharedFunctions.import_AGOLservice(
                    OD_service_name, username=username, password=password)
                Utilityservice = BBB_SharedFunctions.import_AGOLservice(
                    Utility_service_name, username=username, password=password)
            else:
                credentials = arcpy.GetSigninToken()
                if not credentials:
                    arcpy.AddError(
                        "Please sign into ArcGIS Online or pass a username and password to the tool."
                    )
                    raise BBB_SharedFunctions.CustomError
                token = credentials["token"]
                referer = credentials["referer"]
                ODservice = BBB_SharedFunctions.import_AGOLservice(
                    OD_service_name, token=token, referer=referer)
                Utilityservice = BBB_SharedFunctions.import_AGOLservice(
                    Utility_service_name, token=token, referer=referer)

            # Get the service limits from the OD service (how many origins and destinations allowed)
            utilresult = Utilityservice.GetToolInfo(
                "asyncODCostMatrix", "GenerateOriginDestinationCostMatrix")
            utilresultstring = utilresult.getOutput(0)
            utilresultjson = json.loads(utilresultstring)
            origin_limit = int(
                utilresultjson['serviceLimits']['maximumDestinations'])
            destination_limit = int(
                utilresultjson['serviceLimits']['maximumOrigins'])

        except:
            arcpy.AddError(
                "Failed to obtain credentials for and information about OD Cost Matrix service."
            )
            raise

        # ----- Create a feature class of stops ------
        try:
            arcpy.AddMessage("Getting GTFS stops...")
            tempstopsname = "Temp_Stops"
            StopsLayer, StopList = BBB_SharedFunctions.MakeStopsFeatureClass(
                os.path.join(outDir, tempstopsname))

            # Select only the stops within a reasonable distance of points to reduce problem size
            arcpy.management.MakeFeatureLayer(StopsLayer, "StopsToRemove")
            arcpy.management.SelectLayerByLocation(
                "StopsToRemove",
                "WITHIN_A_DISTANCE_GEODESIC",
                inPointsLayer,
                BufferLinearUnit,
                invert_spatial_relationship="INVERT")
            arcpy.management.DeleteRows("StopsToRemove")
            arcpy.management.Delete("StopsToRemove")

            # Make Feature Layer of stops to use later
            arcpy.management.MakeFeatureLayer(StopsLayer, "StopsLayer")
            stopsOID = arcpy.Describe("StopsLayer").OIDFieldName

        except:
            arcpy.AddError("Error creating feature class of GTFS stops.")
            raise

        # ----- Prepare input data -----
        try:
            arcpy.AddMessage("Preparing input points...")

            # Select only the points within a reasonable distance of stops to reduce problem size
            temppointsname = outFilename + "_Temp"
            relevantPoints = os.path.join(outDir, temppointsname)
            arcpy.management.MakeFeatureLayer(inPointsLayer, "PointsToKeep")
            arcpy.management.SelectLayerByLocation(
                "PointsToKeep", "WITHIN_A_DISTANCE_GEODESIC", StopsLayer,
                BufferLinearUnit)
            num_points = int(
                arcpy.management.GetCount("PointsToKeep").getOutput(0))

            # If the number of points is large, sort them spatially for smart chunking
            if num_points > origin_limit:
                shapeFieldName = arcpy.Describe("PointsToKeep").shapeFieldName
                arcpy.management.Sort("PointsToKeep", relevantPoints,
                                      shapeFieldName, "PEANO")
            # Otherwise, just copy them.
            else:
                arcpy.management.CopyFeatures("PointsToKeep", relevantPoints)
            arcpy.management.Delete("PointsToKeep")

            # Store OIDs in a dictionary for later joining
            pointsOIDdict = {}  # {OID: inLocUniqueID}
            with arcpy.da.SearchCursor(relevantPoints,
                                       ["OID@", inLocUniqueID]) as cur:
                for row in cur:
                    pointsOIDdict[row[0]] = row[1]
            relevantpointsOID = arcpy.Describe(relevantPoints).OIDFieldName

        except:
            arcpy.AddError("Error preparing input points for analysis.")
            raise

        #----- Create OD Matrix between stops and user's points -----
        try:
            arcpy.AddMessage("Creating OD matrix between points and stops...")
            arcpy.AddMessage(
                "(This step could take a while for large datasets or buffer sizes.)"
            )

            global PointsAndStops
            # PointsAndStops = {LocID: [stop_1, stop_2, ...]}
            PointsAndStops = {}

            # Chunk the points to fit the service limits and loop through chunks
            points_numchunks = int(math.ceil(float(num_points) / origin_limit))
            points_chunkstart = 0
            points_chunkend = origin_limit
            current_chunk = 0
            for x in range(0, points_numchunks):
                current_chunk += 1
                arcpy.AddMessage("Handling input points chunk %i of %i" %
                                 (current_chunk, points_numchunks))

                # Select only the points belonging to this chunk
                points_chunk = sorted(
                    pointsOIDdict.keys())[points_chunkstart:points_chunkend]
                points_chunkstart = points_chunkend
                points_chunkend = points_chunkstart + origin_limit
                if ispgdb:
                    points_selection_query = '[{0}] IN ({1})'.format(
                        relevantpointsOID, ','.join(map(str, points_chunk)))
                else:
                    points_selection_query = '"{0}" IN ({1})'.format(
                        relevantpointsOID, ','.join(map(str, points_chunk)))
                arcpy.MakeFeatureLayer_management(relevantPoints,
                                                  "PointsLayer",
                                                  points_selection_query)

                # Select only the stops within the safe buffer of these points
                arcpy.management.SelectLayerByLocation(
                    "StopsLayer", "WITHIN_A_DISTANCE_GEODESIC", "PointsLayer",
                    BufferLinearUnit)
                num_stops = int(
                    arcpy.GetCount_management("StopsLayer").getOutput(0))
                stopOIDdict = {}  # {OID: stop_id}
                with arcpy.da.SearchCursor("StopsLayer",
                                           ["OID@", "stop_id"]) as cur:
                    for row in cur:
                        stopOIDdict[row[0]] = row[1]

                # If the number of stops in range exceeds the destination limit, we have to chunk these as well.
                if num_stops > destination_limit:
                    stops_numchunks = int(
                        math.ceil(float(num_stops) / destination_limit))
                    stops_chunkstart = 0
                    stops_chunkend = destination_limit
                    for x in range(0, stops_numchunks):
                        stops_chunk = sorted(stopOIDdict.keys()
                                             )[stops_chunkstart:stops_chunkend]
                        stops_chunkstart = stops_chunkend
                        stops_chunkend = stops_chunkstart + destination_limit
                        if ispgdb:
                            stops_selection_query = '[{0}] IN ({1})'.format(
                                stopsOID, ','.join(map(str, stops_chunk)))
                        else:
                            stops_selection_query = '"{0}" IN ({1})'.format(
                                stopsOID, ','.join(map(str, stops_chunk)))
                        arcpy.MakeFeatureLayer_management(
                            "StopsLayer", "StopsLayer_Chunk",
                            stops_selection_query)
                        runOD("PointsLayer", "StopsLayer_Chunk")
                    arcpy.management.Delete("StopsLayer_Chunk")
                # Otherwise, just run them all.
                else:
                    runOD("PointsLayer", "StopsLayer")

            # Clean up
            arcpy.management.Delete("StopsLayer")
            arcpy.management.Delete("PointsLayer")
            arcpy.management.Delete(StopsLayer)
            arcpy.management.Delete(relevantPoints)

        except:
            arcpy.AddError(
                "Error creating OD matrix between stops and input points.")
            raise

        #----- Query the GTFS data to count the trips at each stop -----
        try:
            arcpy.AddMessage(
                "Calculating the number of transit trips available during the time window..."
            )

            # Get a dictionary of stop times in our time window {stop_id: [[trip_id, stop_time]]}
            stoptimedict = BBB_SharedFunctions.CountTripsAtStops(
                day, start_sec, end_sec,
                BBB_SharedFunctions.CleanUpDepOrArr(DepOrArrChoice), Specific)

        except:
            arcpy.AddError(
                "Error calculating the number of transit trips available during the time window."
            )
            raise

        # ----- Generate output data -----
        try:
            arcpy.AddMessage("Writing output data...")

            arcpy.management.CopyFeatures(inPointsLayer, outFile)
            # Add a field to the output file for number of trips and num trips / hour.
            arcpy.management.AddField(outFile, "NumTrips", "SHORT")
            arcpy.management.AddField(outFile, "NumTripsPerHr", "DOUBLE")
            arcpy.management.AddField(outFile, "NumStopsInRange", "SHORT")
            arcpy.management.AddField(outFile, "MaxWaitTime", "SHORT")

            with arcpy.da.UpdateCursor(outFile, [
                    inLocUniqueID, "NumTrips", "NumTripsPerHr",
                    "NumStopsInRange", "MaxWaitTime"
            ]) as ucursor:
                for row in ucursor:
                    try:
                        ImportantStops = PointsAndStops[str(row[0])]
                    except KeyError:
                        # This point had no stops in range
                        ImportantStops = []
                    NumTrips, NumTripsPerHr, NumStopsInRange, MaxWaitTime =\
                                    BBB_SharedFunctions.RetrieveStatsForSetOfStops(
                                        ImportantStops, stoptimedict, CalcWaitTime,
                                        start_sec, end_sec)
                    row[1] = NumTrips
                    row[2] = NumTripsPerHr
                    row[3] = NumStopsInRange
                    row[4] = MaxWaitTime
                    ucursor.updateRow(row)

        except:
            arcpy.AddError("Error writing output.")
            raise

        arcpy.AddMessage("Done!")
        arcpy.AddMessage("Output files written:")
        arcpy.AddMessage("- " + outFile)

    except BBB_SharedFunctions.CustomError:
        arcpy.AddError("Error counting transit trips at input locations.")
        pass

    except:
        arcpy.AddError("Error counting transit trips at input locations.")
        raise

    finally:
        # Reset overwriteOutput to what it was originally.
        arcpy.env.overwriteOutput = OverwriteOutput
def runTool(outFile, SQLDbase, inPointsLayer, inLocUniqueID, day, start_time,
            end_time, inNetworkDataset, imp, BufferSize, restrictions,
            DepOrArrChoice):
    try:
        # Source FC names are not prepended to field names.
        arcpy.env.qualifiedFieldNames = False
        # It's okay to overwrite in-memory stuff.
        OverwriteOutput = arcpy.env.overwriteOutput  # Get the orignal value so we can reset it.
        arcpy.env.overwriteOutput = True

        BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2")
        ProductName = BBB_SharedFunctions.ProductName
        BBB_SharedFunctions.CheckWorkspace()
        BBB_SharedFunctions.CheckOutNALicense()

        BBB_SharedFunctions.ConnectToSQLDatabase(SQLDbase)

        Specific, day = BBB_SharedFunctions.CheckSpecificDate(day)
        start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(
            start_time, end_time)

        # Will we calculate the max wait time?
        CalcWaitTime = True

        impedanceAttribute = BBB_SharedFunctions.CleanUpImpedance(imp)

        # Hard-wired OD variables
        ExcludeRestricted = "EXCLUDE"
        PathShape = "NO_LINES"
        accumulate = ""
        uturns = "ALLOW_UTURNS"
        hierarchy = "NO_HIERARCHY"

        # Output file designated by user
        outDir = os.path.dirname(outFile)
        outFilename = os.path.basename(outFile)

        inLocUniqueID = BBB_SharedFunctions.HandleOIDUniqueID(
            inPointsLayer, inLocUniqueID)
        inLocUniqueID_qualified = inLocUniqueID + "_Input"

        arcpy.AddMessage("Run set up successfully.")

        # ----- Create a feature class of stops ------
        try:
            arcpy.AddMessage("Getting GTFS stops...")
            tempstopsname = "Temp_Stops"
            if ".shp" in outFilename:
                tempstopsname += ".shp"
            StopsLayer, StopList = BBB_SharedFunctions.MakeStopsFeatureClass(
                os.path.join(outDir, tempstopsname))
        except:
            arcpy.AddError("Error creating feature class of GTFS stops.")
            raise

        #----- Create OD Matrix between stops and user's points -----
        try:
            arcpy.AddMessage("Creating OD matrix between points and stops...")
            arcpy.AddMessage(
                "(This step could take a while for large datasets or buffer sizes.)"
            )

            # Name to refer to OD matrix layer
            outNALayer_OD = "ODMatrix"

            # ODLayer is the NA Layer object returned by getOutput(0)
            ODLayer = arcpy.na.MakeODCostMatrixLayer(
                inNetworkDataset, outNALayer_OD, impedanceAttribute,
                BufferSize, "", accumulate, uturns, restrictions, hierarchy,
                "", PathShape).getOutput(0)

            # To refer to the OD sublayers, get the sublayer names.  This is essential for localization.
            naSubLayerNames = arcpy.na.GetNAClassNames(ODLayer)
            points = naSubLayerNames["Origins"]
            stops = naSubLayerNames["Destinations"]

            # Add a field for stop_id as a unique identifier for stops.
            arcpy.na.AddFieldToAnalysisLayer(outNALayer_OD, stops, "stop_id",
                                             "TEXT")
            # Specify the field mappings for the stop_id field.
            fieldMappingStops = arcpy.na.NAClassFieldMappings(ODLayer, stops)
            fieldMappingStops["Name"].mappedFieldName = "stop_id"
            fieldMappingStops["stop_id"].mappedFieldName = "stop_id"
            # Add the GTFS stops as locations for the analysis.
            arcpy.na.AddLocations(outNALayer_OD, stops, StopsLayer,
                                  fieldMappingStops, "500 meters", "", "", "",
                                  "", "", "", ExcludeRestricted)
            # Clear out the memory because we don't need this anymore.
            arcpy.management.Delete(StopsLayer)

            # Add a field for unique identifier for points.
            arcpy.na.AddFieldToAnalysisLayer(outNALayer_OD, points,
                                             inLocUniqueID_qualified, "TEXT")
            # Specify the field mappings for the unique id field.
            fieldMappingPoints = arcpy.na.NAClassFieldMappings(ODLayer, points)
            fieldMappingPoints["Name"].mappedFieldName = inLocUniqueID
            fieldMappingPoints[
                inLocUniqueID_qualified].mappedFieldName = inLocUniqueID
            # Add the input points as locations for the analysis.
            arcpy.na.AddLocations(outNALayer_OD, points, inPointsLayer,
                                  fieldMappingPoints, "500 meters", "", "", "",
                                  "", "", "", ExcludeRestricted)

            # Solve the OD matrix.
            try:
                arcpy.na.Solve(outNALayer_OD)
            except:
                errs = arcpy.GetMessages(2)
                if "No solution found" in errs:
                    impunits = imp.split(" (Units: ")[1].split(")")[0]
                    arcpy.AddError(
                        "No transit stops were found within a %s %s walk of any of your input points.  \
Consequently, there is no transit service available to your input points, so no output will be generated."
                        % (str(BufferSize), impunits))
                else:
                    arcpy.AddError(
                        "Failed to calculate travel time or distance between transit stops and input points.  OD Cost Matrix error messages:"
                    )
                    arcpy.AddError(errs)
                raise BBB_SharedFunctions.CustomError

            # Make layer objects for each sublayer we care about.
            if ProductName == 'ArcGISPro':
                naSubLayerNames = arcpy.na.GetNAClassNames(ODLayer)
                subLayerDict = dict(
                    (lyr.name, lyr) for lyr in ODLayer.listLayers())
                subLayers = {}
                for subL in naSubLayerNames:
                    subLayers[subL] = subLayerDict[naSubLayerNames[subL]]
            else:
                subLayers = dict(
                    (lyr.datasetName, lyr)
                    for lyr in arcpy.mapping.ListLayers(ODLayer)[1:])
            linesSubLayer = subLayers["ODLines"]
            pointsSubLayer = subLayers["Origins"]
            stopsSubLayer = subLayers["Destinations"]

            # Get the OID fields, just to be thorough
            desc1 = arcpy.Describe(pointsSubLayer)
            points_OID = desc1.OIDFieldName
            desc2 = arcpy.Describe(stopsSubLayer)
            stops_OID = desc2.OIDFieldName

            # Join polygons layer with input facilities to port over the stop_id
            arcpy.management.JoinField(linesSubLayer, "OriginID",
                                       pointsSubLayer, points_OID,
                                       [inLocUniqueID_qualified])
            arcpy.management.JoinField(linesSubLayer, "DestinationID",
                                       stopsSubLayer, stops_OID, ["stop_id"])

            # Use searchcursor on lines to find the stops that are reachable from points.
            global PointsAndStops
            # PointsAndStops = {LocID: [stop_1, stop_2, ...]}
            PointsAndStops = {}
            ODCursor = arcpy.da.SearchCursor(
                linesSubLayer, [inLocUniqueID_qualified, "stop_id"])
            for row in ODCursor:
                PointsAndStops.setdefault(str(row[0]), []).append(str(row[1]))
            del ODCursor

        except:
            arcpy.AddError(
                "Error creating OD matrix between stops and input points.")
            raise

        #----- Query the GTFS data to count the trips at each stop -----
        try:
            arcpy.AddMessage(
                "Calculating the number of transit trips available during the time window..."
            )

            # Get a dictionary of stop times in our time window {stop_id: [[trip_id, stop_time]]}
            stoptimedict = BBB_SharedFunctions.CountTripsAtStops(
                day, start_sec, end_sec,
                BBB_SharedFunctions.CleanUpDepOrArr(DepOrArrChoice), Specific)

        except:
            arcpy.AddError(
                "Error calculating the number of transit trips available during the time window."
            )
            raise

        # ----- Generate output data -----
        try:
            arcpy.AddMessage("Writing output data...")

            arcpy.management.CopyFeatures(inPointsLayer, outFile)
            # Add a field to the output file for number of trips and num trips / hour.
            if ".shp" in outFilename:
                arcpy.management.AddField(outFile, "NumTrips", "SHORT")
                arcpy.management.AddField(outFile, "TripsPerHr", "DOUBLE")
                arcpy.management.AddField(outFile, "NumStops", "SHORT")
                arcpy.management.AddField(outFile, "MaxWaitTm", "SHORT")
            else:
                arcpy.management.AddField(outFile, "NumTrips", "SHORT")
                arcpy.management.AddField(outFile, "NumTripsPerHr", "DOUBLE")
                arcpy.management.AddField(outFile, "NumStopsInRange", "SHORT")
                arcpy.management.AddField(outFile, "MaxWaitTime", "SHORT")

            if ".shp" in outFilename:
                ucursor = arcpy.da.UpdateCursor(outFile, [
                    inLocUniqueID[0:10], "NumTrips", "TripsPerHr", "NumStops",
                    "MaxWaitTm"
                ])
            else:
                ucursor = arcpy.da.UpdateCursor(outFile, [
                    inLocUniqueID, "NumTrips", "NumTripsPerHr",
                    "NumStopsInRange", "MaxWaitTime"
                ])
            for row in ucursor:
                try:
                    ImportantStops = PointsAndStops[str(row[0])]
                except KeyError:
                    # This point had no stops in range
                    ImportantStops = []
                NumTrips, NumTripsPerHr, NumStopsInRange, MaxWaitTime =\
                                BBB_SharedFunctions.RetrieveStatsForSetOfStops(
                                    ImportantStops, stoptimedict, CalcWaitTime,
                                    start_sec, end_sec)
                row[1] = NumTrips
                row[2] = NumTripsPerHr
                row[3] = NumStopsInRange
                if ".shp" in outFilename and MaxWaitTime == None:
                    row[4] = -1
                else:
                    row[4] = MaxWaitTime
                ucursor.updateRow(row)

        except:
            arcpy.AddError("Error writing output.")
            raise

        arcpy.AddMessage("Done!")
        arcpy.AddMessage("Output files written:")
        arcpy.AddMessage("- " + outFile)

    except BBB_SharedFunctions.CustomError:
        arcpy.AddError("Error counting transit trips at input locations.")
        pass

    except:
        arcpy.AddError("Error counting transit trips at input locations.")
        raise

    finally:
        # Reset overwriteOutput to what it was originally.
        arcpy.env.overwriteOutput = OverwriteOutput
示例#8
0
def runTool(outStops, SQLDbase, day, start_time, end_time, DepOrArrChoice):
    try:

        BBB_SharedFunctions.CheckArcVersion(min_version_pro="1.2")
        BBB_SharedFunctions.ConnectToSQLDatabase(SQLDbase)

        Specific, day = BBB_SharedFunctions.CheckSpecificDate(day)
        start_sec, end_sec = BBB_SharedFunctions.ConvertTimeWindowToSeconds(
            start_time, end_time)

        # Will we calculate the max wait time?
        CalcWaitTime = True

        # ----- Create a feature class of stops and add fields for transit trip counts ------
        try:
            arcpy.AddMessage("Creating feature class of GTFS stops...")

            # Create a feature class of transit stops
            outStops, StopIDList = BBB_SharedFunctions.MakeStopsFeatureClass(
                outStops)

            # Add a field to the output file for number of trips, num trips / hour, and max wait time
            if ".shp" in outStops:
                # Shapefiles can't have long field names
                arcpy.management.AddField(outStops, "NumTrips", "SHORT")
                arcpy.management.AddField(outStops, "TripsPerHr", "DOUBLE")
                arcpy.management.AddField(outStops, "MaxWaitTm", "SHORT")
            else:
                arcpy.management.AddField(outStops, "NumTrips", "SHORT")
                arcpy.management.AddField(outStops, "NumTripsPerHr", "DOUBLE")
                arcpy.management.AddField(outStops, "MaxWaitTime", "SHORT")

        except:
            arcpy.AddError("Error creating feature class of GTFS stops.")
            raise

        #----- Query the GTFS data to count the trips at each stop -----
        try:
            arcpy.AddMessage(
                "Calculating the number of transit trips available during the time window..."
            )

            # Get a dictionary of {stop_id: [[trip_id, stop_time]]} for our time window
            stoptimedict = BBB_SharedFunctions.CountTripsAtStops(
                day, start_sec, end_sec,
                BBB_SharedFunctions.CleanUpDepOrArr(DepOrArrChoice), Specific)

        except:
            arcpy.AddError(
                "Error counting arrivals or departures at stop during time window."
            )
            raise

        # ----- Write to output -----
        try:
            arcpy.AddMessage("Writing output data...")

            # Create an update cursor to add numtrips, trips/hr, and maxwaittime to stops
            if ".shp" in outStops:
                ucursor = arcpy.da.UpdateCursor(
                    outStops,
                    ["stop_id", "NumTrips", "TripsPerHr", "MaxWaitTm"])
            else:
                ucursor = arcpy.da.UpdateCursor(
                    outStops,
                    ["stop_id", "NumTrips", "NumTripsPerHr", "MaxWaitTime"])
            for row in ucursor:
                NumTrips, NumTripsPerHr, NumStopsInRange, MaxWaitTime = \
                            BBB_SharedFunctions.RetrieveStatsForSetOfStops(
                                [row[0]], stoptimedict, CalcWaitTime,
                                start_sec, end_sec)
                row[1] = NumTrips
                row[2] = NumTripsPerHr
                if ".shp" in outStops and MaxWaitTime == None:
                    row[3] = -1
                else:
                    row[3] = MaxWaitTime
                ucursor.updateRow(row)

        except:
            arcpy.AddError("Error writing to output.")
            raise

        arcpy.AddMessage("Finished!")
        arcpy.AddMessage("Your output is located at " + outStops)

    except BBB_SharedFunctions.CustomError:
        arcpy.AddError("Failed to count trips at stops.")
        pass

    except:
        arcpy.AddError("Failed to count trips at stops.")
        raise