def timed(*args, **kwargs): ts = time.time() result = func(*args, **kwargs) te = time.time() status_writer = status.Writer() status_writer.send_status( 'func:{} args:[{}, {}] took: {:.2f} sec'.format( func.__name__, args, kwargs, te - ts)) return result
def convert_to_kml(geodatabase): """Convert the contents of a geodatabase to KML. :param geodatabase: path to a geodatabase """ import arcpy status_writer = status.Writer() arcpy.env.workspace = geodatabase arcpy.env.overwriteOutput = True feature_classes = arcpy.ListFeatureClasses() count = len(feature_classes) for i, fc in enumerate(feature_classes, 1): arcpy.MakeFeatureLayer_management(fc, "temp_layer") arcpy.LayerToKML_conversion( "temp_layer", '{0}.kmz'.format(os.path.join(os.path.dirname(geodatabase), fc)), 1) status_writer.send_percent( float(i) / count, _('Converted: {0}').format(fc), 'convert_to_kml') arcpy.Delete_management("temp_layer")
def clip_mxd_layers(mxd_path, aoi, workspace, map_frame=None): """Clips each layer in the map document to output workspace and re-sources each layer and saves a copy of the mxd. :param mxd_path: path to the input map document :param aoi: area of interest as a polygon object or feature class :param workspace: output workspace :param map_frame: name of the mxd's map frame/data frame """ import arcpy arcpy.env.workspace = workspace status_writer = status.Writer() mxd = arcpy.mapping.MapDocument(mxd_path) layers = arcpy.mapping.ListLayers(mxd) if map_frame: df = arcpy.mapping.ListDataFrames(mxd, map_frame)[0] df_layers = arcpy.mapping.ListLayers(mxd, data_frame=df) [[ arcpy.mapping.RemoveLayer(d, l) for l in arcpy.mapping.ListLayers(mxd) ] for d in arcpy.mapping.ListDataFrames(mxd)] [arcpy.mapping.AddLayer(df, l) for l in df_layers] layers = df_layers for layer in layers: try: out_name = arcpy.CreateUniqueName(layer.datasetName, arcpy.env.workspace) if layer.isFeatureLayer: arcpy.Clip_analysis(layer.dataSource, aoi, out_name) if arcpy.env.workspace.endswith('.gdb'): layer.replaceDataSource(arcpy.env.workspace, 'FILEGDB_WORKSPACE', out_name, False) else: layer.replaceDataSource(arcpy.env.workspace, 'SHAPEFILE_WORKSPACE', out_name, False) elif layer.isRasterLayer: ext = '{0} {1} {2} {3}'.format(aoi.extent.XMin, aoi.extent.YMin, aoi.extent.XMax, aoi.extent.YMax) arcpy.Clip_management(layer.dataSource, ext, out_name) if arcpy.env.workspace.endswith('.gdb'): layer.replaceDataSource(arcpy.env.workspace, 'FILEGDB_WORKSPACE', out_name, False) else: layer.replaceDataSource(arcpy.env.workspace, 'RASTER_WORKSPACE', out_name, False) except arcpy.ExecuteError: status_writer.send_state(status.STAT_WARNING, arcpy.GetMessages(2)) # Save a new copy of the mxd with all layers clipped and re-sourced. if mxd.description == '': mxd.description = os.path.basename(mxd.filePath) if arcpy.env.workspace.endswith('.gdb'): new_mxd = os.path.join(os.path.dirname(arcpy.env.workspace), os.path.basename(mxd.filePath)) else: new_mxd = os.path.join(arcpy.env.workspace, os.path.basename(mxd.filePath)) mxd.saveACopy(new_mxd) del mxd