Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass numpy-nDarray directly to spectralpython #3

Closed
epifanio opened this issue Mar 18, 2014 · 4 comments
Closed

Pass numpy-nDarray directly to spectralpython #3

epifanio opened this issue Mar 18, 2014 · 4 comments
Labels

Comments

@epifanio
Copy link

In my workflow the data are coming from GRASS - GIS, from which i'm exporting the raster layers as an Envi multiband dataset which i use as input to spectralpython.
In GRASS the same layers are available as numpy N-Darray without involve any export, how can i use spectralpython to read a numpy N-DArray as input instead of use the envi dataformat ?

@tboggs
Copy link
Member

tboggs commented Mar 18, 2014

Most of the spectral functions can operate on numpy.ndarray objects so you should be able to stack the GRASS layers into an ndarray and pass it to the spectral functions. If you need to read the data from a file, rather than interface GRASS directly from python, you can use numpy.save to write the array to a .npy file, then use numpy.load to read it.

@epifanio
Copy link
Author

It worked as aspected :)
i first stored the image name in a list
then i read the dataset as a numpy array and I append each array in a empty list
then i concatenate all the layer from the list of arrays in a single one

#img = ['layername1', 'layername2', ... , ....]
import numpy as np
arraylist = []
for i in img[]:
    rl = garray.array()
    rl.read(i)
    arraylist.append(rl)
imagegroup = np.concatenate([aux[..., np.newaxis] for aux in arraylist], axis=2)

maybe there is abetter way to concatenate my layers, but as it is works great!
Thank you so much!!!

@tboggs
Copy link
Member

tboggs commented Mar 18, 2014

The way you are doing it looks fine. If you know the shape, data type, and number layers, you could save some memory by pre-allocating imagegroup, then setting the bands directly (e.g., imagegroup[:, :, i] = rl). That way, you wouldn't have all the layers in memory twice (in both arraylist and imagegroup).

@tboggs tboggs closed this as completed Mar 18, 2014
@epifanio
Copy link
Author

👍

i changed the code to :

rl = garray.array()
imagegroup = np.empty((rl.shape[0], rl.shape[1], 8), dtype='float')
for i,v in enumerate(img[3:]):
    rl.read(v)
    imagegroup[:, :, i] = rl

Thanks a lot!

tboggs added a commit that referenced this issue Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants