Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise ValueError('generatecrs missing bounds and file crs')
is_longlat = _is_longlat(file_crs)
if method == 'default':
# Check if file_crs _is_longlat, if not use that.
# Option to continue returning default if we didn't get a file_crs
if is_longlat:
method = 'local'
else:
return fiona.crs.to_string(file_crs)
if is_longlat:
longlat_bounds = bounds
else:
longlat_bounds = bounding.transform(file_crs, DEFAULT_GEOID, bounds)
minx, miny, maxx, maxy = longlat_bounds
if method == 'utm':
midx = (minx + maxx) / 2
midy = (miny + maxy) / 2
return utm_proj4(midx, midy)
elif method == 'local':
# Create a custom TM projection
x0 = (float(minx) + float(maxx)) // 2
return tm_proj4(x0, miny, maxy)
def testConvertBbox(self):
bounds = (-100, -100, 100, 100)
self.assertSequenceEqual(bounding.pad(bounds, ext=100), (-200, -200, 200, 200))
self.assertSequenceEqual(bounding.pad(bounds, ext=10), (-110, -110, 110, 110))
def test_updatebounds(self):
bounds1 = (None, 0.1, None, 1.1)
bounds2 = (0.2, 0.2, 1.2, 1.2)
bounds3 = (0.1, 0.3, 1.5, 1.1)
bounds4 = (0.05, 0.4, float('inf'), 1.2)
bounds5 = (0.05, -1 * float('inf'), 1.4, 1.2)
self.assertSequenceEqual(bounding.update(bounds1, bounds2), (0.2, 0.1, 1.2, 1.2))
self.assertSequenceEqual(bounding.update(bounds3, bounds2), (0.1, 0.2, 1.5, 1.2))
self.assertSequenceEqual(bounding.update(bounds3, bounds4), (0.05, 0.3, 1.5, 1.2))
self.assertSequenceEqual(bounding.update(bounds3, bounds5), (0.05, 0.3, 1.5, 1.2))
Args:
bounds (tuple): In WGS84 coordinates.
step (int): Distance between graticule lines, in the output projection.
crs_or_method (str): A projection specification.
Returns:
A generator that yields GeoJSON-like dicts of graticule features.
'''
if crs_or_method == 'file':
raise ValueError("'file' is not a valid option for projecting graticules.")
if crs_or_method:
out_crs = projection.pick(crs_or_method, bounds=bounds, file_crs=utils.DEFAULT_GEOID)
bounds = bounding.transform(utils.DEFAULT_GEOID, out_crs, bounds)
unproject = partial(fiona.transform.transform, out_crs, utils.DEFAULT_GEOID)
else:
def unproject(x, y):
return x, y
minx, miny, maxx, maxy = bounds
minx, miny = utils.modfloor(minx, step), utils.modfloor(miny, step)
maxx, maxy = utils.modceil(maxx, step), utils.modceil(maxy, step)
frange = partial(utils.frange, cover=True)
for i, X in enumerate(frange(minx, maxx + step, step), 1):
coords = unproject(*list(zip(*[(X, y) for y in frange(miny, maxy + step, step / 2.)])))
yield _feature(i, tuple(zip(*coords)), axis='x', coord=X)
def update_projected_bounds(self, in_crs, out_crs, bounds, padding=None):
'''
Extend projected_bounds bbox with self.padding.
Args:
in_crs (dict): CRS of bounds.
out_crs (dict) desired output CRS.
bounds (tuple): bounding box.
Returns:
(tuple) bounding box in out_crs coordinates.
'''
# This may happen many times if we were passed bounds, but it's a cheap operation.
projected = bounding.transform(in_crs, out_crs, bounds)
self._projected_bounds = bounding.pad(projected, padding or 0)
return self._projected_bounds
def bounds(layer, crs, latlon=False):
'''
Return the bounds for a given layer, optionally projected.
'''
meta = meta_complete(layer)
# If crs==file, these will basically be no ops.
out_crs = projection.pick(crs, meta['bounds'], file_crs=meta['crs'])
result = bounding.transform(meta['crs'], out_crs, meta['bounds'])
if latlon:
fmt = '{0[1]} {0[0]} {0[3]} {0[2]}'
else:
fmt = '{0[0]} {0[1]} {0[2]} {0[3]}'
click.echo(fmt.format(result), file=sys.stdout)
def _get_clipper(self, layer_bounds, out_bounds, scalar=None):
'''
Get a clipping function for the given input crs and bounds.
Args:
layer_bounds (tuple): The bounds of the layer.
out_bounds (tuple): The desired output bounds (in layer coordinates).
scalar (float): Map scale.
Returns:
None if layer_bounds are inside out_bounds or clipping is off.
'''
if not self.clip or bounding.covers(out_bounds, layer_bounds):
return None
scalar = scalar or self.scalar
if not self.clipper:
padded_bounds = bounding.pad(self.projected_bounds, 1000)
self.clipper = transform.clipper([c * scalar for c in padded_bounds])
return self.clipper
Args:
layer_bounds (tuple): The bounds of the layer.
out_bounds (tuple): The desired output bounds (in layer coordinates).
scalar (float): Map scale.
Returns:
None if layer_bounds are inside out_bounds or clipping is off.
'''
if not self.clip or bounding.covers(out_bounds, layer_bounds):
return None
scalar = scalar or self.scalar
if not self.clipper:
padded_bounds = bounding.pad(self.projected_bounds, 1000)
self.clipper = transform.clipper([c * scalar for c in padded_bounds])
return self.clipper
def __init__(self, files, bounds=None, crs=None, **kwargs):
self.log = logging.getLogger('svgis')
if isinstance(files, string_types):
self.files = [files]
elif isinstance(files, Iterable):
self.files = files
else:
raise ValueError("'files' must be a file name or list of file names")
self.log.info('starting SVGIS, files: %s', ', '.join(self.files))
if bounding.check(bounds):
self._unprojected_bounds = bounds
elif bounds:
self.log.warning("ignoring invalid bounds: %s", bounds)
# This may return a keyword, which will require more updating.
# If so, will update when files are open.
self._out_crs = projection.pick(crs)
self.scalar = kwargs.pop('scalar', 1) or 1
self.style = STYLE + (kwargs.pop('style', '') or '')
self.padding = kwargs.pop('padding', 0) or 0
self.precision = kwargs.pop('precision', None)