📄 partitionedoverlay.py
字号:
else:
gp.AddMessage(msgCreatingArtificialPoint)
pnt = gp.CreateObject("Point")
PntArray = gp.CreateObject("Array")
pnt.x = float(xmax) + (xdif * .01)
pnt.y = float(ymax) + (ydif * .01)
feat = cur.NewRow()
feat.shape = PntArray
cur.InsertRow(feat)
del cur
del pnt
del PntArray
else:
gp.AddMessage(msgContainsFeatures)
except:
ErStr = msgFailureCreateTempFeature + "\n" + str(gp.getmessages(2))
raise Exception, ErStr
def CreateOutTempFCName(output_fc_dir):
#Create temp FC name for the overlay operation output for the current partition based on the
#type of output specified by the user.
try:
if output_fc_dir[-4:].lower() == ".mdb":
out_tempgdb = os.path.dirname(output_fc_dir) + "\\xxxtemp.mdb"
if gp.exists(out_tempgdb):
out_temp = out_tempgdb + "\\xxx"
else:
out_tempgdb = gp.createpersonalgdb(os.path.dirname(output_fc_dir), "xxxtemp.mdb")
out_temp = out_tempgdb + "\\xxx"
elif output_fc_dir.find(".mdb") > 0:
out_tempgdb = os.path.dirname(os.path.dirname(output_fc_dir)) + "\\xxxtemp.mdb"
if gp.exists(out_tempgdb):
out_temp = out_tempgdb + "\\xxx"
else:
out_tempgdb = gp.createpersonalgdb(os.path.dirname(output_fc_dir), "xxxtemp.mdb")
out_temp = out_tempgdb + "\\xxx"
elif output_fc_dir.find(".sde") > 0:
if gp.scratchworkspace.find(".mdb") > 0:
out_tempgdb = gp.scratchworkspace
else:
out_tempgdb = gp.scratchworkspace + "\\xxxtemp.mdb"
if gp.exists(out_tempgdb):
out_temp = out_tempgdb + "\\xxx"
else:
out_tempgdb = gp.createpersonalgdb(gp.scratchworkspace, "xxxtemp.mdb")
out_temp = out_tempgdb + "\\xxx"
else:
out_temp = output_fc_dir + "\\xxx.shp"
return out_temp
except:
ErStr = msgFailureCreateOutTempFCName + "\n" + str(gp.getmessages(2))
raise Exception, ErStr
def CreatePermanentOutFC(tile_fc, all_fcs, perm_output_fc, temp_output_fc):
#Create the permanent output FC to which all the partitions will be appended
try:
#Create Spatial Reference object
dsc = gp.describe(tile_fc)
sr = dsc.spatialreference
gp.createspatialreference(sr, "", "", "", "", all_fcs)
source = os.path.dirname(perm_output_fc)
basename = os.path.basename(perm_output_fc)
if basename.find(".shp") > 0:
basename_list = basename.split(".")
basename = basename_list[0]
if basename.find(".") > 0:
basename_list = basename.split(".")
basename = basename_list[-1]
gp.createfeatureclass(source, basename, shape_type, temp_output_fc, "", "", sr)
gp.outputCoordinateSystem = sr
gp.xydomain = sr.domain
except:
ErStr = msgFailureCreatePermanentOutFC + "\n" + str(gp.GetMessages(2))
raise Exception, ErStr
def CreateTempFolder(param_list, param_number):
#Creates a folder to store the temporary data created.
try:
if gp.scratchworkspace == "":
p = os.path.dirname(param_list[param_number])
if p[-4:].lower() == ".mdb":
p = os.path.dirname(p)
elif p.find(".mdb") > 0:
p = os.path.dirname((os.path.dirname(p)))
sw = gp.createfolder(p, "xxtiles")
gp.scratchworkspace = sw
else:
sw = gp.scratchworkspace
dsc = gp.describe(gp.scratchworkspace)
if dsc.workspacetype == "LocalDatabase":
tilefc = sw + "\\tilex"
else:
tilefc = sw + "\\tilex.shp"
return tilefc
except:
ErStr = msgFailureCreateTempFolder + "\n" + str(gp.GetMessages(2))
raise Exception, ErStr
def CreateTile(tile_featureclass, tileid):
#Creates a shapefile representing one tile.
try:
tilefolder = os.path.dirname(tile_featureclass)
lyr = "tilelyr"
dsc = gp.describe(tile_featureclass)
if dsc.datatype == "ShapeFile":
gp.makefeaturelayer(tile_featureclass, lyr, "FID = %s" %(tileid))
if gp.getcount(lyr) == 1:
shpname = tilefolder + "\\" + lyr + ".shp"
tile = gp.CopyFeatures(lyr, shpname)
else:
raise Exception, msgZeroSelected
elif dsc.datatype == "CoverageFeatureClass":
gp.makefeaturelayer(tile_featureclass, lyr, "[OBJECTID] = %s" %(tileid))
if gp.getcount(lyr) == 1:
dir = os.path.dirname(tile_featureclass)
cov = os.path.basename(dir)
fcname = tilefolder + "\\" + cov
tile = gp.CopyFeatures(lyr, fcname)
else:
gp.makefeaturelayer(tile_featureclass, lyr, "[OBJECTID] = %s" %(tileid))
if gp.getcount(lyr) == 1:
fcname = tilefolder + "\\" + lyr
tile = gp.CopyFeatures(lyr, fcname)
else:
raise Exception, msgZeroSelected
return tile
except Exception, ErStr:
ErStr = msgFailureCreateTile + "\n" + str(ErStr)
raise Exception, ErStr
except:
ErStr = msgFailureCreateTile + "\n" + str(gp.GetMessages(2))
raise Exception, ErStr
def DeleteTempInput(tempinput):
try:
if str(tempinput).find(".mdb") > 0:
gp.delete(os.path.dirname(tempinput))
else:
gp.delete(tempinput)
except:
gp.delete(tempinput)
pass
def SetClustTol(toolname, featureclasses, inputClustTol):
try:
global cluster_tolerance_dict
global cluster_tolerance
global location_of_testGPRAM
global location_of_testGPRAM_NOSPACES
#si = win32process.STARTUPINFO()
#Check if the location of TestGPRam.exe contains spaces
if os.path.dirname(sys.argv[0]).find(" ") > 0:
location_of_testGPRAM = '"' + os.path.dirname(sys.argv[0]) + "\\testGPRAM.exe" + '"'
else:
location_of_testGPRAM_NOSPACES = os.path.dirname(sys.argv[0]) + "\\testGPRAM.exe"
if inputClustTol == "#":
inputClustTol = ""
#Check for spaces in the input parameters
if featureclasses.find(" ") > 0:
testgpram_params = '"' + featureclasses + '"' + " LCT " + '"' + win32api.GetEnvironmentVariable("TEMP") + '\\xxxLCTfile.txt" ' + inputClustTol
testgpram_params = testgpram_params.replace("'", "")
else:
testgpram_params = featureclasses + " LCT " + win32api.GetEnvironmentVariable("TEMP") + "\\xxxLCTfile.txt " + inputClustTol
#Run TestGPRam to Set the Cluster Tolerance
if not location_of_testGPRAM == "":
_CreateProcess(location_of_testGPRAM, testgpram_params)
else:
_CreateProcess(location_of_testGPRAM_NOSPACES, testgpram_params)
#Get the temp file containing the cluster tolerance value
infile = win32api.GetEnvironmentVariable("TEMP") + "\\xxxLCTfile.txt"
fcs = featureclasses.split(";")
count = -1
#Open the file
for line in fileinput.input(infile):
if count == -1:
cluster_tolerance = line
else:
if fcs[count].find(".sde") > 0:
name_of_temp = fcs[count].split(".")[-1]
if name_of_temp[-1] == "'" or name_of_temp[-1] == '"':
name_of_temp = name_of_temp[:-1]
elif os.path.basename(fcs[count]).find(".shp") > 0:
name_of_temp = os.path.basename(fcs[count]).replace(".shp", "")
else:
name_of_temp = os.path.basename(fcs[count])
fcs[count] = fcs[count].replace("'","")
dsc = gp.describe(fcs[count])
if dsc.datatype == "CoverageFeatureClass":
dir = os.path.dirname(fcs[count])
name_of_temp = os.path.basename(dir)
cluster_tolerance_dict[name_of_temp] = float(line)
count = count + 1
for x in cluster_tolerance_dict.keys():
gp.AddMessage("Key: " + str(x) + " Value: " + str(cluster_tolerance_dict[x]))
except Exception, ErStr:
raise Exception, ErStr
except:
ErStr = msgSettingClusterTolerance + "\n" + str(gp.GetMessages(2))
raise Exception, ErStr
def RunLargeOverlay(tool_name, parameter_list):
try:
#Checks what tool is being run and creates an execution string for use with testGPRAM.exe.
#testGPRAM.exe creates the partition shapefile. Finally the "CutItUp" function is called.
gp.AddMessage(msgCreatingPartitions)
#Check ToolName and run testGPRAM.exe with appropriate parameters.
#Check ToolName and run testGPRAM.exe with appropriate parameters.
if tool_name == "intersect_analysis":
tilefc = CreateTempFolder(parameter_list, 1)
if parameter_list[0].find(" ") > 0:
testgpram_params = '"' + parameter_list[0] + '"' + ' tile "' + tilefc + '" min'
testgpram_params = testgpram_params.replace("'", "")
else:
testgpram_params = parameter_list[0] + " 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)))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -