Python shapefile.Writer() Examples

The following are 9 code examples of shapefile.Writer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module shapefile , or try the search function .
Example #1
Source File: pyshpLargeWriter.py    From Learn with MIT License 7 votes vote down vote up
def __init__(self, filename=None, shapeType=1, hasShx=True):
    self.filename = filename
    self.hasShx = hasShx
    # Count records for metadata
    self.count = 0
    # Maximum number of records before disk flush
    self.max = 1000
    self.started = False
    self.minx = 0
    self.miny = 0
    self.maxx = 0
    self.maxy = 0
    self.numRecs = 0
    self.tmpShp = cStringIO.StringIO()
    if self.hasShx:
      self.tmpShx = cStringIO.StringIO()
    self.tmpDbf = cStringIO.StringIO()
    self.shp = open("%s.shp" % self.filename, "wb")
    if self.hasShx:
      self.shx = open("%s.shx" % self.filename, "wb")
    self.dbf = open("%s.dbf" % self.filename, "wb")
    self.dbfHdrLen = 0
    self.w = shapefile.Writer(shapeType) 
Example #2
Source File: test_geom_map.py    From plotnine with GNU General Public License v2.0 5 votes vote down vote up
def _point_file(test_file):
    with shapefile.Writer(test_file, shapefile.POINT) as shp:
        shp.field('name', 'C')

        shp.point(0, 0)
        shp.record('point1')

        shp.point(0, 1)
        shp.record('point2')

        shp.point(1, 1)
        shp.record('point3')

        shp.point(1, 0)
        shp.record('point4') 
Example #3
Source File: test_geom_map.py    From plotnine with GNU General Public License v2.0 5 votes vote down vote up
def _polygon_file(test_file):
    with shapefile.Writer(test_file, shapefile.POLYGON) as shp:
        shp.field('name', 'C')

        shp.poly([
            [[.25, -.25], [.25, .25], [.75, .25], [.75, -.25]],
        ])
        shp.record('polygon1')

        shp.poly([
            [[.25, .75], [.75, .75], [.5, 1.25]]
        ])
        shp.record('polygon2') 
Example #4
Source File: test_geom_map.py    From plotnine with GNU General Public License v2.0 5 votes vote down vote up
def _polyline_file(test_file):
    with shapefile.Writer(test_file, shapefile.POLYLINE) as shp:
        shp.field('name', 'C')

        n = 5
        x = np.repeat(np.linspace(0, 1, n), 2)
        y = np.tile([0.375, 0.625], n)
        shp.line([list(zip(x, y))])
        shp.record('line1') 
Example #5
Source File: test_read_shapefile.py    From landlab with MIT License 5 votes vote down vote up
def test_bad_points():
    shp = BytesIO()
    shx = BytesIO()
    dbf = BytesIO()
    w = shapefile.Writer(shp=shp, shx=shx, dbf=dbf)
    w.shapeType = 3
    w.field("spam", "N")
    w.line([[[5, 5], [10, 10]]])
    w.record(37)
    w.line([[[5, 0], [5, 5]]])
    w.record(100)
    w.line([[[5, 5], [0, 10]]])
    w.record(239)
    w.close()

    # pass a line shapefile here insted.
    p_shp = BytesIO()
    p_shx = BytesIO()
    p_dbf = BytesIO()
    p_w = shapefile.Writer(shp=p_shp, shx=p_shx, dbf=p_dbf)
    w.shapeType = 3
    p_w.field("spam", "N")
    p_w.line([[[5, 5], [10, 10]]])
    p_w.record(37)
    p_w.line([[[5, 0], [5, 5]]])
    p_w.record(100)
    p_w.line([[[5, 5], [0, 10]]])
    p_w.record(239)
    p_w.close()

    with raises(ValueError):
        read_shapefile(shp, dbf=dbf, points_shapefile=p_shp, points_dbf=p_dbf) 
Example #6
Source File: test_read_shapefile.py    From landlab with MIT License 5 votes vote down vote up
def test_points_but_too_far():
    shp = BytesIO()
    shx = BytesIO()
    dbf = BytesIO()
    w = shapefile.Writer(shp=shp, shx=shx, dbf=dbf)
    w.shapeType = 3
    w.field("spam", "N")
    w.line([[[5, 5], [10, 10]]])
    w.record(37)
    w.line([[[5, 0], [5, 5]]])
    w.record(100)
    w.line([[[5, 5], [0, 10]]])
    w.record(239)
    w.close()

    # make a
    p_shp = BytesIO()
    p_shx = BytesIO()
    p_dbf = BytesIO()
    p_w = shapefile.Writer(shp=p_shp, shx=p_shx, dbf=p_dbf)
    p_w.shapeType = 1
    p_w.field("eggs", "N")
    p_w.point(5, 0)
    p_w.record(2)
    p_w.point(5, 5)
    p_w.record(4)
    p_w.point(0, 10)
    p_w.record(8)
    p_w.point(12, 10)
    p_w.record(6)
    p_w.close()

    with raises(ValueError):
        read_shapefile(shp, dbf=dbf, points_shapefile=p_shp, points_dbf=p_dbf) 
