python点位在哪个区域内,打标签

该代码实现从MySQL数据库中查询和更新数据,包括读取配置文件获取数据库连接参数,创建连接,执行SQL(如:选择、更新操作)。此外,还定义了一个IsPtInPoly函数,用于判断一个点是否在给定多边形内,适用于地理空间数据处理。
摘要由CSDN通过智能技术生成
import MySQLdb
import json
import os

source_system='parking'
filePath = os.path.expanduser('~')+ os.sep + 'etlconfig' +os.sep +source_system + os.sep + 'database.conf'
with open(filePath,'r') as confFile:
    confStr = confFile.read()
conf = json.JSONDecoder().decode(confStr)

targetHost=conf['targetHost']
targetUser=conf['targetUser']
targetPassword=conf['targetPassword']
targetDatabase=conf['targetDatabase']
targetPort=conf['targetPort']


def create_target_conn():
    conn = MySQLdb.connect(targetHost,targetUser, targetPassword, targetDatabase, targetPort, charset='utf8')
    cursor = conn.cursor()
    return conn, cursor


def close_target_conn(targetConn, targetCur):
    targetCur.close()
    targetConn.close()


def select(sql):
    targetConn, targetCur = create_target_conn()
    len = targetCur.execute(sql)
    result = targetCur.fetchall()
    resultList = list(result)
    close_target_conn(targetConn,targetCur)
    return resultList

def update(sql):
    targetConn, targetCur = create_target_conn()
    len = targetCur.execute(sql)
    targetConn.commit()
    close_target_conn(targetConn,targetCur)


def IsPtInPoly(aLon, aLat, pointList):
    '''
    :param aLon: double 经度
    :param aLat: double 纬度
    :param pointList: list [(lon, lat)...] 多边形点的顺序需根据顺时针或逆时针,不能乱
    '''
    iSum = 0
    iCount = len(pointList)
    if(iCount < 3):
        return False
    for i in range(iCount):
        pLon1 = float(pointList[i][0])
        pLat1 = float(pointList[i][1])
        if(i == iCount - 1):
            pLon2 = float(pointList[0][0])
            pLat2 = float(pointList[0][1])
        else:
            pLon2 = float(pointList[i + 1][0])
            pLat2 = float(pointList[i + 1][1])
        if ((aLat >= pLat1) and (aLat < pLat2)) or ((aLat>=pLat2) and (aLat < pLat1)):
            if (abs(pLat1 - pLat2) > 0):
                pLon = pLon1 - ((pLon1 - pLon2) * (pLat1 - aLat)) / (pLat1 - pLat2);
                if(pLon < aLon):
                    iSum += 1
    if(iSum % 2 != 0):
        return True
    else:
        return False


#主函数用法
if __name__ == '__main__':
    dataList = select('SELECT * FROM zljjfz_block_info WHERE  flag =1')
    for i in range(len(dataList)):
        blockId = dataList[i][2]
        latLngList = select("SELECT longitude,latitude FROM zljjfz_block_area WHERE  block_id ='"+blockId+"'")
        entInfoList = select("select longitude,latitudes,ent_id from zlaj_ent_info where ent_name IS NOT NULL AND longitude is not NULL AND latitudes is not NULL")
        for j in range(len(entInfoList)):
            lat = float(str(entInfoList[j][1]))
            lng = float(str(entInfoList[j][0]))
            if(IsPtInPoly(lng,lat,latLngList)):
                print(entInfoList[j][2])
                update("update zlaj_ent_info set block_id ='"+blockId+"'" +" where ent_id='"+str(entInfoList[j][2])+"'")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科学的N次方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值