Spaces:
Runtime error
Runtime error
Commit
·
f186d18
1
Parent(s):
2ef729d
Update biomap/utils_gee.py
Browse files- biomap/utils_gee.py +156 -156
biomap/utils_gee.py
CHANGED
|
@@ -1,157 +1,157 @@
|
|
| 1 |
-
import io
|
| 2 |
-
import requests
|
| 3 |
-
import ee
|
| 4 |
-
import numpy as np
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
|
| 7 |
-
#Initialize
|
| 8 |
-
service_account = 'cvimg-355@cvimg-377115.iam.gserviceaccount.com'
|
| 9 |
-
credentials = ee.ServiceAccountCredentials(service_account, '
|
| 10 |
-
ee.Initialize(credentials)
|
| 11 |
-
|
| 12 |
-
#delete clouds
|
| 13 |
-
def maskS2clouds(image):
|
| 14 |
-
qa = image.select('QA60');
|
| 15 |
-
|
| 16 |
-
# // Bits 10 and 11 are clouds and cirrus, respectively.
|
| 17 |
-
cloudBitMask = 1 << 10;
|
| 18 |
-
cirrusBitMask = 1 << 11;
|
| 19 |
-
|
| 20 |
-
# // Both flags should be set to zero, indicating clear conditions.
|
| 21 |
-
mask = (qa.bitwiseAnd(cloudBitMask).eq(0))and(qa.bitwiseAnd(cirrusBitMask).eq(0))
|
| 22 |
-
|
| 23 |
-
return image.updateMask(mask).divide(10000);
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#find ee_img
|
| 27 |
-
def extract_ee_img(location,start_date,end_date, width = 0.01 , len = 0.01) :
|
| 28 |
-
"""Extract the earth engine image
|
| 29 |
-
|
| 30 |
-
Args:
|
| 31 |
-
location (list[float]):
|
| 32 |
-
start_date (str): the start date for finding an image
|
| 33 |
-
end_date (str): the end date for finding an image
|
| 34 |
-
width (float, optional): _description_. Defaults to 0.01.
|
| 35 |
-
len (float, optional): _description_. Defaults to 0.01.
|
| 36 |
-
|
| 37 |
-
Returns:
|
| 38 |
-
_type_: _description_
|
| 39 |
-
"""
|
| 40 |
-
# define the polygone
|
| 41 |
-
polygone =[[[float(location[0])-0.01,float(location[1])+0.01],
|
| 42 |
-
[float(location[0])-0.01,float(location[1])-0.01],
|
| 43 |
-
[float(location[0])+0.01,float(location[1])-0.01],
|
| 44 |
-
[float(location[0])+0.01,float(location[1])+0.01],
|
| 45 |
-
]]
|
| 46 |
-
|
| 47 |
-
#define the ee geometry
|
| 48 |
-
geometry = ee.Geometry.Polygon(polygone, None, False);
|
| 49 |
-
|
| 50 |
-
#extract the dataset
|
| 51 |
-
dataset = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')\
|
| 52 |
-
.filterDate(start_date, end_date)\
|
| 53 |
-
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',1))\
|
| 54 |
-
.map(maskS2clouds)
|
| 55 |
-
return dataset.mean(), geometry
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Get URL
|
| 60 |
-
def get_url(ee_img, geometry, scale=5):
|
| 61 |
-
"""Get the url of a dataset and a geometry
|
| 62 |
-
|
| 63 |
-
Args:
|
| 64 |
-
ee_img (ee.ImageCollection: meta data on the image
|
| 65 |
-
geometry (ee.Geometry.Polygon): geometry of the desired landscape
|
| 66 |
-
scale (int, optional): _description_. Defaults to 5.
|
| 67 |
-
|
| 68 |
-
Returns:
|
| 69 |
-
str: the url to use to ask the server
|
| 70 |
-
"""
|
| 71 |
-
region = geometry
|
| 72 |
-
|
| 73 |
-
# collectionList = ee_img.toList(ee_img.size())
|
| 74 |
-
# collectionSize = collectionList.size().getInfo()
|
| 75 |
-
# for i in xrange(collectionSize):
|
| 76 |
-
# ee.batch.Export.image.toDrive(
|
| 77 |
-
# image = ee.Image(collectionList.get(i)).clip(rectangle),
|
| 78 |
-
# fileNamePrefix = 'foo' + str(i + 1),
|
| 79 |
-
# dimensions = '128x128').start()
|
| 80 |
-
|
| 81 |
-
url = ee_img.getDownloadURL({
|
| 82 |
-
# 'min': 0.0,
|
| 83 |
-
# 'max': 0.3,
|
| 84 |
-
'bands': ['B4', 'B3', 'B2'],
|
| 85 |
-
'region' : region,
|
| 86 |
-
'scale' : scale,
|
| 87 |
-
'format' : 'NPY'
|
| 88 |
-
})
|
| 89 |
-
|
| 90 |
-
return url
|
| 91 |
-
|
| 92 |
-
def extract_np_from_url(url):
|
| 93 |
-
"""extract a numpy array based on a url
|
| 94 |
-
|
| 95 |
-
Args:
|
| 96 |
-
url (str): _description_
|
| 97 |
-
|
| 98 |
-
Returns:
|
| 99 |
-
numpyarray: response from earth engine as numpy
|
| 100 |
-
"""
|
| 101 |
-
#get the response from url
|
| 102 |
-
response = requests.get(url)
|
| 103 |
-
|
| 104 |
-
#transform it into numpy
|
| 105 |
-
data = np.load(io.BytesIO(response.content))
|
| 106 |
-
|
| 107 |
-
#transform numpy of tuples to 3D numpy
|
| 108 |
-
temp1 = []
|
| 109 |
-
|
| 110 |
-
for x in data:
|
| 111 |
-
temp2 = []
|
| 112 |
-
for y in x :
|
| 113 |
-
temp2.append([z for z in y])
|
| 114 |
-
temp1.append(temp2)
|
| 115 |
-
|
| 116 |
-
data = np.array(temp1)
|
| 117 |
-
|
| 118 |
-
return data
|
| 119 |
-
|
| 120 |
-
#Fonction globale
|
| 121 |
-
def extract_img(location,start_date,end_date, width = 0.01 , len = 0.01,scale=5):
|
| 122 |
-
"""Extract an image of the landscape at the selected longitude and latitude with the selected width and length
|
| 123 |
-
|
| 124 |
-
Args:
|
| 125 |
-
location (list[float]): [latitude of the center of the landscape, longitude of the center of the landscape]
|
| 126 |
-
start_date (str): the start date
|
| 127 |
-
end_date (str): _description_
|
| 128 |
-
width (float, optional): _description_. Defaults to 0.01.
|
| 129 |
-
len (float, optional): _description_. Defaults to 0.01.
|
| 130 |
-
scale (int, optional): _description_. Defaults to 5.
|
| 131 |
-
|
| 132 |
-
Returns:
|
| 133 |
-
img: image as numpy array
|
| 134 |
-
"""
|
| 135 |
-
ee_img, geometry = extract_ee_img(location, width,start_date,end_date , len)
|
| 136 |
-
url = get_url(ee_img, geometry, scale)
|
| 137 |
-
img = extract_np_from_url(url)
|
| 138 |
-
|
| 139 |
-
return img
|
| 140 |
-
|
| 141 |
-
# transform img from numpy to PIL
|
| 142 |
-
def transform_ee_img(img, min = 0, max=0.3):
|
| 143 |
-
"""Transform an img from numpy to PIL
|
| 144 |
-
|
| 145 |
-
Args:
|
| 146 |
-
img (numpy array): the original image as a numpy array
|
| 147 |
-
min (int, optional): _description_. Defaults to 0.
|
| 148 |
-
max (float, optional): _description_. Defaults to 0.3.
|
| 149 |
-
|
| 150 |
-
Returns:
|
| 151 |
-
img_test: a PIL image
|
| 152 |
-
"""
|
| 153 |
-
img_test=img
|
| 154 |
-
img_test=np.minimum(img_test*255/max,np.ones(img.shape)*255)
|
| 155 |
-
img_test=np.uint8((np.rint(img_test)).astype(int))
|
| 156 |
-
plt.imshow(img_test)
|
| 157 |
return img_test
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import requests
|
| 3 |
+
import ee
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
#Initialize
|
| 8 |
+
service_account = 'cvimg-355@cvimg-377115.iam.gserviceaccount.com'
|
| 9 |
+
credentials = ee.ServiceAccountCredentials(service_account, 'biomap/.private-key.json')
|
| 10 |
+
ee.Initialize(credentials)
|
| 11 |
+
|
| 12 |
+
#delete clouds
|
| 13 |
+
def maskS2clouds(image):
|
| 14 |
+
qa = image.select('QA60');
|
| 15 |
+
|
| 16 |
+
# // Bits 10 and 11 are clouds and cirrus, respectively.
|
| 17 |
+
cloudBitMask = 1 << 10;
|
| 18 |
+
cirrusBitMask = 1 << 11;
|
| 19 |
+
|
| 20 |
+
# // Both flags should be set to zero, indicating clear conditions.
|
| 21 |
+
mask = (qa.bitwiseAnd(cloudBitMask).eq(0))and(qa.bitwiseAnd(cirrusBitMask).eq(0))
|
| 22 |
+
|
| 23 |
+
return image.updateMask(mask).divide(10000);
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
#find ee_img
|
| 27 |
+
def extract_ee_img(location,start_date,end_date, width = 0.01 , len = 0.01) :
|
| 28 |
+
"""Extract the earth engine image
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
location (list[float]):
|
| 32 |
+
start_date (str): the start date for finding an image
|
| 33 |
+
end_date (str): the end date for finding an image
|
| 34 |
+
width (float, optional): _description_. Defaults to 0.01.
|
| 35 |
+
len (float, optional): _description_. Defaults to 0.01.
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
_type_: _description_
|
| 39 |
+
"""
|
| 40 |
+
# define the polygone
|
| 41 |
+
polygone =[[[float(location[0])-0.01,float(location[1])+0.01],
|
| 42 |
+
[float(location[0])-0.01,float(location[1])-0.01],
|
| 43 |
+
[float(location[0])+0.01,float(location[1])-0.01],
|
| 44 |
+
[float(location[0])+0.01,float(location[1])+0.01],
|
| 45 |
+
]]
|
| 46 |
+
|
| 47 |
+
#define the ee geometry
|
| 48 |
+
geometry = ee.Geometry.Polygon(polygone, None, False);
|
| 49 |
+
|
| 50 |
+
#extract the dataset
|
| 51 |
+
dataset = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')\
|
| 52 |
+
.filterDate(start_date, end_date)\
|
| 53 |
+
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',1))\
|
| 54 |
+
.map(maskS2clouds)
|
| 55 |
+
return dataset.mean(), geometry
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Get URL
|
| 60 |
+
def get_url(ee_img, geometry, scale=5):
|
| 61 |
+
"""Get the url of a dataset and a geometry
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
ee_img (ee.ImageCollection: meta data on the image
|
| 65 |
+
geometry (ee.Geometry.Polygon): geometry of the desired landscape
|
| 66 |
+
scale (int, optional): _description_. Defaults to 5.
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
str: the url to use to ask the server
|
| 70 |
+
"""
|
| 71 |
+
region = geometry
|
| 72 |
+
|
| 73 |
+
# collectionList = ee_img.toList(ee_img.size())
|
| 74 |
+
# collectionSize = collectionList.size().getInfo()
|
| 75 |
+
# for i in xrange(collectionSize):
|
| 76 |
+
# ee.batch.Export.image.toDrive(
|
| 77 |
+
# image = ee.Image(collectionList.get(i)).clip(rectangle),
|
| 78 |
+
# fileNamePrefix = 'foo' + str(i + 1),
|
| 79 |
+
# dimensions = '128x128').start()
|
| 80 |
+
|
| 81 |
+
url = ee_img.getDownloadURL({
|
| 82 |
+
# 'min': 0.0,
|
| 83 |
+
# 'max': 0.3,
|
| 84 |
+
'bands': ['B4', 'B3', 'B2'],
|
| 85 |
+
'region' : region,
|
| 86 |
+
'scale' : scale,
|
| 87 |
+
'format' : 'NPY'
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
return url
|
| 91 |
+
|
| 92 |
+
def extract_np_from_url(url):
|
| 93 |
+
"""extract a numpy array based on a url
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
url (str): _description_
|
| 97 |
+
|
| 98 |
+
Returns:
|
| 99 |
+
numpyarray: response from earth engine as numpy
|
| 100 |
+
"""
|
| 101 |
+
#get the response from url
|
| 102 |
+
response = requests.get(url)
|
| 103 |
+
|
| 104 |
+
#transform it into numpy
|
| 105 |
+
data = np.load(io.BytesIO(response.content))
|
| 106 |
+
|
| 107 |
+
#transform numpy of tuples to 3D numpy
|
| 108 |
+
temp1 = []
|
| 109 |
+
|
| 110 |
+
for x in data:
|
| 111 |
+
temp2 = []
|
| 112 |
+
for y in x :
|
| 113 |
+
temp2.append([z for z in y])
|
| 114 |
+
temp1.append(temp2)
|
| 115 |
+
|
| 116 |
+
data = np.array(temp1)
|
| 117 |
+
|
| 118 |
+
return data
|
| 119 |
+
|
| 120 |
+
#Fonction globale
|
| 121 |
+
def extract_img(location,start_date,end_date, width = 0.01 , len = 0.01,scale=5):
|
| 122 |
+
"""Extract an image of the landscape at the selected longitude and latitude with the selected width and length
|
| 123 |
+
|
| 124 |
+
Args:
|
| 125 |
+
location (list[float]): [latitude of the center of the landscape, longitude of the center of the landscape]
|
| 126 |
+
start_date (str): the start date
|
| 127 |
+
end_date (str): _description_
|
| 128 |
+
width (float, optional): _description_. Defaults to 0.01.
|
| 129 |
+
len (float, optional): _description_. Defaults to 0.01.
|
| 130 |
+
scale (int, optional): _description_. Defaults to 5.
|
| 131 |
+
|
| 132 |
+
Returns:
|
| 133 |
+
img: image as numpy array
|
| 134 |
+
"""
|
| 135 |
+
ee_img, geometry = extract_ee_img(location, width,start_date,end_date , len)
|
| 136 |
+
url = get_url(ee_img, geometry, scale)
|
| 137 |
+
img = extract_np_from_url(url)
|
| 138 |
+
|
| 139 |
+
return img
|
| 140 |
+
|
| 141 |
+
# transform img from numpy to PIL
|
| 142 |
+
def transform_ee_img(img, min = 0, max=0.3):
|
| 143 |
+
"""Transform an img from numpy to PIL
|
| 144 |
+
|
| 145 |
+
Args:
|
| 146 |
+
img (numpy array): the original image as a numpy array
|
| 147 |
+
min (int, optional): _description_. Defaults to 0.
|
| 148 |
+
max (float, optional): _description_. Defaults to 0.3.
|
| 149 |
+
|
| 150 |
+
Returns:
|
| 151 |
+
img_test: a PIL image
|
| 152 |
+
"""
|
| 153 |
+
img_test=img
|
| 154 |
+
img_test=np.minimum(img_test*255/max,np.ones(img.shape)*255)
|
| 155 |
+
img_test=np.uint8((np.rint(img_test)).astype(int))
|
| 156 |
+
plt.imshow(img_test)
|
| 157 |
return img_test
|