# INSTALL
!pip install geemap geopandasRequirement already satisfied: geemap in /usr/local/lib/python3.12/dist-packages (0.37.3)
Requirement already satisfied: geopandas in /usr/local/lib/python3.12/dist-packages (1.1.3)
Requirement already satisfied: anywidget in /usr/local/lib/python3.12/dist-packages (from geemap) (0.9.21)
Requirement already satisfied: bqplot in /usr/local/lib/python3.12/dist-packages (from geemap) (0.12.47)
Requirement already satisfied: earthengine-api>=1.6.12 in /usr/local/lib/python3.12/dist-packages (from geemap) (1.7.29)
Requirement already satisfied: eerepr>=0.1.0 in /usr/local/lib/python3.12/dist-packages (from geemap) (0.1.2)
Requirement already satisfied: folium>=0.17.0 in /usr/local/lib/python3.12/dist-packages (from geemap) (0.20.0)
Requirement already satisfied: geocoder in /usr/local/lib/python3.12/dist-packages (from geemap) (1.38.1)
Requirement already satisfied: ipyevents in /usr/local/lib/python3.12/dist-packages (from geemap) (2.0.4)
Requirement already satisfied: ipyfilechooser>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from geemap) (0.6.0)
Requirement already satisfied: ipyleaflet>=0.19.2 in /usr/local/lib/python3.12/dist-packages (from geemap) (0.20.0)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.12/dist-packages (from geemap) (3.10.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.12/dist-packages (from geemap) (2.0.2)
Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (from geemap) (2.2.2)
Requirement already satisfied: plotly in /usr/local/lib/python3.12/dist-packages (from geemap) (5.24.1)
Requirement already satisfied: pyperclip in /usr/local/lib/python3.12/dist-packages (from geemap) (1.11.0)
Requirement already satisfied: pyshp>=2.3.1 in /usr/local/lib/python3.12/dist-packages (from geemap) (3.0.9)
Requirement already satisfied: python-box in /usr/local/lib/python3.12/dist-packages (from geemap) (7.4.1)
Requirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (from geemap) (2.32.4)
Requirement already satisfied: scooby in /usr/local/lib/python3.12/dist-packages (from geemap) (0.11.2)
… (127 baris lagi)# IMPORT
import ee
import geemap
import geopandas as gpd
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from google.colab import drive# MOUNT DRIVE & AUTH
drive.mount('/content/drive')
ee.Authenticate()
ee.Initialize(project='ee-defaniarman')Loading...
Mounted at /content/drive
# LOAD SHAPEFILE
shp_path = '/content/drive/MyDrive/sampel_lapangan.shp'
gdf = gpd.read_file(shp_path)
gdf = gdf.to_crs(epsg=4326)
print(f'{len(gdf)} titik | CRS: {gdf.crs}')
gdf.head()Loading...
50 titik | CRS: EPSG:4326
Loading...
# CONVERT TO EE FEATURECOLLECTION
fc_samples = geemap.gdf_to_ee(gdf)Loading...
# AOI + S2 COMPOSITE
aoi = ee.FeatureCollection('projects/ee-defaniarman/assets/aoi_crb').geometry()
csPlus = ee.ImageCollection('GOOGLE/CLOUD_SCORE_PLUS/V1/S2_HARMONIZED')
def maskS2CSPlus(image):
qa = image.select('cs_cdf')
return image.updateMask(qa.gte(0.60))
s2_base = (ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterBounds(aoi)
.filterDate('2024-01-01', '2024-12-31')
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)))
composite = (s2_base
.linkCollection(csPlus, ['cs_cdf'])
.map(maskS2CSPlus)
.median()
.clip(aoi)
.divide(10000))
print('Composite bands:', composite.bandNames().getInfo())Loading...
Composite bands: ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B11', 'B12', 'AOT', 'WVP', 'SCL', 'TCI_R', 'TCI_G', 'TCI_B', 'MSK_CLDPRB', 'MSK_SNWPRB', 'QA10', 'QA20', 'QA60', 'MSK_CLASSI_OPAQUE', 'MSK_CLASSI_CIRRUS', 'MSK_CLASSI_SNOW_ICE', 'cs_cdf']
# HITUNG INDEKS
B3 = composite.select('B3') # Green 560 nm
B4 = composite.select('B4') # Red 665 nm
B8 = composite.select('B8') # NIR 842 nm
B11 = composite.select('B11') # SWIR1 1610 nm
NDVI = composite.normalizedDifference(['B8', 'B4']).rename('NDVI')
NDWI = composite.normalizedDifference(['B3', 'B8']).rename('NDWI')
NDMI = composite.normalizedDifference(['B8', 'B11']).rename('NDMI')
NDBI = composite.normalizedDifference(['B11', 'B8']).rename('NDBI')
BANDS_SPECTRAL = ['B2', 'B3', 'B4', 'B8', 'B11', 'B12']
img_full = (composite.select(BANDS_SPECTRAL)
.addBands([NDVI, NDWI, NDMI, NDBI]))
print('Full image bands:', img_full.bandNames().getInfo())Loading...
Full image bands: ['B2', 'B3', 'B4', 'B8', 'B11', 'B12', 'NDVI', 'NDWI', 'NDMI', 'NDBI']
# SAMPLING
sampled = img_full.sampleRegions(
collection = fc_samples,
properties = ['rand_point', 'id'],
scale = 10,
geometries = True
)
df_sample = geemap.ee_to_df(sampled)
print(df_sample.shape)
df_sample.head()Loading...
(50, 12)
Loading...
# TAMBAH KOORDINAT
coords = gdf[['geometry']].copy()
coords['lon'] = gdf.geometry.x
coords['lat'] = gdf.geometry.y
coords['rand_point'] = gdf['rand_point']
df_out = df_sample.merge(
coords[['rand_point', 'lon', 'lat']],
on='rand_point', how='left'
)
col_order = ['rand_point', 'id', 'lon', 'lat',
'B2', 'B3', 'B4', 'B8', 'B11', 'B12',
'NDVI', 'NDWI', 'NDMI', 'NDBI']
df_out = df_out[[c for c in col_order if c in df_out.columns]]
df_out = df_out.sort_values('rand_point').reset_index(drop=True)
print(df_out.shape)
df_out.head()Loading...
(50, 14)
Loading...
# EXPORT CSV + GPKG
out_dir = '/content/drive/MyDrive/'
csv_path = out_dir + 'sampel_indeks.csv'
gpkg_path = out_dir + 'sampel_indeks.gpkg'
df_out.to_csv(csv_path, index=False)
gdf_out = gpd.GeoDataFrame(
df_out,
geometry=gpd.points_from_xy(df_out['lon'], df_out['lat']),
crs='EPSG:4326'
)
gdf_out.to_file(gpkg_path, driver='GPKG', layer='sampel_indeks')
print('CSV ->', csv_path)
print('GPKG ->', gpkg_path)Loading...
CSV -> /content/drive/MyDrive/sampel_indeks.csv
GPKG -> /content/drive/MyDrive/sampel_indeks.gpkg