99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀淺談使用ArcPy執(zhí)行大數(shù)據(jù)量處理任務(wù)
淺談使用ArcPy執(zhí)行大數(shù)據(jù)量處理任務(wù)
2017-12-27
收藏

淺談使用ArcPy執(zhí)行大數(shù)據(jù)量處理任務(wù)

Python功能強(qiáng)大而易于學(xué)習(xí)。對(duì)于ArcGIS for Desktop用戶來講,Python是提高工作效率的不二選擇。

Arcpy是esri提供的用于高效數(shù)據(jù)處理分析、制圖等的Python站點(diǎn)包。 利用ArcPy,我們可以在ArcMap的Python窗口交互執(zhí)行腳本,還可以創(chuàng)建自定義腳本工具或腳本工具箱,還可以在ArcGIS之外運(yùn)行獨(dú)立腳本,享受更純正的python體驗(yàn)。
這一篇說說如何利用Python批量執(zhí)行數(shù)據(jù)處理任務(wù),這個(gè)問題也是前段時(shí)間遇到的用戶的實(shí)際問題,比較有價(jià)值。
需求
還是從實(shí)例開始……
有一個(gè)簡(jiǎn)單但耗體力的裁剪任務(wù),希望通過大量面分割(逐一裁剪)大量的數(shù)據(jù),類似Split工具要完成的任務(wù),并且要按照一定的規(guī)則命名將分割結(jié)果輸出到指定的位置,例如要求有指定前綴。
實(shí)現(xiàn)
例如,一種思路是逐一遍歷面要素,然后去裁剪目標(biāo)數(shù)據(jù)再輸出,這時(shí)你可能會(huì)遇到下面的小問題:
我如何通過ArcPy獲取要素的幾何?
在ArcPy中提供了一個(gè)數(shù)據(jù)訪問模塊/Data Access (arcpy.da),我們可以通過游標(biāo)(Cursor)來查詢要素的幾何或?qū)傩浴T谶@個(gè)需求中是逐一遍歷面要素的幾何,我們選擇 SearchCursor,通過 SHAPE@ 可以訪問要素的幾何。
語法: SearchCursor(in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})
了解詳細(xì)的幫助信息點(diǎn)這里。
那么可以把函數(shù)主體定義成這樣,即可實(shí)現(xiàn)需求:

def MyBatchClip(Parameter):
    # 參數(shù)
    inputFC = Parameter[0]
    ClipArea = Parameter[1]
    OutputWS = Parameter[2]
    Prefix = Parameter[3]
    # 字段列表,SHAPE@ 訪問要素幾何對(duì)象
    Fields = ['FID','SHAPE@']
    # 遍歷面要素逐一裁剪目標(biāo)數(shù)據(jù)并輸出自定義前綴的結(jié)果。
    with arcpy.da.SearchCursor(ClipArea,Fields) as cursor:
        for row in cursor:
            outputFC = os.path.join(OutputWS, Prefix+str(row[0])+'.shp')
            arcpy.Clip_analysis(inputFC, row[1], outputFC)
多進(jìn)程
如果這個(gè)批量任務(wù)是大量的,如何更高效地開動(dòng)起來?
這里按照esri以前的一篇 Blog 提到的方法分享給大家,使用Multiprocessing模塊并行處理。 Multiprocessing 模塊是Python的一個(gè)標(biāo)準(zhǔn)庫,通過這個(gè)庫,我們可以利用多核CPU,來實(shí)現(xiàn)多進(jìn)程處理大數(shù)據(jù)量的任務(wù)。
可以通過 multiprocessing.Pool 來使用進(jìn)程池,Pool類可以管理固定數(shù)目的進(jìn)程,默認(rèn)是開啟和機(jī)器CPU數(shù)目相同的進(jìn)程。
    語法:
    multiprocessing.Pool([processes[, initializer[, initargs[, maxtasksperchild]]]])
    processes表示pool中進(jìn)程的數(shù)目,默認(rèn)地為當(dāng)前CPU的核數(shù)。
    initializer表示新進(jìn)程的初始化函數(shù)。
    initargs表示新進(jìn)程的初始化函數(shù)的參數(shù)。
    maxtasksperchild表示每個(gè)進(jìn)程執(zhí)行task的最大數(shù)目
