Esempio n. 1
0
def queryClusterPointInfo():
    """
    根据聚类点id查询聚类点信息:id、客户名称、人数、站点名称、经纬度
    """
    res = ResMsg()
    try:
        logger.info("begin queryClusterPointInfo!")
        aiBusModel = AiBusModel()
        userInfo = session.get("userInfo")
        data = request.get_json()
        clusterId = data["id"]
        clusterPoint = aiBusModel.selectClusterPointById(clusterId)
        siteIdList = clusterPoint["siteSet"].split(",")
        siteInfo = aiBusModel.selectClientNameByIds(siteIdList)
        res.update(code=ResponseCode.Success, data=siteInfo)
        return res.data
    except Exception as e:
        logger.error("queryClusterPointInfo exception:{}".format(str(e)))
        res.update(code=ResponseCode.Fail, msg="查询聚类点信息报错!")
        return res.data
Esempio n. 2
0
def queryClusterResult():
    res = ResMsg()
    try:
        logger.info("begin queryClusterResult!")
        aiBusModel = AiBusModel()
        userInfo = session.get("userInfo")
        data = request.get_json()
        fileId = data["fileId"]
        clusterOutPoints = []
        clusterCorePoints = []
        clusterAroundPoints = []
        #1)先查询聚类参数
        clusterParams = aiBusModel.selectClusterParams((fileId))
        if not clusterParams:
            clusterParams = aiBusModel.selectClusterParamsBySiteFileId(
                (fileId))
            if not clusterParams:
                res.update(code=ResponseCode.Success)
                return res.data
            fileId = clusterParams["id"]
        #2)查询聚类结果
        clusterResut = aiBusModel.selectClusterResult((1, fileId))
        for item in clusterResut:
            if item["relativeProperty"] == 1:
                siteProperty = "固定"
            elif item["relativeProperty"] == 0:
                siteProperty = "临时"
            else:
                siteProperty = "自定义"

            #根据site id查询网点信息
            clientNames = []
            users = []
            if item["siteSet"] != '':
                users = item["siteSet"].split(",")
                siteInfo = aiBusModel.selectClientNameByIds(users)
                for site in siteInfo:
                    clientNames.append(site["clientName"])

            row={"id":item["id"],"siteName":item["clusterName"],"siteProperty":siteProperty,\
                    "longitude":item["longitude"],"latitude":item["latitude"],"number":item["number"],\
                        "users":users,"userNames":clientNames}
            if item["clusterProperty"] == 1:
                clusterCorePoints.append(row)
            elif item["clusterProperty"] == 2:
                clusterAroundPoints.append(row)
            else:
                clusterOutPoints.append(row)
        res.update(code=ResponseCode.Success,
                   data={
                       "epsRadius": clusterParams["clusterRadius"],
                       "minSamples": clusterParams["clusterMinSamples"],
                       "clusterCorePoints": clusterCorePoints,
                       "clusterAroundPoints": clusterAroundPoints,
                       "clusterOutPoints": clusterOutPoints
                   })
        return res.data
    except Exception as e:
        logger.error("generateClusterPoints exception:{}".format(str(e)))
        res.update(code=ResponseCode.Fail, msg="查询聚类结果报错!")
        return res.data
Esempio n. 3
0
def generateClusterPoints():
    """
    根据网点文件id生成聚类点
    """
    res = ResMsg()
    try:
        logger.info("begin generateClusterPoints!")
        aiBusModel = AiBusModel()
        userInfo = session.get("userInfo")
        data = request.get_json()
        fileId = data["fileId"]
        epsRadius = float(data["epsRadius"])
        minSamples = data["minSamples"]
        #先判断网点文件是否有效
        fileProperty = aiBusModel.selectSiteFileStatus(fileId)
        if fileProperty["fileStatus"] == 0:
            res.update(code=ResponseCode.Fail, msg="该文件已经失效!")
            return res.data

        #根据网点文件查询网点list
        #先判断是该文件是聚类文件还是网点文件
        if fileProperty["clusterStatus"] == 1:
            siteFileId = fileProperty["siteFileId"]
        else:
            siteFileId = fileId
            #对网点文件的临时聚类结果进行失效
            aiBusModel.updateClusterResultByFileId(
                (0, userInfo["userName"], fileId), [2])

        siteGeoList = aiBusModel.selectSiteGeoListByFileId(siteFileId)
        if not siteGeoList:
            res.update(code=ResponseCode.Fail, msg="网点为空!")
            return res.data

        #{"noiseList":[ids],"aroundList":[ids],"clusterDict":{"id":[ids],"id":[ids]}}
        clusterInfo = clusterByAdaptiveDbscan(siteGeoList, epsRadius,
                                              minSamples)
        #将聚类结果保存至表中
        insertVals = []
        siteGeoDict = {}
        for site in siteGeoList:
            siteGeoDict[str(site["id"])] = site

        clusterOutPoints = []
        clusterCorePoints = []
        clusterAroundPoints = []
        #1)处理异常点fileId,siteId,clusterName,clusterProperty,clusterStatus,longitude,latitude,number,siteSet
        for id in clusterInfo["noiseList"]:
            relativeId = "site_" + str(id)
            site = siteGeoDict[str(id)]
            insertVals.append(
                (fileId, relativeId, site["siteName"], site["siteProperty"], 0,
                 2, site["lng"], site["lat"], site["number"], str(id),
                 userInfo["userName"], userInfo["userName"]))
            #clusterOutPoints.append({"id":relativeId,"siteName":site["siteName"],"siteProperty":site["siteProperty"],"number":site["number"],"longitude":site["lng"],"latitude":site["lat"]})

        #2)处理聚类点
        for key in clusterInfo["clusterDict"].keys():
            clusterSet = clusterInfo["clusterDict"][key]
            #处理聚类核心点
            clusterNumber = 0
            site = siteGeoDict[str(key)]
            for id in clusterSet:
                sitePoint = siteGeoDict[str(id)]
                clusterNumber += int(sitePoint["number"])
            clusterIdStr = ",".join(map(str, clusterSet))
            insertVals.append((fileId, "site_" + str(key), site["siteName"],
                               site["siteProperty"], 1, 2, site["lng"],
                               site["lat"], clusterNumber, clusterIdStr,
                               userInfo["userName"], userInfo["userName"]))

        #处理边界点
        for id in clusterInfo["aroundList"]:
            relativeId = "site_" + str(id)
            aroundSite = siteGeoDict[str(id)]
            insertVals.append(
                (fileId, relativeId, aroundSite["siteName"],
                 aroundSite["siteProperty"], 2, 2,
                 aroundSite["lng"], aroundSite["lat"], aroundSite["number"],
                 str(id), userInfo["userName"], userInfo["userName"]))
        #3)插入聚类点并返回
        aiBusModel.batchClusterSites(insertVals)

        #4)返回聚类结果
        clusterResut = aiBusModel.selectClusterResult((2, fileId))
        for item in clusterResut:
            if item["relativeProperty"] == 1:
                siteProperty = "固定"
            elif item["relativeProperty"] == 0:
                siteProperty = "临时"
            else:
                siteProperty = "自定义"

            #根据site id查询网点信息
            siteInfo = aiBusModel.selectClientNameByIds(
                item["siteSet"].split(","))
            clientNames = []
            for site in siteInfo:
                clientNames.append(site["clientName"])

            row={"id":item["id"],"siteName":item["clusterName"],"siteProperty":siteProperty,\
                    "longitude":item["longitude"],"latitude":item["latitude"],"number":item["number"],\
                        "users":item["siteSet"].split(","),"userNames":clientNames}
            if item["clusterProperty"] == 1:
                clusterCorePoints.append(row)
            elif item["clusterProperty"] == 2:
                clusterAroundPoints.append(row)
            else:
                clusterOutPoints.append(row)
        res.update(code=ResponseCode.Success,
                   data={
                       "clusterCorePoints": clusterCorePoints,
                       "clusterAroundPoints": clusterAroundPoints,
                       "clusterOutPoints": clusterOutPoints
                   })
        return res.data
    except Exception as e:
        logger.error("generateClusterPoints exception:{}".format(str(e)))
        res.update(code=ResponseCode.Fail, msg=str(e))
        return res.data