Skip to content

Commit

Permalink
reducing code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
ungarj committed Apr 5, 2017
1 parent 8e98cad commit e527dec
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 40 deletions.
21 changes: 9 additions & 12 deletions mapchete/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def __init__(self, config, with_cache=False):
config = MapcheteConfig(config)
assert isinstance(config, MapcheteConfig)
self.config = config
config.output
# TODO assert this line is really not necessary
# config.output
py_compile.compile(self.config.process_file, doraise=True)
self.process_name = os.path.splitext(
os.path.basename(self.config.process_file))[0]
Expand Down Expand Up @@ -189,17 +190,13 @@ def write(self, process_tile):
error = "no errors"
else:
message = "write"
try:
if self.config.mode == "continue" and (
self.config.output.tiles_exist(process_tile)
):
error = "exists, not overwritten"
else:
self.config.output.write(copy(process_tile))
error = "no errors"
except Exception as e:
raise
error = e
if self.config.mode == "continue" and (
self.config.output.tiles_exist(process_tile)
):
error = "exists, not overwritten"
else:
self.config.output.write(copy(process_tile))
error = "no errors"
endtime = time.time()
elapsed = "%ss" % (round((endtime - starttime), 3))
LOGGER.info(
Expand Down
1 change: 0 additions & 1 deletion mapchete/cli/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Command line utility to execute a Mapchete process."""

import os
import argparse
from functools import partial
from multiprocessing import cpu_count
from multiprocessing.pool import Pool
Expand Down
6 changes: 3 additions & 3 deletions mapchete/formats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def available_output_formats():
v.load().OutputData.METADATA["driver_name"])
except AttributeError:
pass
except:
except Exception:
raise
return output_formats

Expand All @@ -45,7 +45,7 @@ def available_input_formats():
for v in pkg_resources.iter_entry_points(_DRIVERS_ENTRY_POINT):
try:
input_formats.append(v.load().InputData.METADATA["driver_name"])
except:
except Exception:
pass
return input_formats

Expand Down Expand Up @@ -139,7 +139,7 @@ def _file_ext_to_driver():
mapping[ext].append(driver_name)
else:
mapping[ext] = [driver_name]
except:
except Exception:
pass
if not mapping:
raise RuntimeError("no drivers could be found")
Expand Down
6 changes: 3 additions & 3 deletions mapchete/formats/default/png.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def read(self, output_tile):
data=data,
mask=np.stack((mask, mask, mask, ))
)
except:
except Exception:
output_tile.data = self.empty(output_tile)
return output_tile

Expand Down Expand Up @@ -286,7 +286,7 @@ def prepare_data(self, data):
assert band.shape == band.mask.shape
out_data += (band, )
out_mask += (band.mask, )
except:
except AssertionError:
out_data += (band.data, )
out_mask += (
np.where(band.data == self.nodata, True, False), )
Expand All @@ -306,7 +306,7 @@ def prepare_data(self, data):
try:
assert data.shape == data.mask.shape
return data.astype("uint8")
except:
except AssertionError:
return ma.MaskedArray(
data=data.astype("uint8"),
mask=np.where(data == self.nodata, True, False),
Expand Down
7 changes: 2 additions & 5 deletions mapchete/io/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def read_raster_window(input_file, tile, indexes=None, resampling="nearest"):
for band_idx in band_indexes:
yield _get_warped_edge_array(
tile=tile, input_file=input_file, band_idx=band_idx,
dst_bounds=tile.bounds, dst_shape=tile.shape,
dst_affine=tile.affine, dst_crs=tile.crs, resampling=resampling
)
resampling=resampling)

# If tile boundaries don't exceed pyramid boundaries, simply read window
# once.
Expand All @@ -80,8 +78,7 @@ def read_raster_window(input_file, tile, indexes=None, resampling="nearest"):


def _get_warped_edge_array(
tile=None, input_file=None, band_idx=None, dst_bounds=None, dst_shape=None,
dst_affine=None, dst_crs=None, resampling="nearest"
tile=None, input_file=None, band_idx=None, resampling="nearest"
):
tile_boxes = clip_geometry_to_srs_bounds(
tile.bbox, tile.tile_pyramid, multipart=True)
Expand Down
27 changes: 11 additions & 16 deletions mapchete/io/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ def reproject_geometry(
try:
bbox_intersection = crs_bbox.intersection(geometry_4326)
except TopologicalError:
try:
bbox_intersection = crs_bbox.intersection(
geometry_4326.buffer(0)
)
warnings.warn("geometry fixed after clipping")
except:
raise
bbox_intersection = crs_bbox.intersection(
geometry_4326.buffer(0)
)
warnings.warn("geometry fixed after clipping")
# clip geometry dst_crs boundaries
return _reproject_geom(
bbox_intersection,
Expand Down Expand Up @@ -115,7 +112,7 @@ def _reproject_geom(
if validity_check:
try:
assert out_geom.is_valid
except:
except AssertionError:
raise RuntimeError("invalid geometry after reprojection")
return out_geom

Expand Down Expand Up @@ -143,7 +140,7 @@ def read_vector_window(input_file, tile, validity_check=True):
"""
try:
assert os.path.isfile(input_file)
except:
except AssertionError:
raise IOError("input file does not exist: %s" % input_file)
# Check if potentially tile boundaries exceed tile matrix boundaries on
# the antimeridian, the northern or the southern boundary.
Expand All @@ -158,10 +155,9 @@ def read_vector_window(input_file, tile, validity_check=True):
tile.bbox, tile.tile_pyramid, multipart=True)
return chain.from_iterable(
_get_reprojected_features(
input_file=input_file, dst_bounds=bbox.bounds, dst_crs=tile.crs,
validity_check=validity_check)
for bbox in tile_boxes
)
input_file=input_file, dst_bounds=bbox.bounds,
dst_crs=tile.crs, validity_check=validity_check)
for bbox in tile_boxes)
else:
features = _get_reprojected_features(
input_file=input_file, dst_bounds=tile.bounds,
Expand Down Expand Up @@ -199,7 +195,7 @@ def write_vector_window(in_tile, out_schema, out_tile, out_path):
if clipped.geom_type != target_type:
try:
out_geom = clean_geometry_type(clipped, target_type)
except:
except Exception:
warnings.warn("failed geometry cleaning during writing")
continue
if out_geom:
Expand Down Expand Up @@ -336,5 +332,4 @@ def extract_from_tile(in_tile, out_tile):
return [
feature
for feature in in_tile.data
if shape(feature["geometry"]).touches(out_tile.bbox)
]
if shape(feature["geometry"]).touches(out_tile.bbox)]

0 comments on commit e527dec

Please sign in to comment.