本文目录一览:
- 1、如何使用PYTHON 向arcmap中加载shapefile 不是即时python窗口
- 2、arcgisserevr 可以将python脚本发布为服务吗
- 3、如何利用python 批量导出mxd至jpg
- 4、求助,怎么运用Python脚本批量替换mxd文件中的文本?
- 5、如何将模板文档导入python并创建
- 6、关于arcgis的python脚本编程, shape文件出png图片问题
如何使用PYTHON 向arcmap中加载shapefile 不是即时python窗口
# 获得当前的document
mxd = arcpy.mapping.MapDocument("CURRENT")
# 获得data frame
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
# 创建新图层
# path_to_shapefile_or_feature_class:shapefile路径或者要素类
newlayer = arcpy.mapping.Layer(path_to_shapefile_or_feature_class)
# 添加图层,放到最下面一层
arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")
arcgisserevr 可以将python脚本发布为服务吗
通常我们使用ArcCatalog或者ArcGIS Server Manager等图形界面工具来发布ArcGIS服务,然而有些情况下,比如需要发布的服务数量很多,而且需要不定期随时发布的时候,再使用这些工具进行人工操作就显得力不从心了。ArcGIS的强大之处就在于其为我们提供了多种选择,我们可以通过Python编写脚本,调用ArcPy模块中的功能来实现自动扫描文件夹并将其中的地图文档发布为地图服务。
本文将创建一个publishHelper.py文件,在其中编写两个方法,一个是PublishAll,用来遍历文件夹并调用另一个方法PublishMxd,后者完成具体的服务发布工作。
在服务发布之前,需要首先检查mxd文档路径,读取mxd文档,转为msd,分析文档等,最后才是发布服务。
[html] view plain copy
# -*- coding: utf-8 -*-
import arcpy, os
__name__ = 'publishHelper'
# 将指定目录下所有的.mxd文档发布为地图服务
# folder:包含mxd文档的文件夹路径
# serviceDir:服务目录URL,例如
# serviceFolder:服务所在文件夹,如果为空,则表示根目录
def PublishAll(folder,serviceDir,serviceFolder):
print "检查文件夹路径……"
if os.path.isdir(folder) == False:
print "输入的文件夹路径无效!"
return
print "遍历文件夹……"
files = os.listdir(folder)
for f in files:
if f.endswith(".mxd"):
mxdPath = os.path.join(folder, f)
print "publishing: " + f
PublishMxd(mxdPath, serviceDir, serviceFolder)
else:
continue
#将mxd文档发布为服务:1.将mxd转为msd;2.分析msd;3.发布msd
def PublishMxd(mxdPath, serviceDir, serviceFolder):
#检查mxd和msd文件是否存在
print "检查文件路径……"
if os.path.exists(mxdPath) == False:
print "指定路径的mxd文档不存在!"
return
# 打开mxd文档
try:
print "正在打开mxd文档……"
mxd = arcpy.mapping.MapDocument(mxdPath)
except Exception, e:
print "open mxd error: ", e
return
else:
print "mxd文档打开成功……"
# 获取默认的数据框
print "正在读取mxd文档默认数据框……"
df = ""
try:
frames = arcpy.mapping.ListDataFrames(mxd, "图层")
if len(frames) == 0:
frames = arcpy.mapping.ListDataFrames(mxd, "Layers")
df = frames[0]
except Exception, e:
print "读取mxd文档默认数据框失败:", e
return
# 构造msd文档名称
msdPath = mxdPath.replace(".mxd", ".msd")
# 将mxd转为msd
print "正在将mxd文档转换为msd文档……"
arcpy.mapping.ConvertToMSD(mxd, msdPath, df, "NORMAL", "NORMAL")
# 分析msd
print "正在分析文档……"
analysis = arcpy.mapping.AnalyzeForMSD(mxd)
# 列出分析结果信息
for key in ('messages', 'warnings', 'errors'):
print "----" + key.upper() + "---"
vars = analysis[key]
for ((message, code), layerlist) in vars.iteritems():
print " ", message, " (CODE %i)" % code
print " applies to:",
for layer in layerlist:
print layer.name,
#获取服务器信息
serviceName = os.path.basename(msdPath).replace(".msd", "")
serverName = serviceDir.split("/")[2]
try:
#发布msd
print "正在发布服务……"
arcpy.mapping.PublishMSDToServer (msdPath, serviceDir, serverName, serviceName, serviceFolder, ["WMS", "KML"])
except Exception, e:
print "发布服务失败:", e
else:
print "服务发布成功!"
调用代码:
[html] view plain copy
import sys
sys.path.append("E:\\Codes\\Python")
from publishHelper import PublishAll # 必须要有正确的许可,否则导入失败
PublishAll("D:\\TestData", "", "sichuan")
如此即可通过Python脚本去遍历文件夹,并发布其中的mxd文档为地图服务。稍作完善,还可作为windows服务自动运行,实现服务的随到随发。
如何利用python 批量导出mxd至jpg
你好,arcpy.mapping提供了如下的函数:
arcpy.mapping 函数
AddLayer(data_frame, add_layer, {add_position})
AddLayerToGroup(data_frame, target_group_layer, add_layer, {add_position})
AnalyzeForMSD(map_document)
ConvertToMSD(map_document, out_msd, {data_frame}, {msd_anti_aliasing}, {msd_text_anti_aliasing})
DeleteMapService(connection_url_or_name, server, service_name, {folder_name}, {connection_username}, {connection_password}, {connection_domain})
ExportToAI(map_document, out_ai, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {image_quality}, {colorspace}, {picture_symbol}, {convert_markers})
ExportToBMP(map_document, out_bmp, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {world_file}, {color_mode}, {rle_compression})
ExportToEMF(map_document, out_emf, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {image_quality}, {description}, {picture_symbol}, {convert_markers})
ExportToEPS(map_document, out_eps, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {image_quality}, {colorspace}, {ps_lang_level}, {image_compression}, {picture_symbol}, {convert_markers}, {embed_fonts}, {jpeg_compression_quality})
ExportToGIF(map_document, out_gif, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {world_file}, {color_mode}, {gif_compression}, {background_color}, {transparent_color}, {interlaced})
ExportToJPEG(map_document, out_jpeg, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {world_file}, {color_mode}, {jpeg_quality}, {progressive})
ExportToPDF(map_document, out_pdf, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {image_quality}, {colorspace}, {compress_vectors}, {image_compression}, {picture_symbol}, {convert_markers}, {embed_fonts}, {layers_attributes}, {georef_info}, {jpeg_compression_quality})
ExportToPNG(map_document, out_png, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {world_file}, {color_mode}, {background_color}, {transparent_color}, {interlaced})
求助,怎么运用Python脚本批量替换mxd文件中的文本?
import arcpy, string, os
#Read input parameters from script tool
Path = arcpy.GetParameterAsText(0)
oldText = arcpy.GetParameterAsText(1)
newText = arcpy.GetParameterAsText(2)
case = arcpy.GetParameter(3)
exact = arcpy.GetParameter(4)
outputMXD = arcpy.GetParameterAsText(5)
try:
#Referent the map document
mxd = arcpy.mapping.MapDocument(mxdPath)
#Find all page layout text elements
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if exact:
if case:
if oldText == elm.text:
elmText = elm.text.replace(oldText, newText)
elm.text = elmText
else:
if oldText.upper() == elm.text.upper():
elmText = elm.text.upper().replace(oldText, newText)
elm.text = elmText
else:
if case:
if oldText in elm.text:
elmText = elm.text.replace(oldText, newText)
elm.text = elmText
else:
if oldText.upper() in elm.text.upper():
elmText = elm.text.upper().replace(oldText, newText)
elm.text = elmText
mxd.saveACopy(outputMXD)
del mxd
except Exception, e:
import traceback
map(arcpy.AddError, traceback.format_exc().split("\n"))
arcpy.AddError(str(e))
如何将模板文档导入python并创建
模板文档导入python并创建的方法如下:
1.1 需要导入的模板在同一文件夹中
比如admin.py调用test.py模板,可以在admin.py中直接执行
因为python解释器查找模块时会找同一文件夹,所以不用导入路径名。
1.2 需要导入的模板在当前文件夹的子文件夹中
比如 manage.py调用setting.py模板,可以在manage.py中直接执行
无需加其他路径说明,因为 crm 也是一个模块(模块的集合)。
1.3 需要导入的模板在父文件夹中
比如admin.py想调用surround.py模板(在admin.py父文件夹project下),因为解释器找模块只会去本文件夹及本文件夹的子文件夹去找,不会去父文件夹找,为什么?试试看,先在admin.py中执行
当一个的模块被导入的时候,解释器首先寻找具有该名称的内置模块。如果没有找到,然后解释器从 sys.path 变量给出的目录列表里寻找该模板。所以输出结果可以看到是一个路径列表,其它路径不需要看,只看第一项
说明解释器就是从 pro文件夹开始往下找,而surround.py是在 project/crm文件夹下,显然是找不到的。解决方法,在admin.py中执行手动把相应的路径添加到 sys.path 中就可以了,这样导入就相当于可以从添加的相应路径开始往下找模块了。可以找到,结果也不会报错。
关于arcgis的python脚本编程, shape文件出png图片问题
1、点类型的转栅格:
PointToRaster example 1 (Python
window)
Converts point features to a raster dataset.
import arcpy
from arcpy import env
env.workspace = "c:/data"
arcpy.PointToRaster_conversion("ca_ozone_pts.shp", "ELEVATION",
"c:/output/ca_elev", "MAXIMUM", "", 2000)
PointToRaster example 2
(stand-alone script)
Converts point features to a raster dataset.
# Name: PointToRaster_Ex_02.py
# Description: Converts point features to a raster dataset.
# Requirements: ArcInfo
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inFeatures = "ca_ozone_pts.shp"
valField = "ELEVATION"
outRaster = "c:/output/ca_elev02"
assignmentType = "MAXIMUM"
priorityField = ""
cellSize = 2000
# Execute PointToRaster
arcpy.PointToRaster_conversion(inFeatures, valField, outRaster,
assignmentType, priorityField, cellSize)
2、面类型的转栅格:
PolygonToRaster example 1
(Python window)
Converts polygon features to a raster dataset.
import arcpy
from arcpy import env
env.workspace = "c:/data"
arcpy.PolygonToRaster_conversion("ca_counties.shp", "NAME",
"c:/output/ca_counties.img",
"MAXIMUM_AREA", "MALES", 0.25)
PolygonToRaster example 2
(stand-alone script)
Converts polygon features to a raster dataset.
# Name: PolygonToRaster_Ex_02.py
# Description: Converts polygon features to a raster dataset.
# Requirements: ArcInfo
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inFeatures = "ca_counties.shp"
valField = "NAME"
outRaster = "c:/output/ca_counties"
assignmentType = "MAXIMUM_AREA"
priorityField = "MALES"
cellSize = 0.5
# Execute PolygonToRaster
arcpy.PolygonToRaster_conversion(inFeatures, valField, outRaster,
assignmentType, priorityField, cellSize)
3、线类型转栅格
PolylineToRaster example 1
(Python window)
Converts polyline features to a raster dataset.
import arcpy
from arcpy import env
env.workspace = "c:/data"
arcpy.PolylineToRaster_conversion("roads.shp", "CLASS", "c:/output/roads.img",
"MAXIMUM_COMBINED_LENGTH", "LENGTH", 30)
PolylineToRaster example 2
(stand-alone script)
Converts polyline features to a raster dataset.
# Name: PolylineToRaster_Ex_02.py
# Description: Converts polyline features to a raster dataset.
# Requirements: ArcInfo
# Import system modules
import arcpy
from arcpy import env
# Set environment settings
env.workspace = "C:/data"
# Set local variables
inFeatures = "roads.shp"
valField = "CLASS"
outRaster = "c:/output/roads.tif"
assignmentType = "MAXIMUM_COMBINED_LENGTH"
priorityField = "LENGTH"
cellSize = 30
# Execute PolylineToRaster
arcpy.PolylineToRaster_conversion(inFeatures, valField, outRaster,
assignmentType, priorityField, cellSize)