Here's a little function to remove all joins. Also see (and vote up!) Remove all joins programatically
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">RemoveAllJoins</SPAN><SPAN class="punctuation token">(</SPAN>tbl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""Remove all joins from a layer or table view"""</SPAN> flds <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>f<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>tbl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> wk <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>dirname<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>tbl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>catalogPath<SPAN class="punctuation token">)</SPAN> joins <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>ParseFieldName<SPAN class="punctuation token">(</SPAN>f<SPAN class="punctuation token">,</SPAN> wk<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">", "</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> f <SPAN class="keyword token">in</SPAN> flds<SPAN class="punctuation token">]</SPAN> joins <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>set<SPAN class="punctuation token">(</SPAN>joins<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># unique the list</SPAN> <SPAN class="keyword token">if</SPAN> joins<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">"(null)"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"no join active"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># remove base table name from list and remove all joins</SPAN> joins<SPAN class="punctuation token">.</SPAN>remove<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>tbl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>baseName<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> j <SPAN class="keyword token">in</SPAN> joins<SPAN class="punctuation token">:</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RemoveJoin_management<SPAN class="punctuation token">(</SPAN>tbl<SPAN class="punctuation token">,</SPAN> j<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Removed join {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>j<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
# --------------------------------------------------------------------------- # RemoveJoins.py # --------------------------------------------------------------------------- import arcpy,sys from arcpy.mapping import * ## ======================================================================= def RemoveAllJoins(theInputTable): ## Remove all joined tables. try: arcpy.AddMessage("Remove All Joins from " + theInputTable.name) theJoinList = [] theDesc = arcpy.Describe(theInputTable) if theDesc.DataType.upper() == "FEATURELAYER": # If it's a shapefile, remove the extension from the name. theFC = theDesc.Featureclass.Name.replace(".shp","") else: arcpy.AddMessage("\n ERROR: " + theInputTable + " is not a feature layer!\n") return False # Look at the fields for this layer. A full field name includes # the table name, i.e., TABLE.FIELD, where TABLE matches the name # of the feature class. However, if there is a Join on the table, # the joined field names will be like JOINTABLE.FIELD, so we can # look for those other table names which represent Joins. theFields = theDesc.Fields for theField in theFields: FullName = theField.Name # Get rid of spaces in field name components and split them into a list. nameList = arcpy.ParseFieldName(FullName).replace(" ","").split(",") theTableName = nameList[2] if theTableName != theFC and not theTableName in theJoinList: # Keep track of Joins that were removed; remove only once. theJoinList.append(theTableName) arcpy.AddMessage(" Removing join: " + theTableName) arcpy.RemoveJoin_management(theInputTable, theTableName) return True except Exception as e: #arcpy.AddError(e.message) return False mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] for item in df: RemoveAllJoins(item)
## ======================================================================= def RemoveAllJoins(theInputTable): ## Remove all joined tables. try: arcpy.AddMessage("Remove All Joins from " + theInputTable) theJoinList = [] theDesc = arcpy.Describe(theInputTable) if theDesc.DataType.upper() == "FEATURELAYER": # If it's a shapefile, remove the extension from the name. theFC = theDesc.Featureclass.Name.replace(".shp","") else: arcpy.AddMessage("\n ERROR: " + theInputTable + " is not a feature layer!\n") return False # Look at the fields for this layer. A full field name includes # the table name, i.e., TABLE.FIELD, where TABLE matches the name # of the feature class. However, if there is a Join on the table, # the joined field names will be like JOINTABLE.FIELD, so we can # look for those other table names which represent Joins. theFields = theDesc.Fields for theField in theFields: FullName = theField.Name # Get rid of spaces in field name components and split them into a list. nameList = arcpy.ParseFieldName(FullName).replace(" ","").split(",") theTableName = nameList[2] if theTableName != theFC and not theTableName in theJoinList: # Keep track of Joins that were removed; remove only once. theJoinList.append(theTableName) arcpy.AddMessage(" Removing join: " + theTableName) arcpy.RemoveJoin_management(theInputTable, theTableName) return True except Exception as e: arcpy.AddError(e.message) return False
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.