def export_maps(mxd, btn_layer, btn_export_pdf, btn_export_png): map_name = str(btn_layer.objectName())[6:] if btn_export_png.isChecked() == True: #print btn_layer.text(), ' Print PNG' map.ExportToPNG(mxd, map_name+".png", resolution=300, \ transparent_color='255, 255, 255') if btn_export_pdf.isChecked() == True: #print btn_layer.text(), ' Print PDF' map.ExportToPDF(mxd, map_name+".pdf" )
def exportMap(parameter): inMap = parameter[0] outMap = parameter[1] formatMap = parameter[2] arcpy.env.overwriteOutput = True mxd = mapping.MapDocument(inMap) mxd.activeView = 'PAGE_LAYOUT' if formatMap == 'JPEG': mapping.ExportToJPEG(mxd, outMap) if formatMap == 'PDF': mapping.ExportToPDF(mxd, outMap) if formatMap == 'BMP': mapping.ExportToBMP(mxd, outMap) if formatMap == 'PNG': mapping.ExportToPNG(mxd, outMap) if formatMap == 'TIFF': mapping.ExportToTIFF(mxd, outMap) if formatMap == 'GIF': mapping.ExportToGIF(mxd, outMap) else: mapping.ExportToJPEG(mxd, outMap)
backup_mxd = os.path.join(backup_dir, mdoc_name + '.mxd') print "Saving as %s" % backup_mxd shutil.copy(mapdoc, backup_mxd) # update position of legend element #main_df = mp.ListDataFrames(mxd)[1] #main_lyrs = mp.ListLayers(mxd, "", main_df) leg_elm = mp.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0] leg_elm.elementPositionX = 8.8 leg_elm.elementPositionY = 5.7 - leg_elm.elementHeight #save mapdoc mxd.save() #export map map_name = os.path.join(out_dir, mdoc_name) print "Exporting %s" % mapdoc mp.ExportToPNG(mxd, map_name, resolution=96) print '\n' del mxd count += 1
mapping.AddLayer(df, addIdwLyr, "AUTO_ARRANGE") legend1.autoAdd = False legend2.autoAdd = True mapping.AddLayer(df, addSoilPointLyr, "AUTO_ARRANGE") legend1.autoAdd = False legend2.autoAdd = False mapping.AddLayer(df, addBoundaryLyr, "AUTO_ARRANGE") arcpy.mapping.AddLayer(df, baseLayer, "BOTTOM") # set name of legends legend1.title = baseName.split("_")[-1] + " Corrosion Rate (mils/year)" legend2.title = "" styleItem = mapping.ListStyleItems("ESRI.style", "Legend Items", "Horizontal Single Symbol Label Only")[0] # set layer style for lyr in legend1.listLegendItemLayers(): legend1.updateItem(lyr, styleItem) legend1.adjustColumnCount(3) for lyr in legend2.listLegendItemLayers(): legend2.updateItem(lyr, styleItem) # print map outMapName = baseName.split("_")[0] + depth + baseName.split("_")[-1] outMap = os.path.join(outMapDir, outMapName) mapping.ExportToPNG(mxd,outMap,resolution=300) # clear up layers that added to the mxd template so as to get prepared for mapping the next idw layer for lyr in arcpy.mapping.ListLayers(mxd, "", df): arcpy.mapping.RemoveLayer(df, lyr) print "Mapping Done" mxd.save() del mxd
# Create the vector to loop through lyrVec = mp.ListLayers(df) # To be safe, start by hiding all of the layers for lyr in lyrVec: lyr.visible = False mxd.save() # Loop through all layers and export PNGs for lyr in lyrVec: lyr.visible = True # Set the name for out PNG file lyrName = ap.Describe(lyr).name outPNG = outimgDir + lyrName[:-4] + ".png" mp.ExportToPNG(mxd, outPNG, df, 640, 480, 96, True) lyr.visible = False mxd.save() del mxd, df try: del env.workspace, mapDoc, fldext_srcLyr, wd_srcLyr, fv_srcLyr, cric_srcLyr, rasDir, outlyrDir, outimgDir, refLayer, dirList, rasList, ras, rasName, outLyr, tmpLyr, wrkLyr #Do your cleanup properly except: pass finally:
def mappingProcess(datetime, province, target_area, density_class=None, day_class=10, out_path=None, out_type= "png"): # todo.控制densityClass和dayClass最多为14 cwd = os.getcwd() infeature = ''.join([cwd, u"/data/LightningBulletin.gdb/", target_area]) mxd_density_path = ''.join([cwd, u"/data/LightningBulletin.gdb/", target_area, u"闪电密度空间分布模板.mxd"]) mxd_day_path = ''.join([cwd, u"/data/LightningBulletin.gdb/", target_area, u"地闪雷暴日空间分布模板.mxd"]) workspace = ''.join([cwd, u"/temp/", province, '/', datetime, '/', target_area, '.gdb']) arcpy.env.workspace = workspace arcpy.env.overwriteOutput = True # *****处理地闪密度地图文档******* mxd = mapping.MapDocument(mxd_density_path) mask_lyr = mapping.ListLayers(mxd)[2] target_lyr = mapping.ListLayers(mxd)[3] mask_lyr.replaceDataSource(workspace, "FILEGDB_WORKSPACE", "densityMask") target_lyr.replaceDataSource(workspace, "FILEGDB_WORKSPACE", "lightningDensity") # 修改图例 # 计算数据分割值和不显示值 extract = ExtractByMask("lightningDensity", infeature) raster = Raster("lightningDensity") class_break_values, density_class = densityLegendLabel(extract.minimum / 100, extract.maximum / 100, density_class) excluded_values = ''.join([str(round(raster.minimum - 0.5, 2)), "-", str(round(extract.minimum - 0.5, 2)), ";", str(round(extract.maximum + 0.5, 2)), "-", str(round(raster.maximum + 0.5, 2))]) sym = target_lyr.symbology sym.excludedValues = excluded_values sym.classBreakValues = [x * 100 for x in class_break_values] # 更改标题和图例 layout_items = mapping.ListLayoutElements(mxd, "TEXT_ELEMENT") layout_items[-1].text = ''.join([datetime, target_area, u"闪电密度空间分布"]) # title标题 layout_items[-2].text = ''.join([target_area, u"气象局制"]) # 底注 density_class = class_break_values.__len__() - 1 class_break_values = [str(x) for x in class_break_values] # TODO 将position参数放到设置文件里,或者自动处理 position = {u"绍兴市": (1.0436, 1.6639, 0.9859272727272728), u"嵊州市": (1.1138, 1.2614, 0.9869090909090698), u"上虞区": (1.1372, 1.1617, 0.9862375000000156), u"诸暨市": (1.1138, 1.2614, 0.9869090909090698), u"柯桥区": (1.1372, 1.1617, 0.9862375000000156), u"新昌县": (1.1138, 1.2614, 0.9869090909090698), u"新乡市": (1.0436, 1.6639, 0.9859272727272728)} Y, Xstart, gap = position[target_area] for i in xrange(density_class + 1): layout_items[i].text = class_break_values[i] layout_items[i].elementPositionY = Y layout_items[i].elementPositionX = Xstart + gap * i for i in xrange(density_class + 1, 15): # 最多14类,15个标签 layout_items[i].elementPositionX = -20 mxd.save() if not out_path: out_path = workspace out_name = os.path.join(out_path, ''.join([datetime, target_area, u"闪电密度空间分布"])) out_type = out_type.lower() if out_type in ["jpg", 'gpeg']: mapping.ExportToJPEG(mxd, out_name, resolution=200) elif out_type == "png": mapping.ExportToPNG(mxd, out_name, resolution=200) elif out_type == "pdf": mapping.ExportToPDF(mxd, out_name) elif out_type == "bmp": mapping.ExportToBMP(mxd, out_name, resolution=200) else: mapping.ExportToTIFF(mxd, out_name, resolution=200) # *****处理雷暴日地图文档******* mxd = mapping.MapDocument(mxd_day_path) mask_lyr = mapping.ListLayers(mxd)[2] target_lyr = mapping.ListLayers(mxd)[3] mask_lyr.replaceDataSource(workspace, "FILEGDB_WORKSPACE", "dayMask") target_lyr.replaceDataSource(workspace, "FILEGDB_WORKSPACE", "lightningDay") # 修改图例 # 计算数据分割值和不显示值 extract = ExtractByMask("lightningDay", infeature) raster = Raster("lightningDay") start = floor(extract.minimum) end = ceil(extract.maximum) class_break_values, day_class = dayLegendLabel(start, end, day_class) excluded_values = ''.join([str(round(raster.minimum - 0.5, 2)), "-", str(round(extract.minimum - 0.5, 2)), ";", str(round(extract.maximum + 0.5, 2)), "-", str(round(raster.maximum + 0.5, 2))]) sym = target_lyr.symbology sym.excludedValues = excluded_values sym.classBreakValues = class_break_values # 更改标题和图例 layout_items = mapping.ListLayoutElements(mxd, "TEXT_ELEMENT") layout_items[-1].text = ''.join([datetime, target_area, u"地闪雷暴日空间分布"]) layout_items[-2].text = ''.join([target_area, u"气象局制"]) # 底注 Y, Xstart, gap = position[target_area] for i in xrange(day_class + 1): layout_items[i].text = class_break_values[i] layout_items[i].elementPositionY = Y layout_items[i].elementPositionX = Xstart + gap * i for i in xrange(day_class + 1, 15): # 最多14类,15个标签 layout_items[i].elementPositionX = -20 mxd.save() out_name = os.path.join(out_path, ''.join([datetime, target_area, u"地闪雷暴日空间分布"])) out_type = out_type.lower() if out_type in ["jpg", 'gpeg']: mapping.ExportToJPEG(mxd, out_name, resolution=200) elif out_type == "png": mapping.ExportToPNG(mxd, out_name, resolution=200) elif out_type == "pdf": mapping.ExportToPDF(mxd, out_name) elif out_type == "bmp": mapping.ExportToBMP(mxd, out_name, resolution=200) else: mapping.ExportToTIFF(mxd, out_name, resolution=200)
import arcpy.mapping as mp #walk through directory and sub-dir dir_root = "D:\LUIGI\OML\GIS Project Files" out_dir = "D:\LUIGI\OML\JPEGS WORKING DRAFTS" count = 1 for root, dirs, files in os.walk(dir_root): #ignore .mxd files with keywords if 'GROUND TRUTH' in root: pass elif 'BACKUP' in root: pass elif 'SCALE' in root: pass #perform desired operation on .mxd else: for mapdoc in glob.glob(root+'\*.mxd'): if ", Davao" in mapdoc: pass elif "Davao" in mapdoc or "Iloilo" in mapdoc: print '%d. %s' % (count, mapdoc) mxd = mp.MapDocument(mapdoc) mp.ExportToPNG(mxd, out_dir + '\\' + os.path.basename(mapdoc),resolution=96) del mxd count += 1
lyr_a = mp.ListLayers(mxd, data_frame=df0) lyr_b = mp.ListLayers(mxd, data_frame=df1) i = 0 for FID_value in FIDs: i = i + 1 # LAYER A ---------------------------------------------------------------- lyr_a[2].definitionQuery = '"FID" = ' + str(FID_value) # print lyr_a[2].definitionQuery extentOfLayer = lyr_a[2].getExtent(True) # visible extent of layer df0.extent = extentOfLayer arcpy.RefreshActiveView() # redraw the map df0.scale = 35000 arcpy.RefreshActiveView() # redraw the map # LAYER B ---------------------------------------------------------------- lyr_b[2].definitionQuery = '"FID" = ' + str(FID_value) # print lyr_b[2].definitionQuery outFile = outDir + "/LC_CortaderiaMap_S2Class_Tile_" + str(FID_value).zfill(3) + "_v2.png" mp.ExportToPNG(mxd, outFile, 'PAGE_LAYOUT', width, height, dpi) print "Finished map tile: " + str(FID_value) + " | " + str(round((float(i) / float(len(FIDs))) * 100, 1)) + "%\n" del mxd