美文网首页大数据 爬虫Python AI SqlPython小哥哥
在ArcGIS中Python使用及批量裁剪示技巧讲解 !

在ArcGIS中Python使用及批量裁剪示技巧讲解 !

作者: 14e61d025165 | 来源:发表于2019-07-22 14:58 被阅读0次

    ArcGIS中的工具均提供Python函数,方便支持批量处理。

    以掩膜裁剪(提取分析/Extract by Mask)为例,

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1563778624693" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    Python资源共享群:484031800

    可以查找到帮助文档“按掩膜提取”(源1)(源2),路径“工具参考/工具/Spatial Analyst 工具箱/按掩膜提取”函数为:Extract by Mask

    Extract by Mask的基本语法是:

    ExtractByMask (in_raster, in_mask_data)

    in_raster:提取像元的输入栅格。

    in_mask_data:用于定义提取区域的输入掩膜数据。它可以是栅格,也可以是要素数据集。

    代码示例

    以下以批量读取裁剪某文件夹下的tif数据为例(特别注意Python2脚本循环语句格式的空格不能用Tab,否则会报错Indent;

    另外在使用不同的矢量裁剪同一影像时,为保证裁剪后范围一致需设置范围为输入栅格范围。例如下面的:

    rd = arcpy.sa.Raster(inRaster)

    Set the extent environment as the raster, very important for clip with different vector

    arcpy.env.extent = rd.extent):

    Import system modules

    import arcpy

    from arcpy import env

    from arcpy.sa import *

    Set environment settings

    env.workspace = "D:/img"

    rasterList = arcpy.ListRasters("*","tif")

    输出路径

    output_path = "D:/imgMask/"

    mask shp

    inMaskData = "D:/range.shp"

    for raster in rasterList:

    print raster

    Set local variables

    inRaster = raster

    rd = arcpy.sa.Raster(inRaster)

    Set the extent environment as the raster, very important for clip with different vector

    arcpy.env.extent = rd.extent

    Execute ExtractByMask

    outExtractByMask = ExtractByMask(inRaster, inMaskData)

    Save the output

    out = output_path + inRaster #对生成文件进行命名

    outExtractByMask1.save(out)

    代码执行

    方式一:将代码保存为单独的py文件,cmd或其它IDE执行。cmd执行命令(如何独立运行脚本):

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">C:\Python27\ArcGIS10.3\python.exe my_script.py
    </pre>

    方式二:ArcGIS安装的Python IDE里执行

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1563778624705" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    相关文章

      网友评论

        本文标题:在ArcGIS中Python使用及批量裁剪示技巧讲解 !

        本文链接:https://www.haomeiwen.com/subject/mxkwlctx.html