Clipping a Shapefile in Pure Python

by user7512228   Last Updated August 01, 2020 12:22 PM

I am trying to implement the code found at this site:

The function I am trying to implement:

import shapefile

# Create a reader instance for our US Roads shapefile
r = shapefile.Reader("roadtrl020") 

# Create a writer instance copying the reader's shapefile type
w = shapefile.Writer(r.shapeType)

# Copy the database fields to the writer
w.fields = list(r.fields)

# Our selection box that contains Puerto Rico
xmin = -67.5
xmax = -65.0
ymin = 17.8
ymax = 18.6

# Iterate through the shapes and attributes at the same time
for road in r.iterShapeRecords():
    # Shape geometry
    geom = road.shape
    # Database attributes 
    rec = road.record
    # Get the bounding box of the shape (a single road)
    sxmin, symin, sxmax, symax = geom.bbox
    # Compare it to our Puerto Rico bounding box.
    # go to the next road as soon as a coordinate is outside the box
    if sxmin <  xmin: continue
    elif sxmax > xmax: continue
    elif symin < ymin: continue
    elif symax > ymax: continue
    # Road is inside our selection box.
    # Add it to the new shapefile
# Save the new shapefile! (.shp, .shx, .dbf)"Puerto_Rico_Roads")

I am getting the following error: AttributeError: 'Writer' object has no attribute '_shapes'

I think this issue may be connected to the issue discussed here:

However when I convert the lines:


to something like:


This does not help. And I am unsure of how to append multiple records or shapes to the writer.

Related Questions

Using pyshp to convert .csv file to .shp?

Updated March 29, 2017 07:22 AM