import os
import shutil
location="G:\\guanzhou_all\\BAIYUN_400-100"                                             #Target folder path
folder_parameter="centerline_30"                                           #Folder name
path_parameter=os.path.join(location,folder_parameter)                         #define workspace creat floder
path_testpoint="G:\\guanzhou_all\\BAIYUN_400-100\\TP"                            #Test point folder path
path_building="G:\\guanzhou_all\\BAIYUN_400-100\\BAIYUN_400-100.shp"             #Building .shp file path
if os.path.exists(path_parameter):
   shutil.rmtree(path_parameter) #delete the old folder if it exists    

import arcpy
from arcpy import env  #https://blog.csdn.net/qq_32813465/article/details/119276327
from arcpy.sa import * 

os.mkdir(path_parameter)
env.workspace = path_parameter #define workspace 
radius=0.002

paths_shp = [os.path.join(path_testpoint, j) for j in os.listdir(path_testpoint) if j.endswith('.shp')]
    #find the paths of all shp files
print(paths_shp)

for i in range(len(paths_shp)):
    path_shp = paths_shp[i]
    print("path_shp")
    print(path_shp)
    filename=os.path.basename(path_shp) #read the file names of all shp files
    print(filename)
    filename_buffer='buffer_'+filename # name the output files in buffer analysis
    filename_square= 'square_'+filename # 在要素包络矩形转面中命名输出文件.
    filename_clip ='clip_'+filename # name the output files in clip analysis
    path_site = os.path.join(path_parameter, os.path.splitext(filename)[0]) #define a path for each site (the file extension is excluded)
    os.mkdir(path_site)
    print(path_site)
    folder_backup = "backup" # 命名备份文件夹
    path_backup = os.path.join(path_site, folder_backup) #备份文件夹路径=测点文件夹下新建
    os.mkdir(path_backup) # 创建指定文件夹路径 

    path_buffer_backup = os.path.join(path_backup,filename_buffer) #在备份文件夹中创建buffer文件
    path_square_backup = os.path.join(path_backup,filename_square)#在备份文件夹中创建square文件
    print("circle path")
    print(path_buffer_backup)
    path_clip_backup = os.path.join(path_backup,filename_clip)#在备份文件夹中创建clip文件
    print(path_clip_backup)
    arcpy.Buffer_analysis(path_shp, path_buffer_backup, radius) #create a buffer area for each testpoint，
    arcpy.FeatureEnvelopeToPolygon_management(path_buffer_backup, path_square_backup, "SINGLEPART") #create a square area for each buffer
    arcpy.Clip_analysis(path_square_backup, path_building, path_clip_backup) #clip the building area as a whole for each buffer area
    folder_temp1 = "temp1"
    folder_temp2 = "temp2"
    folder_temp3 = "temp3"
    folder_temp4 = "temp4"
    folder_temp5 = "temp5"
    folder_temp6 = "temp6"
    path_temp1 = os.path.join(path_site, folder_temp1)
    path_temp2 = os.path.join(path_site, folder_temp2)
    path_temp3 = os.path.join(path_site, folder_temp3)
    path_temp4 = os.path.join(path_site, folder_temp4)
    path_temp5 = os.path.join(path_site, folder_temp5)
    path_temp6 = os.path.join(path_site, folder_temp6)
    os.mkdir(path_temp1)
    os.mkdir(path_temp2)
    os.mkdir(path_temp3)
    os.mkdir(path_temp4)
    os.mkdir(path_temp5)
    os.mkdir(path_temp6)
    print('3done!')
    print(path_temp1)
    print(path_temp6)                                                        
    arcpy.SplitByAttributes_analysis(path_clip_backup,path_temp1,"TP") 
    #split the clipped building area
    arcpy.SplitByAttributes_analysis(path_square_backup,path_temp6, "TP")
    #split the buffer area
    print('4done!')
    paths_shp_temp1 = [os.path.join(path_temp1, k) for k in os.listdir(path_temp1) if k.endswith('.shp')] #find the paths of all temp shp files
    print('5done!')
    print(paths_shp_temp1)
    for m in range(len(paths_shp_temp1)):
        path_shp_temp1 = paths_shp_temp1[m]
        filename_temp1 = os.path.basename(path_shp_temp1) #read the file names of all shp files
        print(filename_temp1)
        filename_temp2 = 'separate_' + filename_temp1 #name the output files in MultipartToSinglepart process
        print(filename_temp2)
        filename_temp3 = 'raster_' + os.path.splitext(filename_temp1)[0] #name the output files in Euclidean allocation process (the file extension is excluded)
        filename_temp4 = 'polygon_' + filename_temp1
        filename_temp5 = 'polyline_' + filename_temp1
        filename_temp6 = filename_temp1
        path_filename_temp2 = os.path.join(path_temp2,filename_temp2)
        path_filename_temp3 = os.path.join(path_temp3,filename_temp3)
        path_filename_temp4 = os.path.join(path_temp4,filename_temp4)
        path_filename_temp5 = os.path.join(path_temp5,filename_temp5)
        path_filename_temp6 = os.path.join(path_temp6,filename_temp6)
        path_filename_site = os.path.join(path_site,filename_temp1)
        print(path_filename_temp2)
        arcpy.MultipartToSinglepart_management(path_shp_temp1,path_filename_temp2) #explode the whole building area into multiple parts
        arcpy.env.extent = path_filename_temp6 #set the extent (environment) following the extent of the corresponding buffer area
        eucAllocate = EucAllocation(path_filename_temp2,"","",0.000001,"FID") #calcuate and allocate the Euc distances based on the exploded building polygons
        print('6done!')
        eucAllocate.save(path_filename_temp3)
        arcpy.RasterToPolygon_conversion(path_filename_temp3,path_filename_temp4)
        arcpy.PolygonToLine_management(path_filename_temp4,path_filename_temp5)
        arcpy.AddGeometryAttributes_management(path_filename_temp5, "LINE_START_MID_END")
        print('7done!')
        with arcpy.da.UpdateCursor(path_filename_temp5, "LEFT_FID") as cursor:
            for row in cursor:
                if (row[0] == -1):
                    cursor.deleteRow()
        arcpy.DeleteIdentical_management(path_filename_temp5,["START_X","START_Y","END_X","END_Y"]) # delete the duplicated centerlines
        arcpy.Clip_analysis(path_filename_temp5, path_filename_temp6, path_filename_site)  # clip the centerlines inside the buffer area
        arcpy.env.extent = "default" #set the extent (environment) back to default
        print("道路中心线创建完成。")