1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #!/usr/bin/python
# -*- coding: iso-8859-1 -*-
#### Recherche et copies de .shp dans des répertoires cibles
#### en renomant la cible avec le dossier source
####
import arcpy, os, locale
from arcpy import env
dossier1 = r"E:\diag_preventif"
dossier2 = r"E:\site"
env.workspace = r"E:\SIG\importSIG"
types=".shp"
try:
##traitement du dossier 1
##liste des dossiers et fichiers du dossier1
for f in os.listdir(dossier1):
##identification des dossiers
if os.path.isdir(os.path.join(dossier1, f)):
print f
##itération dans les sous-dossiers pour extraire les shapefiles
path = os.path.join(dossier1, f)
for dir, dirnames, files in arcpy.da.Walk(path):
for file in files:
if file[-4:] in types:
out_name = f + "_" + file
newname = os.path.join(env.workspace, out_name)
filepath = os.path.join(dir, file)
code_site = "'%s'" % f
if os.path.isfile(newname):
out_name2 = f + "_" + file[:-4] + "_2.shp"
newname2 = os.path.join(env.workspace, out_name2)
if os.path.isfile(newname2):
out_name3 = f + "_" + file[:-4] + "_3.shp"
newname3 = os.path.join(env.workspace, out_name3)
arcpy.Copy_management(filepath, newname3, "FeatureClass")
arcpy.AddField_management(newname3, "code_site", "TEXT", "", "", "50")
arcpy.CalculateField_management(newname3, "code_site", code_site, "PYTHON_9.3")
else:
arcpy.Copy_management(filepath, newname2, "FeatureClass")
arcpy.AddField_management(newname2, "code_site", "TEXT", "", "", "50")
arcpy.CalculateField_management(newname2, "code_site", code_site, "PYTHON_9.3")
else:
arcpy.Copy_management(filepath, newname, "FeatureClass")
arcpy.AddField_management(newname, "code_site", "TEXT", "", "", "50")
arcpy.CalculateField_management(newname, "code_site", code_site, "PYTHON_9.3")
print "fin dossier1"
##traitement du dossier 2, traitement idem que dossier1
for f in os.listdir(dossier2):
if os.path.isdir(os.path.join(dossier2, f)):
print f
path = os.path.join(dossier2, f)
for dir, dirnames, files in arcpy.da.Walk(path):
for file in files:
if file[-4:] in types:
out_name = f + "_" + file
newname = os.path.join(env.workspace, out_name)
filepath = os.path.join(dir, file)
code_site = "'%s'" % f
if os.path.isfile(newname):
out_name2 = f + "_" + file[:-4] + "_2.shp"
newname2 = os.path.join(env.workspace, out_name2)
if os.path.isfile(newname2):
out_name3 = f + "_" + file[:-4] + "_3.shp"
newname3 = os.path.join(env.workspace, out_name3)
arcpy.Copy_management(filepath, newname3, "FeatureClass")
arcpy.AddField_management(newname3, "code_site", "TEXT", "", "", "50")
arcpy.CalculateField_management(newname3, "code_site", code_site, "PYTHON_9.3")
else:
arcpy.Copy_management(filepath, newname2, "FeatureClass")
arcpy.AddField_management(newname2, "code_site", "TEXT", "", "", "50")
arcpy.CalculateField_management(newname2, "code_site", code_site, "PYTHON_9.3")
else:
arcpy.Copy_management(filepath, newname, "FeatureClass")
arcpy.AddField_management(newname, "code_site", "TEXT", "", "", "50")
arcpy.CalculateField_management(newname, "code_site", code_site, "PYTHON_9.3")
print "fin dossier2"
##regroupement des shapefiles
fcList1 = arcpy.ListFeatureClasses("", "point")
fcList2 = arcpy.ListFeatureClasses("", "polygone")
fcList3 = arcpy.ListFeatureClasses("", "polyline")
arcpy.Merge_management(fcList1, os.path.join(env.workspace, "all_pt.shp"))
arcpy.Merge_management(fcList2, os.path.join(env.workspace, "all_plg.shp"))
arcpy.Merge_management(fcList3, os.path.join(env.workspace, "all_pl.shp"))
print "Ouaisssssssss c finit"
except Exception as e:
print e.message
arcpy.AddError(e.message) |
Partager