Exemplo n.º 1
0
    def apply_spatial_filter(self, collection, kernel, threshold):
        kernel = ee.Kernel.fixed(len(kernel[0]), len(kernel), kernel,
                                 -(len(kernel[0]) / 2), -(len(kernel) / 2),
                                 False)
        new_collection = ImageCollection([])
        for year in self.__years:
            image = Image(
                collection.filter(ee.Filter.eq('year',
                                               year)).first()).unmask(None)
            filename = "{0}_{1}".format(self.__collection_prefix, str(year))
            roi = image.geometry()

            filtered_image = image.convolve(kernel).gte(threshold)

            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.updateMask(filtered_image.eq(1))
            self.add_image_in_batch(final_name, {
                "image": final_image,
                "year": int(year),
                "geometry": roi
            })

        return new_collection
Exemplo n.º 2
0
    def run(self):
        features = self._feature_collection.get_features()
        for year in self._years:
            for feature in features:
                path = feature.get('PATH')
                row = feature.get('ROW')
                geometry = ee.Geometry.MultiPolygon(feature.get('GEOMETRY'))

                neighbors = self.get_neighbors(features, int(path), int(row))
                neighbors_paths = map(lambda x: x[0], neighbors)
                neighbors_rows = map(lambda x: x[1], neighbors)
                neighbors_geometry = ee.Geometry.MultiPolygon(map(lambda x: x[2], neighbors))

                image_collection = self._collection \
                    .filter(ee.Filter.inList('WRS_PATH', ee.List(neighbors_paths))) \
                    .filter(ee.Filter.inList('WRS_ROW', ee.List(neighbors_rows)))

                final_image = Image()
                for period in self._periods:
                    period_dates = feature.get(period)
                    images_by_period = ImageCollection(image_collection).filter_by_period(year, period_dates, self._offset)

                    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)

                    image_reduced = Image(images_by_period.apply_reducers(self._reducers))

                    final_image = final_image.addBands(
                        image_reduced.rename(image_reduced.bandNames().map(lambda band: ee.String(period).cat('_').cat(band)))
                    )

                if settings.GENERATION_EXTRA_BANDS:
                    final_image = final_image.addBands(Image(final_image).get_bands(settings.GENERATION_EXTRA_BANDS))

                final_name = "{0}_{1}_{2}".format(settings.COLLECTION_PREFIX, "{0}{1}".format(path, row), str(year))
                final_image = final_image.select(settings.GENERATION_VARIABLES + settings.GENERATION_EXTRA_VARIABLES).set('year', year)

                self.add_image_in_batch(final_name,
                                        {"image": final_image, "year": int(year), "path": int(path), "row": int(row), "geometry": geometry,
                                         'neighbors': neighbors_geometry})
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
 def apply_smoothing(self):
     collection = self
     time_field = 'system:time_start'
     join = ee.Join.saveAll(matchesKey='images')
     diff_filter = ee.Filter.maxDifference(difference=1000 * 60 * 60 * 24 *
                                           17,
                                           leftField=time_field,
                                           rightField=time_field)
     three_neighbor_join = join.apply(primary=collection,
                                      secondary=collection,
                                      condition=diff_filter)
     smoothed = ImageCollection(
         three_neighbor_join.map(lambda image: Image(image).addBands(
             ImageCollection.fromImages(image.get('images')).mean(), None,
             True)))
     return smoothed
Exemplo n.º 5
0
    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.map(lambda image: image.clip(geometry))

                images = []
                for period in self._periods:
                    period_dates = feature.get(period)

                    images_by_period = ImageCollection(image_collection).filter_by_period(year, period_dates, self._offset)

                    images_by_period = images_by_period.apply_smoothing()

                    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)

                    image_reduced = Image(images_by_period.apply_reducers(self._reducers))
                    image_reduced = image_reduced.rename(image_reduced.bandNames().map(
                        lambda band: ee.String(period).cat('_').cat(band))
                    )
                    images.append(image_reduced)

                final_image = Image.cat(images)

                if settings.GENERATION_EXTRA_BANDS:
                    final_image = final_image.addBands(Image(final_image).get_bands(settings.GENERATION_EXTRA_BANDS))

                final_name = "{0}_{1}_{2}".format(settings.COLLECTION_PREFIX, "{0}{1}".format(path, row), str(year))
                final_image = final_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})
Exemplo n.º 6
0
 def apply_bands(self, bands):
     return self.map(
         lambda image: Band.rescale(Image(image).get_bands(bands)))
Exemplo n.º 7
0
 def clip_geometry(self):
     return self.map(lambda image: Image(image).clip(image.geometry(
     ).buffer(settings.EXPORT_BUFFER)))
Exemplo n.º 8
0
 def apply_qamask(self):
     return self.map(
         lambda image: image.updateMask(Image(image).get_qamask()))
Exemplo n.º 9
0
 def apply_brdf(self):
     return self.map(lambda image: Image(image).apply_brdf())
Exemplo n.º 10
0
	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})