📄 partitionedoverlay.py
字号:
elif tool_name == "union_analysis":
tilefc = CreateTempFolder(parameter_list, 1)
if parameter_list[0].find(" ") > 0:
testgpram_params = '"' + parameter_list[0] + '"' + ' tile "' + tilefc + '" max'
testgpram_params = testgpram_params.replace("'", "")
else:
testgpram_params = parameter_list[0] + " tile " + tilefc + " max"
if location_of_testGPRAM != "":
_CreateProcess(location_of_testGPRAM, testgpram_params)
else:
_CreateProcess(location_of_testGPRAM_NOSPACES, testgpram_params)
gp.refreshcatalog(os.path.dirname(tilefc))
gp.AddMessage(msgNumberPartitions % (gp.getcount(tilefc)))
elif tool_name == "clip_analysis":
tilefc = CreateTempFolder(parameter_list, 2)
if parameter_list[0].find(" ") > 0 or parameter_list[1].find(" ") > 0:
testgpram_params = '"' + parameter_list[0] + ';' + parameter_list[1] + '" tile "' + tilefc + '" min'
testgpram_params = testgpram_params.replace("'", "")
else:
testgpram_params = parameter_list[0] + ";" + parameter_list[1] + " tile " + tilefc + " min"
if location_of_testGPRAM != "":
_CreateProcess(location_of_testGPRAM, testgpram_params)
else:
_CreateProcess(location_of_testGPRAM_NOSPACES, testgpram_params)
gp.refreshcatalog(os.path.dirname(tilefc))
gp.AddMessage(msgNumberPartitions % (gp.getcount(tilefc)))
else:
tilefc = CreateTempFolder(parameter_list, 2)
if parameter_list[0].find(" ") > 0 or parameter_list[1].find(" ") > 0:
testgpram_params = '"' + parameter_list[0] + ';' + parameter_list[1] + '" tile "' + tilefc + '" max'
testgpram_params = testgpram_params.replace("'", "")
else:
testgpram_params = parameter_list[0] + ';' + parameter_list[1] + " tile " + tilefc + " max"
if location_of_testGPRAM != "":
_CreateProcess(location_of_testGPRAM, testgpram_params)
else:
_CreateProcess(location_of_testGPRAM_NOSPACES, testgpram_params)
gp.refreshcatalog(os.path.dirname(tilefc))
gp.AddMessage(msgNumberPartitions % (gp.getcount(tilefc)))
#Cut it all up
CutItUp(tool_name, parameter_list, tilefc)
except Exception, ErStr:
raise Exception, str(ErStr)
except:
ErStr = msgFailureRunLargeOverlay + "\n" + str(gp.getmessages(2))
raise Exception, ErStr
def ScratchWorkspaceExists(toolname, param_list):
try:
sw = gp.scratchworkspace
if toolname == "intersect_analysis" or toolname == "union_analysis":
fcs = param_list[0].split(";")
fc1 = fcs[1]
fc1 = os.path.dirname(fc1)
if sw.upper() == fc1.upper():
raise Exception, msgScratchWorkspaceNotEqual
else:
fcs = [param_list[0], param_list[1]]
if sw.upper() == os.path.dirname(param_list[0].upper()):
raise Exception, msgScratchWorkspaceNotEqual
for fc in fcs:
if fc.find(".sde") > 0 and gp.scratchworkspace == "":
raise Exception, msgScratchWorkspaceEmpty
elif gp.scratchworkspace.find(".sde") > 0:
raise Exception, msgScratchWorkspaceNotSde
except Exception, ErStr:
raise Exception, ErStr
except:
ErStr = msgFailureInScratchWSExists + "\n" + str(gp.getmessages(2))
raise Exception, ErStr
def QualifyTableNameSDE(parameter):
try:
if parameter.find(".sde") > 0:
table_name = gp.qualifytablename(os.path.basename(parameter), os.path.dirname(parameter))
parameter = parameter.replace(os.path.basename(parameter), table_name)
return parameter
except:
ErStr = msgFailureInQualifyTableNameSDE + "\n" + str(gp.getmessages(2))
raise Exception, ErStr
def CutItUp(toolname, param_list, tile_featureclass):
#For each tile, this function cuts the input feature classes using the testGPRAM.exe tool. The overlay
#operation in question is then run for the tile and appended to the output.
try:
global empty_fc
totaltiles = gp.getcount(tile_featureclass)
fctilelist = []
#The input feature classes for Intersect and Union are input with a Multivalue list, therefore all feature classes
#that need to be cut are in the zero parameter. The other overlay operations have an input feature class
#and then an operation feature class (e.g. the "erase" feature class), both of which must be cut to tile.
if toolname == "intersect_analysis" or toolname == "union_analysis":
fcs = '(' + param_list[0] + ')'
else:
fcs = "'" + param_list[0] + "';'" + param_list[1] + "'"
#Create a value table of input feature classes.
vt = gp.CreateObject("ValueTable")
vt.LoadFromString(fcs)
row_count = vt.RowCount
#Get number of records in tile featureclass
tiles = gp.getcount(tile_featureclass)
#Get location for the tile featureclass
tilefolder = os.path.dirname(tile_featureclass)
#Loop through each tile in the tile featureclass
for tile in range(tiles):
if tile_featureclass.find(".mdb") > 0:
tile = tile + 1
totaltiles = totaltiles + 1
fctilelist = []
empty_fc = []
#Select a tile from the tile feature class.
t = CreateTile(tile_featureclass, tile)
#Loop through each input feature class.
for x in range(0, row_count):
inFC = vt.GetValue(x, 0)
if inFC[0] == "'" and inFC[-1] == "'":
inFC = inFC[1:-1]
dsc = gp.describe(inFC)
#Create a name for the piece of the input feature class and make sure it is put in the correct location
#based on the type of feature class.
fcname = os.path.basename(inFC)
#Create a temp GDB to hold the temporary "partitioned" feature classes (if needed).
if dsc.datatype == "ShapeFile":
outfc_partition = tilefolder + "\\" + fcname
elif dsc.datatype == "CoverageFeatureClass":
dir = os.path.dirname(inFC)
fcname = os.path.basename(dir)
if gp.scratchworkspace.find(".mdb") > 0:
outfc_partition = tilefolder + "\\" + fcname
else:
outfc_partition = tilefolder + "\\" + fcname + ".shp"
elif inFC.find(".sde") > 0:
fcname = inFC.split(".")[-1]
if gp.scratchworkspace.find(".mdb") > 0:
tempgdbname = "tile_" + fcname
outfc_partition = gp.scratchworkspace + "\\" + tempgdbname
else:
tempgdbname = "tile_" + fcname + ".mdb"
outputgdb = gp.createpersonalgdb(gp.scratchworkspace, tempgdbname)
outfc_partition = outputgdb + "\\" + fcname
else:
if gp.scratchworkspace.find(".mdb") > 0:
tempgdbname = "tile_" + fcname
outfc_partition = gp.scratchworkspace + "\\" + fcname
else:
tempgdbname = "tile_" + fcname + ".mdb"
outputgdb = gp.createpersonalgdb(gp.scratchworkspace, tempgdbname)
outfc_partition = outputgdb + "\\" + fcname
#Piecewise cut using testGPRAM.exe.
gp.AddMessage(msgCuttingPartitionNumber % (inFC, tile, totaltiles - 1))
try:
testgpram_params = ' "' + inFC + '" CUT "' + outfc_partition + '" "' + tile_featureclass + '" ' + str(tile)
if location_of_testGPRAM != "":
_CreateProcess(location_of_testGPRAM, testgpram_params)
else:
_CreateProcess(location_of_testGPRAM_NOSPACES, testgpram_params)
gp.refreshcatalog(os.path.dirname(outfc_partition))
gp.AddMessage("Number of features in " + outfc_partition + " " + str(gp.getcount(outfc_partition)))
except Exception, ErStr:
tempErCutStr = msgErrorCutting % (inFC, tile)
ErStr = str(ErStr) + "\n" + tempErCutStr
raise Exception, ErStr
#append the feature class to the list of feature classes in the tile.
fctilelist.append(outfc_partition)
#Check if the partitioned piece of the feature class is empty. Only check if overlay operation is
#Union, Identity, or SymDif.
if toolname != "intersect_analysis" and toolname != "clip_analysis" and toolname != "erase_analysis":
#Check if the feature class in the tile has any features. If not, then create feature.
if toolname == "identity_analysis" and gp.getcount(fctilelist[0]) == 0:
print "input features are empty"
else:
CheckIfEmpty_and_CreateTempFeature(outfc_partition, t)
#*************************************
#INTERSECT TOOL
#<in_features> <out_feature_class> {NO_FID | ONLY_FID | ALL} {cluster_tolerance} {INPUT | LINE | POINT}
#*************************************
if toolname == "intersect_analysis":
try:
inputs = ""
for input in fctilelist:
inputs = inputs + ";" + input
#Creates an acceptable name for the temporary output fc
out_temp = CreateOutTempFCName(os.path.dirname(param_list[1]))
#Run the actual overlay operation on the partitioned feature classes
gp.AddMessage(msgIntersecting)
gp.intersect_analysis(inputs, out_temp, param_list[2], cluster_tolerance, param_list[4])
for input in fctilelist:
DeleteTempInput(input)
#Do a CreateFeatureClass, but only once. This is the fc to which we append.
if not gp.exists(param_list[1]):
CreatePermanentOutFC(tile_featureclass, fcs, param_list[1], out_temp)
#Append out_temp into specified output feature class
gp.AddMessage(msgAppending)
gp.append_management(out_temp, param_list[1])
DeleteTempInput(out_temp)
except:
errormsg = gp.getmessages(2)
if errormsg.find(msgUnexpectedTopoEngine) >= 0:
try:
#Run op again to clear topoengine cache in case the reason for failure was a topo engine error.
gp.AddMessage(msgClearCache)
gp.intersect_analysis(inputs, out_temp, param_list[2], cluster_tolerance, param_list[4])
except:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -