Motivation
📌Google Earth Engine use less line of code to do Zonal statistic compare with GEEMAP and no limitation of the CRS of your raster data.
import numpy as np
import pandas as pd
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
import geopandas as gpd
import folium
import matplotlib.pyplot as pl
import pyproj
The code below is Java script
You need to import your data into google earth engine
Add raster data
var dataset=ee.ImageCollection(ntl)
var style= {
max:100,
palette:['black','white','orange','yellow']
}
Map.addLayer(dataset, style, 'Night time light')
Map
Add shp
var china= ee.FeatureCollection(shp)
var shp_style={color:'yellow'}
Map.addLayer(china,shp_style, 'China')
Map
Zonal statistic--Mean
var y2020 = ntl.reduceRegions({
reducer: ee.Reducer.mean(),
collection: China
});
print(y2020)
Export result into GeoJSON / CSV
print(y2020)
Export.table.toDrive({
collection: y2020,
description: 'zonal',
folder: 'file',
fileNamePrefix: 'Chinam',
fileFormat: 'GeoJSON'
})
Data Visualization
The code below is Python code
gdf=gpd.read_file('/work/Chinam.geojson')
gdf
idobject
000000000000000000002.9%
000000000000000000012.9%
32 others94.1%
ADM0_ENobject
China100%
0
00000000000000000000
China
1
00000000000000000001
China
2
00000000000000000002
China
3
00000000000000000003
China
4
00000000000000000004
China
5
00000000000000000005
China
6
00000000000000000006
China
7
00000000000000000007
China
8
00000000000000000008
China
9
00000000000000000009
China
gdf.describe( ).round(2)
meanfloat64
count
34
mean
12.88
std
12.5
min
0.3
25%
5.89
50%
8.75
75%
16.9
max
52.1
gdf.plot()
gdf.explore(column='mean',cmap='cividis')
/opt/conda/lib/python3.9/site-packages/folium/features.py:906: UserWarning: GeoJsonTooltip is not configured to render for GeoJson GeometryCollection geometries. Please consider reworking these features: [{'ADM0_EN': 'China', 'ADM0_PCODE': 'CN', 'ADM0_ZH': '中国', 'ADM1_EN': 'Guangdong Province', 'ADM1_PCODE': 'CN044', 'ADM1_ZH': '广东省', '__folium_color': '#4d556c', 'id': '0000000000000000001d', 'mean': 15.42415764131296}, {'ADM0_EN': 'China', 'ADM0_PCODE': 'CN', 'ADM0_ZH': '中国', 'ADM1_EN': 'Hainan Province', 'ADM1_PCODE': 'CN046', 'ADM1_ZH': '海南省', '__folium_color': '#3a486c', 'id': '00000000000000000021', 'mean': 11.455399013786865}] to MultiPolygon for full functionality.
https://tools.ietf.org/html/rfc7946#page-9
warnings.warn(
gdf.explore(
column='mean',
tooltip=['ADM1_ZH', 'mean'],
scheme='naturalbreaks',
cmap='coolwarm',
legend=True,
style_kwds =dict(color="gray", weight=0.5),
legend_kwds=dict(colorbar=False)
)
/opt/conda/lib/python3.9/site-packages/folium/features.py:906: UserWarning: GeoJsonTooltip is not configured to render for GeoJson GeometryCollection geometries. Please consider reworking these features: [{'ADM0_EN': 'China', 'ADM0_PCODE': 'CN', 'ADM0_ZH': '中国', 'ADM1_EN': 'Guangdong Province', 'ADM1_PCODE': 'CN044', 'ADM1_ZH': '广东省', '__folium_color': '#dddddd', 'id': '0000000000000000001d', 'mean': 15.42415764131296}, {'ADM0_EN': 'China', 'ADM0_PCODE': 'CN', 'ADM0_ZH': '中国', 'ADM1_EN': 'Hainan Province', 'ADM1_PCODE': 'CN046', 'ADM1_ZH': '海南省', '__folium_color': '#dddddd', 'id': '00000000000000000021', 'mean': 11.455399013786865}] to MultiPolygon for full functionality.
https://tools.ietf.org/html/rfc7946#page-9
warnings.warn(