Python 3 uses arcpy to customize GIS toolbox to realize excel reading and spatial interpolation

1, Purpose of the experiment
1. Master the basic syntax and function of Arcpy
2. Learn to use Arcpy custom GIS toolbox
3. Spatial interpolation with arcpy
4. Customize the Arcmap mapping template created, and use arcpy to adjust the location of legend, annotation, scale, etc
2, Experiment content
1. The arcpy package customizes the toolbox, selects files, generates new file names, and generates point vector graphics according to the longitude of column 4 and the latitude of column 5
2. According to the sixth row of ground elevation data, spatial interpolation is carried out to generate spatial interpolation layer. In Chapter 11, page 75 of Arcpy analysis method programming 1, Arcpy grid data analysis
3. Overlay and display the interpolated elevation map and point layer by calling arcpy package, create a customized template, and output the map piece to jpg according to the number of the map piece
3, Experimental steps and results display
1. Right click Arctoolbox, add toolbox, and click to create a new toolbox named Point shape created from Excel.tbx

2. Create code for the toolbox above, based on the file in the specified path,
The code is as follows:

# coding=gbk
import arcpy
import xlrd
try:
    indir = 'E:\\Autumn course PPT\\Python Spatial data processing\\python Spatial data processing-Assignment 03\\Basic data sheet of geological survey points new.xls'
    xl = xlrd.open_workbook(indir)
    sheet = xl.sheet_by_name('data')
    samples = sheet.ncols
    lines = sheet.nrows
    #Read latitude and longitude and elevation values
    longitude = sheet.col_values(4,1,lines-1)
    latitude = sheet.col_values(5,1,lines-1)
    elevation = sheet.col_values(6,1,lines-1)
    #Close input file
    #xl.close() 'Book' object has no attribute 'close'
    #Create shape
    outdir1 = 'E:\\Autumn course PPT\\Python Spatial data processing\\python Spatial data processing-Assignment 03\\'
    outdir2 = 'pointshp_created_by_excel.shp'
    if arcpy.Exists(outdir2):
        arcpy.Delete_management(outdir2)
    #python2 does not have gdal installed, nor does it support pip install after January 7, 2020, so it is unable to use the ogr driver function
    #driver = ogr.GetDriverByName('ESRI Shapefile')
    #if os.access(filename,os.F_OK):
        #driver.DeleteDataSource(outdir2)
    spatial_reference = arcpy.SpatialReference(4326)
    arcpy.CreateFeatureclass_management(outdir1,outdir2,"POINT")#, spatial_reference) will report an error, Buddha
    arcpy.AddField_management(outdir2, "elevation", "FLOAT",2)#2 decimal places
    rows = arcpy.InsertCursor(outdir2)
    point = arcpy.Point()
    for i in range(0,lines-2):
        row =  rows.newRow()
        point.X = longitude[i]
        point.Y = latitude[i]
        pointGeometry = arcpy.PointGeometry(point)
        row.shape = pointGeometry
        row.elevation = elevation[i]
        rows.insertRow(row)
    del row
    del rows
    arcpy.DefineProjection_management(outdir2, spatial_reference)
except Exception as e:
        print str(e)

Then replace the path assignment statement with arcpy.getparameterastext (0) arcpy.getparameterastext (1)
As follows:

# coding=gbk
import arcpy
import xlrd
import os
try:
    indir = arcpy.GetParameterAsText(0)
    outdir = arcpy.GetParameterAsText(1)
    xl = xlrd.open_workbook(indir)
    sheet = xl.sheet_by_name('data')
    samples = sheet.ncols
    lines = sheet.nrows
    #Read latitude and longitude and elevation values
    longitude = sheet.col_values(4,1,lines-1)
    latitude = sheet.col_values(5,1,lines-1)
    elevation = sheet.col_values(6,1,lines-1)
    #Close input file
    #xl.close() 'Book' object has no attribute 'close'
    #Create shape
    pathdir = os.path.split(outdir)[0]
    filename = os.path.split(outdir)[1]
    #outdir2 = 'pointshp_created_by_excel.shp'
    if arcpy.Exists(filename):
        arcpy.Delete_management(filename)
    #python2 does not have gdal installed, nor does it support pip install after January 7, 2020, so it is unable to use the ogr driver function
    #driver = ogr.GetDriverByName('ESRI Shapefile')
    #if os.access(filename,os.F_OK):
        #driver.DeleteDataSource(outdir2)
    spatial_reference = arcpy.SpatialReference(4326)
    arcpy.CreateFeatureclass_management(pathdir,filename,"POINT")#, spatial_reference) will report an error, Buddha
    arcpy.AddField_management(filename, "elevation", "FLOAT")
    rows = arcpy.InsertCursor(filename)
    point = arcpy.Point()
    for i in range(0,lines-2):
        row =  rows.newRow()
        point.X = longitude[i]
        point.Y = latitude[i]
        pointGeometry = arcpy.PointGeometry(point)
        row.shape = pointGeometry
        row.elevation = elevation[i]
        rows.insertRow(row)
    del row
    del rows
    arcpy.DefineProjection_management(filename, spatial_reference)
