Ten skrypt w Pythonie jest swego rodzaju metodą „taśmy izolacyjnej”. Jest ograniczony do 20 pól, ale można go łatwo dodać, aby umożliwić działanie dla dowolnej liczby pól w danych.
Skrypt został zaprojektowany do uruchamiania jako narzędzie skryptowe, dlatego należy ustawić parametry w ArcCatalog.
Parametry powinny być następujące:
- InputFC - klasa funkcji
- Lokalizacja wyjściowa - przestrzeń robocza
- Nazwa wyjściowa - ciąg
- Field1 - Field
- Field2 - Field
- Aż do Field20
Kod:
import arcpy, os, sys
from arcpy import env
arcpy.env.overwriteOutput = True
inFC = arcpy.GetParameterAsText(0)
outLoc = arcpy.GetParameterAsText(1)
outName = arcpy.GetParameterAsText(2)
field1 = arcpy.GetParameterAsText(3)
field2 = arcpy.GetParameterAsText(4)
field3 = arcpy.GetParameterAsText(5)
field4 = arcpy.GetParameterAsText(6)
field5 = arcpy.GetParameterAsText(7)
field6 = arcpy.GetParameterAsText(8)
field7 = arcpy.GetParameterAsText(9)
field8 = arcpy.GetParameterAsText(10)
field9 = arcpy.GetParameterAsText(11)
field10 = arcpy.GetParameterAsText(12)
field11 = arcpy.GetParameterAsText(13)
field12 = arcpy.GetParameterAsText(14)
field13 = arcpy.GetParameterAsText(15)
field14 = arcpy.GetParameterAsText(16)
field15 = arcpy.GetParameterAsText(17)
field16 = arcpy.GetParameterAsText(18)
field17 = arcpy.GetParameterAsText(19)
field18 = arcpy.GetParameterAsText(20)
field19 = arcpy.GetParameterAsText(21)
field20 = arcpy.GetParameterAsText(22)
fieldList = ["SHAPE@"]
arcpy.AddMessage(" ")
arcpy.AddMessage("Appending field choices to new List ")
arcpy.AddMessage(" ")
if (field1 != ""):
fieldList.append(field1)
if (field2 != ""):
fieldList.append(field2)
if (field3 != ""):
fieldList.append(field3)
if (field4 != ""):
fieldList.append(field4)
if (field5 != ""):
fieldList.append(field5)
if (field6 != ""):
fieldList.append(field6)
if (field7 != ""):
fieldList.append(field7)
if (field8 != ""):
fieldList.append(field8)
if (field9 != ""):
fieldList.append(field9)
if (field10 != ""):
fieldList.append(field10)
if (field11 != ""):
fieldList.append(field1)
if (field12 != ""):
fieldList.append(field12)
if (field13 != ""):
fieldList.append(field13)
if (field14 != ""):
fieldList.append(field14)
if (field15 != ""):
fieldList.append(field15)
if (field16 != ""):
fieldList.append(field16)
if (field17 != ""):
fieldList.append(field17)
if (field18 != ""):
fieldList.append(field18)
if (field19 != ""):
fieldList.append(field19)
if (field20 != ""):
fieldList.append(field20)
arcpy.AddMessage(" ")
#arcpy.AddMessage(fieldList)
oldFieldList = arcpy.ListFields(inFC)
fieldTypes = []
numOfFields = len(fieldList)
fieldIndex = 1
reorderedFields = []
for fld in fieldList:
for f in oldFieldList:
if f.name == fld:
arcpy.AddMessage(f.name)
reorderedFields.append(f)
break
arcpy.AddMessage(" ")
arcpy.AddMessage(reorderedFields)
desc = arcpy.Describe(inFC)
geoType = desc.shapeType.upper()
spatRef = arcpy.Describe(inFC).spatialReference
arcpy.CreateFeatureclass_management(outLoc, outName, geoType, "", "", "", spatRef)
newFeat = os.path.join(outLoc, outName)
for flds in reorderedFields:
if (flds.type == "String"):
fLength = flds.length
arcpy.AddField_management(newFeat, flds.name, flds.type, "", "", str(fLength))
else:
arcpy.AddField_management(newFeat, flds.name, flds.type)
arcpy.AddMessage(" ")
arcpy.AddMessage(fieldList)
arcpy.AddMessage(" ")
arcpy.AddMessage("Features will be copied with new Schema...")
count = 0
with arcpy.da.SearchCursor(inFC, fieldList) as cursor:
for row in cursor:
insertCursor = arcpy.da.InsertCursor(newFeat, (fieldList))
if (numOfFields == 21):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]) + ', ' + str(row[20]))
count += 1
elif (numOfFields == 20):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]) + ', ' + str(row[19]))
count += 1
elif (numOfFields == 19):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]) + ', ' + str(row[18]))
count += 1
elif (numOfFields == 18):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]) + ', ' + str(row[17]))
count += 1
elif (numOfFields == 17):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15], row[16]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]) + ', ' + str(row[16]))
count += 1
elif (numOfFields == 16):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14], row[15]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]) + ', ' + str(row[15]))
count += 1
elif (numOfFields == 15):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]) + ', ' + str(row[14]))
count += 1
elif (numOfFields == 14):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]) + ', ' + str(row[13]))
count += 1
elif (numOfFields == 13):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]) + ', ' + str(row[12]))
count += 1
elif (numOfFields == 12):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]) + ', ' + str(row[11]))
count += 1
elif (numOfFields == 11):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]) + ', ' + str(row[10]))
count += 1
elif (numOfFields == 10):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]) + ', ' + str(row[9]))
count += 1
elif (numOfFields == 9):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]) + ', ' + str(row[8]))
count += 1
elif (numOfFields == 8):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]) + ', ' + str(row[7]))
count += 1
elif (numOfFields == 7):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5], row[6]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]) + ', ' + str(row[6]))
count += 1
elif (numOfFields == 6):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4], row[5]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]) + ', ' + str(row[5]))
count += 1
elif (numOfFields == 5):
insertCursor.insertRow((row[0], row[1], row[2], row[3], row[4]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]) + ', ' + str(row[4]))
count += 1
elif (numOfFields == 4):
insertCursor.insertRow((row[0], row[1], row[2], row[3]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]) + ', ' + str(row[3]))
count += 1
elif (numOfFields == 3):
insertCursor.insertRow((row[0], row[1], row[2]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]) + ', ' + str(row[2]))
count += 1
elif (numOfFields == 2):
insertCursor.insertRow((row[0], row[1]))
arcpy.AddMessage(" ")
arcpy.AddMessage("Index: " + str(count) + " -----> " + str(row[1]))
count += 1
Zdaję sobie sprawę, że nie jest to najładniejszy ani najwydajniejszy sposób na zrobienie tego, ale tak naprawdę działa całkiem nieźle i napisanie go zajęło około pół godziny. Używam tego cały czas, gdy muszę zmienić kolejność pól i nie wymaga to programu Diagrammer ani Microsoft Access. Oczywiście maksymalna liczba pól może być problemem, ale znowu można ją dostosować w skrypcie.