Advertisement
Abhisek92

spatrial_sample_group.py

May 30th, 2024 (edited)
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. import random
  2. import numpy as np
  3. import pandas as pd
  4. import rasterio as rio
  5. import geopandas as gpd
  6. from sklearn.cluster import KMeans
  7.  
  8.  
  9. def sample_coordinates(bool_array, n):
  10.     true_coords = np.argwhere(bool_array)
  11.  
  12.     if len(true_coords) < n:
  13.         return true_coords
  14.         # raise ValueError("Not enough True values to sample from")
  15.  
  16.     kmeans = KMeans(n_clusters=n)
  17.     kmeans.fit(true_coords)
  18.  
  19.     sampled_coords = list()
  20.     for cluster_idx in range(n):
  21.         cluster_points = true_coords[kmeans.labels_ == cluster_idx]
  22.         sampled_point = cluster_points[random.randint(0, len(cluster_points) - 1)]
  23.         sampled_coords.append(tuple(sampled_point))
  24.  
  25.     return np.array(sampled_coords)
  26.  
  27. def sample_raster(src_path):
  28.     starts = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0009, 0.001, 0.002]
  29.     stops = starts[1:] + [np.inf]
  30.     groups = [chr(65+i) for i in range(len(starts))]
  31.     frames = list()
  32.     with rio.open(src_path, 'r') as src:
  33.         forward = np.array(src.transform).reshape((3, 3))
  34.         img = src.read(1, masked=True)
  35.         for a, b, g in zip(starts, stops, groups):
  36.             mask = np.logical_and((a <= img), (img < b))
  37.             try:
  38.                 coords = sample_coordinates(mask, 15)
  39.             except ValueError:
  40.                 print(a, b)
  41.             vals = img[coords[:, 0], coords[:, 1]]
  42.             coords = np.stack([coords[:, 1], coords[:, 0]], axis=1)
  43.             coords = np.concatenate([(coords + 0.5), np.ones((coords.shape[0], 1))], axis=1)
  44.             coords = np.einsum(
  45.                 "ij,kj -> ki", forward, coords
  46.             )
  47.             coords[:, -1] = vals
  48.             df = pd.DataFrame(coords, columns=["x", "y", "Value"])
  49.             df["Group"] = g
  50.             gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.x, df.y))
  51.             gdf = gdf.drop(columns=['x', 'y'])
  52.             gdf.set_crs(crs=src.crs, inplace=True)
  53.             frames.append(gdf)
  54.     main_df = pd.concat(frames, ignore_index=True, axis=0)
  55.     return main_df
  56.  
  57. if __name__ == "__main__":
  58.     geo_df = sample_raster("gs_10m_meanm.tif")
  59.     geo_df.to_file(filename="Sampled_Points.geojson", driver="GeoJSON")
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement