Using matplotlib and Rasterio I am trying to save a raster as a GeoTIFF as well as repoject it?
I have been able to plot and display my raster images using matplotlib. That part is successful. The part which I am stuck on is be able to save that plot somehow. For rasterio I've found two helpful tutorials:
https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html
and
https://www.earthdatascience.org/courses/earth-analytics-python/multispectral-remote-sensing-in-python/export-numpy-array-to-geotiff-in-python/
I've gotten a calculate for a function called NDVI and through matplotlib I can display it just the way I want with the following code. But when I go to save the file as a GeoTIFF the image on my desktop is all black. I plan to reproject the data too and I have that code commented out.
Here is my code:
import rasterio
import matplotlib.pyplot as plt
import numpy as np
nirband = r"LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF"
redband =r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF"
#rasterio.windows.Window(col_off, row_off, width, height)
window = rasterio.windows.Window(2000,2000,800,600)
with rasterio.open(nirband) as src:
subset = src.read(1, window=window)
fig, ax = plt.subplots(figsize=(12,6))
plt.imshow(subset)
plt.title(f'Band 5 Subset')
with rasterio.open(nirband) as src:
nir = src.read(1, window=window)
with rasterio.open(redband) as src:
red = src.read(1, window=window)
red = red.astype(float)
nir = nir.astype(float)
np.seterr(divide='ignore', invalid='ignore')
ndvi = np.empty(nir.shape, dtype=rasterio.float32)
check = np.logical_or ( red > 0, nir > 0 )
naip_ndvi = np.where ( check, (1.0*(nir - red )) / (1.0*( nir + red )),-2 )
fig, ax = plt.subplots(figsize=(12,6))
ndvi = ax.imshow(naip_ndvi)
ax.set(title="NDVI")
with rasterio.open("LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF") as src:
naip_data_ras = src.read()
naip_meta = src.profile
with rasterio.open('MyExample.tif', 'w',**naip_meta) as dst:
dst.write(naip_ndvi, window=window)
# =============================================================================
# with rasterio.open('example.tif') as dataset:
#
# # Read the dataset's valid data mask as a ndarray.
# mask = dataset.dataset_mask()
#
# # Extract feature shapes and values from the array.
# for geom, val in rasterio.features.shapes(
# mask, transform=dataset.transform):
#
# # Transform shapes from the dataset's own coordinate
# # reference system to CRS84 (EPSG:4326).
# geom = rasterio.warp.transform_geom(
# dataset.crs, 'EPSG:4326', geom, precision=6)
#
# # Print GeoJSON shapes to stdout.
# print(geom)
# =============================================================================
Here is what NDVI looks like when I use matplotlib (I want to save this to my desktop as a GeoTIFF file):
Thank you for any and all help!
python numpy matplotlib rasterio
add a comment |
I have been able to plot and display my raster images using matplotlib. That part is successful. The part which I am stuck on is be able to save that plot somehow. For rasterio I've found two helpful tutorials:
https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html
and
https://www.earthdatascience.org/courses/earth-analytics-python/multispectral-remote-sensing-in-python/export-numpy-array-to-geotiff-in-python/
I've gotten a calculate for a function called NDVI and through matplotlib I can display it just the way I want with the following code. But when I go to save the file as a GeoTIFF the image on my desktop is all black. I plan to reproject the data too and I have that code commented out.
Here is my code:
import rasterio
import matplotlib.pyplot as plt
import numpy as np
nirband = r"LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF"
redband =r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF"
#rasterio.windows.Window(col_off, row_off, width, height)
window = rasterio.windows.Window(2000,2000,800,600)
with rasterio.open(nirband) as src:
subset = src.read(1, window=window)
fig, ax = plt.subplots(figsize=(12,6))
plt.imshow(subset)
plt.title(f'Band 5 Subset')
with rasterio.open(nirband) as src:
nir = src.read(1, window=window)
with rasterio.open(redband) as src:
red = src.read(1, window=window)
red = red.astype(float)
nir = nir.astype(float)
np.seterr(divide='ignore', invalid='ignore')
ndvi = np.empty(nir.shape, dtype=rasterio.float32)
check = np.logical_or ( red > 0, nir > 0 )
naip_ndvi = np.where ( check, (1.0*(nir - red )) / (1.0*( nir + red )),-2 )
fig, ax = plt.subplots(figsize=(12,6))
ndvi = ax.imshow(naip_ndvi)
ax.set(title="NDVI")
with rasterio.open("LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF") as src:
naip_data_ras = src.read()
naip_meta = src.profile
with rasterio.open('MyExample.tif', 'w',**naip_meta) as dst:
dst.write(naip_ndvi, window=window)
# =============================================================================
# with rasterio.open('example.tif') as dataset:
#
# # Read the dataset's valid data mask as a ndarray.
# mask = dataset.dataset_mask()
#
# # Extract feature shapes and values from the array.
# for geom, val in rasterio.features.shapes(
# mask, transform=dataset.transform):
#
# # Transform shapes from the dataset's own coordinate
# # reference system to CRS84 (EPSG:4326).
# geom = rasterio.warp.transform_geom(
# dataset.crs, 'EPSG:4326', geom, precision=6)
#
# # Print GeoJSON shapes to stdout.
# print(geom)
# =============================================================================
Here is what NDVI looks like when I use matplotlib (I want to save this to my desktop as a GeoTIFF file):
Thank you for any and all help!
python numpy matplotlib rasterio
add a comment |
I have been able to plot and display my raster images using matplotlib. That part is successful. The part which I am stuck on is be able to save that plot somehow. For rasterio I've found two helpful tutorials:
https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html
and
https://www.earthdatascience.org/courses/earth-analytics-python/multispectral-remote-sensing-in-python/export-numpy-array-to-geotiff-in-python/
I've gotten a calculate for a function called NDVI and through matplotlib I can display it just the way I want with the following code. But when I go to save the file as a GeoTIFF the image on my desktop is all black. I plan to reproject the data too and I have that code commented out.
Here is my code:
import rasterio
import matplotlib.pyplot as plt
import numpy as np
nirband = r"LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF"
redband =r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF"
#rasterio.windows.Window(col_off, row_off, width, height)
window = rasterio.windows.Window(2000,2000,800,600)
with rasterio.open(nirband) as src:
subset = src.read(1, window=window)
fig, ax = plt.subplots(figsize=(12,6))
plt.imshow(subset)
plt.title(f'Band 5 Subset')
with rasterio.open(nirband) as src:
nir = src.read(1, window=window)
with rasterio.open(redband) as src:
red = src.read(1, window=window)
red = red.astype(float)
nir = nir.astype(float)
np.seterr(divide='ignore', invalid='ignore')
ndvi = np.empty(nir.shape, dtype=rasterio.float32)
check = np.logical_or ( red > 0, nir > 0 )
naip_ndvi = np.where ( check, (1.0*(nir - red )) / (1.0*( nir + red )),-2 )
fig, ax = plt.subplots(figsize=(12,6))
ndvi = ax.imshow(naip_ndvi)
ax.set(title="NDVI")
with rasterio.open("LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF") as src:
naip_data_ras = src.read()
naip_meta = src.profile
with rasterio.open('MyExample.tif', 'w',**naip_meta) as dst:
dst.write(naip_ndvi, window=window)
# =============================================================================
# with rasterio.open('example.tif') as dataset:
#
# # Read the dataset's valid data mask as a ndarray.
# mask = dataset.dataset_mask()
#
# # Extract feature shapes and values from the array.
# for geom, val in rasterio.features.shapes(
# mask, transform=dataset.transform):
#
# # Transform shapes from the dataset's own coordinate
# # reference system to CRS84 (EPSG:4326).
# geom = rasterio.warp.transform_geom(
# dataset.crs, 'EPSG:4326', geom, precision=6)
#
# # Print GeoJSON shapes to stdout.
# print(geom)
# =============================================================================
Here is what NDVI looks like when I use matplotlib (I want to save this to my desktop as a GeoTIFF file):
Thank you for any and all help!
python numpy matplotlib rasterio
I have been able to plot and display my raster images using matplotlib. That part is successful. The part which I am stuck on is be able to save that plot somehow. For rasterio I've found two helpful tutorials:
https://rasterio.readthedocs.io/en/latest/topics/windowed-rw.html
and
https://www.earthdatascience.org/courses/earth-analytics-python/multispectral-remote-sensing-in-python/export-numpy-array-to-geotiff-in-python/
I've gotten a calculate for a function called NDVI and through matplotlib I can display it just the way I want with the following code. But when I go to save the file as a GeoTIFF the image on my desktop is all black. I plan to reproject the data too and I have that code commented out.
Here is my code:
import rasterio
import matplotlib.pyplot as plt
import numpy as np
nirband = r"LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF"
redband =r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF"
#rasterio.windows.Window(col_off, row_off, width, height)
window = rasterio.windows.Window(2000,2000,800,600)
with rasterio.open(nirband) as src:
subset = src.read(1, window=window)
fig, ax = plt.subplots(figsize=(12,6))
plt.imshow(subset)
plt.title(f'Band 5 Subset')
with rasterio.open(nirband) as src:
nir = src.read(1, window=window)
with rasterio.open(redband) as src:
red = src.read(1, window=window)
red = red.astype(float)
nir = nir.astype(float)
np.seterr(divide='ignore', invalid='ignore')
ndvi = np.empty(nir.shape, dtype=rasterio.float32)
check = np.logical_or ( red > 0, nir > 0 )
naip_ndvi = np.where ( check, (1.0*(nir - red )) / (1.0*( nir + red )),-2 )
fig, ax = plt.subplots(figsize=(12,6))
ndvi = ax.imshow(naip_ndvi)
ax.set(title="NDVI")
with rasterio.open("LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF") as src:
naip_data_ras = src.read()
naip_meta = src.profile
with rasterio.open('MyExample.tif', 'w',**naip_meta) as dst:
dst.write(naip_ndvi, window=window)
# =============================================================================
# with rasterio.open('example.tif') as dataset:
#
# # Read the dataset's valid data mask as a ndarray.
# mask = dataset.dataset_mask()
#
# # Extract feature shapes and values from the array.
# for geom, val in rasterio.features.shapes(
# mask, transform=dataset.transform):
#
# # Transform shapes from the dataset's own coordinate
# # reference system to CRS84 (EPSG:4326).
# geom = rasterio.warp.transform_geom(
# dataset.crs, 'EPSG:4326', geom, precision=6)
#
# # Print GeoJSON shapes to stdout.
# print(geom)
# =============================================================================
Here is what NDVI looks like when I use matplotlib (I want to save this to my desktop as a GeoTIFF file):
Thank you for any and all help!
python numpy matplotlib rasterio
python numpy matplotlib rasterio
edited Jan 6 at 7:27
prusswan
5,41232749
5,41232749
asked Nov 19 '18 at 22:45
yuen2yuen2
194
194
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383733%2fusing-matplotlib-and-rasterio-i-am-trying-to-save-a-raster-as-a-geotiff-as-well%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383733%2fusing-matplotlib-and-rasterio-i-am-trying-to-save-a-raster-as-a-geotiff-as-well%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown