Exemplo n.º 1
0
def test_TransitObject():
    with open("transit.json", "r") as fr:
        test_data = json.load(fr)
    trans = TransitObject()
    trans.parse(test_data)
    assert trans.total_line == 99
    df = trans.to_dataframe()
    assert len(df) == 10
    df.to_csv("test.csv",encoding="utf-8")
    
Exemplo n.º 2
0
for name in targets_name:
    pois = place.search(targets_name[name], region=name)
    poi = pois[0]

    loc = poi.get_location()

    for i in origins_data.index:
        obj = TransitObject()
        coords = (round(origins_data["lat"][i],
                        5), round(origins_data["lng"][i], 5))
        # 飞机
        content = dirction.transit(loc,
                                   coords,
                                   tactics_incity=4,
                                   trans_type_intercity=1)
        content = json.loads(content)
        if "status" in content and content["status"] == 0:
            obj.parse(content)
            origin = origins_data["详细地址"][i]
            target = targets_name[name]
            result = str(origin) + "\t" + str(target)
            if len(obj.routes) == 0:
                break
            route = obj.routes[0]
            route = route.to_dict()
            result = result + "\t" + "\t".join(str(r)
                                               for r in route.values()) + "\n"
            fw.write(result)

fw.close()
Exemplo n.º 3
0
class MapDirection(APIBase):
    '''
    Direction API, include transit, riding and driving. It only support Areas of China. If you want
    to watch other places, you can use the class of OverseasDirection.
    '''
    def __init__(self, ak, sk):
        self.TransitObject = TransitObject()
        APIBase.__init__(self, ak, sk)

    def _getPayload(self, origin, destination, **kwargs):
        orig = common.convertCoord(origin)
        dest = common.convertCoord(destination)
        payload = {
            "origin": orig,
            "destination": dest,
            "ak": self.ak,
            "timestamp": int(time.time())
        }
        if kwargs:
            payload.update(kwargs)
        return payload

    def _get_raw_transit(self, origin, destination, **kwargs):
        '''get Public transit route direction.

        Parameters:

            origin : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",  
                    
            destination : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",
    
        Returns:

            content: string, the content of get from baidu API

        site: http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2
        '''
        urlPath = "/direction/v2/transit"
        payload = self._getPayload(origin, destination, **kwargs)
        content = self.encry.get(HOST, urlPath, payload)
        print("Direction transit URL: ", self.encry.url)
        return content

    @parse_object(TransitObject)
    def transit(self, origin, destination, **kwargs):
        '''get Public transit route direction.

        Parameters:

            origin : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",  
                    
            destination : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",
    
        Returns:

            TransitObject: TransitObject, transit object include all routes
            information.

        site: http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2
        '''
        return self._get_raw_transit(origin, destination, **kwargs)

    def _get_raw_riding(self, origin, destination, **kwargs):
        '''
        get riding route direction.

        parameter:

            origin : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",

            destination : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",
        
        Returns:

            content: string, the content of get from baidu API

        site: http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2
        '''
        urlPath = "/direction/v2/riding"
        payload = self._getPayload(origin, destination, **kwargs)
        content = self.encry.get(HOST, urlPath, payload)
        print("Direction riding URL: ", self.encry.url)
        return content

    def riding(self, origin, destination, **kwargs):
        '''
        get riding route direction.

        parameter:

            origin : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",

            destination : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",
        
        Returns:

            TransitObject: TransitObject, transit object include all routes
            information.

        site: http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2
        '''
        content = self._get_raw_riding(origin, destination, **kwargs)
        common.check_response(content)
        self.TransitObject.parse(content)
        return self.TransitObject

    def _get_raw_driving(self, origin, destination, **kwargs):
        '''
        get driving route direction.

        parameter:

            origin : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",

            destination : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",

        Returns:

            content: string, the content of get from baidu API

        site: http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2
        '''
        urlPath = "/direction/v2/driving"
        payload = self._getPayload(origin, destination, **kwargs)
        content = self.encry.get(HOST, urlPath, payload)
        print("Direction driving URL: ", self.encry.url)
        return content

    @parse_object(DriveObject)
    def driving(self, origin, destination, **kwargs):
        '''
        get driving route direction.

        parameter:

            origin : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",

            destination : string, include "latitude,longitude", 
            for exmaple: guangzhou coordinate value is "23.137903,113.34348",

        Returns:

            DriveObject: DriveObject, transit object include all routes
            information.

        site: http://lbsyun.baidu.com/index.php?title=webapi/direction-api-v2
        '''
        return self._get_raw_driving(origin, destination, **kwargs)