ArcGIS + Python Batch cropping, adding x/y coordinate scripts

Source: Internet
Author: User
Tags glob python script

Objective


Some time ago, the data range of the colleague is too large, not so much (only need a township, the results brought to the county), too much load is also a problem. So let me deal with it.

Due to the large number of files, manual cropping, I am a one with the tools in the ArcGIS Toolbox to crop, the overall use of about 3 hours. Later, because of the need to narrow the scope again, this time one more manual then it is too pit. I thought I could use Python to write my script in batches.

Body

When you install the ArcGIS software, the Python environment is installed by default, and the arcpy library is installed. However, this Python is 2.7, I used to learn to use python3.x, I originally thought directly with 3.X, but in the "import arcpy" where there has been a problem, find some methods or not, finally can only use 2.7 of the.

The following is a batch-trimmed Python script:

ImportarcpyImportGlobImportos arcpy. Checkoutextension ('Spatial') #Specify the table of contents before croppingINWS = R"D:\PythonClip\oldData" #Specify the cropped storage directoryOutws = R"D:\PythonClip\Newdata" #Specify the SHP scope boundary file, which is the boundary of the target areaMask = R"D:\PythonClip\panhuo.shp"cluster_tolerance="0.0000001 decimaldegrees" #Use the Glob package to read all SHP files under INWS to ShpsShps = Glob.glob (Os.path.join (INWS,"*.shp")) #Loop through all the images in the SHPS and extract them by mask forshpinchShps:outname= Os.path.join (Outws, Os.path.basename (SHP))#specify how the output file is to be named    PrintOutnameTry: arcpy. Clip_analysis (Shp,mask,outname)exceptException as E:PrintE.message

Due to the large number of files, it is the shp file in the specified directory, one clip and the output saved.

After cropping, you need to add the X/y coordinates of the file to the attribute list. I think this also use the script to write, good no problem The following also successfully completed:

Importarcpydefcalcxy ():Try:        Print "Set ENV"Arcpy.env.workspace="D:\\pythonclip\\newdata"        Print "Start AddField and Calculate centroid"Shps= arcpy. Listfiles ("*.shp")         forshpinchshps:fieldlist= arcpy. ListFields (SHP,"","Double") FieldNames= []             forFieldinchfieldList:fieldNames.append (field.name)if "X"  not inchfieldnames:arcpy. Addfield_management (SHP,"X","DOUBLE", 20,4) arcpy. Addfield_management (SHP,"Y","DOUBLE", 20,4) arcpy. Calculatefield_management (SHP,"X","! SHAPE. Centroid. x!","python_9.3") arcpy. Calculatefield_management (SHP,"Y","! SHAPE. Centroid. y!","python_9.3")                PrintshpPrint "AddField and Calculate centroid End"    exceptException as E:Printe.message Calcxy () calcxy ()

The above is the code to add x, Y.

These simple scripts can be found in the ArcGIS Help documentation, and each one has a complete example. There are some Python basics that can be implemented.

ArcGIS + Python Batch cropping, adding x/y coordinate scripts

Related Article

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.