import geemap
import ee
import folium
import geopandas as gpd
c:\Users\jtrum\miniconda3\envs\wash\lib\site-packages\geopandas\_compat.py:124: UserWarning: The Shapely GEOS version (3.11.1-CAPI-1.17.1) is incompatible with the GEOS version PyGEOS was compiled with (3.10.4-CAPI-1.16.2). Conversions between both will be slow. warnings.warn( C:\Users\jtrum\AppData\Local\Temp\ipykernel_9400\3016180286.py:4: DeprecationWarning: Shapely 2.0 is installed, but because PyGEOS is also installed, GeoPandas still uses PyGEOS by default. However, starting with version 0.14, the default will switch to Shapely. To force to use Shapely 2.0 now, you can either uninstall PyGEOS or set the environment variable USE_PYGEOS=0. You can do this before starting the Python process, or in your code before importing geopandas: import os os.environ['USE_PYGEOS'] = '0' import geopandas In the next release, GeoPandas will switch to using Shapely by default, even if PyGEOS is installed. If you only have PyGEOS installed to get speed-ups, this switch should be smooth. However, if you are using PyGEOS directly (calling PyGEOS functions on geometries from GeoPandas), this will then stop working and you are encouraged to migrate from PyGEOS to Shapely 2.0 (https://shapely.readthedocs.io/en/latest/migration_pygeos.html). import geopandas as gpd
ee.Authenticate()
ee.Initialize()
To authorize access needed by Earth Engine, open the following URL in a web browser and follow the instructions:
The authorization workflow will generate a code, which you should paste in the box below.
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) c:\Users\jtrum\Desktop\maps\map_2_land-cover.ipynb Cell 2 line 1 ----> <a href='vscode-notebook-cell:/c%3A/Users/jtrum/Desktop/maps/map_2_land-cover.ipynb#W2sZmlsZQ%3D%3D?line=0'>1</a> ee.Authenticate() <a href='vscode-notebook-cell:/c%3A/Users/jtrum/Desktop/maps/map_2_land-cover.ipynb#W2sZmlsZQ%3D%3D?line=1'>2</a> ee.Initialize() File c:\Users\jtrum\miniconda3\envs\wash\lib\site-packages\ee\__init__.py:104, in Authenticate(authorization_code, quiet, code_verifier, auth_mode, scopes) 77 def Authenticate( 78 authorization_code: Optional[str] = None, 79 quiet: bool = False, (...) 82 scopes: Optional[Sequence[str]] = None, 83 ) -> None: 84 """Prompts the user to authorize access to Earth Engine via OAuth2. 85 86 Args: (...) 102 (auth_url, code_verifier) when called with quiet='init_only' 103 """ --> 104 oauth.authenticate( 105 authorization_code, quiet, code_verifier, auth_mode, scopes 106 ) File c:\Users\jtrum\miniconda3\envs\wash\lib\site-packages\ee\oauth.py:430, in authenticate(cli_authorization_code, quiet, cli_code_verifier, auth_mode, scopes) 427 if flow.display_instructions(quiet): 428 _open_new_browser(flow.auth_url) --> 430 flow.save_code() File c:\Users\jtrum\miniconda3\envs\wash\lib\site-packages\ee\oauth.py:479, in Flow.save_code(self, code) 477 redirect_uri = self.server.url 478 code = self.server.fetch_code() # Waits for oauth callback --> 479 _obtain_and_write_token(code, self.code_verifier, self.scopes, redirect_uri) File c:\Users\jtrum\miniconda3\envs\wash\lib\site-packages\ee\oauth.py:197, in _obtain_and_write_token(auth_code, code_verifier, scopes, redirect_uri) 194 fetch_client = urllib.request.Request(FETCH_URL, data=data, headers=headers) 195 fetched_info = json.loads( 196 urllib.request.urlopen(fetch_client).read().decode()) --> 197 client_info = {k: fetched_info[k] for k in ['client_id', 'client_secret']} 198 scopes = fetched_info.get('scopes') or scopes 199 token = request_token(auth_code.strip(), code_verifier, **client_info) File c:\Users\jtrum\miniconda3\envs\wash\lib\site-packages\ee\oauth.py:197, in <dictcomp>(.0) 194 fetch_client = urllib.request.Request(FETCH_URL, data=data, headers=headers) 195 fetched_info = json.loads( 196 urllib.request.urlopen(fetch_client).read().decode()) --> 197 client_info = {k: fetched_info[k] for k in ['client_id', 'client_secret']} 198 scopes = fetched_info.get('scopes') or scopes 199 token = request_token(auth_code.strip(), code_verifier, **client_info) KeyError: 'client_id'
datadir = 'C:/Users/jtrum/world_bank/data/'
aoi_path = datadir + 'aoiLuanda.geojson'
aoi = geemap.geojson_to_ee(aoi_path)
esa = ee.ImageCollection("ESA/WorldCover/v100").first()
esa = esa.clip(aoi)
esa_vis = {'bands': ['Map']}
Map = geemap.Map()
Map.add_basemap('CartoDB.DarkMatter')
Map.addLayer(esa, esa_vis, "ESA Land Cover")
Map.add_legend(title="ESA Land Cover", builtin_legend='ESA_WorldCover')
Map.centerObject(aoi, 10)
#add title
Map.add_text(x=-8.8, y=11.8, text="Land Cover (Luanda, Angola)", fontsize=20, fontweight='bold', color='white')
Map
Map(center=[-8.980207755364754, 13.310742868350161], controls=(WidgetControl(options=['position', 'transparent…
df = geemap.image_area_by_group(esa, scale=1000, denominator=1e6, decimal_places=4, verbose=True)
import pandas as pd
data = {
'cover': ['Trees', 'Shrublands', 'Grasslands', 'Croplands', 'Built-Up', 'Barren', 'Snow and Ice', 'Water Bodies', 'Herbaceous Wetlands', 'Mangroves', 'Moss and Lichen'],
'group': ['10', '20', '30', '40', '50', '60', '70', '80', '90', '95', '100']
}
lc_df = pd.DataFrame(data)
#join the two dataframes by 'group'
lc = lc_df.join(df, on='group')
lc['percentage'] = lc['percentage']*100
lc = lc.fillna(0)
lc
Calculating area for group 10 ... Calculating area for group 20 ... Calculating area for group 30 ... Calculating area for group 40 ... Calculating area for group 50 ... Calculating area for group 60 ... Calculating area for group 80 ... Calculating area for group 90 ... Calculating area for group 95 ...
cover | group | |
---|---|---|
0 | Trees | 10 |
1 | Shrublands | 20 |
2 | Grasslands | 30 |
3 | Croplands | 40 |
4 | Built-Up | 50 |
5 | Barren | 60 |
6 | Snow and Ice | 70 |
7 | Water Bodies | 80 |
8 | Herbaceous Wetlands | 90 |
9 | Mangroves | 95 |
10 | Moss and Lichen | 100 |
df
area | percentage | |
---|---|---|
group | ||
10 | 42.6993 | 0.0175 |
20 | 10.7903 | 0.0044 |
30 | 1460.9425 | 0.5987 |
40 | 6.7740 | 0.0028 |
50 | 505.1714 | 0.2070 |
60 | 135.6323 | 0.0556 |
80 | 37.0484 | 0.0152 |
90 | 195.5900 | 0.0802 |
95 | 45.3820 | 0.0186 |
cover | group | area | percentage | |
---|---|---|---|---|
0 | Trees | 10 | 42.6993 | 1.75 |
1 | Shrublands | 20 | 10.7903 | 0.44 |
2 | Grasslands | 30 | 1460.9425 | 59.87 |
3 | Croplands | 40 | 6.7740 | 0.28 |
4 | Built-Up | 50 | 505.1714 | 20.70 |
5 | Barren | 60 | 135.6323 | 5.56 |
6 | Snow and Ice | 70 | 0.0000 | 0.00 |
7 | Water Bodies | 80 | 37.0484 | 1.52 |
8 | Herbaceous Wetlands | 90 | 195.5900 | 8.02 |
9 | Mangroves | 95 | 45.3820 | 1.86 |
10 | Moss and Lichen | 100 | 0.0000 | 0.00 |
#make a bar chart, ordered descending by percentage
import matplotlib.pyplot as plt
import seaborn as sns
color_codes = ['#006400', '#FFBB22', '#FFFF4C', '#F096FF', '#FA0000', '#B4B4B4', '#F0F0F0', '#0064C8', '#0096A0', '#00CF75', '#FAE6A0']
land_cover_categories = ['Trees', 'Shrublands', 'Grasslands', 'Croplands', 'Built-Up', 'Barren', 'Snow and Ice', 'Water Bodies', 'Herbaceous Wetlands', 'Mangroves', 'Moss and Lichen']
sns.set_style("whitegrid")
fig, ax = plt.subplots(figsize=(12, 8))
#order descending by percentage
lc = lc.sort_values(by=['percentage'], ascending=False)
#add percentage to be just right of the bar, rounded to 2 decimal places
for index, row in lc.iterrows():
ax.text(row.percentage+0.5, row.cover, str(round(row.percentage, 2)), color='black', ha="left", va='center', fontsize=12)
ax = sns.barplot(x="percentage", y="cover", data=lc, palette=color_codes)
ax.set(xlabel="Percentage (%)", ylabel="")
plt.show()