def create_grid_mapping_variable(nco, crs): if crs.geographic: crs_var = _create_latlon_grid_mapping_variable(nco, crs) elif crs.projected: crs_var = _create_projected_grid_mapping_variable(nco, crs) else: raise ValueError('Unknown CRS') crs_var.semi_major_axis = crs.semi_major_axis crs_var.semi_minor_axis = crs.semi_minor_axis crs_var.inverse_flattening = crs.inverse_flattening crs_var.crs_wkt = crs.wkt crs_var.spatial_ref = crs.wkt dims = crs.dimensions xres, xoff = data_resolution_and_offset(nco[dims[1]]) yres, yoff = data_resolution_and_offset(nco[dims[0]]) crs_var.GeoTransform = [xoff, xres, 0.0, yoff, 0.0, yres] left, right = nco[dims[1]][0]-0.5*xres, nco[dims[1]][-1]+0.5*xres bottom, top = nco[dims[0]][0]-0.5*yres, nco[dims[0]][-1]+0.5*yres points = [[left, bottom], [left, top], [right, top], [right, bottom]] _write_geographical_extents_attributes(nco, GeoPolygon(points, crs)) return crs_var
def create_grid_mapping_variable(nco, crs, name=DEFAULT_GRID_MAPPING): if crs.geographic: crs_var = _create_latlon_grid_mapping_variable(nco, crs, name) elif crs.projected: crs_var = _create_projected_grid_mapping_variable(nco, crs, name) else: raise ValueError('Unknown CRS') # mark crs variable as a coordinate coords = getattr(nco, 'coordinates', None) coords = [] if coords is None else coords.split(',') if name not in coords: coords.append(name) nco.coordinates = ','.join(coords) crs_var.semi_major_axis = crs.semi_major_axis crs_var.semi_minor_axis = crs.semi_minor_axis crs_var.inverse_flattening = crs.inverse_flattening crs_var.crs_wkt = crs.wkt crs_var.spatial_ref = crs.wkt dims = crs.dimensions xres, xoff = data_resolution_and_offset(nco[dims[1]]) yres, yoff = data_resolution_and_offset(nco[dims[0]]) crs_var.GeoTransform = [xoff, xres, 0.0, yoff, 0.0, yres] left, right = nco[dims[1]][0] - 0.5 * xres, nco[dims[1]][-1] + 0.5 * xres bottom, top = nco[dims[0]][0] - 0.5 * yres, nco[dims[0]][-1] + 0.5 * yres _write_geographical_extents_attributes( nco, geometry.box(left, bottom, right, top, crs=crs)) return crs_var
def create_grid_mapping_variable(nco, crs): if crs.geographic: crs_var = _create_latlon_grid_mapping_variable(nco, crs) elif crs.projected: crs_var = _create_projected_grid_mapping_variable(nco, crs) else: raise ValueError('Unknown CRS') crs_var.semi_major_axis = crs.semi_major_axis crs_var.semi_minor_axis = crs.semi_minor_axis crs_var.inverse_flattening = crs.inverse_flattening crs_var.crs_wkt = crs.wkt crs_var.spatial_ref = crs.wkt dims = crs.dimensions xres, xoff = data_resolution_and_offset(nco[dims[1]]) yres, yoff = data_resolution_and_offset(nco[dims[0]]) crs_var.GeoTransform = [xoff, xres, 0.0, yoff, 0.0, yres] left, right = nco[dims[1]][0] - 0.5 * xres, nco[dims[1]][-1] + 0.5 * xres bottom, top = nco[dims[0]][0] - 0.5 * yres, nco[dims[0]][-1] + 0.5 * yres _write_geographical_extents_attributes( nco, geometry.box(left, bottom, right, top, crs=crs)) return crs_var
def calculate_bounds_geotransform(dataset): # Convert rasterio CRS into datacube CRS object if isinstance(dataset.crs, xr.DataArray): crs_dict = dataset.crs.to_dict() _crs = CRS(str(crs_dict['attrs']['spatial_ref'])) # Leave the CRS as it is (datacube CRS object) elif isinstance(dataset.crs, datacube.utils.geometry._base.CRS): _crs = dataset.crs else: raise Exception( 'dataset.crs datatype not know (please check calculate_bounds_geotransform)' ) #crs_dict = dataset.crs.to_dict() #_crs = CRS(str(crs_dict['attrs']['spatial_ref'])) dims = _crs.dimensions xres, xoff = data_resolution_and_offset(dataset[dims[1]]) yres, yoff = data_resolution_and_offset(dataset[dims[0]]) GeoTransform = [xoff, xres, 0.0, yoff, 0.0, yres] left, right = dataset[dims[1]][0] - 0.5 * xres, dataset[ dims[1]][-1] + 0.5 * xres bottom, top = dataset[dims[0]][0] - 0.5 * yres, dataset[ dims[0]][-1] + 0.5 * yres return { 'left': left, 'right': right, 'bottom': bottom, 'top': top, 'GeoTransform': GeoTransform }
def _xarray_affine(obj): crs = _norm_crs(obj.crs) if crs is None: return None dims = crs.dimensions xres, xoff = data_resolution_and_offset(obj[dims[1]].values) yres, yoff = data_resolution_and_offset(obj[dims[0]].values) return Affine.translation(xoff, yoff) * Affine.scale(xres, yres)
def _xarray_affine(obj): crs = _norm_crs(_get_crs(obj)) if crs is None: return None dims = crs.dimensions try: xres, xoff = data_resolution_and_offset(obj[dims[1]].values) yres, yoff = data_resolution_and_offset(obj[dims[0]].values) except ValueError: xres = obj[dims[1]].attrs['resolution'] xoff = obj[dims[1]].values[0] - xres / 2 yres = obj[dims[0]].attrs['resolution'] yoff = obj[dims[0]].values[0] - yres / 2 return Affine.translation(xoff, yoff) * Affine.scale(xres, yres)
def _xarray_affine(obj): dims = obj.crs.dimensions xres, xoff = data_resolution_and_offset(obj[dims[1]].values) yres, yoff = data_resolution_and_offset(obj[dims[0]].values) return Affine.translation(xoff, yoff) * Affine.scale(xres, yres)
def transform(self): dims = self.crs.dimensions xres, xoff = data_resolution_and_offset(self.dataset[dims[1]]) yres, yoff = data_resolution_and_offset(self.dataset[dims[0]]) return Affine.translation(xoff, yoff) * Affine.scale(xres, yres)