except Exception as e:
        print str(e)

It is worth noting that they should be put together after the code starts to facilitate the efficient operation of the code. Otherwise, if the code is specified when it needs to be used, the result will be: after entering a file, the later code will be run, run for a period of time, and then specify the second one, the code after the second designated place will be started, and the first one will be opened When the first response is completed, the user does not need to wait for the second response. For the code, see the attachment "create > shapepoint > from > excel > arcpy. Py", and "create > shapepoint > from > excel. Py" is the file under the specified path, which is the complete experiment file before replacement.

3. Create? Shapepoint? From? Excel? Arcpy.py is the code behind the toolbox. It specifies two input parameters of the toolbox: select a file and generate a new file name.
The input excel file belongs to tables and the output is shp

4. Use toolbox



It is worth noting that when specifying the input file, you cannot specify the suffix as Table, because if you specify the suffix as Table, you will further import the specific sheet, which is inconsistent with the program. To ensure the correct operation of the program, you can specify the suffix as file.
5. Display of toolbox operation results:

6.spline space interpolation. For the introduction of interpolation, refer to page 75 of Arcpy analysis method programming 1 in Chapter 11.
It is worth noting that if Spline spline function without barries is used for interpolation directly, the results are as follows:

It can be seen from the figure that this method loses the local characteristics and takes care of the edge. Obviously, it can't be interpolated by Spline method. Therefore, Spline method with Barriers is selected for interpolation. It is worth noting that a large vector surface layer should be drawn first, based on the following corner coordinates:
Left: 115.593700 dd right: 115.963166 dd up: 39.796028 dd down: 39.592500 dd take this layer as the barriers layer, and name it "spline_barriers.shp". See spline_interpolation.py for the code,

import arcpy
from arcpy import env
from arcpy.sa import *
import numpy
env.workspace = 'E:\\Autumn course PPT\\Python Spatial data processing\\python Spatial data processing-Assignment 03\\'
inPointFeatures = 'pointshp_created_by_excel.shp'
inBarrierFeature = 'spline_barriers.shp'
zField = 'elevation'
cellSize = 0.00081411112
arcpy.CheckOutExtension("Spatial")
# Execute Spline
outSplineBarriers = SplineWithBarriers(inPointFeatures, zField,inBarrierFeature,cellSize)
#Set the minimum value after interpolation to 0, rather than unlimited small value, so that the value of the main part cannot be displayed normally at all, which is white
#Since this problem cannot be solved temporarily, the above generated cache layer is manually saved with nodata set to 0
#Outsplinebariers. Save ('e: / fall course PPT/Python spatial data processing / python spatial data processing - assignment 03/splinebout.tif',nodata_to_value=0)
print 'processing is over!'

Running in python window of GIS,
The results are as follows:


It is worth noting that the same above code will report an error when executed in IDLE, but no error when executed in python command window of GIS window.
7. When saving the above results, use the outsplinebariers.save ('E: / autumn course PPT/Python spatial data processing / python spatial data processing - assignment 03/splinebout01.tif ') statement, but the problem is that the above seems to be completed, but after loading in, it is found that the bottom value is nodata value -3.40282e+038,

Obviously there was a problem with the code saving the grid. Therefore, to shield the saved code, manually save the cache layer as "outsplinebariers1. TIF", as shown in the figure:

8. as shown in the picture:

9. Drawing template. Scale, compass, legend and annotation are selected in the experiment. Due to time and technical factors, the position of each part is not well adjusted in arcpy. In order to make the image more beautiful, manual adjustment is carried out. See "elevation map of a certain area. jpg" for the drawing, as follows:

It should be noted that the survey point document updated by Mr. Rui for the second time has no frame number, so the block drawing is not considered, and all of them are made on one scene image.

Because of the technical problems, there is no code about arcpy driving GIS mapping.

I will never take the initiative to those who don't like me, because if I go after you, you will never like it. And if you take the initiative, I think you are good, then I will run straight to you and give you all the good.

18 original articles published, 24 praised, 3981 visited
Private letter follow

Tags: Python Excel Programming Attribute

Posted on Mon, 20 Jan 2020 00:33:12 -0500 by ziola