Example #7
Source File: test_read_shapefile.py    From landlab with MIT License 5 votes vote down vote up
def test_points_but_not_one_one():
    shp = BytesIO()
    shx = BytesIO()
    dbf = BytesIO()
    w = shapefile.Writer(shp=shp, shx=shx, dbf=dbf)
    w.shapeType = 3
    w.field("spam", "N")
    w.line([[[5, 5], [10, 10]]])
    w.record(37)
    w.line([[[5, 0], [5, 5]]])
    w.record(100)
    w.line([[[5, 5], [0, 10]]])
    w.record(239)
    w.close()

    # make a
    p_shp = BytesIO()
    p_shx = BytesIO()
    p_dbf = BytesIO()
    p_w = shapefile.Writer(shp=p_shp, shx=p_shx, dbf=p_dbf)
    p_w.shapeType = 1
    p_w.field("eggs", "N")
    p_w.point(5, 0)
    p_w.record(2)
    p_w.point(5, 5)
    p_w.record(4)
    p_w.point(0, 10)
    p_w.record(8)
    p_w.point(10, 10)
    p_w.record(6)
    p_w.point(10, 10)
    p_w.record(7)
    p_w.close()

    with raises(ValueError):
        read_shapefile(shp, dbf=dbf, points_shapefile=p_shp, points_dbf=p_dbf) 
Example #8
Source File: test_read_shapefile.py    From landlab with MIT License 5 votes vote down vote up
def test_points_but_one_missing():
    shp = BytesIO()
    shx = BytesIO()
    dbf = BytesIO()
    w = shapefile.Writer(shp=shp, shx=shx, dbf=dbf)
    w.shapeType = 3
    w.field("spam", "N")
    w.line([[[5, 5], [10, 10]]])
    w.record(37)
    w.line([[[5, 0], [5, 5]]])
    w.record(100)
    w.line([[[5, 5], [0, 10]]])
    w.record(239)
    w.close()

    # make a
    p_shp = BytesIO()
    p_shx = BytesIO()
    p_dbf = BytesIO()
    p_w = shapefile.Writer(shp=p_shp, shx=p_shx, dbf=p_dbf)
    p_w.shapeType = 1
    p_w.field("eggs", "N")
    p_w.point(5, 0)
    p_w.record(2)
    p_w.point(5, 5)
    p_w.record(4)
    p_w.point(0, 10)
    p_w.record(8)
    p_w.close()

    with raises(ValueError):
        read_shapefile(shp, dbf=dbf, points_shapefile=p_shp, points_dbf=p_dbf) 
Example #9
Source File: spatial.py    From swmmio with MIT License 5 votes vote down vote up
def write_shapefile(df, filename, geomtype='line', prj=None):
    """
    create a shapefile given a pandas Dataframe that has coordinate data in a
    column called 'coords'.
    """

    import shapefile
    df['Name'] = df.index

    # create a shp file writer object of geom type 'point'
    if geomtype == 'point':
        w = shapefile.Writer(shapefile.POINT)
    elif geomtype == 'line':
        w = shapefile.Writer(shapefile.POLYLINE)
    elif geomtype == 'polygon':
        w = shapefile.Writer(shapefile.POLYGON)

    # use the helper mode to ensure the # of records equals the # of shapes
    # (shapefile are made up of shapes and records, and need both to be valid)
    w.autoBalance = 1

    # add the fields
    for fieldname in df.columns:
        w.field(fieldname, "C")

    for k, row in df.iterrows():
        w.record(*row.tolist())
        w.line(parts=[row.coords])

    w.save(filename)

    # add projection data to the shapefile,
    if prj is None:
        # if not sepcified, the default, projection is used (PA StatePlane)
        prj = os.path.join(ROOT_DIR, 'swmmio/defs/default.prj')
    prj_filepath = os.path.splitext(filename)[0] + '.prj'
    shutil.copy(prj, prj_filepath)