Solved! Go to Solution.
import arcpy import os import re import datetime #-------------------PARAMETERS-------------------------------------------------- textfilePath = arcpy.GetParameterAsText(0) # Path to text file with parcel descriptions. outputFolder = arcpy.GetParameterAsText(1) # Out folder path exportdataCheckbox = arcpy.GetParameterAsText(2) # Checkbox to export data to a geodatabase. #------------------------------------------------------------------------------- def ReadTextFile(path): textFile = open(path, "r") lines = textFile.readlines() textFile.close return lines def ExportKMZ(lotplan): try: # Output file name. If a file of the same name exists then delete it # and create a new file outKMZ = os.path.join(outputFolder,lotplan, "lot" + lotplan + "_" + today + ".kmz") if arcpy.Exists(outKMZ): os.remove(outKMZ) # Get layer extent rectangle and remove superfluos information. The # remaining coordinates should be in format LL UR extent = subjectLayer.getSelectedExtent(False) cleanextent = re.sub('[A-Za-z]', '', str(extent)) # Create KMZ arcpy.MapToKML_conversion(mxd.filePath,"KMZExportMap", outKMZ,"0","NO_COMPOSITE","VECTOR_TO_IMAGE",cleanextent,"1024","96","CLAMPED_TO_GROUND") arcpy.AddMessage("KMZ exported: " + outKMZ) except Exception, e: arcpy.AddError(e.message) print e.message def CreateOutputFolder(cleanlotplan): # Create a project folder if needed global outFolder outFolder = os.path.join(outputFolder,cleanlotplan) if not os.path.exists(outFolder): os.makedirs(outFolder) def ExportSubject(cleanlotplan): try: # Create a project geodatabase if needed if not arcpy.Exists(os.path.join(outFolder, "Data_" + today +".gdb")): arcpy.CreateFileGDB_management(outFolder, "Data_" + today +".gdb") # Create a subject layer in geodatabase global outGDB # Create object as 'global' as it will be used 'ron outGDB = os.path.join(outFolder, "Data_" + today +".gdb") subjectlayerName = os.path.join(outGDB,"SubjectLot" + cleanlotplan + "_" + today) if arcpy.Exists(subjectlayerName): arcpy.Delete_management(subjectlayerName) # Export subject features arcpy.CopyFeatures_management(subjectLayer, subjectlayerName ) arcpy.AddMessage("Subject parcels exported to: " + subjectlayerName) del subjectlayerName except Exception, e: arcpy.AddError(e.message) print e.message def ExportDataToGeodatabase(cleanlotplan): try: # Get extent from 'extent_layer' object desc = arcpy.Describe(subjectLayer) extent = desc.extent arcpy.env.workspace = outGDB arcpy.AddMessage("Old Exten: " + str(arcpy.env.extent)) arcpy.env.extent = extent arcpy.AddMessage("Old Exten: " + str(arcpy.env.extent)) # Iterate layers and export to GDB. Check to see if layer is a # featurelayer and is not missing datasources. layers = arcpy.mapping.ListLayers(mxd) for layer in layers: if "Data Group" in layer.longName and layer.isFeatureLayer == True: outlayerPath = os.path.join(outGDB,layer.datasetName + "_" + today) arcpy.CopyFeatures_management(layer, outlayerPath) arcpy.AddMessage("Layer (" + layer.name + ") exported to: " + outlayerPath) # Refresh view and TOC arcpy.RefreshActiveView arcpy.RefreshTOC except Exception, e: arcpy.AddError(e.message) finally: # Reset geoprocessing environment settings arcpy.ResetEnvironments() arcpy.AddMessage("Subject parcel list: " + textfilePath) arcpy.AddMessage("Output location: " + outputFolder) # Get parcel description from .txt file lotplanList = ReadTextFile(textfilePath) # Get MXD, etc. mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "KMZExportMap") [0] subjectLayer = arcpy.mapping.ListLayers(mxd,"Subject Lot Plan", df)[0] # global variables outFolder = None outGDB = None failedexportList = [] # Get date today = re.sub('[-]',"",str(datetime.date.today())) # Iterate items in list for lotplan in lotplanList: try: # Remove whitespace and capitalise lotplan cleanlotplan = lotplan.strip().upper() arcpy.AddMessage("Processing lot description: " + cleanlotplan) whereClause = """"LOTPLAN" = '""" + cleanlotplan + "'" subjectLayer.definitionQuery = whereClause # Save mxd so the definition query is picked up in the KMZ export mxd.save() # Use feature count to determine if lot/plan was found. count = int(arcpy.GetCount_management(subjectLayer).getOutput(0)) # Conditional statement to determine if a feature(s) within the subject # layer match the lotplan used in definition query. If count is 0 then # not matching feature were found - move to next lot/plan. If count > 0 # then the parcel has been found. Call geoprocessing and export routines # using the parcels details to create the outputs. if count > 0: arcpy.AddMessage("Found parcel: " + cleanlotplan) # Create main output folder for lot description CreateOutputFolder(cleanlotplan) # If selected export ESRI data if str(exportdataCheckbox) == "true": ExportSubject(cleanlotplan) ExportDataToGeodatabase(cleanlotplan) ExportKMZ(cleanlotplan) else: arcpy.AddWarning("Could not find parcel: " + cleanlotplan) failedexportList.append(lotplan) except Exception, e: arcpy.AddError(e.message) finally: # Always remove deinition query before processing the next lotplan subjectLayer.definitionQuery = None # Create text file to store unfound parcel descriptions. txtfilePath = os.path.join(outputFolder,"UnfoundLots_" + today + ".txt") txtFile = open(txtfilePath, "a") if len(failedexportList) > 0: for lot in failedexportList: txtFile.write(lot) txtFile.flush() txtFile.close() del txtFile del failedexportList del outFolder del outGDB del subjectLayer del mxd del lotplanList
#-------------------PARAMETERS-------------------------------------------------- textfilePath = r"C:\_TEMP_SSD_PERFORMANCE\ToKML Test\SLATS1011_LotPlans2.txt" mxdPath = r"C:\_TEMP_SSD_PERFORMANCE\ToKML Test\ExportToKMZ.mxd" outputFolder = r"C:\_TEMP_SSD_PERFORMANCE\ToKML Test\Outputs" #------------------------------------------------------------------------------- import arcpy import os import re def ReadTextFile(path): textFile = open(path, "r") lines = textFile.readlines() textFile.close return lines def Extract(lotplan, extent): try: outKMZ = os.path.join(outputFolder, lotplan + ".kmz") if arcpy.Exists(outKMZ): os.remove(outKMZ) cleanextent = re.sub('[A-Za-z]', '', str(extent)) arcpy.MapToKML_conversion(mxdPath,"KMZExportMap", outKMZ,"0","NO_COMPOSITE","VECTOR_TO_IMAGE",cleanextent,"1024","96","CLAMPED_TO_GROUND") except Exception, e: print e.message # Get parcel description from .txt file lotplanList = ReadTextFile(textfilePath) # Get MXD, etc. mxd = arcpy.mapping.MapDocument(mxdPath) df = arcpy.mapping.ListDataFrames(mxd, "KMZExportMap") [0] subjectLayer = arcpy.mapping.ListLayers(mxd,"Subject Area", df)[0] # Iterate parcels, apply definition query and export for lotplan in lotplanList: cleanlotplan = lotplan.strip() whereClause = """"LOTPLAN" = '""" + cleanlotplan + "'" subjectLayer.definitionQuery = whereClause extent = subjectLayer.getSelectedExtent(False) print cleanlotplan Extract(cleanlotplan,extent) subjectLayer.definitionQuery = None del subjectLayer del mxd del lotplanList
#-------------------PARAMETERS-------------------------------------------------- textfilePath = r"C:\_TEMP_SSD_PERFORMANCE\ToKML Test\SLATS1011_LotPlans2.txt" mxdPath = r"C:\_TEMP_SSD_PERFORMANCE\ToKML Test\ExportToKMZ.mxd" outputFolder = r"C:\_TEMP_SSD_PERFORMANCE\ToKML Test\Outputs" #------------------------------------------------------------------------------- import arcpy import os import re def ReadTextFile(path): textFile = open(path, "r") lines = textFile.readlines() textFile.close return lines def Extract(lotplan, extent, tempMXD): try: outKMZ = os.path.join(outputFolder, lotplan + ".kmz") if arcpy.Exists(outKMZ): os.remove(outKMZ) cleanextent = re.sub('[A-Za-z]', '', str(extent)) arcpy.MapToKML_conversion(tempMXD,"KMZExportMap", outKMZ,"0","NO_COMPOSITE","VECTOR_TO_IMAGE",cleanextent,"1024","96","CLAMPED_TO_GROUND") except Exception, e: print e.message # Get parcel description from .txt file lotplanList = ReadTextFile(textfilePath) # Get MXD, etc. mxd = arcpy.mapping.MapDocument(mxdPath) df = arcpy.mapping.ListDataFrames(mxd, "KMZExportMap") [0] subjectLayer = arcpy.mapping.ListLayers(mxd,"Subject Area", df)[0] # Iterate parcels, apply definition query and export for lotplan in lotplanList: cleanlotplan = lotplan.strip() whereClause = """"LOTPLAN" = '""" + cleanlotplan + "'" subjectLayer.definitionQuery = whereClause extent = subjectLayer.getSelectedExtent(False) print cleanlotplan #save a copy of the mxd with the updated def query tempMXD = os.path.join(arcpy.env.scratchFolder, "tempmxd.mxd") mxd.saveACopy(tempMXD) Extract(cleanlotplan,extent, tempMXD) #delete that temp mxd os.remove(tempMXD) subjectLayer.definitionQuery = None del subjectLayer del mxd del lotplanList
extent = subjectLayer.getSelectedExtent(False)I think this is just the way the tool works and have now resolved to finding a workaround. The options I am considering are exporting the parcel to a new temporary layer and running MapToKML or attempting to use a uniquevaluerenderer and only add the value for the subject parcel - I am not having any luck with the latter at the moment. When I have a suitable workaround I will post but in the mean time any help is greatly appreciated.
import arcpy import os import re import datetime #-------------------PARAMETERS-------------------------------------------------- textfilePath = arcpy.GetParameterAsText(0) # Path to text file with parcel descriptions. outputFolder = arcpy.GetParameterAsText(1) # Out folder path exportdataCheckbox = arcpy.GetParameterAsText(2) # Checkbox to export data to a geodatabase. #------------------------------------------------------------------------------- def ReadTextFile(path): textFile = open(path, "r") lines = textFile.readlines() textFile.close return lines def ExportKMZ(lotplan): try: # Output file name. If a file of the same name exists then delete it # and create a new file outKMZ = os.path.join(outputFolder,lotplan, "lot" + lotplan + "_" + today + ".kmz") if arcpy.Exists(outKMZ): os.remove(outKMZ) # Get layer extent rectangle and remove superfluos information. The # remaining coordinates should be in format LL UR extent = subjectLayer.getSelectedExtent(False) cleanextent = re.sub('[A-Za-z]', '', str(extent)) # Create KMZ arcpy.MapToKML_conversion(mxd.filePath,"KMZExportMap", outKMZ,"0","NO_COMPOSITE","VECTOR_TO_IMAGE",cleanextent,"1024","96","CLAMPED_TO_GROUND") arcpy.AddMessage("KMZ exported: " + outKMZ) except Exception, e: arcpy.AddError(e.message) print e.message def CreateOutputFolder(cleanlotplan): # Create a project folder if needed global outFolder outFolder = os.path.join(outputFolder,cleanlotplan) if not os.path.exists(outFolder): os.makedirs(outFolder) def ExportSubject(cleanlotplan): try: # Create a project geodatabase if needed if not arcpy.Exists(os.path.join(outFolder, "Data_" + today +".gdb")): arcpy.CreateFileGDB_management(outFolder, "Data_" + today +".gdb") # Create a subject layer in geodatabase global outGDB # Create object as 'global' as it will be used 'ron outGDB = os.path.join(outFolder, "Data_" + today +".gdb") subjectlayerName = os.path.join(outGDB,"SubjectLot" + cleanlotplan + "_" + today) if arcpy.Exists(subjectlayerName): arcpy.Delete_management(subjectlayerName) # Export subject features arcpy.CopyFeatures_management(subjectLayer, subjectlayerName ) arcpy.AddMessage("Subject parcels exported to: " + subjectlayerName) del subjectlayerName except Exception, e: arcpy.AddError(e.message) print e.message def ExportDataToGeodatabase(cleanlotplan): try: # Get extent from 'extent_layer' object desc = arcpy.Describe(subjectLayer) extent = desc.extent arcpy.env.workspace = outGDB arcpy.AddMessage("Old Exten: " + str(arcpy.env.extent)) arcpy.env.extent = extent arcpy.AddMessage("Old Exten: " + str(arcpy.env.extent)) # Iterate layers and export to GDB. Check to see if layer is a # featurelayer and is not missing datasources. layers = arcpy.mapping.ListLayers(mxd) for layer in layers: if "Data Group" in layer.longName and layer.isFeatureLayer == True: outlayerPath = os.path.join(outGDB,layer.datasetName + "_" + today) arcpy.CopyFeatures_management(layer, outlayerPath) arcpy.AddMessage("Layer (" + layer.name + ") exported to: " + outlayerPath) # Refresh view and TOC arcpy.RefreshActiveView arcpy.RefreshTOC except Exception, e: arcpy.AddError(e.message) finally: # Reset geoprocessing environment settings arcpy.ResetEnvironments() arcpy.AddMessage("Subject parcel list: " + textfilePath) arcpy.AddMessage("Output location: " + outputFolder) # Get parcel description from .txt file lotplanList = ReadTextFile(textfilePath) # Get MXD, etc. mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "KMZExportMap") [0] subjectLayer = arcpy.mapping.ListLayers(mxd,"Subject Lot Plan", df)[0] # global variables outFolder = None outGDB = None failedexportList = [] # Get date today = re.sub('[-]',"",str(datetime.date.today())) # Iterate items in list for lotplan in lotplanList: try: # Remove whitespace and capitalise lotplan cleanlotplan = lotplan.strip().upper() arcpy.AddMessage("Processing lot description: " + cleanlotplan) whereClause = """"LOTPLAN" = '""" + cleanlotplan + "'" subjectLayer.definitionQuery = whereClause # Save mxd so the definition query is picked up in the KMZ export mxd.save() # Use feature count to determine if lot/plan was found. count = int(arcpy.GetCount_management(subjectLayer).getOutput(0)) # Conditional statement to determine if a feature(s) within the subject # layer match the lotplan used in definition query. If count is 0 then # not matching feature were found - move to next lot/plan. If count > 0 # then the parcel has been found. Call geoprocessing and export routines # using the parcels details to create the outputs. if count > 0: arcpy.AddMessage("Found parcel: " + cleanlotplan) # Create main output folder for lot description CreateOutputFolder(cleanlotplan) # If selected export ESRI data if str(exportdataCheckbox) == "true": ExportSubject(cleanlotplan) ExportDataToGeodatabase(cleanlotplan) ExportKMZ(cleanlotplan) else: arcpy.AddWarning("Could not find parcel: " + cleanlotplan) failedexportList.append(lotplan) except Exception, e: arcpy.AddError(e.message) finally: # Always remove deinition query before processing the next lotplan subjectLayer.definitionQuery = None # Create text file to store unfound parcel descriptions. txtfilePath = os.path.join(outputFolder,"UnfoundLots_" + today + ".txt") txtFile = open(txtfilePath, "a") if len(failedexportList) > 0: for lot in failedexportList: txtFile.write(lot) txtFile.flush() txtFile.close() del txtFile del failedexportList del outFolder del outGDB del subjectLayer del mxd del lotplanList