Python Forum
Keep inner Values of 2D array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keep inner Values of 2D array
#1
Hi
I have a tiff with lakes, which I converted into a 2D array. I would like to keep the outline of the lakes in a 2D array.

import rasterio
import numpy as np

with rasterio.open('myfile.tif') as dtm:
    array = dtm.read(1)

array[array>0] = 1
array = array.astype(float)
array[array==0] = np.nan
My array looks like this now, a lake can be seen in the upper right corner:


     [[      nan       nan       nan ... 2888.001  [b]2877.458  2867.5798[/b]]
     [      nan       nan       nan ... 2890.188 [b] 2879.2876 2869.0415][/b]
     [      nan       nan       nan ... 2892.2622 2880.9907 2870.4985]
     ...
     [      nan       nan       nan ...       nan       nan       nan]
     [      nan       nan       nan ...       nan       nan       nan]
     [      nan       nan       nan ...       nan       nan       nan]]
I wish to keep the outline of the lakes, thus I have to set all values to nan, which are NOT located next to a nan (marked in bold).

I have tried:

array[1:-1, 1:-1] = np.nan
However, this converts ALL inner values of the entire array to nan, not just the inner values of the lakes.

If you know of a completely different way how to keep the outline of the lakes (maybe with rasterio), I would also be thankful. I hope I made clear what I mean with inner values of the lakes. Important: A lake can appear anywhere in the array not just in a corner

Tim
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace sets of values in an array without using loops paul18fr 7 1,630 Jun-20-2022, 08:15 PM
Last Post: paul18fr
  Identifying consecutive masked values in a 3D data array chai0404 12 5,636 Feb-01-2020, 12:59 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020