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])+"'")
python点位在哪个区域内,打标签
最新推荐文章于 2024-04-27 11:41:25 发布
该代码实现从MySQL数据库中查询和更新数据,包括读取配置文件获取数据库连接参数,创建连接,执行SQL(如:选择、更新操作)。此外,还定义了一个IsPtInPoly函数,用于判断一个点是否在给定多边形内,适用于地理空间数据处理。
摘要由CSDN通过智能技术生成