Esempio n. 1
0
def mapMaking(task_id):
    client = pymongo.MongoClient("127.0.0.1", 27017)
    list = []
    dict = {}
    list1 = []
    try:
        db = client.crawlData  # 连接test数据库
        collection = db.taobao  # 访问test数据库中things集合
        for m in collection.find({"task_id": task_id}, {
                "address": 1,
                "_id": 0
        }):
            if m['address'] in citys:
                list.append(m['address'])
    except:
        pass
    finally:
        client.close()
    # print list

    # 生成关系,查找键值
    for index in list:  # 遍历词语列表
        if index in dict:
            dict[index] += 1  # 根据字典键访问键值,如果该键在字典中,则其值+1
        else:
            dict[index] = 1  # 如果键不在字典中,则设置其键值为1

    # print dict      #{u'\u4e0a\u6d77': 1, u'\u9633\u6c5f': 6, u'\u6210\u90fd': 1, u'\u91d1\u534e': 1}
    for item in dict.items():  # 化成列表类型
        list1.append(item[0])
        list1.append(item[1])

    # print len(list1)
    list2 = []

    for i in range(0, len(list1), 2):  # 拼成pycharts能够输入的格式,步长为2
        a = (list1[i], list1[i + 1])
        list2.append(a)

    data = list2
    geo = Geo(u"商品在全国售卖主要城市",
              u"商品",
              title_color="#fff",
              title_pos="center",
              width=1000,
              height=600,
              background_color='#404a59')
    geo.height = 800
    geo.width = 1500
    attr, value = geo.cast(data)
    geo.add("",
            attr,
            value,
            visual_range=[0, 60],
            maptype='china',
            visual_text_color="#fff",
            symbol_size=10,
            is_visualmap=True)
    geo.render("static/maps/map" + task_id + ".html")  # 生成html文件