增加军民港口文件区分

master
陈增辉 2025-09-22 17:14:55 +08:00
parent 2b72ea22db
commit a9f390d2f8
3 changed files with 311 additions and 24 deletions

View File

@ -0,0 +1,188 @@
import numpy as np
import os
from glob import glob
from pathlib import Path
from multiprocessing import Pool
import hashlib # md5
import time
import shutil
import datetime
########################################################
# 函数区
########################################################
spacetySliceEnvPathExEPath=r"d:\ProgramData\anaconda3\envs\spacetySliceEnv\python.exe"
logPath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\log\process.log"
def existOrCreate(dirpath):
if not os.path.exists(dirpath):
os.makedirs(dirpath)
def moveDir(srcfolderPath,targetfolderPath):
try:
shutil.copytree(srcfolderPath,targetfolderPath,dirs_exist_ok=True)
print("sucess: copy ", srcfolderPath,targetfolderPath )
shutil.rmtree(srcfolderPath)
print("sucess: rmtree ", srcfolderPath)
return "sucess: copy and rmtree from {} to {}".format(srcfolderPath,targetfolderPath),True
except Exception as e:
print("failed: copy ", srcfolderPath, targetfolderPath)
return "failed: copy and rmtree from {} to {}".format(srcfolderPath,targetfolderPath),False
def writeoutlog(logPath,cmdtxt):
with open(logPath,'a',encoding="utf-8") as f:
f.write(cmdtxt)
f.write("\n")
def preProcessTiFF(tiffpath,txtpath,preFolderPath):
file_path = Path(tiffpath)
directory_path = str(file_path.resolve().parent.name)
rootname=Path(tiffpath).stem
outpngpath=os.path.join(preFolderPath,"I"+directory_path+"_"+rootname+'.png')
programpath=r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\generatorRasterSlicesTools\SpacetyTIFFDataStretch2PNG_AC.py"
# cmdtxt=r"{} {} -i {} -o {} --filemode --SquareRoot".format(spacetySliceEnvPathExEPath,programpath,tiffpath,outpngpath)
cmdtxt=r"{} {} -i {} -p {} -o {} --slicemode --SquareRoot".format(
spacetySliceEnvPathExEPath,programpath,
tiffpath,
txtpath,
preFolderPath
) # 直接切片
if os.system(cmdtxt) ==2:
print("sucess:",cmdtxt)
writeoutlog(logPath, "sucess: {}\n".format(cmdtxt))
return 2
else:
print("failed:",cmdtxt)
writeoutlog(logPath, "failed: {}\n".format(cmdtxt))
return 3
def processMJPort(srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,MLCShapeFilePath,JLCShapeFilePath,MJLCShapeFilePath):
programpath = r"R:\TYSAR-德清院\A-预处理-未标注\A0-算法版本\AA\SpacetySliceDataTools\generatorRasterSlicesTools\SplitShipPortRasterTools_AC.py"
cmdtxt=r"{} {} -s {} -o {} -f {} -m {} -j {} --jmlc {}".format(
spacetySliceEnvPathExEPath,programpath,
srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,
MLCShapeFilePath, JLCShapeFilePath, MJLCShapeFilePath
)
if os.system(cmdtxt) ==2:
print("sucess:",cmdtxt)
return "sucess: {}".format(cmdtxt)
else:
print("failed:",cmdtxt)
return "failed: {}".format(cmdtxt)
def preProcessShipPortTools(srcFolderPath,targetFolderPath,outTargetFolderPath):
writeoutlog(logPath, "====================================================================================\n")
writeoutlog(logPath, "time: {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
writeoutlog(logPath, "srcFolderPath: {}".format(srcFolderPath))
writeoutlog(logPath, "targetFolderPath: {}".format(targetFolderPath))
writeoutlog(logPath, "process start \n")
# 文件接口创建
MPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","民港口")
JPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","军港口")
MJPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","混合港口")
NoPortFolder=os.path.join(targetFolderPath,"AC-图像预处理","无港口")
existOrCreate(MPortFolder)
existOrCreate(JPortFolder)
existOrCreate(MJPortFolder)
existOrCreate(NoPortFolder)
outMJPortSumTxtPath=os.path.join(targetFolderPath,"AC-图像预处理","sumMJPort.txt")
outMJPortFolderPath=os.path.join(targetFolderPath,"AC-图像预处理","sumMJPortFolder")
MLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\港口(民船).shp"
JLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军港.shp"
MJLCShapeFilePath=r"D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军民一体港口.shp"
# 港口归属
if processMJPort(srcFolderPath,outMJPortSumTxtPath,outMJPortFolderPath,MLCShapeFilePath,JLCShapeFilePath,MJLCShapeFilePath)==2:
# 逐行处理
MJlines=None
with open(outMJPortSumTxtPath,"r",encoding="utf-8") as f:
MJlines=f.readlines()
MLCName = "MLC" # M
JLCName = "JLC" # J
MJLCName = "MJLC" # JM 混合
NOLCName = "NOLC" # 没有港口
async_results = []
with Pool(processes=6) as pool:
for linestr in MJlines:
if len(linestr)>3:
clsname=linestr.split("\t\t")[0]
tiffpath=linestr.split("\t\t")[1]
rootname=Path(tiffpath).stem
portTxtpath = os.path.join(outMJPortFolderPath, rootname + ".txt")
tarPortFolder=None
if clsname==MLCName:
tarPortFolder=MPortFolder
elif clsname==JLCName:
tarPortFolder=JPortFolder
elif clsname==MJLCName:
tarPortFolder=MJPortFolder
elif clsname==NOLCName:
tarPortFolder=NoPortFolder
else:
continue
async_results.append(preProcessTiFF(tiffpath,portTxtpath,tarPortFolder))
else:
continue
# 处理分块
for pidx in range(len(async_results)):
async_result=async_results[pidx]
print("{}/{}".format(pidx+1,len(async_results)),async_result.get())
writeoutlog(logPath,"{}/{} {}".format(pidx+1,len(async_results),async_result.get()))
pass
else:
pass
writeoutlog(logPath, "\nprocess finished \n")
moverTip,moveflag=moveDir(targetFolderPath, outTargetFolderPath)
writeoutlog(logPath, moverTip)
writeoutlog(logPath, "====================================================================================\n")
if moveflag:
pass
else:
exit(3)
if __name__ == '__main__':
# 20250813-不分类 条带模式
# srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\0-原图"
# preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\A-预处理\AB-图像预处理"
# targetfolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250813-不分类\A-预处理\AB-图像预处理"
# preProcessShipPortTools(srcFolderPath, preFolderPath, targetfolderPath,logPath)
#
# # 20250818 条带模式
# srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\0-原图"
# preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\A-预处理\AB-图像预处理"
# targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250818-不分类\A-预处理\AB-图像预处理"
# preProcessShipPortTools(srcFolderPath, preFolderPath,targetfolderPath, logPath)
#
# 20250826-不分类 条带模式
srcFolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\0-原图"
preFolderPath = r"D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理\AB-图像预处理"
targetfolderPath = r"R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250826-不分类\A-预处理\AB-图像预处理"
preProcessShipPortTools(srcFolderPath, preFolderPath, targetfolderPath,logPath)

View File

@ -13,8 +13,8 @@ import math
from pathlib import Path
sliceSize=1024
portsliceSize=5000
shipsliceSize=1024
BlockOverLayer=0.25
def get_filename_without_ext(path):
@ -253,15 +253,15 @@ def getNextSliceNumber(n,sliceSize,overlap=0.25):
print("重叠率:",movelayer)
return newN,ti
def sliceDataset(rootname,im_data,src_im_data, im_Geotrans, im_proj, outfolder):
def sliceShipDataset(rootname,im_data,src_im_data, im_Geotrans, im_proj, outfolder):
binfolder=os.path.join(outfolder,"unit8binfolder")
pngfolder=os.path.join(outfolder,"pngfolder")
tifffolder=os.path.join(outfolder,"tifffolder")
h,w=im_data.shape
nextH,ht=getNextSliceNumber(h,sliceSize,BlockOverLayer)
nextW,wt=getNextSliceNumber(w,sliceSize,BlockOverLayer)
nextH,ht=getNextSliceNumber(h,shipsliceSize,BlockOverLayer)
nextW,wt=getNextSliceNumber(w,shipsliceSize,BlockOverLayer)
padH=nextH-h
padW=nextW-w
im_data=np.pad(im_data,((0,padH),(0,padW)),mode='constant',constant_values=0)
@ -270,8 +270,8 @@ def sliceDataset(rootname,im_data,src_im_data, im_Geotrans, im_proj, outfolder):
for hi in ht:
for wi in wt:
geotrans_temp=getsliceGeotrans(im_Geotrans,wi,hi)
im_data_temp=im_data[hi:hi+1024,wi:wi+1024]
src_im_data_temp=src_im_data[hi:hi+1024,wi:wi+1024]
im_data_temp=im_data[hi:hi+shipsliceSize,wi:wi+shipsliceSize]
src_im_data_temp=src_im_data[hi:hi+shipsliceSize,wi:wi+shipsliceSize]
slice_ID = slice_ID + 1
if not is_all_same(im_data_temp):
sliceBinPath=os.path.join(binfolder, rootname+"_"+str(slice_ID).zfill(4)+"_image.tiff")
@ -283,9 +283,94 @@ def sliceDataset(rootname,im_data,src_im_data, im_Geotrans, im_proj, outfolder):
Image.fromarray(im_data_temp).save(slicepngPath,compress_level=0)
print("图像切片结束")
return slice_ID
def stretchSliceProcess(infilepath, outfolder, strechmethod):
def ishasPort(im_Geotrans,im_data,MLCPoints,JLCPoints,MJLCPoints):
LCpoints=MLCPoints+JLCPoints+MJLCPoints
# 获取范围
rows=im_data.shape[0]
cols=im_data.shape[1]
x1=im_Geotrans[0]+im_Geotrans[1]*0
x2=im_Geotrans[0]+im_Geotrans[1]*cols
y1=im_Geotrans[3]+im_Geotrans[5]*0
y2=im_Geotrans[3]+im_Geotrans[5]*rows
xmin=min(x1,x2)
xmax=max(x1,x2)
ymin=min(y1,y2)
ymax=max(y1,y2)
# 数据处理
for p in LCpoints:
x_in_range = (p[0] >= xmin) & (p[0]<= xmax)
y_in_range = (p[1] >= ymin) & (p[1] <= ymax)
within_rect_indices_mask = x_in_range & y_in_range
if within_rect_indices_mask:
return True
return False
def slicePortDataset(rootname,im_data,src_im_data, im_Geotrans, im_proj, outfolder,slice_ID,portfilestr):
# 读取portfilestr 中文件
MLCPoints=[]
JLCPoints=[]
MJLCPoints=[]
with open(portfilestr,"r",encoding="utf-8") as portfile:
for line in portfile.readlines():
if(len(line)>3):
linemetas=line.split("\t\t")
clsname=linemetas[0]
pointstr=linemetas[1]
pointx=float(pointstr.split(",")[0])
pointy=float(pointstr.split(",")[1])
if clsname=="JLC":
JLCPoints.append([pointx,pointy])
elif clsname=="MJLC":
MJLCPoints.append([pointx,pointy])
elif clsname=="MLC":
MLCPoints.append([pointx,pointy])
# 处理文件脚本
if len(MLCPoints)==0 and len(JLCPoints)==0 and len(MJLCPoints)==0:
return
else:
pass
# 切片主流程
binfolder=os.path.join(outfolder,"unit8binfolder")
pngfolder=os.path.join(outfolder,"pngfolder")
tifffolder=os.path.join(outfolder,"tifffolder")
h,w=im_data.shape
nextH,ht=getNextSliceNumber(h,portsliceSize,BlockOverLayer)
nextW,wt=getNextSliceNumber(w,portsliceSize,BlockOverLayer)
padH=nextH-h
padW=nextW-w
im_data=np.pad(im_data,((0,padH),(0,padW)),mode='constant',constant_values=0)
src_im_data=np.pad(src_im_data,((0,padH),(0,padW)),mode='constant',constant_values=0)
for hi in ht:
for wi in wt:
geotrans_temp=getsliceGeotrans(im_Geotrans,wi,hi)
im_data_temp=im_data[hi:hi+portsliceSize,wi:wi+portsliceSize]
src_im_data_temp=src_im_data[hi:hi+portsliceSize,wi:wi+portsliceSize]
slice_ID = slice_ID + 1
if ishasPort(geotrans_temp,src_im_data_temp,MLCPoints,JLCPoints,MJLCPoints):
sliceBinPath=os.path.join(binfolder, rootname+"_"+str(slice_ID).zfill(4)+"_image.tiff")
slicepngPath=os.path.join(pngfolder, rootname+"_"+str(slice_ID).zfill(4)+"_image.png")
slicesrctiffPath=os.path.join(tifffolder, rootname+"_"+str(slice_ID).zfill(4)+"_image.tiff")
write_tiff(src_im_data_temp, geotrans_temp, im_proj, slicesrctiffPath)
write_envi(im_data_temp,geotrans_temp,im_proj,sliceBinPath)
Image.fromarray(im_data_temp).save(slicepngPath,compress_level=0)
print("图像切片结束")
return slice_ID
def stretchSliceProcess(infilepath, outfolder,portfilestr, strechmethod):
binfolder=os.path.join(outfolder,"unit8binfolder")
pngfolder=os.path.join(outfolder,"pngfolder")
tifffolder=os.path.join(outfolder,"tifffolder")
@ -305,7 +390,8 @@ def stretchSliceProcess(infilepath, outfolder, strechmethod):
rootname=Path(infilepath).stem
allImagePath=os.path.join(allpngfolder, rootname+"_all.png")
Image.fromarray(im_data).save(allImagePath,compress_level=0)
sliceDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder)
slice_ID=sliceShipDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder)
slice_ID=slicePortDataset(rootname,im_data, src_im_data,im_Geotrans, im_proj, outfolder,slice_ID,portfilestr)
print("图像切片与拉伸完成")
pass
@ -313,7 +399,7 @@ def stretchSliceProcess(infilepath, outfolder, strechmethod):
def getParams():
parser = argparse.ArgumentParser()
parser.add_argument('-i','--infile',type=str,default=r"F:\天仪SAR卫星数据集\舰船数据\bc2-sp-org-vv-20250205t032055-021998-000036-0055ee-01.tiff", help='输入shapefile文件')
# parser.add_argument('-o', '--outfile',type=str,default=r"F:\天仪SAR卫星数据集\舰船数据\bc2-sp-org-vv-20250205t032055-021998-000036-0055ee-01.png", help='输出geojson文件')
parser.add_argument('-p', '--portfile',type=str,default=r"F:\天仪SAR卫星数据集\舰船数据\bc2-sp-org-vv-20250205t032055-021998-000036-0055ee-01.txt", help='输出geojson文件')
parser.add_argument('-o', '--outfile',type=str,default=r"F:\天仪SAR卫星数据集\舰船数据\切片结果", help='输出geojson文件')
group = parser.add_mutually_exclusive_group()
group.add_argument(
@ -378,6 +464,7 @@ if __name__ == '__main__':
intiffPath=parser.infile
modestr=parser.mode
methodstr = parser.method
if modestr == "filemode":
outbinPath = parser.outfile
print('infile=', intiffPath)
@ -386,10 +473,12 @@ if __name__ == '__main__':
stretchProcess(intiffPath, outbinPath, methodstr)
elif modestr == "slicemode":
outfolder = parser.outfile
portfilestr = parser.portfile
print('infile=', intiffPath)
print('outfolder=', outfolder)
print('method=', methodstr)
stretchSliceProcess(intiffPath, outfolder, methodstr)
print('portfile=', portfilestr)
stretchSliceProcess(intiffPath, outfolder,portfilestr, methodstr)
pass
else:
print("模式错误")

View File

@ -146,7 +146,9 @@ def getTiffsInfo(tiffnames,folderpath):
return tiffdict
def getMJSignal(tiffpath,shipPortTree):
def getMJSignal(tiffpath,shipPortTree,outfolderPath):
rootname=Path(tiffpath).stem
portTxtpath=os.path.join(outfolderPath,rootname+".txt")
im_proj, im_Geotrans, geoExtend = read_tifInfo(tiffpath) # geoExtend : [xmin,ymin,xmax,ymax]
[xmin, ymin, xmax, ymax]=geoExtend
center_x = (xmin + xmax) / 2.0
@ -180,6 +182,10 @@ def getMJSignal(tiffpath,shipPortTree):
final_indices = np.array(potential_indices)[within_rect_indices_mask]
if final_points.shape[0]>0:
MLCFlag=True
with open(portTxtpath,"w",encoding="utf-8") as f:
for i in range(final_points.shape[0]):
f.write("{}\t\t{},{}\n".format("MLC",final_points[i,0],final_points[i,1]))
pass
if JLCName in shipPortTree and not shipPortTree[JLCName] is None:
# 3. 使用 query_ball_point 查找以中心点为圆心radius_to_corner 为半径的圆内的所有点的索引
potential_indices = shipPortTree[JLCName].query_ball_point(center_point, r=radius_to_corner)
@ -202,13 +208,17 @@ def getMJSignal(tiffpath,shipPortTree):
final_indices = np.array(potential_indices)[within_rect_indices_mask]
if final_points.shape[0]>0:
JLCFlag=True
with open(portTxtpath,"a",encoding="utf-8") as f:
for i in range(final_points.shape[0]):
f.write("{}\t\t{},{}\n".format("JLC",final_points[i,0],final_points[i,1]))
pass
# 处理软件
return MLCFlag,JLCFlag
def getTiffInPort(shipPortTree,srcFolderPath_0img,outTiffInfoFilePath):
def getTiffInPort(shipPortTree,srcFolderPath_0img,outTiffInfoFilePath,outfolderPath):
tiffpaths=find_tif_files_pathlib(srcFolderPath_0img)
tiffLCPort={
MLCName:[],
@ -217,7 +227,7 @@ def getTiffInPort(shipPortTree,srcFolderPath_0img,outTiffInfoFilePath):
NOLCName:[]
}
for tiffpath in tiffpaths:
MLCFlag,JLCFlag=getMJSignal(tiffpath,shipPortTree)
MLCFlag,JLCFlag=getMJSignal(tiffpath,shipPortTree,outfolderPath)
if MLCFlag and JLCFlag:
tiffLCPort[MJLCName].append(tiffpath)
@ -235,9 +245,7 @@ def getTiffInPort(shipPortTree,srcFolderPath_0img,outTiffInfoFilePath):
f.write("{}\t\t{}\n".format(k,tiffpath))
def SpliteProcess(srcfolderpath,outfolderpath,MLCPath,JLCPath,JMLCPath):
if not os.path.exists(outfolderpath):
os.makedirs(outfolderpath)
def SpliteProcess(srcfolderpath,outfolderPath,outfilepath,MLCPath,JLCPath,JMLCPath):
shipPort={
MLCName:getshapefileInfo(MLCPath),
JLCName:getshapefileInfo(JLCPath),
@ -250,8 +258,7 @@ def SpliteProcess(srcfolderpath,outfolderpath,MLCPath,JLCPath,JMLCPath):
# "JMLC":KDTree(shipPort["JMLC"]),
}
srcFolderPath_0img=os.path.join(srcfolderpath,"0-原图") # 0-原图 文件路径
outTiffInfoFilePath=os.path.join(outfolderpath,"JMPort.txt")
getTiffInPort(shipPortTree, srcFolderPath_0img, outTiffInfoFilePath)
getTiffInPort(shipPortTree, srcFolderPath_0img, outfilepath,outfolderPath)
return True
pass
@ -259,10 +266,11 @@ def SpliteProcess(srcfolderpath,outfolderpath,MLCPath,JLCPath,JMLCPath):
def getParams():
parser = argparse.ArgumentParser()
parser.add_argument('-s','--srcfolder',type=str,default=r'R:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类', help='输入shapefile文件')
parser.add_argument('-o', '--outfolder',type=str,default=r'D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\A-预处理', help='输出geojson文件')
parser.add_argument('-o', '--outfilepath',type=str,default=r'D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\A-预处理\JMPort.txt', help='输出geojson文件')
parser.add_argument('-f', '--folderOutpath',type=str,help=r'PortfolderOutpath', default=r'D:\TYSAR-德清院\TYSAR-条带模式(SM)\港口\20250903-不分类\A-预处理\PortPoints')
parser.add_argument('-m', '--mLC',type=str,help=r'MLC', default=r'D:\TYSAR-德清院\目标点位信息更新\0828目标点位\港口(民船).shp')
parser.add_argument('-j', '--jLC',type=str,help=r'JLC' ,default=r'D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军港.shp')
parser.add_argument('-jm', '--jmLC',type=str,help=r'MJLC', default=r'D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军民一体港口.shp')
parser.add_argument('-jm', '--jmlc',type=str,help=r'MJLC', default=r'D:\TYSAR-德清院\目标点位信息更新\0828目标点位\军民一体港口.shp')
args = parser.parse_args()
return args
@ -270,16 +278,18 @@ if __name__ == '__main__':
try:
parser = getParams()
srcfolder=parser.srcfolder
outfolder=parser.outfolder
outfilepath=parser.outfilepath
folderOutpath=parser.folderOutpath
mLCPath=parser.mLC
jLCPath=parser.jLC
jmLCPath=parser.jmLC
print('srcfolder=',srcfolder)
print('outfolder=',outfolder)
print('outfile=',outfilepath)
print('outfolder=',folderOutpath)
print('mLCPath=',mLCPath)
print('jLCPath=',jLCPath)
print('jmLCPath=',jmLCPath)
SpliteProcess(srcfolder,outfolder,mLCPath,jLCPath,jmLCPath)
SpliteProcess(srcfolder,folderOutpath,outfilepath,mLCPath,jLCPath,jmLCPath)
exit(2)
except Exception as e:
print(e)