Search code examples
pythongisshapesshapefile

Pyshp: PolyLineZ drawing draws lines between my lines


My lines are connecting, even though I have not set them to polygons.

I am basing my script on the pyshp package.

My script looks like this:

w=Shapefile.Writer()
#shapetype 11 is a polylineZ
w.poly(parts=[listOfCoordinates], shapeType = 11)
w.record(this,and,that)
w.save(file)

The problem is that when I generate multiple poly's Qgis which I open them in draws a line between them. Example:

One line goes from A to B.

Another line goes from C to D

For some reason Qgis draws a line between B and C. I think this has to do with the pyshp handing of the shapefile. More specific the 'bbox' field which sets the bounderies for each shape.

The solution would make the line between B and C go away.


Solution

  • You are probably not nesting the parts lists properly. I assume you're trying to create a multi-part polylineZ shapefile where the lines share a single dbf record. Also the polylineZ type is actually 13 and not 11.

    The following code creates two shape files with three parallel lines each. In this example I'm not bothering with the Z coordinate. The first shapefile is a multi-part like I assume you're creating. The second shapefile is gives each line its own record. Both use the same line geometries.

    import shapefile
    
    # Create a polylineZ shapefile writer
    w = shapefile.Writer(shapeType = 13)
    # Create a field called "Name"
    w.field("NAME")
    # Create 3 parallel, 2-point lines
    line_A = [[5, 5], [10, 5]]
    line_B = [[5, 15], [10, 15]]
    line_C = [[5, 25], [10, 25]]
    # Write all 3 as a multi-part shape
    # sharing one record
    w.poly(parts=[line_A, line_B, line_C])
    # Give the shape a name attribute
    w.record("Multi Example")
    # save
    w.save("multi_part")
    
    # Create another polylineZ shapefile writer
    w = shapefile.Writer(shapeType = 13)
    # Create a field called "Name"
    w.field("NAME")
    # This time write each line separately
    # with its own dbf record
    w.poly(parts=[line_A])
    w.record("Line A")
    w.poly(parts=[line_B])
    w.record("Line B")
    w.poly(parts=[line_C])
    w.record("Line C")
    # Save
    w.save("single_parts")