def run(self): for year in self._years: for feature in self._feature_collection.get_features(): path = feature.get('PATH') row = feature.get('ROW') geometry = ee.Geometry.MultiPolygon(feature.get('GEOMETRY')) image_collection = self._collection.filter(ee.Filter.eq('WRS_PATH', int(path))).filter(ee.Filter.eq('WRS_ROW', int(row))) for period in self._periods: images_by_period = ImageCollection(image_collection).filter_by_period(year, feature.get(period), self._offset) if self._apply_brdf: images_by_period = images_by_period.apply_brdf() if self._clip_geometry: images_by_period = images_by_period.clip_geometry() if self._apply_mask: images_by_period = images_by_period.apply_qamask() if self._bands: images_by_period = images_by_period.apply_bands(self._bands) if self._reducers: image_reduced = Image(ImageCollection(images_by_period).apply_reducers(self._reducers)) image_reduced = image_reduced.rename(image_reduced.bandNames().map( lambda band: ee.String(period).cat('_').cat(band)) ) final_name = "{0}_{1}_{2}".format(settings.COLLECTION_PREFIX, "{0}{1}".format(path, row), str(year)) final_image = image_reduced.clip(geometry).select(settings.GENERATION_VARIABLES + settings.GENERATION_EXTRA_VARIABLES).set('year', year).set('system:footprint', geometry) self.add_image_in_batch(final_name, {"image": final_image, "year": int(year), "path": int(path), "row": int(row), "geometry": geometry}) else: images_by_period = images_by_period.map( lambda image: Image(image).rename(Image(image).bandNames().map( lambda band: ee.String(period).cat('_').cat(band)) ) ) images_by_period = images_by_period.toList(settings.MAX_IMAGES) for i in xrange(images_by_period.size().getInfo()): image = Image(images_by_period.get(i)) date = image.date().format('yyyyMMdd').getInfo() final_name = "{0}_{1}_{2}".format(settings.COLLECTION_PREFIX, "{0}{1}".format(path, row), str(date)) final_image = Image(image).clip(geometry).select(settings.GENERATION_VARIABLES + settings.GENERATION_EXTRA_VARIABLES).set('year', year).set('system:footprint', geometry) self.add_image_in_batch(final_name, {"image": final_image, "year": int(year), "path": int(path), "row": int(row), "geometry": geometry})
def apply_temporal_filter(self, collection, offset, initial_threshold): collection = collection.toList(settings.MAX_IMAGES) new_collection = ImageCollection([]) for index in xrange(len(self.__years)): left, center, right, threshold = self.__break_list( collection, index, offset, 0, len(self.__years) - 1, initial_threshold) year = self.__years[index] roi = center.geometry() filename = "{0}_{1}".format(self.__collection_prefix, str(year)) center = center.unmask(None).eq(self.__class_of_reference) left = ImageCollection(left) right = ImageCollection(right) sides = ImageCollection(left.merge(right)).map(lambda image: Image( image).eq(self.__class_of_reference)).sum() mask = center.add(sides.eq(0)).neq(2) image = center.add(sides).gte(threshold + 1) filtered_image = Image(center.add(image)).updateMask(mask).gte(1) filtered_image = filtered_image.clip(roi).set( 'system:index', filename).set('year', year).set('system:footprint', roi) new_collection = new_collection.merge( ImageCollection(filtered_image)) final_name = filename final_image = filtered_image self.add_image_in_batch(final_name, { "image": final_image, "year": int(year), "geometry": roi }) return new_collection