def test_SearchPlace(): search = SearchPlace(AK, SK) allregion = search.listAllRegion() assert "北京" in allregion and "广州" in allregion query = "银行" region = "338" # 肇庆 content = search.searchRegion(query=query, region=region, output="json") result = json.loads(content) assert result["status"] == 0 content = search.fuzzySearchRegion(query="北京路", region="广州", city_limit="false", output="json") result = json.loads(content) assert result["status"] == 0 poi = "银行$酒店" location = "23.137903,113.34348" content = search.searchCircularArea(query=poi, location=location, output="json") result = json.loads(content) assert result["status"] == 0 bounds = "23.137903,113.34348,40.063597,116.364973" content = search.searchRectangularArea(query=poi, bounds=bounds, output="json") result = json.loads(content) assert result["status"] == 0 # 测试批量百度坐标查询 places = { "香港": "香港国际机场", "广州": "广州白云国际机场", "深圳": "深圳宝安国际机场", "珠海": "珠海金湾国际机场", "澳门": "澳门国际机场", "佛山": "佛山沙堤机场", "惠州": "惠州平潭机场" } result = search.queryBatchPlaceCoord(places) result = list(result) assert len(result) == 7 assert len(result[0]) == 2 # 测试 search 方法 pois = search.search(query, region=region) for p in pois: assert isinstance(p, POI)
def __init__(self, ak, sk): self.origin = list() self.destination = list() self.place = SearchPlace(ak, sk) self.dirction = MapDirection(ak, sk) self.recov = recovsys()
AK = os.environ["BaiduAK"] SK = os.environ["BaiduSK"] origins_data = pd.read_csv("data/exmaple_citydata_coords.csv", encoding="utf-8") targets_name = { "香港": "香港国际机场", "广州": "广州白云国际机场", "深圳": "深圳宝安国际机场", "珠海": "珠海金湾国际机场", "澳门": "澳门国际机场", "佛山": "佛山沙堤机场", "惠州": "惠州平潭机场" } place = SearchPlace(AK, SK) dirction = MapDirection(AK, SK) fw = open("transit_plane_result.csv", "w", encoding="utf-8") fw.write( "origin\t target\t distance\t duration\t price\t vehicle\t description \n") 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()
''' # constant AK = os.environ["BaiduAK"] SK = os.environ["BaiduSK"] # get street name df = pd.read_csv("exmaple_citydata.csv", encoding="utf-8") df["lat"] = 0.0 df["lng"] = 0.0 df["三级区县"] = df["三级区县"].fillna("") df["详细地址"] = df["三级区县"] + df["四级镇街"] search = SearchPlace(AK, SK) for i in df.index: print(df["详细地址"][i], df["二级市"][i]) content = search.searchRegion(query=df["详细地址"][i], region=df["二级市"][i], output="json") result = json.loads(content) assert result["status"] == 0 if len(result["results"]) > 0: df["lat"][i] = result["results"][0]["location"]["lat"] df["lng"][i] = result["results"][0]["location"]["lng"] df.to_csv("exmaple_citydata_coords.csv", encoding="utf-8", index=False)