Skip to content

Commit

Permalink
Cleanup notebooks.
Browse files Browse the repository at this point in the history
Improve data download.
Add NDVI function.
Add SAM example.
  • Loading branch information
milos.colic committed Sep 11, 2023
1 parent 076bdc2 commit ed9d80e
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 366 deletions.
32 changes: 23 additions & 9 deletions notebooks/prototypes/grid_tiles/00 Download STACs.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,19 @@

# COMMAND ----------

time_range = "2021-06-01/2021-06-07"
time_range = "2021-06-01/2021-06-30"

# COMMAND ----------

cell_jsons = cells\
.withColumn("area_id", F.hash("geom_0"))\
.withColumn("h3", F.col("grid.index_id"))\
.withColumn("geojson", mos.st_asgeojson("grid.wkb"))\
.drop("geom_0")
.groupBy("h3")\
.agg(
mos.st_union_agg("grid.wkb").alias("geom_1")
)\
.withColumn("geojson", mos.st_asgeojson(mos.grid_boundaryaswkb("h3")))\
.drop("count", "geom_1")

# COMMAND ----------

Expand All @@ -151,6 +155,11 @@

# COMMAND ----------

# MAGIC %%mosaic_kepler
# MAGIC cell_jsons h3 h3

# COMMAND ----------

# MAGIC %md
# MAGIC Our framework allows for easy preparation of stac requests with only one line of code. This data is delta ready as this point and can easily be stored for lineage purposes.

Expand All @@ -172,7 +181,12 @@
# COMMAND ----------

# MAGIC %sql
# MAGIC DROP DATABASE IF EXISTS odin_alaska CASCADE;
# MAGIC CREATE DATABASE IF NOT EXISTS odin_alaska;

# COMMAND ----------

# MAGIC %sql
# MAGIC USE odin_alaska;

# COMMAND ----------
Expand Down Expand Up @@ -249,11 +263,7 @@ def download_band(eod_items, band_name):

# COMMAND ----------

# MAGIC %fs ls /FileStore/geospatial/odin/alaska/

# COMMAND ----------

# MAGIC %fs ls /FileStore/geospatial/odin/alaska/B02
# MAGIC %fs ls /FileStore/geospatial/odin/alaska/B08

# COMMAND ----------

Expand All @@ -262,6 +272,10 @@ def download_band(eod_items, band_name):
from rasterio.plot import show

fig, ax = pyplot.subplots(1, figsize=(12, 12))
raster = rasterio.open("""/dbfs/FileStore/geospatial/odin/alaska/B02/-718806860.tif""")
raster = rasterio.open("""/dbfs/FileStore/geospatial/odin/alaska/B08/2764922.tif""")
show(raster, ax=ax, cmap='Greens')
pyplot.show()

# COMMAND ----------


38 changes: 26 additions & 12 deletions notebooks/prototypes/grid_tiles/01 Gridded EOD Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# COMMAND ----------

spark.conf.set("spark.sql.adaptive.coalescePartitions.enabled", "false")
spark.conf.set("spark.sql.adaptive.enabled", "false")

# COMMAND ----------

Expand Down Expand Up @@ -49,8 +50,8 @@
# COMMAND ----------

catalog_df = \
spark.read.table("alaska_b02")\
.withColumn("souce_band", F.lit("B02"))
spark.read.table("alaska_b04")\
.withColumn("souce_band", F.lit("B04"))
catalog_df.display()

# COMMAND ----------
Expand Down Expand Up @@ -91,11 +92,13 @@
.withColumn("raster", mos.rst_tessellate("raster", F.lit(6)))\
.withColumn("index_id", F.col("raster.index_id"))

# COMMAND ----------

to_plot = grid_tessellate_df.limit(50).collect()

# COMMAND ----------

library.plot_raster(to_plot[15]["raster"]["raster"])
library.plot_raster(to_plot[18]["raster"]["raster"])

# COMMAND ----------

Expand Down Expand Up @@ -124,13 +127,20 @@ def index_band(band_table, resolution):

# COMMAND ----------

for tbl in tables_to_index:
index_band(tbl, 5)
index_band("alaska_b02", 6)

# COMMAND ----------

index_band("alaska_b03", 6)

# COMMAND ----------

index_band("alaska_b04", 6)

# COMMAND ----------

grid_tessellate_df = spark.read.table("alaska_b02_indexed")
grid_tessellate_df.display()
grid_tessellate_df.limit(20).display()

# COMMAND ----------

Expand All @@ -144,7 +154,7 @@ def index_band(band_table, resolution):

# COMMAND ----------

line_example = "LINESTRING(-122.2163001236001 47.77530703528161,-122.1503821548501 47.51996083856245,-121.8867102798501 47.62743233444236,-122.0954505142251 47.360200479212935,-121.8152991470376 47.41970286748326,-121.5131751236001 47.360200479212935,-121.7603675064126 47.23726461439514,-122.2547522720376 47.0691640536914,-121.9361487564126 47.08038730142549,-121.3813391861001 47.10282670591806,-121.2110511001626 47.31925361681828,-120.9308997329751 47.56816499155946,-120.7661048111001 47.41226874260139,-121.1616126236001 47.11404286281199,-121.7933264907876 46.885516358226546,-122.3206702407876 46.79909683431514)"
line_example = "LINESTRING(-158.34445841325555 68.0176784075422,-155.55393106950555 68.0423396963395,-154.82883341325555 67.84431100260183,-159.33322794450555 67.81114172848677,-160.01438028825555 67.47684671455214,-154.43332560075555 67.56925103744871,-154.01584513200555 67.30791374746678,-160.16818888200555 67.25700024664256,-160.58566935075555 66.94924133006975,-153.73020060075555 67.0693906319206,-154.49924356950555 66.70715520513478,-160.12424356950555 66.70715520513478,-159.02561075700555 66.37476822845568,-154.56516153825555 66.49774379983036,-155.04855997575555 66.22462528148408,-158.76193888200555 66.16254082040112,-157.94895060075555 65.94851918639993,-155.64182169450555 66.0021934684043,-158.58615763200555 66.55900493948819,-155.26828653825555 67.43472555587037,-161.64035685075555 67.86087797718164,-161.66232950700555 67.44315575603868)"

line_df = spark.createDataFrame([line_example], "string")\
.select(F.col("value").alias("wkt"))\
Expand All @@ -169,7 +179,7 @@ def index_band(band_table, resolution):

# COMMAND ----------

cells_of_interest.display()
cells_of_interest.limit(10).display()

# COMMAND ----------

Expand Down Expand Up @@ -248,15 +258,19 @@ def mean_band_1(dataset):

# COMMAND ----------

df_12_13 = measurements.where("date == '2020-12-13'")
df_12_05 = measurements.where("date == '2020-12-05'")
df_06_20 = measurements.where("date == '2021-06-20'")
df_06_03 = measurements.where("date == '2021-06-03'")

# COMMAND ----------

# MAGIC %%mosaic_kepler
# MAGIC df_12_13 index_id h3 5000
# MAGIC df_06_20 index_id h3 5000

# COMMAND ----------

# MAGIC %%mosaic_kepler
# MAGIC df_12_05 index_id h3 5000
# MAGIC df_06_03 index_id h3 5000

# COMMAND ----------


Loading

0 comments on commit ed9d80e

Please sign in to comment.