Files
UtilityPole_Info/MgrUtilityPoleConverter.py
2025-11-15 15:59:49 +09:00

263 lines
9.3 KiB
Python

import os
import sys
import UtilPack as util
import PoleXLS as myxl
import ExtEXIFData as exEXIF
import VWorldAPIs as vwapi
import JusoMngr
mCurPath = "" # 스크립트가 실행되는 경로
mXlsPath = "" # 엑셀 파일이 있는 경로, 틀리면 안됨.
# "/Volumes/ExSSD/Working/Images" # 이미지가 들어있는 폴더, 이 아래에 A,B,Pole 등이 있어야 한다.
mImgPath = ""
def WorkingFolderCheck(path, xlsPath):
if False == os.path.exists(path):
print(f"Error: Working Folder is not exist. {path}")
return False
pathList = ["A", "B", "Pole"]
for folder in pathList:
fullPath = os.path.join(path, folder)
if False == os.path.isdir(fullPath):
print(f"Error: {folder} Folder is not exist. {fullPath}")
return False
mImgPath = path
mCurPath = os.getcwd()
if False == os.path.exists(xlsPath):
mXlsPath = os.getcwd()
else:
mXlsPath = xlsPath
return True
def GetPoleInfos(xls, nRow):
if None == xls or False == isinstance(xls, myxl.PoleXLS):
return []
retList = xls.GetAllRowValue("PoleInfo", nRow, 5)
return retList
# 필요한 이미지 정보를 모아 한 시트에 전부 넣는다.
def CollectImageData(xls, sheetTrg, listImg, pathImgDir):
if None == xls or False == isinstance(xls, myxl.PoleXLS) or True == util.IsEmptyStr(sheetTrg):
return -1
pathcsv = os.path.join(mCurPath, "JusoDB.csv")
jusomgr = JusoMngr.JusoDB(pathcsv)
nIdx = 1
for item in listImg:
# 이미지가 삽입되었는지 여부 확인
if 0 < xls.FindInsertedImgPath(pathImgDir, item):
nIdx = xls.FindLastRow(sheetTrg) + 1
continue
all_date, lat, lon = exEXIF.GetDateInfo(item)
pathRel = os.path.relpath( item, pathImgDir)
level = util.GetParentDirName(item, 1)
if level == "Thumb":
continue
print(f"DBG : {nIdx}, CollectImageData : {item}, {pathImgDir}, {pathRel}")
# 0 : 지번, 1 : 구역, 2 : 도로명, 3 : 도로명 구역
juso = vwapi.GetJusofromGPS(lat, lon)
# 주소 조회가 안되는 현상이 있다. 재시도...
if lat != 0.0 and lon != 0.0 and juso[2] == "":
juso = vwapi.GetJusofromGPS(lat, lon)
# 도로명 주소를 못 끌어오면, 저장해 둔 목록에서 한번 더 조회
if juso[0] != "" and juso[2] == "":
juso[2] = jusomgr.ConvJusoToRoad(juso[0])
juso[3] = jusomgr.GetRoadArea(juso[2])
xls.SetCellValueStr(sheetTrg, 1, nIdx, nIdx)
xls.SetCellValueStr(sheetTrg, 2, nIdx, "") # 이미지가 들어갈 시트 이름. 넣을 떄 넣자.
xls.SetCellValueStr(sheetTrg, 3, nIdx, all_date)
xls.SetCellValueStr(sheetTrg, 4, nIdx, level)
xls.SetCellValueStr(sheetTrg, 5, nIdx, pathRel)
xls.SetCellValueStr(sheetTrg, 6, nIdx, juso[3])
xls.SetCellValueStr(sheetTrg, 7, nIdx, juso[2])
xls.SetCellValueStr(sheetTrg, 8, nIdx, f"{lat},{lon}")
xls.SetCellValueStr(sheetTrg, 9, nIdx, juso[1])
xls.SetCellValueStr(sheetTrg, 10, nIdx, juso[0])
# VWorld 좌표로 변환하여 저장
lat_Vworld, lon_Vworld = 0.0, 0.0
if lat != 0.0 and lon != 0.0 :
lat_Vworld, lon_Vworld = vwapi.Transform4326to3857(lat, lon)
xls.SetCellValueStr(sheetTrg, 11, nIdx, f"{lat_Vworld},{lon_Vworld}")
nIdx += 1
return nIdx
# DB 시트에 있는 파일을 골라내서 PoleInfo 시트에 넣는다.
# PoleInfo : 번호, GPS 좌표, 구역, 주소, 사진
def CollectPoleInfoData(xls, TrgSheetName, DBSheetName, pathImgDir):
if None == xls:
return -1
nLastDBRow = xls.FindLastRow( DBSheetName )
if 0 >= nLastDBRow:
print(f"{DBSheetName} Sheet is Empty.")
return -1
# 헤더가 있는 시트는 헤더가 1, 그래서 2부터 시작
nTrgRow = xls.FindLastRow(TrgSheetName) + 1
for nDBRow in range(1, nLastDBRow + 1):
listValues = xls.GetAllRowValue(DBSheetName, nDBRow, 8)
# 0: Index, 1: Sheetname, 2:Date, 3:Level, 4:Rel Path, 5:Area, 6:Juso, 7:GPS(Lat,Lon)
if None == listValues:
continue
if listValues[1] == TrgSheetName:
continue
if listValues[3] != "Pole" :
continue
if listValues[7] == "" or listValues[7] == "0.0,0.0":
continue
xls.SetCellValueStr(TrgSheetName, 1, nTrgRow, str(nTrgRow -1))
xls.SetCellValueStr(TrgSheetName, 2, nTrgRow, listValues[7])
xls.SetCellValueStr(TrgSheetName, 3, nTrgRow, listValues[5])
xls.SetCellValueStr(TrgSheetName, 4, nTrgRow, listValues[6])
imgPath = os.path.join(pathImgDir, listValues[4])
imgPath = exEXIF.ProcessImg(imgPath, 0.1, 80, True)
xls.SetCellValueImg(TrgSheetName, 5, nTrgRow, imgPath)
xls.SetCellValueStr(TrgSheetName, 6, nTrgRow, listValues[0])
# 목표 시트에 넣었으니 시트 이름을 DB 시트에 적어 넣는다.
xls.SetCellValueStr(DBSheetName, 2, nDBRow, TrgSheetName)
nTrgRow += 1
return nTrgRow
# DB 시트와 점봇대 정보 시트로 정보를 찾고 계산한다.
# Pole : 연번, 점검일자, 구역, 가까운 전신주 번호, 가까운 전신주 거리, 심각, 양호, 등급, 비고
def CollectPoleData( xls, TrgSheetName, InfoSheetName, DBSheetName, pathImgDir ):
if None == xls:
return -1
nLastDBRow = xls.FindLastRow( DBSheetName )
if 0 >= nLastDBRow:
print(f"{DBSheetName} Sheet is Empty.")
return -1
# 헤더가 있는 시트는 헤더가 1, 그래서 2부터 시작
nTrgRow = xls.FindLastRow(TrgSheetName) + 1
for nDBRow in range(1, nLastDBRow + 1):
listValues = xls.GetAllRowValue(DBSheetName, nDBRow, 8)
# 0: Index, 1: Sheetname, 2:Date, 3:Level, 4:Rel Path, 5:Area, 6:Juso, 7:GPS(Lat,Lon)
if None == listValues:
continue
if listValues[1] == TrgSheetName:
continue
if listValues[3] != "A" and listValues[3] != "B" :
continue
if listValues[7] == "" or listValues[7] == "0.0,0.0":
continue
# 날짜만 골라낸다.
strDate = ""
if listValues[2] != "":
strDate, strTime = listValues[2] .split(' ')
strDate = strDate.replace(':','/')
xls.SetCellValueStr(TrgSheetName, 1, nTrgRow, str(nTrgRow -1))
xls.SetCellValueStr(TrgSheetName, 2, nTrgRow, strDate)
# 가장 가까운 점봇대를 찾는다. 인덱스를 얻어서...
strLat, strLon = listValues[7] .split(',')
nPoleIdx, nDistance = xls.FindCloestPole(float(strLat), float(strLon))
# 배열은 0부터, Pole 인덱스는 1부터 시작하고, 셀 번호는 헤더를 포함해서 2 부터 시작한다.
# 셀 번호를 넣어서 해당하는 정보를 가져온다.
# 0:번호, 1:GPS 좌표, 2:구역, 3:주소, 4:사진
#print( f"{nDBRow} : {nTrgRow} : {nPoleIdx}")
retList = xls.GetAllRowValue(InfoSheetName, nPoleIdx, 5)
xls.SetCellValueStr(TrgSheetName, 3, nTrgRow, retList[2])
xls.SetCellValueStr(TrgSheetName, 4, nTrgRow, nPoleIdx)
xls.SetCellValueStr(TrgSheetName, 5, nTrgRow, nDistance)
ColIdx = 6
if listValues[3] == "B" :
ColIdx = 7
imgAbsPath = os.path.join(pathImgDir, listValues[4])
imgpath = exEXIF.ProcessImg(imgAbsPath, 0.1, 80, True)
xls.SetCellValueImg(TrgSheetName, ColIdx, nTrgRow, imgpath)
xls.SetCellValueStr(TrgSheetName, 8, nTrgRow, listValues[3])
xls.SetCellValueStr(TrgSheetName, 9, nTrgRow, nDBRow)
# 목표 시트에 넣었으니 시트 이름을 DB 시트에 적어 넣는다.
xls.SetCellValueStr(DBSheetName, 2, nDBRow, TrgSheetName)
nTrgRow += 1
return nTrgRow
def GetJPGFileLIst(pathImageDir):
trgDirList = util.GetSubDirectories(pathImageDir)
#print(f"DBG : GetJPGFileLIst : {pathImageDir}, {trgDirList}")
#input("Press Enter to continue...")
retImgList = []
for dirName in trgDirList:
if dirName.lower() == "thumb":
continue
imgSubDirPath = os.path.join(pathImageDir, dirName)
contents = os.listdir(imgSubDirPath)
for item in contents:
name, ext = os.path.splitext(item)
if ext.lower() == '.jpg':
imgPath = os.path.join(imgSubDirPath, item)
retImgList.append(imgPath)
return retImgList
def Process(PathImg, PathXLS):
# 이미지 파일 목록 작성
trgImgList = GetJPGFileLIst(PathImg)
# 엑셀을 연다.
XLSPath = os.path.join(PathXLS, "EPoleDB.xlsx")
tempxls = myxl.PoleXLS(XLSPath)
tempxls.DBXLSOpen()
# 이미지 정보를 전부 모아서 한 시트에 저장
CollectImageData(tempxls, "ImgInfo", trgImgList, PathImg)
# 모아 놓은 이미지 정보에서 점봇대 정보를 골라서 저장
CollectPoleInfoData(tempxls, "PoleInfo", "ImgInfo", PathImg)
# 점봇대 정보 시트를 바탕으로 제보받은 이미지를 판별, 계산하여 저장
CollectPoleData(tempxls, "Poles", "PoleInfo", "ImgInfo", PathImg)
tempxls.DBXLSClose()