Skip to content

Commit

Permalink
Merge pull request #171 from umr-lops/landmask_fix_issue
Browse files Browse the repository at this point in the history
Landmask fix issue
  • Loading branch information
agrouaze authored Jun 14, 2023
2 parents 96ef302 + b361abf commit b0d5951
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
88 changes: 66 additions & 22 deletions docs/examples/projections.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"cell_type": "code",
"execution_count": null,
"id": "40fd3e6a-67c4-4a0b-9136-a083729e94d4",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import xsar\n",
Expand All @@ -41,7 +43,9 @@
"cell_type": "code",
"execution_count": null,
"id": "4e174aac-ce89-4559-826a-c78909da7add",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"filename = xsar.get_test_file('S1B_IW_GRDH_1SDV_20181013T062322_20181013T062347_013130_018428_Z010.SAFE')\n",
Expand All @@ -63,7 +67,9 @@
"cell_type": "code",
"execution_count": null,
"id": "a6146297-5776-4f18-aae5-4ab01323bf69",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"hv.Image(xsar_obj.dataset.sigma0.sel(pol='VV')).opts(alpha=0.7, cmap='gray', clim=(0,0.05))"
Expand All @@ -83,7 +89,9 @@
"cell_type": "code",
"execution_count": null,
"id": "0b1e0636-39a0-4b1f-be1c-f5288afb51a6",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"hv.Image(xsar_obj.dataset.sigma0.sel(pol='VV'), kdims=['sample', 'line']).opts(alpha=0.7, cmap='gray', clim=(0,0.05))"
Expand All @@ -108,7 +116,9 @@
"cell_type": "code",
"execution_count": null,
"id": "530ac853-1e43-4fbe-b1af-ad6b99dd3d76",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import rasterio\n",
Expand All @@ -129,7 +139,9 @@
"cell_type": "code",
"execution_count": null,
"id": "bb9e2502-58e7-4adb-bd1c-2e1e52ee9e94",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"sigma0_image = gv.Image(sigma0_proj.sel(pol='VV')).opts(alpha=0.3, cmap='gray', clim=(0,0.05))\n",
Expand All @@ -153,7 +165,9 @@
"cell_type": "code",
"execution_count": null,
"id": "b9d8658b-a5e3-456f-b046-db180b01d48e",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"sigma0_proj.sel(pol='VV').rio.to_raster('/tmp/sigma0_nocolor.tiff')"
Expand All @@ -164,17 +178,24 @@
"id": "2f872bee-95b4-4109-9f54-63da680bd3c9",
"metadata": {},
"source": [
"The geotiff can be read with `gv.load_tiff` but it's stored as float, and we have to pass a cmap. This image cannot be viewed with google earth "
"The geotiff can be read with `rioxarray` via `rasterio` but it's stored as float, and we have to pass a cmap. This image cannot be viewed with google earth "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4143b543-6d00-4c02-95ab-dfafe7dae274",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"gv.tile_sources.Wikipedia * gv.load_tiff('/tmp/sigma0_nocolor.tiff').opts(alpha=0.7, cmap='gray', clim=(0,0.05))"
"#gv.tile_sources.Wikipedia * gv.load_tiff('/tmp/sigma0_nocolor.tiff').opts(alpha=0.7, cmap='gray', clim=(0,0.05))\n",
"import rioxarray\n",
"tmp = rioxarray.open_rasterio('/tmp/sigma0_nocolor.tiff')\n",
"tmp = tmp.rename('sigma0_nocolor')\n",
"tmp2 = gv.util.from_xarray(tmp)\n",
"gv.tile_sources.Wikipedia * gv.Image(tmp2,vdims='sigma0_nocolor',kdims=['x','y']).opts(alpha=0.7, cmap='gray', clim=(0,0.05),colorbar=True,width=500,height=200)"
]
},
{
Expand All @@ -191,7 +212,9 @@
"cell_type": "code",
"execution_count": null,
"id": "04a8eacf-f173-480f-92eb-35340644e4fe",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from matplotlib import cm\n",
Expand All @@ -218,7 +241,9 @@
"cell_type": "code",
"execution_count": null,
"id": "6a1fa55c-cb1d-4ff7-9c91-50a06348c01b",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#xsar_obj.dataset['sigma0_rgba'] = rgb_sigma0 \n",
Expand All @@ -232,12 +257,19 @@
"cell_type": "code",
"execution_count": null,
"id": "13bf1e5b-4641-4536-b487-28d4062cf1dc",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# there is a transparency bug in geoviews (https://github.com/holoviz/geoviews/issues/571)\n",
"# but if loading this tiff in google earth, it should render properly\n",
"(gv.tile_sources.Wikipedia * gv.load_tiff('/tmp/sigma0_color.tiff')).opts(width=600,height=600)"
"tmp = rioxarray.open_rasterio('/tmp/sigma0_color.tiff')\n",
"tmp = tmp.rename('sigma0_color').isel(band=0)\n",
"tmp2 = gv.util.from_xarray(tmp)\n",
"print(tmp2)\n",
"# (gv.tile_sources.Wikipedia * gv.load_tiff('/tmp/sigma0_color.tiff')).opts(width=600,height=600)\n",
"(gv.tile_sources.Wikipedia * gv.Image(tmp2,vdims='sigma0_color',kdims=['x','y']).opts(cmap='magma',colorbar=True)).opts(width=600,height=600)"
]
},
{
Expand All @@ -256,7 +288,9 @@
"cell_type": "code",
"execution_count": null,
"id": "d0e23462-e809-4b10-b5eb-5f76e46bb9b0",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from rasterio.crs import CRS\n",
Expand Down Expand Up @@ -296,7 +330,9 @@
"cell_type": "code",
"execution_count": null,
"id": "2b614346-a467-46c7-9678-e700515f541a",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"\n",
Expand Down Expand Up @@ -325,7 +361,9 @@
"cell_type": "code",
"execution_count": null,
"id": "03cd39e3-5b5a-458c-9173-216f0cea27d5",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import holoviews as hv\n",
Expand All @@ -345,7 +383,9 @@
"cell_type": "code",
"execution_count": null,
"id": "e3c49945-7e49-48e4-bf86-653a7cae2b18",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"merged_lonlat = merged_grid.rio.reproject(4326)\n",
Expand All @@ -363,7 +403,7 @@
"source": [
"## Mapping a raster onto original xsar grid\n",
"\n",
"Using `rioxarray.reproject_match` with a destination grid containing gcps (like xsar dataset) is currently not supported, but xsar provide [xsar.Sentinel1Dataset.map_raster](../basic_api.rst#xsar.Sentinel1Dataset.map_raster).\n",
"Using `rioxarray.reproject_match` with a destination grid containing gcps (like xsar dataset) is currently not supported, but xsar provide [xsar.BaseDataset.map_raster](../basic_api.rst#xsar.BaseDataset.map_raster).\n",
"\n",
"It's argument is a projected dataset, that we want to map onto the xsar grid.\n",
"\n"
Expand All @@ -373,7 +413,9 @@
"cell_type": "code",
"execution_count": null,
"id": "e7d86b33-6a1b-464b-bbcd-e89bbb9e575a",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"#xsar_obj.dataset['sigma0_rasterized'] = \n",
Expand All @@ -387,7 +429,9 @@
"cell_type": "code",
"execution_count": null,
"id": "14381b11-d17c-432c-bea3-6a695802e000",
"metadata": {},
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(\n",
Expand All @@ -413,7 +457,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
"version": "3.9.15"
}
},
"nbformat": 4,
Expand Down
10 changes: 8 additions & 2 deletions src/xsar/base_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,15 @@ def get_mask(self, name, describe=False):
return self._mask_geometry[name]

def _get_mask_intersecting_geometries(self, name):
"""
:param name: str(eg land)
:return:
"""
if self._mask_intersecting_geometries[name] is None:
gseries = gpd.GeoSeries(self._get_mask_feature(name)
.intersecting_geometries(self.footprint.bounds))
gseries = gpd.GeoSeries(self._get_mask_feature(name).geometries())
# gseries = gpd.GeoSeries(self._get_mask_feature(name)
# .intersecting_geometries(self.footprint))
if len(gseries) == 0:
# no intersection with mask, but we want at least one geometry in the serie (an empty one)
gseries = gpd.GeoSeries([Polygon()])
Expand Down

0 comments on commit b0d5951

Please sign in to comment.