def _correctly_load_bbox(bbox, path, is_zipped=False): """ Helper method for loading old version of pickled BBox object :param bbox: BBox object which was incorrectly loaded with pickle :type bbox: sentinelhub.BBox :param path: Path to file where BBox object is stored :type path: str :param is_zipped: `True` if file is zipped and `False` otherwise :type is_zipped: bool :return: Correctly loaded BBox object :rtype: sentinelhub.BBox """ warnings.warn( "Bounding box of your EOPatch is saved in old format which in the future won't be supported " "anymore. Please save bounding box again, you can overwrite the existing one", DeprecationWarning, stacklevel=4) with gzip.open(path) if is_zipped else open(path, 'rb') as pickle_file: crs_cnt = -1 for _, arg, _ in pickletools.genops(pickle_file): if arg == 'sentinelhub.constants CRS': crs_cnt = 2 if crs_cnt == 0: return sentinelhub.BBox(tuple(bbox), sentinelhub.CRS(arg)) crs_cnt -= 1 raise ValueError( 'Failed to correctly load BBox object, try downgrading sentinelhub package to <=2.4.7' )
def _parse_feature_type_value(feature_type, value): """ Checks or parses value which will be assigned to a feature type attribute of `EOPatch`. If the value cannot be parsed correctly it raises an error. :raises: TypeError, ValueError """ if feature_type.has_dict() and isinstance(value, dict): return value if isinstance(value, _FeatureDict) else _FeatureDict( value, feature_type) if feature_type is FeatureType.BBOX: if value is None or isinstance(value, sentinelhub.BBox): return value if isinstance(value, (tuple, list)) and len(value) == 5: return sentinelhub.BBox(value[:4], crs=value[4]) if feature_type is FeatureType.TIMESTAMP: if isinstance(value, (tuple, list)): return [ timestamp if isinstance(timestamp, datetime.date) else dateutil.parser.parse(timestamp) for timestamp in value ] raise TypeError('Attribute {} requires value of type {} - ' 'failed to parse given value'.format( feature_type, feature_type.type()))
def get_gridded_boundary(shapefile, cell_size=1000): # Get its bounds from new projected bbox = shapefile.geometry.bounds # Get the bounding box of the area area_bbox = sentinelhub.BBox(bbox=[(bbox['minx'], bbox['miny']), (bbox['maxx'], bbox['maxy'])], crs=sentinelhub.CRS.WGS84) # Convert into UTM projection (in meters) area_bbox = sentinelhub.geo_utils.to_utm_bbox(area_bbox) shapefile = shapefile.to_crs(str(area_bbox.crs)) bbox = shapefile.geometry.bounds ###Build the grid def get_shape_area(bbox, distance=cell_size): ##Would like division into 100*100 patches # Number of vertical patch xmin - xmax c1 = int((bbox['maxx'] - bbox['minx']) / distance) # + int((bbox['maxx'] - bbox['minx'])%distance) # Number of horizontal patch xmin - xmax c2 = int((bbox['maxy'] - bbox['miny']) / distance) # + int((bbox['maxy'] - bbox['miny'])%distance) return ((c1, c2)) split_shape = get_shape_area(bbox, distance=cell_size) bbox_splitter = BBoxSplitter([area_bbox.geometry], area_bbox.crs, split_shape) bbox_list = np.array(bbox_splitter.get_bbox_list()) info_list = np.array(bbox_splitter.get_info_list()) # Prepare info of selected EOPatches geometry = [Polygon(bbox.get_polygon()) for bbox in bbox_list] # idxs = [info['index'] for info in info_list] idxs_x = [info['index_x'] for info in info_list] idxs_y = [info['index_y'] for info in info_list] gdf = gpd.GeoDataFrame({ 'index_x': idxs_x, 'index_y': idxs_y }, crs=shapefile.crs, geometry=geometry) gdf.reset_index(inplace=True) # Get the intersection of the contours from shapefile with the grid gdf['results'] = gdf.geometry.apply( lambda x: shapefile.geometry.intersection(x)) # Construct a boolean associated booleans = np.array([(1 - k.is_empty) for k in gdf.results]) gdf['check'] = booleans valid_obs = gdf.check.astype(bool) gdf = gdf.drop(['check'], axis=1) return gdf[valid_obs]
def get_all_scenes(polygon, dataset, start_datetime, end_datetime): mn = numpy.min(polygon, 0) mx = numpy.max(polygon, 0) search_bbox = sentinelhub.BBox(bbox=[mn[1], mn[0], mx[1], mx[0]], crs=sentinelhub.CRS.WGS84) start_str = start_datetime.strftime('%Y-%m-%dT%H:%M:%S') end_str = end_datetime.strftime('%Y-%m-%dT%H:%M:%S') search_time_interval = (start_str, end_str) wfs_iterator = sentinelhub.WebFeatureService( search_bbox, search_time_interval, data_source=sentinelhub.DataSource.SENTINEL2_L1C, maxcc=1.0, instance_id=INSTANCE_ID) return {(tn + date + str(n)): (tn, date, n) for tn, date, n in wfs_iterator.get_tiles()}