The purpose of this tutorial is to show how to use the classes defined in xmsextractor to extract data at given locations on an XmUGrid with scalar values, using outputs from a numerical model.
First, we will load a UGrid containing elevation values. The data comes from a Sedimentation and River Hydraulics – Two-Dimensional (SRH-2D) model.
import numpy as np
import geoviews as gv
import holoviews as hv
import pandas as pd
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import panel as pn
from shapely.geometry import Polygon, shape
from holoviews.streams import Params
import param
import hvplot.pandas
import hvplot.xarray
import xmsextractor
import xmsgrid
import ugrid_tools
import extractor_tools
import shapefile
hv.extension('bokeh')
srs = ccrs.LambertConformal(central_longitude=-98, central_latitude=35.0, false_easting=600000,
standard_parallels=(35.5667, 36.7667))
boundary_polygon = gv.Shape.from_shapefile("boundary_polys.shp", crs=srs).options(color="red", alpha=0.25)
basemap = gv.tile_sources.ESRI()
%opts Shape Polygons Points [height=700 width=700]
basemap * boundary_polygon
extractor_example_data = extractor_tools.ExtractorExample()
ugrid = xmsgrid.ugrid.UGrid(extractor_example_data.points, extractor_example_data.cells)
ugrid_cells = []
for i in range(0, ugrid.get_cell_count()):
ugrid_cells.extend(ugrid.get_cell_cellstream(i)[1][2:])
polygons, lines, points = ugrid_tools.create_hv_objects_for_image(ugrid, _projection=srs)
basemap * polygons * lines * points
extractor = xmsextractor.extractor.UGrid2dPolylineDataExtractor(ugrid, xmsextractor.extractor.data_location_enum.LOC_POINTS)
polyline = [(657675.3, 110106.7, 275.0), (657716.7, 110192.5, 275.0)]
extractor.set_polyline(polyline)
extracted_locations = extractor.get_extract_locations()
extractor.set_grid_scalars(extractor_example_data.wdepth_m, [], xmsextractor.extractor.data_location_enum.LOC_CELLS)
print(ugrid.get_locations())
extracted_data = extractor.extract_data()
print(str(["{0:0.4f}".format(i) for i in extracted_data]).replace("'", ""))
extract_scatter_vals = np.array([
(extracted_locations[i][0], extracted_locations[i][1], extracted_data[i])
for i in range(0, len(extracted_data))
])
min_x = min([x[0] for x in polyline])
min_y = min([y[1] for y in polyline])
max_x = max([x[0] for x in polyline])
max_y = max([y[1] for y in polyline])
padding = (max((max_x-min_x), (max_y-min_y))) * 0.15
#hv_extract_values = gv.Scatter(extract_scatter_vals, crs=srs, vdims=['y','z'])
hv_polyline = gv.Path([polyline,], crs=srs).options(line_color='red')
%opts Polygons Points Path [height=500 width=500]
%opts Scatter [color_index=2 colorbar=True tools=['hover']] (size=10)
#polygons, lines, points = ugrid_tools.create_hv_objects_for_image(ugrid, _projection=srs)
polygons = polygons.redim.range(
x=(min_x-padding, max_x+padding),
y=(min_y-padding, max_y+padding)
)
# draw the geoviews map
basemap * polygons * lines * points * hv_polyline * hv_extract_values
pxn = extracted_locations[-1][0]
px0 = extracted_locations[0][0]
distance = pxn - px0
hv_point_locations = [(extracted_locations[i][0] - px0, extracted_data[i]) for i in range(0, len(extracted_data))]
height = max([y[1] for y in hv_point_locations])
x_range = (min([x[0] for x in hv_point_locations]) - (distance*0.1), max([x[0] for x in hv_point_locations]) + (distance*0.1))
y_range = (min([y[1] for y in hv_point_locations]) - (height*0.1), max([y[1] for y in hv_point_locations]) + (height*0.1))
%opts Points [colorbar=True tools=['hover']] (size=4 color='red')
graph_points = hv.Points(hv_point_locations)
graph_line = hv.Path([hv_point_locations,],)
graph_line * graph_points.redim.range(x=x_range, y=y_range)