python加載mxd文檔的簡單介紹

本文目錄一覽:

如何使用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,

print

#獲取服務器信息

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)

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/249808.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 17:11
下一篇 2024-12-12 17:11

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29

發表回復

登錄後才能評論