















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Ce document propose un ensemble de scripts Python exclusivement dédiés au développement d'outils pour ArcGIS for Desktop.
Typology: Study notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Afficher les coordonnées (x, y) des entités de type point ou multi-‐point
_# -‐-‐ coding utf8 -‐-‐ #-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
#-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐_ import arcpy fcIn = arcpy.GetParameterAsText( 0 ) champ = arcpy.GetParameterAsText( 1 ) dscFC = arcpy.Describe(fcIn) if champ: listJetons = ["OID@", "Shape@XY", champ] else : listJetons = ["OID@", "Shape@XY"] if dscFC.shapeType == "Point": arcpy.AddMessage(u"\n\nAffichage de chaque entité point :") with arcpy.da.SearchCursor(fcIn, listJetons) as curseur: for row in curseur: x, y = row[ 1 ] if champ: arcpy.AddMessage(u"\nPoint ID {0} : {3}\n -‐ X : {1}\n -‐ Y : {2}" .format(row[ 0 ], x, y, row[ 2 ])) else : arcpy.AddMessage(u"\nPoint ID {0} :\n -‐ X : {1}\n -‐ Y : {2}" .format(row[ 0 ], x, y)) elif dscFC.shapeType == "Multipoint": arcpy.AddMessage(u"\n\nAffichage de chaque entité multipoint :") listJetons[ 1 ] = "Shape@" with arcpy.da.SearchCursor(fcIn, listJetons) as curseur: for row in curseur: if champ: arcpy.AddMessage("\nMultipoint ID {0} : {1}\n".format(row[ 0 ], row[ 2 ])) else : arcpy.AddMessage("\nMultipoint ID {0} :\n".format(row[ 0 ])) for pnt in row[ 1 ]: arcpy.AddMessage(" -‐ X : {0}\n -‐ Y : {1}\n".format(pnt.X, pnt.Y)) arcpy.AddMessage(u"\n-‐-‐ Il n'y a plus d'entités : processus terminé -‐-‐\n")
Afficher les coordonnées (x, y) des entités de type polygone ou polyline
_# -‐-‐ coding utf8 -‐-‐ #-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
#-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐_ import arcpy fcIn = arcpy.GetParameterAsText( 0 ) with arcpy.da.SearchCursor(fcIn, ["OID@", "Shape@ "]) as rows: for row in rows: arcpy.AddMessage(u"\nEntité {0}:".format(row[ 0 ])) idPart = 0 for part in row[ 1 ]: arcpy.AddMessage(" Partie {0}:".format(idPart)) idSurfInt = 0 for point in part: if point: arcpy.AddMessage(" X : {0}, Y : {1}" .format( round (point.X, 2 ), round (point.Y, 2 ))) else : arcpy.AddMessage(" Surface interne {0}:".format(idSurfInt)) idSurfInt += 1 idPart += 1 arcpy.AddMessage(u"\n-‐-‐ Il n'y a plus d'entités : processus terminé -‐-‐\n")
Création d’entités dans une classe d'entités de type multipoint
_# -‐-‐ coding utf 8 -‐-‐
import arcpy arcpy.env.overwriteOutput = True fcOut = arcpy.GetParameterAsText( 0 ) prj = arcpy.GetParameterAsText( 1 ) rep = os.path.split(fcOut)[ 0 ] nFcOut = os.path.split(fcOut)[ 1 ] arcpy.CreateFeatureclass_management(rep, nFcOut, "MULTIPOINT", "", "", "", prj) listMPt = [[[ 1 , 1 ],[ 2 , 1 ],[ 4 , 1 ]],[[ 3 , 1 ]],[[ 5 , 1 ]]] point = arcpy.Point() array = arcpy.Array() with arcpy.da.InsertCursor(fccOut, ["SHAPE@"]) as rows: for row in listMPt: for coord in row: point.X, point.Y = coord[ 0 ], coord[ 1 ] array.add(point) multiPoint = arcpy.Multipoint(array) rows.insertRow([multiPoint]) array.removeAll()
Création d’entités dans une classe d'entités de type polyline
_# -‐-‐ coding: utf8 -‐-‐
import arcpy, os arcpy.env.overwriteOutput = True fcOut = arcpy.GetParameterAsText( 0 ) prj = arcpy.GetParameterAsText( 1 ) rep = os.path.split(fcOut)[ 0 ] fcOut = os.path.split(fcOut)[ 1 ] arcpy.CreateFeatureclass_management(rep, fcOut, "POLYLINE", "", "", "", prj) listPlg = [[[[ 1 , 11 ],[ 1 , 19 ],[ 9 , 19 ],[ 9 , 11 ]]], [[[ 11 , 11 ],[ 11 , 19 ],[ 19 , 19 ],[ 19 , 11 ]]], [[[ 1 , 1 ],[ 1 , 8 ],[ 8 , 1 ]], [[ 2 , 9 ],[ 9 , 9 ],[ 9 , 2 ]]], [[[ 11 , 1 ],[ 11 , 9 ],[ 19 , 9 ],[ 19 , 1 ]], [[ 12 , 3 ],[ 12 , 8 ],[ 17 , 8 ]], [[ 13 , 2 ],[ 18 , 7 ],[ 18 , 2 ]]]] point = arcpy.Point() array = arcpy.Array() pArray = arcpy.Array() with arcpy.da.InsertCursor(fcOut, ["SHAPE@"]) as rows: for row in listPlg: for part in row: for coord in part: point.X, point.Y = coord[ 0 ], coord[ 1 ] array.add(point) pArray.add(array) array.removeAll() polylines = arcpy.Polyline(pArray) rows.insertRow([polylines]) pArray.removeAll()
Sélection selon les attributs avec récupération de la sélection dans une classe d’entités
_# -‐-‐ coding utf 8 -‐-‐ #-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
#-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐_ import arcpy arcpy.env.overwriteOutput = True fcIn = arcpy.GetParameterAsText( 0 ) fcOut = arcpy.GetParameterAsText( 1 ) reqSQL = arcpy.GetParameterAsText( 2 ) lyrOut = "Couche_Temp" arcpy.MakeFeatureLayer_management(fcIn, lyrOut, reqSQL) arcpy.CopyFeatures_management(lyrOut, fcOut)
Sélection selon l’emplacement avec récupération de la sélection dans une classe d’entités
_# -‐-‐ coding utf 8 -‐-‐ #-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
#-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐_ import arcpy arcpy.env.overwriteOutput = True fcCible = arcpy.GetParameterAsText( 0 ) reqSp = arcpy.GetParameterAsText( 1 ) fcSource = arcpy.GetParameterAsText( 2 ) dist = arcpy.GetParameterAsText( 3 ) typSel = arcpy.GetParameterAsText( 4 ) fcOut = arcpy.GetParameterAsText( 5 ) lyrCible = "Couche_Temp" arcpy.MakeFeatureLayer_management(fcCible, lyrCible) listTrue = ["INTERSECT","INTERSECT_3D","WITHIN_A_DISTANCE","WITHIN_A_DISTANCE_3D", "CONTAINS","WITHIN","COMPLETELY_WITHIN","HAVE_THEIR_CENTER_IN"] if reqSp in listTrue: arcpy.SelectLayerByLocation_management(lyrCible, reqSp, fcSource, dist, typSel) else : arcpy.SelectLayerByLocation_management(lyrCible, reqSp, fcSource, "", typSel) arcpy.CopyFeatures_management(lyrOut, fcOut)
Lister les classes d’entités d’une géodatabase
_# -‐-‐ coding utf 8 -‐-‐ #-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
#-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐_ import arcpy, os rep = arcpy.GetParameterAsText( 0 ) repSplit = os.path.split(rep)[ 1 ] arcpy.env.workspace = rep # -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ arcpy.AddMessage(u"\nClasses d’entités de '{0}' :".format(repSplit)) for fc in arcpy.ListFeatureClasses(): arcpy.AddMessage(fc) # -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ arcpy.AddMessage(u"\nClasses d’entités de type 'Point' de '{0}' :".format(repSplit)) for fc in arcpy.ListFeatureClasses("", "Point"): arcpy.AddMessage(fc) arcpy.AddMessage("\n")
Lister les champs d’une classe d’entités
_# -‐-‐ coding utf 8 -‐-‐ #-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
#-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐_ import arcpy, os fcIn = arcpy.GetParameterAsText( 0 ) fcSplit = os.path.split(fcIn)[ 1 ] # -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ arcpy.AddMessage(u"\nChamps de la classe d’entités {0}".format(fcSplit)) for field in arcpy.ListFields(fcIn): arcpy.AddMessage(field.name) # -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ arcpy.AddMessage(u"\nChamps ('String') de la classe d’entités {0}".format(fcSplit) for field in arcpy.ListFields(fcIn, "", "String"): arcpy.AddMessage(field.name) arcpy.AddMessage("\n")
Écrire du contenu dans un fichier texte
_# -‐-‐ coding utf8 -‐-‐ #-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐
#-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐_ import arcpy, os, codecs ficTxt = arcpy.GetParameterAsText( 0 ) listVille = [u"Villes\t\tDépt\r\n\r\n", u"Paris\t\t75\r\n", u"Marseille\t13\r\n", u"Lyon\t\t69\r\n"] ficTxtW = codecs.open(ficTxt, "w", "utf8") # -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ ficSplit = os.path.split(ficTxt)[ 1 ] arcpy.AddMessage(u"\nÉcriture dans le fichier texte '{0}' :\n".format(ficSplit)) for ligne in listVille: arcpy.AddMessage(u"\nÉcriture de la chaine : '{0}'".format(ligne)) ficTxtW.writelines(ligne) ficTxtW.close()