def transport_pois_by_map(self, poi, location1,
                              location2, griddepth, limit=20):
        """Return a list of points of interest within a map grid defined by
        location1 & location2

        Arguments:
            poi: either a transport mode or outlet. A list of poi types can be
                passed in as a comma separated
            location1 & location2:
                - are one of (lat, lon), a Location object, or something that
                  has a location property (which would be a Location object).
                - define the top left corner (location1) and bottom right
                  corner (location2) of a rectangle on a map
            griddepth: number of cell blocks per cluster
            limit: minimum number of POIs required to create a cluster as well
                as the maximum number of POIs returned
        Returns:
            a dictionary in the format, the primary components being in the
            'locations' value
            {'locations': [ Stop or Outlet objects ],
             '...'
             }
        """

        lat1, lon1 = parse_location(location1)
        lat2, lon2 = parse_location(location2)

        base_path = "/v2/poi/{poi}/lat1/{lat1}/long1/{lon1}/" + \
                    "lat2/{lat2}/long2/{lon2}/" + \
                    "griddepth/{griddepth}/limit/{limit}"

        poi_ids = ','.join([str(self.MODES[p]) for p in poi.split(',')])

        path = base_path.format(poi=poi_ids, lat1=lat1, lon1=lon1,
                                lat2=lat2, lon2=lon2,
                                griddepth=griddepth, limit=limit)

        data = self._api_request(path)

        stop_factory = StopFactory(self)
        outlet_factory = OutletFactory(self)

        out = {}
        for k, v in data.items():
            if k == "locations":
                out['locations'] = []
                for location in v:
                    # either a Stop of an Outlet
                    if 'transport_type' in location:
                        item = stop_factory.create(**location)
                    else:
                        outlet_type = location.pop('outlet_type')
                        item = outlet_factory.create(transport_type=outlet_type,
                                                     **location)
                    out['locations'].append(item)
            else:
                out[k] = v

        return out
Beispiel #2
0
    def service_add(self, service):
        # We have received a service location broadcast
        # message.  We now have to decide whether it is
        # an EVENT service, and if we would like to subscribe
        # to it, given our username and app_name settings.
        # If we have an empty username and/or appname, we
        # will subscribe to every EVENT service.
        if service.service_name != "EVENT":
            return

        if self.user_name != "":
            # We have been configured with a non-empty username.
            # Only subscribe to the service if it matches.
            if service.user_name != self.user_name:
                return

        if self.application_name != "":
            # We have been configured with a non-empty appname.
            # Only subscribe to the service if it matches.
            if service.application_name != self.application_name:
                return

        # We have found a matching service.  We must now get
        # the location of this service and open up a SUB socket
        # to it.  Once open, we must subscribe to the events
        # of interest.
        Llog.LogInfo("Subscribing to EVENT source: " + str(service))

        addr_info = location.parse_location(service.location)
        if addr_info is None:
            Llog.LogError("Invalid location: " + service.location)
            return

        zsock = zsocket.ZSocketClient(zmq.SUB,
                                      "tcp",
                                      addr_info['address'],
                                      "EVENT",
                                      addr_info['port'])
        assert(zsock is not None)
        zsock.connect()
        self.interface.add_socket(zsock)
        for event_type in self.event_types:
            if event_type == "*":
                # subscribe to everything, i.e the empty-string
                zsock.subscribe("")
            else:
                zsock.subscribe(str(event_type))
    def stops_nearby(self, location, mode=None, limit=None,
                     with_distance=False):
        """Return stops near a location.

        Args:
            location: one of (lat, lon), a Location object, or something that
                    has a location property (which would be a Location object)
            mode: (optional) filter results for only this tramsport mode
            limit: (optional) only return this many results
            with_distance: (optional) return tuples of (Stop, distance)
        Returns:
            List of stops or a list of tuples in the form (stop, distance) if
            with_distance is True
        """

        base_path = "/v2/nearme/latitude/{lat}/longitude/{lon}"

        lat, lon = parse_location(location)

        path = base_path.format(lat=lat, lon=lon)

        stops = self._api_request(path)

        stop_factory = StopFactory(self)

        out = [stop_factory.create(**stop['result']) for stop in stops]

        # only provide certain stop types if we are provided with a mode
        if mode is not None:
            out = [stop for stop in out if stop.transport_type == mode]

        # enforce limit if provided
        if limit is not None:
            out = out[:limit]

        # convert into tuple of (Stop, distance)
        if with_distance:
            out = [(stop, location.distance(stop.location)) for stop in out]

        return out