Example of how to write shapefiles using pyshp

  The Python Shapefile Library (PyShp) provides read and write support for the Esri Shapefile file format. The Shapefile format is a popular GIS vector data format created by Esri. Shapefiles are used to describe geometric objects: points, polylines and polygons.
  Shapefile refers to a method of file storage. In fact, this file format is composed of multiple files. Among them, to form a Shapefile, three files are essential, they are ".shp", ".shx" and ".dbf" files. A group of files representing the same data should have the same filename prefix. For example, to store the geometry and attribute data about a lake, there must be three files: lake.shp, lake.shx and lake.dbf. The suffix of the "real" Shapefile is shp, but only this file data is incomplete, and the other two must be attached to form a complete set of geographic data. In addition to these three required files, there are eight optional files that can be used to enhance the expressiveness of spatial data. All file names must follow the MS DOS 8.3 file name standard (8 characters for the file prefix and 3 characters for the suffix, such as shapefil.shp) to facilitate compatibility with some old applications, although many new programs now support long file names. Also, all files must be located in the same directory.

pyshp github address: https://github.com/GeospatialPython/pyshp
pyshp official documentation homepage: https://pypi.org/project/pyshp/

from ctypes import pointer
import shapefile

# Shape types are represented by numbers between 0 and 31 as defined 
# by the shapefile specification and listed below. 
# It is important to note that the numbering system has several reserved numbers 
# that have not been used yet, 
# therefore the numbers of the existing shape types are not sequential:
#     NULL = 0
#     POINT = 1
#     POLYLINE = 3
#     POLYGON = 5
#     MULTIPOINT = 8
#     POINTZ = 11
#     POLYLINEZ = 13
#     POLYGONZ = 15
#     MULTIPOINTZ = 18
#     POINTM = 21
#     POLYLINEM = 23
#     POLYGONM = 25
#     MULTIPOINTM = 28
#     MULTIPATCH = 31


# The field names of a shapefile are available as soon as you read a shapefile. 
# You can call the "fields" attribute of the shapefile as a Python list. 
# Each field is a Python list with the following information:
#     Field name: the name describing the data at this column index.
#     Field type: the type of data at this column index. Types can be:
#         "C": Characters, text.
#         "N": Numbers, with or without decimals.
#         "F": Floats (same as "N").
#         "L": Logical, for boolean True/False values.
#         "D": Dates.
#         "M": Memo, has no meaning within a GIS and is part of the xbase spec instead.
#     Field length: the length of the data found at this column index. 
#         Older GIS software may truncate this length to 8 or 11 characters for "Character" fields.
#     Decimal length: the number of decimal places found in "Number" fields.


def write_points_shp(file_dir, points):
    point_w = shapefile.Writer(file_dir, shapefile.POINT)
    point_w.autoBalance = True

    # point: 0: x
    #        1: y
    #        2: z
    #        3: id
    #        4: name
    #        5: class
    point_w.field('IMAGE_ID', 'N')
    point_w.field('IMAGE_NAME', 'C')
    point_w.field('CLASS', 'N')    # point belong to which class
    for ptid, point in points.items():
        point_w.point(point[0], point[1])
        point_w.record(IMAGE_ID=ptid, IMAGE_NAME=point[4], CLASS=point[5])

    point_w.close()

def write_lines_shp(file_dir, lines):
    line_w = shapefile.Writer(file_dir, shapeType=shapefile.POLYLINE)
    line_w.autoBalance = True
    
    # line: 0: point1 []
    #       1: point2 []
    #       2: point1_id
    #       3: point2_id
    #       4: id
    #       5: class, 0 stands for inter_line
    line_w.field('LINE_ID', 'N')
    line_w.field('CLASS', 'N')
    for liid, line in lines.items():
        line_w.line([line[0], line[1]])
        line_w.record(LINE_ID=line[4], CLASS=line[5])
    
    line_w.close()

Guess you like

Origin blog.csdn.net/weixin_44120025/article/details/130979574