Skip to content

Commit

Permalink
Merge pull request #23 from nens/casper-merge-shapefile
Browse files Browse the repository at this point in the history
Fix merge_files for shapefiles without tiling
  • Loading branch information
arjanverkerk committed Dec 20, 2019
2 parents c676390 + 6fa9896 commit 277f541
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
15 changes: 9 additions & 6 deletions dask_geomodeling/geometry/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,21 @@ def merge_files(path, target, remove_source=False):
if os.path.exists(target):
raise IOError("Target '{}' already exists".format(target))

ext = os.path.splitext(target)[1]
target_base, ext = os.path.splitext(target)
source_paths = glob.glob(os.path.join(path, '*' + ext))
if len(source_paths) == 0:
raise IOError(
"No source files found with matching extension '{}'".format(ext)
)
elif len(source_paths) == 1:
# shortcut for single file
if remove_source:
shutil.move(source_paths[0], target)
else:
shutil.copy(source_paths[0], target)
# shortcut for single file. we need to copy/move all base_name.*
# files (e.g. shapefiles have multiple files)
source_base = os.path.splitext(source_paths[0])[0]
move_or_copy = shutil.move if remove_source else shutil.copy
for file_path in glob.glob(source_base + '.*'):
move_or_copy(
file_path, target_base + os.path.splitext(file_path)[1]
)
return

with utils.fiona_env():
Expand Down
19 changes: 17 additions & 2 deletions dask_geomodeling/tests/test_geometry_sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,31 @@ def test_with_tiler(self):
assert len(df) == 1
assert df.crs["init"] == "epsg:3857"

def test_to_file(self):
def test_to_file_geojson(self):
self.source.to_file(self.path + ".geojson", **self.request)
actual = gpd.read_file(self.path + ".geojson")
# compare dataframes without checking the order of records / columns
assert_frame_equal(actual, self.expected, check_like=True)

def test_to_file_with_tiling(self):
def test_to_file_shapefile(self):
self.source.to_file(self.path + ".shp", **self.request)
actual = gpd.read_file(self.path + ".shp")
# compare dataframes without checking the order of records / columns
assert_frame_equal(actual, self.expected, check_like=True)

def test_to_file_with_tiling_geojson(self):
self.source.to_file(
self.path + ".geojson", tile_size=10, **self.request_tiled
)
actual = gpd.read_file(self.path + ".geojson")
# because we lose the index in the saving process, just check the len
assert len(actual) == 2

def test_to_file_with_tiling_shapefile(self):
self.source.to_file(
self.path + ".shp", tile_size=10, **self.request_tiled
)
actual = gpd.read_file(self.path + ".shp")
# because we lose the index in the saving process, just check the len
assert len(actual) == 2

0 comments on commit 277f541

Please sign in to comment.