Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update pyshp API refernce in export_to_shapefile #138

Merged
merged 1 commit into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions swmmio/tests/test_spatial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
import tempfile
import os

from swmmio.examples import philly


class TestSpatialFunctions(unittest.TestCase):
def setUp(self) -> None:
self.test_dir = tempfile.gettempdir()

def test_write_shapefile(self):
with tempfile.TemporaryDirectory() as tmp_dir:

philly.export_to_shapefile(tmp_dir)
nodes_path = os.path.join(tmp_dir, f'{philly.name}_nodes.shp')
links_path = os.path.join(tmp_dir, f'{philly.name}_conduits.shp')

self.assertTrue(os.path.exists(nodes_path))
self.assertTrue(os.path.exists(links_path))
17 changes: 8 additions & 9 deletions swmmio/utils/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,24 @@ def write_shapefile(df, filename, geomtype='line', prj=None):

# create a shp file writer object of geom type 'point'
if geomtype == 'point':
w = shapefile.Writer(shapefile.POINT)
w = shapefile.Writer(filename, shapefile.POINT, autoBalance=True)
elif geomtype == 'line':
w = shapefile.Writer(shapefile.POLYLINE)
w = shapefile.Writer(filename, shapefile.POLYLINE, autoBalance=True)
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
w = shapefile.Writer(filename, shapefile.POLYGON, autoBalance=True)

# 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])
if geomtype == 'line':
w.line([row.coords])
if geomtype == 'point':
w.point(*row.coords[0])

w.save(filename)
w.close()

# add projection data to the shapefile,
if prj is None:
Expand Down