예제 #1
0
파일: main.py 프로젝트: acocac/CNN-tests
def ee_pre_processing(request):
    request = request.get_json()

    # Variables
    collection = request.get('collection')
    startDate = ee.Date(request.get('start'))
    stopDate  = ee.Date(request.get('end'))
    scale  = request.get('scale')
    # Bands
    bands = ee_collection_specifics.ee_bands(collection)

    # Get composite
    image = ee_collection_specifics.Composite(collection)(startDate, stopDate)
    image = image.select(bands)

    # Normalize images
    if ee_collection_specifics.normalize(collection):
        # Get min man values for each band
        values = min_max_values(image, collection, scale)

        # Normalize images
        image = normalize_ee_images(image, collection, values)
    else:
        values = {}
        
    return json.dumps({'bands_min_max': values, 'composite': image.serialize()})
    def create_collection(self, scale=None):

        years = np.arange(self.start_year, self.stop_year + 1)

        dic = ee_collection_specifics.date_range(self.instrument)
        years_range = list(map(list, list(dic.values())))
        collections = list(dic.keys())

        image_list = []
        for year in years:
            n = 0
            in_range = False
            for sub_years in years_range:
                if year in sub_years:
                    in_range = True
                    break
                n = +1

            if not in_range:
                raise ValueError(f'Year out of range.')

            collection = collections[n]

            if scale:
                scale = scale
            else:
                scale = ee_collection_specifics.scales(collection)

            # Image Collection
            image_collection = ee_collection_specifics.ee_collections(
                collection)

            # Image Visualization parameters
            vis = ee_collection_specifics.ee_vis(collection)

            step_range = ee_collection_specifics.step_range(collection)

            startDate = ee.Date(str(year + step_range[0]) + '-12-31')
            stopDate = ee.Date(str(year + step_range[1]) + '-12-31')

            image = ee_collection_specifics.Composite(collection)(
                image_collection, startDate, stopDate, scale, self.polygon)

            # convert image to an RGB visualization
            image_list.append(self.visFun(image, vis))

        return image_list
예제 #3
0
    def read_datasets(self):
        
        ## Composite
        image = ee_collection_specifics.Composite(self.collection)(self.image_collection, self.startDate, self.stopDate, self.geom)
        
        ## Calculate normalized Difference
        if self.normDiff_bands:
            for n, normDiff_band in enumerate(self.normDiff_bands):
                image_nd = image.normalizedDifference(normDiff_band)
                ## Concatenate images into one multi-band image
                if n == 0:
                    image = ee.Image.cat([image.select(self.bands), image_nd])
                else:
                    image = ee.Image.cat([image, image_nd])
        else:
            image = image.select(self.bands)
        
        # Choose the scale
        image =  image.reproject(crs='EPSG:4326', scale=self.scale)
            
        # Download images as tif
        download_image_tif(image, 'data.zip', scale = self.scale, region = self.region)
        
        # Load data
        directory = "./data/"

        files = sorted(f for f in os.listdir(directory) if f.endswith('.' + 'tif'))
        
        data = load_tif_bands(directory, files)
        
        # Remove data folders and files
        file="data.zip"
        ## If file exists, delete it ##
        if os.path.isfile(file):
            os.remove(file)
        else:    ## Show an error ##
            print("Error: %s file not found" % file)
        ## Try to remove tree; if failed show an error using try...except on screen
        folder = "./data"
        try:
            shutil.rmtree(folder)
        except OSError as e:
            print ("Error: %s - %s." % (e.filename, e.strerror))
            
        return data
예제 #4
0
    def read_datasets(self):
        
        ## Composite
        image = ee_collection_specifics.Composite(self.collection)(self.image_collection, self.startDate, self.stopDate, self.geom)
        
        # Choose the scale
        image =  image.reproject(crs='EPSG:4326', scale=self.scale)
        
        ## Select the bands
        if self.normDiff_bands:
            for n, normDiff_band in enumerate(self.normDiff_bands):
                image_nd = image.normalizedDifference(normDiff_band)
                ## Concatenate images into one multi-band image
                if n == 0:
                    image_nd = image_nd.rename(self.normDiff_bands_names[0])
                    image = ee.Image.cat([image.select(self.bands), image_nd])
                else:
                    image_nd = image_nd.rename(self.normDiff_bands_names[1])
                    image = ee.Image.cat([image, image_nd])
        else:
            image = image.select(self.bands)
            

        # get the lat lon
        latlon = ee.Image.pixelLonLat().addBands([image])
 
        # apply reducer to list
        latlon = latlon.reduceRegion(
          reducer=ee.Reducer.toList(),
          geometry=self.region,
          maxPixels=1e8,
          scale=self.scale);

        # get list of bands
        bands = list(latlon.getInfo().keys())
        bands.remove('latitude'); bands.remove('longitude')
        nbands = len(bands)
    
        # get data into three different arrays
        lats = np.array((ee.Array(latlon.get("latitude")).getInfo()))
        lons = np.array((ee.Array(latlon.get("longitude")).getInfo()))
 
        # get the unique coordinates
        uniqueLats = np.unique(lats)
        uniqueLons = np.unique(lons)
 
        # get number of columns and rows from coordinates
        ncols = len(uniqueLons)    
        nrows = len(uniqueLats)
 
        # determine pixelsizes
        ys = uniqueLats[1] - uniqueLats[0] 
        xs = uniqueLons[1] - uniqueLons[0]
 
        # create an array with dimensions of image
        arr = np.zeros([nrows, ncols, nbands], np.float32) #-9999
 

        for n, band in enumerate(bands):
            data = np.array((ee.Array(latlon.get(band)).getInfo()))

            # fill the array with values
            counter =0
            for y in range(0,len(arr),1):
                for x in range(0,len(arr[0]),1):
                    if lats[counter] == uniqueLats[y] and lons[counter] == uniqueLons[x] and counter < len(lats)-1:
                        counter+=1
                        arr[len(uniqueLats)-1-y,x,n] = data[counter] # we start from lower left corner
        return arr
예제 #5
0
 def read_composite(self):
     
     ## Composite
     self.image = ee_collection_specifics.Composite(self.collection)(self.image_collection, self.startDate, self.stopDate, self.geom, self.scale)
     
     self.image = self.image.visualize(**self.vis)