#!/usr/bin/env python # coding: utf-8 # # Xns11 - Basic reading and plotting of cross sections # #### Opening files and finding cross sections. # In[1]: import mikeio1d xns = mikeio1d.open("../tests/testdata/mikep_cs_demo.xns11") xns # In[2]: # How many cross sections in the file? len(xns) # In[3]: # You can index specific cross sections using their location id, chainage, and topo id. # For example, to access the cross section at location 'basin_left1', chainage '122.042', and topo id '1': xns["basin_left1", "122.042", "1"] # In[4]: # Alternatively, you can use 'sel()' method to be more explicit (however without autocompletion): xns.sel(location_id="basin_left1", chainage="122.042", topo_id="1") # In[5]: # Use ':' or '...' as a wildcard to get a list of CrossSection objects matching the specified criteria. xns["basin_left1", :, "1"] # In[6]: # You can also provide fewer arguments and the remaining ones will automatically use '...'. # For example, xns['basin_left1', ..., ...] can be more concisely expressed as: xns["basin_left1"] # In[7]: # Or with the 'sel()' method: xns.sel(location_id="basin_left1") # In[8]: # Get a nice overview of the collection as a pandas DataFrame. xns.to_dataframe() # In[9]: # You can access some basic properties of the collection: print(xns.location_ids) print(xns.chainages) print(xns.topo_ids) # ### Reading and plotting cross sections # In[10]: # Access the raw data of a cross section using the 'raw' attribute, which returns a Pandas DataFrame. df = xns["basin_left1", "481.451", "1"].raw df.head() # In[11]: # Plot the cross section. ax = xns["basin_left1", "122.042", "1"].plot() # In[12]: # Plot another cross section on the same axis. ax = xns["basin_left1", "166.107", "1"].plot(ax=ax, linestyle="--") ax.figure # In[13]: # Turn off marker labels if it gets too crowded. ax = xns["basin_left1", "166.107", "1"].plot(with_marker_labels=False) # In[14]: # Or turn off markers completely. ax = xns["basin_left1", "166.107", "1"].plot(with_markers=False) # ### Creating subsets of Xns11 objects # In[15]: # You can create a new Xns11 object from a subset of the original collection. from mikeio1d import Xns11 new_xns11 = Xns11(xns.sel(location_id="basin_left1")) new_xns11 # In[16]: # Plot all cross sections part of the new collection. ax = new_xns11.plot(with_marker_labels=False) # ### Plotting cross sections with GeoPandas # In[17]: # You can convert xsections into a GeoDataframe. Note that this requires installing GeoPandas seperately. gdf = xns.to_geopandas() gdf.plot() # In[18]: # You can use convert any subset to a GeoDataFrame. subset = Xns11(xns.sel(location_id="basin_left1")) subset.to_geopandas().plot() # In[19]: # This xns11 file has three topo ids, let's get an overview of where they are located. gdf = xns.to_geopandas() gdf.plot(column="topo_id", legend=True, legend_kwds={"title": "Topo"}) # In[20]: # You can check for overlapping cross sections by seeing if there's duplicate geometries. # Here there's no duplicates, however, we notice a number of cross sections have no coordinates. # This can be the case where cross section coordinates are not defined in the xns11 file. For example, # sometimes coordinates are excluded, then MIKE+ will estimate the coordinates based on the river geometry, # cross section chainage, and the river's centerline. gdf[gdf.geometry.duplicated()] # In[21]: # Plot all of the markers. xns.to_geopandas(mode='markers').plot(legend=True) # In[22]: # Plot markers on top of cross sections. gdf1 = xns.to_geopandas(mode='sections') gdf2 = xns.to_geopandas(mode='markers') ax = gdf1.plot(figsize=(15, 15)) gdf2.plot(ax=ax, column="marker_label", markersize=10, zorder=10, legend=True)