把腳本修改下,加上多進(jìn)程處理的部分:
# -*- coding:utf-8 -*-
__author__ = 'kikita'

import arcpy
import timeit
import time
import multiprocessing
import os

arcpy.env.workspace =  r'D:\LearnAboutPython\MyPythonProject\UsingCurser\DemoDataS.gdb'
arcpy.env.overwriteOutput = True

# 批量裁剪函數(shù)
def MyBatchClip(Parameter):
    # 參數(shù)
    inputFC = Parameter[0]
    ClipArea = Parameter[1]
    OutputWS = Parameter[2]
    Prefix = Parameter[3]
    # 字段列表,其中 SHAPE@用于訪問數(shù)據(jù)幾何
    Fields = ['OBJECTID','SHAPE@']
    with arcpy.da.SearchCursor(ClipArea,Fields) as cursor:
        for row in cursor:
            outputFC = os.path.join(OutputWS, Prefix+str(row[0])+'.shp')
            arcpy.Clip_analysis(inputFC, row[1], outputFC)
            print Prefix+str(row[0])+'.shp'


if __name__ == '__main__':
    # 參數(shù)
    OutputWS = r'D:\LearnAboutPython\MyPythonProject\UsingCurser\OutputWS'
    # SDE庫輸出
    #OutputWS = r'C:\Connection131.sde'
    Parameter1 = ['CountyPoints','Area_A',OutputWS, 'AAA_']
    Parameter2 = ['hyd_line','Area_B',OutputWS, 'BBB_']
    Parameter3 = ['River_line.shp','Area_C.shp',OutputWS,'CCC_']
    Parameters = [Parameter1,Parameter2,Parameter3 ]
    # 當(dāng)前CPU核數(shù)
    print 'CPU Count:' + str(multiprocessing.cpu_count())
    # 進(jìn)程池
    MyGPpool = multiprocessing.Pool()
    # 多進(jìn)程并行處理
    StartTime = time.time()
    results = MyGPpool.map(MyBatchClip,Parameters)
    EndTime = time.time()
    print 'Elapsed:  ' + str(EndTime - StartTime) + '  Seconds...'
結(jié)果

CPU Count:8
AAA_0.shp
BBB_0.shp
CCC_0.shp
BBB_1.shp
AAA_1.shp
CCC_1.shp
BBB_2.shp
AAA_2.shp
CCC_2.shp
……
……
……
BBB_28.shp
AAA_27.shp
BBB_29.shp
CCC_28.shp
CCC_29.shp
AAA_28.shp
BBB_30.shp
CCC_30.shp
AAA_29.shp
AAA_30.shp
Elapsed:  28.628000021  Seconds...
一點(diǎn)有用的提示:
1.在使用Multiprocessing時(shí),注意數(shù)據(jù)鎖定(Schema Lock)的問題,例如這個(gè)例子中,當(dāng)輸出工作空間選擇為FileGDB時(shí)出現(xiàn)異常。 使用文件夾輸出 Shapefile,或者以SDE數(shù)據(jù)庫作為輸出工作空間,都是可以的。
2.我在代碼中也加入了計(jì)時(shí),用于比較并行與否的耗時(shí)情況。 但是有時(shí)確實(shí)會(huì)發(fā)現(xiàn),較簡(jiǎn)單的處理任務(wù)時(shí),多進(jìn)程并行并不比單進(jìn)程快,因?yàn)閷?dǎo)入模塊和啟動(dòng)進(jìn)程都需要花時(shí)間。

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }