303 lines
11 KiB
Python
303 lines
11 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 = "" # 스크립트가 실행되는 경로
|
|
mArcPath = "Zips" # 압축파일이 있는 폴더, 하위를 훑는다.
|
|
mXlsPath = "" # 엑셀 파일이 있는 경로, 틀리면 안됨.
|
|
mImgPath = "/Volumes/ExSSD/Working/Images" # 이미지가 들어있는 폴더, 이 아래에 A,B,Pole 등이 있어야 한다.
|
|
|
|
# 분류작업을 위해 임시로 생성
|
|
mTempPath = "/Volumes/ExSSD/Working/TmpImgs"
|
|
|
|
#def main(argc, argv):
|
|
# if argc != 2:
|
|
# printUsage()
|
|
# return
|
|
|
|
# 기준 경로, 사용할 경로
|
|
# 사용할 경로가 상대경로면 기준 경로를 이용해 계산,
|
|
# 사용할 경로가 절대경로만 경로가 유효한지 확인해서 그대로 반환, 아니라면 기준경로 반환
|
|
# 기준경로는 따로 입력되는게 없으면 실행 경로
|
|
def GetAbsPath(PathCri, PathTrg):
|
|
if True == util.IsEmptyStr(PathCri):
|
|
PathCri = os.getcwd()
|
|
|
|
PathRet = PathCri
|
|
if True == os.path.isabs(PathTrg):
|
|
PathRet = os.path.join(PathCri, PathTrg)
|
|
else:
|
|
PathRet = PathTrg
|
|
|
|
if False == os.path.exists(PathTrg):
|
|
PathRet = PathCri
|
|
|
|
return PathTrg
|
|
|
|
def ExtractArchives(pathArc, pathTrg):
|
|
if False == os.path.exists(pathArc):
|
|
return []
|
|
|
|
ZIPList = os.listdir(pathArc)
|
|
|
|
retUNZipList = []
|
|
for zipFile in ZIPList:
|
|
zipName, zipExt = os.path.splitext(zipFile)
|
|
trgPath = os.path.join(pathTrg, zipName)
|
|
|
|
# 압축을 푼 폴더가 존재한다면 넘어간다. 이후 보강해야 함
|
|
if True == os.path.exists(trgPath):
|
|
continue
|
|
|
|
zipPath = os.path.join(pathArc, zipFile)
|
|
util.ExtractZIP(zipPath, trgPath)
|
|
|
|
retUNZipList.append(trgPath)
|
|
|
|
return retUNZipList
|
|
|
|
|
|
def GetPoleInfos(xls, nRow):
|
|
if None == xls or False == isinstance(xls, myxl.PoleXLS):
|
|
return []
|
|
|
|
retList = xls.GetAllRowValue("PoleInfo", nRow, 5)
|
|
return retList
|
|
|
|
|
|
def GetJPGFileList(listUNZip, pathImage):
|
|
trgDirList = []
|
|
# 압축 푼 폴더가 있으면 그것만, 아니면 이미지 폴더 전부를 다
|
|
if len(listUNZip) > 0:
|
|
trgDirList = listUNZip
|
|
else:
|
|
trgDirList = util.GetSubDirectories(pathImage)
|
|
|
|
retImgList = []
|
|
for dirName in trgDirList:
|
|
imgSubDirPath = os.path.join(pathImage, 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 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
|
|
|
|
# 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
|
|
|
|
print( listValues )
|
|
|
|
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 main():
|
|
# 경로를 정리하여 절대 경로로 변환해서 저장한다.
|
|
CurPath = GetAbsPath(mCurPath, "")
|
|
ArcDirPath = GetAbsPath(CurPath, mArcPath)
|
|
XlsDirPath = GetAbsPath(CurPath, mXlsPath)
|
|
ImgDirPath = GetAbsPath(CurPath, mImgPath)
|
|
|
|
# 압축해제
|
|
listUNZip = ExtractArchives(ArcDirPath, ImgDirPath)
|
|
|
|
# 이미지 파일 목록 작성
|
|
trgImgList = GetJPGFileList(listUNZip, ImgDirPath)
|
|
|
|
# 엑셀을 연다.
|
|
XLSPath = os.path.join(XlsDirPath, "EPoleDB.xlsx")
|
|
tempxls = myxl.PoleXLS(XLSPath)
|
|
tempxls.DBXLSOpen()
|
|
|
|
# 이미지 정보를 전부 모아서 한 시트에 저장
|
|
CollectImageData(tempxls, "ImgInfo", trgImgList, ImgDirPath)
|
|
# 모아 놓은 이미지 정보에서 점봇대 정보를 골라서 저장
|
|
CollectPoleInfoData(tempxls, "PoleInfo", "ImgInfo", ImgDirPath)
|
|
# 점봇대 정보 시트를 바탕으로 제보받은 이미지를 판별, 계산하여 저장
|
|
CollectPoleData(tempxls, "Poles", "PoleInfo", "ImgInfo", ImgDirPath)
|
|
|
|
tempxls.DBXLSClose()
|
|
|
|
|
|
def printUsage():
|
|
print("Usage : python main.py <Image Folder Path> <Excel File Name>")
|
|
|
|
# For Main Loop
|
|
if __name__ == '__main__':
|
|
# argc = len(sys.argv)
|
|
# argv = sys.argv
|
|
# main(argc, argv)
|
|
main() |