Find A Raster Tile#

Goal: Find the raster tile your pit falls in.

Approach:

  1. Grab the pit location from the a pit layer in the database

  2. Perform an intersect on all raster tiles with the pit location

  3. Plot

Step 1. Grab the pit location from the layer data#

from snowexsql.db import get_db
from snowexsql.data import ImageData, LayerData
from rasterio.plot import show
from sqlalchemy.sql import func
import geoalchemy2.functions as gfunc
from geoalchemy2.shape import to_shape
from rasterio import MemoryFile
import matplotlib.pyplot as plt
import geopandas as gpd
from snowexsql.conversions import raster_to_rasterio

# PIT Site Identifier
site_id = '5S31'

# Connect to the database we made.
db_name = 'db.snowexdata.org/snowex'
engine, session = get_db(db_name, credentials='./credentials.json')

# Grab our pit layers by site id
q = session.query(LayerData).filter(LayerData.site_id == site_id)
layers = q.all()

# Grab the pit location from a single layerdata entry
p = layers[0].geom
p_shp = to_shape(p)

Step 2. Perform an intersect on all raster tiles with the pit location#

# Grab the raster as a tile that intersects with our pit and convert it to a geotiff
print('Grabbing rasters that overlap on the point {}'.format(p_shp))

# Grab the returned raster as a tiff
q = session.query(func.ST_AsTiff(ImageData.raster)).filter(ImageData.observers=='ASO Inc.')
q = q.filter(ImageData.type=='depth')

# Filter rasters by where a tile intersects wihtour pit location
rasters = q.filter(gfunc.ST_Intersects(ImageData.raster, p)).all()

# Convert it to a rasterio dataset for easy plotting
dataset = raster_to_rasterio(session, rasters)[0]
Grabbing rasters that overlap on the point POINT (745458 4322762)

Step 3. Plot it up!#

# Close the session to avoid hanging transactions
session.close()