Crop a raster in bulk using a Python script

Source: Internet
Author: User

???? For the cropping of a raster, we usually crop it with crop (data management-raster-raster processing) or by masking (spatial analysis-extraction analysis), which is typically a feature layer or shape file. If you want to do batch processing, you can use the Bulk processing tool in toolbox. However, sometimes the crop vector feature that we select is not a feature layer, but rather a feature in a feature layer, such as trimming a raster image by a standard map, the previous practice is to export the features one at a time and then batch processing. Quick Export method: ArcGIS Splits files by field attributes.

???? Now, you can use Python script to crop in bulk, and find a great God's original code on the Internet to learn:

  1. #-*-Encoding:utf-8-*-
  2. ?
  3. # Tool Name: Traverse feature clipping raster
  4. # Source Name:extractByMask.py
  5. # Version:arcgis 10.1
  6. # Author:giscloud
  7. # CREATED:2012/11/1
  8. ?
  9. Import arcpy
  10. Import String
  11. From ARCPY.SA Import *
  12. ?
  13. Try:
  14. ???? Raster = arcpy. Getparameterastext (0) #要裁剪的栅格
  15. ???? Clip_feat = arcpy. Getparameterastext (1) #裁剪要素类
  16. ???? field = arcpy. Getparameterastext (2) #命名字段
  17. ???? Outworkspace = arcpy. Getparameterastext (3) #命名字段裁剪后输出目录
  18. ?
  19. ???? for Row in arcpy. Searchcursor (clip_feat):
  20. ???????? Mask=row.getvalue ("Shape")
  21. ???????? outpath=outworkspace+ "\ \" +str (row.getvalue (field))
  22. ???????? Outextractbymask = Extractbymask (raster,mask)
  23. ???????? Outextractbymask.save (Outpath)
  24. ?
  25. Except arcpy. Executeerror:
  26. ???? Print arcpy. Getmessages ()

???? Export script to Toolbox, execute Error: unicodeencodeerror: ' ASCII ' codec can ' t encode characters in position 32-34:ordinal not in range (128) Q The Ulipad compiler is used.

???? Looks like a coding problem. Solution: Add code to set default edits at the beginning

    1. Import Sys
    2. Reload (SYS)
    3. Sys.setdefaultencoding ("utf-8")

      Able to achieve partial trimming, but the last one out of the question, see the message output of the file name of how many more a period?

???? There is no problem viewing the property sheet. The output field "Beichuan Qiang Autonomous County" changed to "Beichuan Qiang autonomous" can be correctly exported, if changed to "Beichuan Qiang autonomous Beichuan Qiang autonomous", and reported the same mistake. The description should be an issue with the output name length.

Also, there is a problem with the different raster boundaries of the output, and if you avoid this, you can only change system variables and output smaller cell sizes.

Reference: http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=121642

???? http://blog.sina.com.cn/s/blog_64a3795a01018vyp.html

?

Crop a raster in bulk using a Python script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.