📄 partitionedoverlay.py
字号:
import win32com.client
import string, os, sys, fileinput, pywintypes, win32process, win32con, win32api, win32security, win32event, time, pythoncom
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
gp.overwriteoutput = 1
#Failure Messages
msgFailureDeleteTempFeature = "Failure in DeleteTempFeature function."
msgFailureCreateTempFeature = "Failure in CheckIfEmpty_and_CreateTempFeature function"
msgFailureCreateOutTempFCName = "Failure in CreateOutTempFCName fucntion."
msgFailureCreatePermanentOutFC = "Failure in CreatePermanentOutFC function"
msgFailureValidateClustTol = "Failure in ValidateClusterTolerance"
msgFailureCreateTempFolder = "Failure in CreateTempFolder"
msgFailureCreateTile = "Failure in CreateTile function"
msgFailureRunLargeOverlay = "Error in RunLargeOverlay function"
msgFailureInScratchWSExists = "Failure in ScratchWorkspaceExists Function"
msgFailureInQualifyTableNameSDE = "Failure in QualifyTableNameSDE Function"
msgFailureInCutItUp = "Failure in CutItUp Function"
msgUnexpectedTopoEngine = "Unexpected TopoEngine Error"
msgEmptyGeometry = "the operation was attempted on an empty geometry"
msgFailurePartitionFCcreation = "Failure Creating Partition Feature Class. Exiting ..."
msgErrorCutting = "Error cutting %s with tile %i"
msgFailureCleanUp = "Failure in CleanUpTempFiles Function"
msgErrorCreatingProcess = "Error in CreateProcess Function."
#Information Messages
msgDeleteFeatureField = "Deleting artificial feature..."
msgFeatureFieldNotFound = "Artificial feature not found. SQL is :%s"
msgZeroFeaturesInPartition = "Partitioned section of feature class contains ZERO features."
msgCreatingArtificialPolygon = "Creating artificial polygon feature..."
msgCreatingArtificialLine = "Creating artificial line feature..."
msgCreatingArtificialPoint = "Creating artificial point feature..."
msgContainsFeatures = "Partitioned section of feature class DOES contain features."
msgCreatingPartitions = "Creating Partitions..."
msgZeroSelected = "Zero features selected."
msgNumberPartitions = "Partition feature class has %i partitions."
msgCuttingPartitionNumber = "Cutting %s with record %i of %i"
msgClearCache = "Running operation again to clear topo engine cache"
msgAppending = "Appending... "
msgIntersecting = "Intersecting... "
msgClipping = "Clipping... "
msgIdentity = "Running Identity... "
msgUnioning = "Unioning... "
msgSymDiff = "Running Symmetrical Difference... "
msgErasing = "Running Erase... "
msgScratchWorkspaceEmpty = "Must set a scratch workspace when working with SDE."
msgScratchWorkspaceNotSde = "Scratch workspace must not be set to SDE."
msgScratchWorkspaceNotEqual = "Scratch workspace can not be the same as the input workspace."
msgSettingClusterTolerance = "Error settting Cluster Tolerance."
msgInserting = "Inserting temporary feature"
msgSmallFalseFeature = "false feature width less than 20x cluster tol.\nPossible that feature will become invalid."
#Global Variables
location_of_testGPRAM = ""
location_of_testGPRAM_NOSPACES = ""
shape_type = ""
empty_fc = []
cluster_tolerance_dict = {}
cluster_tolerance = -1
def _CreateProcess(app, args):
"""Returns the pid.
app = executable name
args = list of arguments
filein = name of file to redirect to stdin
fileout = name of file to redirect the stdout to
"""
try:
SA_proc = win32security.SECURITY_ATTRIBUTES()
SA_thrd = win32security.SECURITY_ATTRIBUTES()
SA_proc.bInheritHandle = 1
SA_thrd.bInheritHandle = 1
#SA = None
SI = win32process.GetStartupInfo()
SI.dwFlags = win32process.STARTF_USESTDHANDLES
SI.hStdInput = SI.hStdInput
SI.hStdOutput = SI.hStdOutput
SI.hStdError = SI.hStdOutput
cmdline = app + " " + args
hProcess, hThread, pid, threadid = \
win32process.CreateProcess(
app, # lpApplicationName
cmdline, # command line
SA_proc, # Process security attributes
SA_thrd, # Thread security attributes
1, # Inherit handles
win32process.DETACHED_PROCESS, #
None,
None,
SI)
win32event.WaitForSingleObject(hProcess, win32event.INFINITE)
return pid
except pythoncom.com_error, (hr, msg, exc, arg):
if exc is None:
ErrorDesc = "testGPRam.exe error %d: %s" % (hr, msg)
else:
wcode, source, text, helpFile, helpId, scode = exc
ErrorDesc = "testGPRam.exe error %d: %s\n(%s) %s" % (hr, msg, source, text)
raise Exception, ErrorDesc
except:
raise Exception, msgErrorCreatingProcess
def DeleteTempFeature(out_temp):
#Function that deletes temporary feature
try:
#Look through the list of empty feature classes
for name_efc in empty_fc:
#Create a string that represents the expected name of the FID field created in the output of the
#overlay function. Then validate this name against the workspace.
empty_fld_name = "FID_" + name_efc
empty_fld_name = gp.validatefieldname(empty_fld_name, os.path.dirname(out_temp))
bu = 0
namecounter = 0
while bu == 0:
namecounter = namecounter + 1
if namecounter == 10:
bu = 1
#See if the postulated field name exists. If it does, query it to find out if it has one feature
#that is different.
if gp.listfields(out_temp, empty_fld_name).next():
if out_temp.find(".mdb") > 0:
mySQL = '[' + empty_fld_name + ']' + ' <> -1'
mySQL2 = '[' + empty_fld_name + ']' + ' = -1'
else:
mySQL = empty_fld_name + ' <> -1'
mySQL2 = empty_fld_name + ' = -1'
gp.makefeaturelayer(out_temp, "mytemplyr", mySQL)
gp.makefeaturelayer(out_temp, "mytemplyr2", mySQL2)
if gp.getcount("mytemplyr") == 1 and gp.getcount("mytemplyr2") > 0:
gp.AddMessage(msgDeleteFeatureField)
gp.deletefeatures("mytemplyr")
bu = 1
else:
#possibly add a check to make sure the workspace is a shapefile.
#Check length, try to create a string that will match the FID if the first did not.
if len(empty_fld_name) == 10 and out_temp.find(".mdb") == -1:
empty_fld_name = empty_fld_name[:-1] + str(namecounter)
empty_fld_name = gp.validatefieldname(empty_fld_name, os.path.dirname(out_temp))
else:
empty_fld_name = empty_fld_name + str(namecounter)
empty_fld_name = gp.validatefieldname(empty_fld_name, os.path.dirname(out_temp))
else:
gp.AddMessage(msgFeatureFieldNotFound % (mySQL))
bu = 1
except:
ErStr = msgFailureDeleteTempFeature + "\n" + str(gp.getmessages(2))
raise Exception, ErStr
def CheckIfEmpty_and_CreateTempFeature(tempFC_in_Tile, in_tile):
#This function checks if the partitioned feature class is empty and creates a temporary feature just outside the
#extent of the tile if it is.
#For overlay functions like Union and Identity when all features and attributes from all feature classes are carried
#through to the output it is necessary that all partitions have at least one feature. If they do not, then the append
#will not work correctly... e.g. not all attributes will there or the attributes will match up incorrectly.
try:
global empty_fc
#Check if the partitioned FC is empty
if gp.getcount(tempFC_in_Tile) == 0:
gp.AddMessage(msgZeroFeaturesInPartition)
#If it is empty, record the name of the FC and store it for later use in the DeleteTempFeature function
name_of_temp = os.path.basename(tempFC_in_Tile)
if name_of_temp.find(".shp") > 0:
name_of_temp = name_of_temp.replace(".shp", "")
empty_fc.append(name_of_temp)
#Create an insert cursor.
cur = gp.insertcursor(tempFC_in_Tile)
#Get the extent of the partition. Need to put the temporary feature just outside its extent so that it
#does not affect the features in the analysis, but causes the attributes to be added to the output.
dsc = gp.describe(in_tile)
xmin, ymin, xmax, ymax = string.split(dsc.extent, " ")
xdif = abs(float(xmax) - float(xmin))
ydif = abs(float(ymax) - float(ymin))
if xdif * 0.01 < cluster_tolerance_dict[name_of_temp] * 20:
gp.AddMessage(msgSmallFalseFeature)
#Find out what type of feature needs to be added and add it.
if shape_type == "POLYGON":
gp.AddMessage(msgCreatingArtificialPolygon)
PolyArray = gp.CreateObject("Array")
PartArray = gp.CreateObject("Array")
pnt = gp.CreateObject("Point")
pnt.x = float(xmax) + (xdif * .01)
pnt.y = float(ymax) + (ydif * .01)
PolyArray.add(pnt)
pnt.x = float(xmax) + (xdif * .01)
pnt.y = float(ymax) + (ydif * .02)
PolyArray.add(pnt)
pnt.x = float(xmax) + (xdif * .02)
pnt.y = float(ymax) + (ydif * .02)
PolyArray.add(pnt)
pnt.x = float(xmax) + (xdif * .02)
pnt.y = float(ymax) + (ydif * .01)
PolyArray.add(pnt)
pnt.x = float(xmax) + (xdif * .01)
pnt.y = float(ymax) + (ydif * .01)
PolyArray.add(pnt)
PartArray.add(PolyArray)
feat = cur.NewRow()
feat.shape = PartArray
gp.AddMessage(msgInserting)
cur.InsertRow(feat)
del cur
del pnt
del PolyArray
del PartArray
elif shape_type == "POLYLINE":
gp.AddMessage(msgCreatingArtificialLine)
LineArray = gp.CreateObject("Array")
PartArray = gp.CreateObject("Array")
pnt = gp.CreateObject("Point")
pnt.x = float(xmax) + (xdif * .01)
pnt.y = float(ymax) + (ydif * .01)
LineArray.add(pnt)
pnt.x = float(xmax) + (xdif * .02)
pnt.y = float(ymax) + (ydif * .02)
LineArray.add(pnt)
PartArray.add(LineArray)
feat = cur.NewRow()
feat.shape = PartArray
cur.InsertRow(feat)
del cur
del pnt
del LineArray
del PartArray
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -