3

I am trying to open the jp2 rasters products in python as raster, without success when we use the "raterio" and "gdal" packages.
I get this errors:

RasterioIOError: 'b4.jp2' not recognized as a supported file format.
RuntimeError: `b4.jp2' not recognized as a supported file format.

I do manage to open them as a regular matrix with "glymur" package but then i lose the geo data.

If someone know other pachage to open jp2 as raster i would be happy to know!

I tried to follow this answer: Python3.7 rasterio library cant open jp2 installed fiona by conda or: https://gis.stackexchange.com/questions/250833/gdal-missing-jp2-driver-files-how-to-install-them/250837#250837 and tried to install :conda create -n mynewenv -c conda-forge gdal rasterio but i still get the same errors.

b4 = rasterio.open('b4.jp2')
b4 = rasterio.open('b4.jp2', driver= 'JP2OpenJPEG')
Alon8200
  • 151
  • 1
  • 2
  • 8
  • can you open it using `gdal.Open()` – dubbbdan Oct 23 '19 at 13:45
  • also what does `rasterio.gdal_version()` return? – dubbbdan Oct 23 '19 at 13:57
  • no i tried, didn't work, gdal version: '2.3.3' – Alon8200 Oct 23 '19 at 14:39
  • how about `gdal.VersionInfo()` – dubbbdan Oct 23 '19 at 14:58
  • gdal.VersionInfo() gives '2030300' – Alon8200 Oct 23 '19 at 15:05
  • I wish i knew the answer to this, but i rememver I had a really hard time getting rasterio to install properly. I am using gdal version `2040100`. – dubbbdan Oct 23 '19 at 15:07
  • according to [this comment](https://github.com/mapbox/rasterio/issues/1790#issuecomment-537297127), you could try installing rasterio and linking it to you system gdal `pip install numpy; pip install -I --no-binary rasterio rasterio`, but that sounds iike a can of worms to me. – dubbbdan Oct 23 '19 at 15:10
  • i'll try this, in a couple of days and let you know.. thanks! – Alon8200 Oct 23 '19 at 15:23
  • Hi, that also doesn't work.. do you have other solutions? – Alon8200 Oct 31 '19 at 09:27
  • I would think about using `gdal warp` to convert `jp2` to `GTiff`. Other than that all you cand do is uninstall everything geospatial, and reinstall specific a specific version of rasterio that supports `.jp2` driver. – dubbbdan Oct 31 '19 at 12:21
  • I just installed rasterio on google colab and apparently `rasterio.__version__` = 1.1.0 with `rasterio.gdal_version()`` = `2.4.2` supports the `JP2OpenJPEG` driver. – dubbbdan Oct 31 '19 at 12:24
  • you can check if the specific rasterio version supports the `.jp2` using `rio`. `rio env --formats` – dubbbdan Oct 31 '19 at 12:25

1 Answers1

5

This is quite similar to what you have tried, but this procedure just worked for me. Note: I am specifying I want a python 3.7 environment. From the anaconda prompt:

conda create --name testing python=3.7
conda activate testing
conda install -c conda-forge gdal rasterio

Verified correct install:

(testing) C:\Users\dan>python
Python 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 22:01:29) [MSC v.1900 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import rasterio
>>> import gdal
>>> gdal.VersionInfo()
'3000100'
>>> rasterio.gdal_version()
'3.0.1'

And check to make sure .jp2 driver is supported. From the anaconda prompt:

rio env --formats

which returns a long list with the entries:

JML: OpenJUMP JML
JP2OpenJPEG: JPEG-2000 driver based on OpenJPEG library
JPEG: JPEG JFIF

And here is the export from conda env export:

name: testing
channels:
  - conda-forge
  - defaults
dependencies:
  - affine=2.3.0=py_0
  - attrs=19.3.0=py_0
  - boost-cpp=1.70.0=h6a4c333_2
  - bzip2=1.0.8=hfa6e2cd_1
  - ca-certificates=2019.9.11=hecc5488_0
  - certifi=2019.9.11=py37_0
  - cfitsio=3.470=hfa6e2cd_2
  - click=7.0=py_0
  - click-plugins=1.1.1=py_0
  - cligj=0.5.0=py_0
  - curl=7.65.3=h4496350_0
  - expat=2.2.5=he025d50_1004
  - freetype=2.10.0=h563cfd7_1
  - freexl=1.0.5=hd288d7e_1002
  - gdal=3.0.1=py37h2fee047_10
  - geos=3.7.2=he025d50_2
  - geotiff=1.5.1=h6e819c3_4
  - gettext=0.19.8.1=hb01d8f6_1002
  - glib=2.58.3=hc0c2ac7_1002
  - hdf4=4.2.13=hf8e6fe8_1002
  - hdf5=1.10.5=nompi_ha405e13_1104
  - icu=64.2=he025d50_1
  - jpeg=9c=hfa6e2cd_1001
  - kealib=1.4.10=hf7dc31f_1005
  - krb5=1.16.3=hdd46e55_1001
  - libblas=3.8.0=14_mkl
  - libcblas=3.8.0=14_mkl
  - libcurl=7.65.3=h4496350_0
  - libffi=3.2.1=h6538335_1006
  - libgdal=3.0.1=h2491c49_10
  - libiconv=1.15=hfa6e2cd_1005
  - libkml=1.3.0=h4ece8bf_1010
  - liblapack=3.8.0=14_mkl
  - libnetcdf=4.7.1=nompi_h8d74e2a_101
  - libpng=1.6.37=h7602738_0
  - libpq=11.5=hb0bdaea_1
  - libspatialite=4.3.0a=hee5a54d_1031
  - libssh2=1.8.2=h642c060_2
  - libtiff=4.0.10=h6512ee2_1003
  - libxml2=2.9.10=h9ce36c8_0
  - lz4-c=1.8.3=he025d50_1001
  - numpy=1.17.3=py37hc71023c_0
  - openjpeg=2.3.1=hb24c2e3_1
  - openssl=1.1.1c=hfa6e2cd_0
  - pcre=8.43=h6538335_0
  - poppler=0.67.0=h92819f6_7
  - poppler-data=0.4.9=1
  - postgresql=11.5=h06f7779_1
  - proj=6.2.0=ha7a8c7b_1
  - pyparsing=2.4.2=py_0
  - python=3.7.3=h510b542_1
  - rasterio=1.1.0=py37h2617b1b_0
  - snuggs=1.4.7=py_0
  - tbb=2018.0.5=he980bc4_0
  - tiledb=1.6.2=h30b6f50_1
  - tk=8.6.9=hfa6e2cd_1003
  - xerces-c=3.2.2=h6538335_1004
  - xz=5.2.4=h2fa13f4_1001
  - zlib=1.2.11=h2fa13f4_1006
  - zstd=1.4.0=hd8a0e53_0
  - intel-openmp=2019.4=245
  - m2w64-expat=2.1.1=2
  - m2w64-gcc-libgfortran=5.3.0=6
  - m2w64-gcc-libs=5.3.0=7
  - m2w64-gcc-libs-core=5.3.0=7
  - m2w64-gettext=0.19.7=2
  - m2w64-gmp=6.1.0=2
  - m2w64-libiconv=1.14=6
  - m2w64-libwinpthread-git=5.0.0.4634.697f757=2
  - m2w64-xz=5.2.2=2
  - mkl=2019.4=245
  - msys2-conda-epoch=20160418=1
  - pip=19.3.1=py37_0
  - setuptools=41.6.0=py37_0
  - sqlite=3.30.1=he774522_0
  - vc=14.1=h0510ff6_4
  - vs2015_runtime=14.16.27012=hf0eaf9b_0
  - wheel=0.33.6=py37_0
  - wincertstore=0.2=py37_0
prefix: C:\Anaconda2\envs\testing

proof it works:

(testing) C:\Users\RDCRLDDH>python
Python 3.7.5 (default, Oct 31 2019, 15:18:51) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import rasterio
>>> ds = rasterio.open(r"C:\Users\RDCRLDDH\Desktop\T36QVM_20191023T082009_B01.jp2")
>>> ds.crs
CRS.from_epsg(32636)
>>> ds.read(1)
array([[2372, 2344, 2312, ..., 1423, 1406, 1481],
       [2326, 2292, 2279, ..., 1492, 1462, 1495],
       [2317, 2271, 2261, ..., 1529, 1517, 1511],
       ...,
       [1958, 1946, 1947, ..., 1697, 1734, 1730],
       [1960, 1960, 1967, ..., 1714, 1758, 1721],
       [1959, 1957, 1960, ..., 1769, 1752, 1701]], dtype=uint16)
>>> ds.transform
Affine(60.0, 0.0, 399960.0,
       0.0, -60.0, 2700000.0)
>>>

From spyder, you can check to see where spyder is running from using

import sys
print(sys.executable)

You should see something like:

C:\Anaconda2\envs\testing\pythonw.exe

The installation directory C:\Anaconda2\ will depend on where you have anaconda installed to, But the latter part envs\testing\pythonw.exe should be the same.

dubbbdan
  • 2,650
  • 1
  • 25
  • 43
  • Hi, installed as mentioned here, at prompt i get for rio env --formats the jp2 entries (which is good i didn't get that before). and the same as yours for conda env export. but at the spyder editor, gdal.VersionInfo() gives '2030300' , rasterio.gdal_version() gives '2.3.3'. however i still can't open jp2 with rasterio.open.. – Alon8200 Nov 03 '19 at 12:28
  • I tried couple of jp2 files with both rasterio and gdal open, with no successes.. I agree that's frustrating! – Alon8200 Nov 05 '19 at 11:51
  • Hi, maybe there is another solution? – Alon8200 Nov 07 '19 at 09:26
  • ooh, I just re-read your first comment. Install spyder into the environment `testing`. `pip install spyder`. Then run spyder from that environment. – dubbbdan Nov 07 '19 at 14:05
  • Hi, I pip install spyder again, after conda activate testing, thats what you mean? rasterio.gdal_version() still gives '2.3.3'.. – Alon8200 Nov 09 '19 at 21:52
  • That doesn't make any sense. If you installed following my directions, you should see the `rasterio.gdal_version() == '3.0.1'`. What if you open a python interpreter in the command prompt as I have shown above? – dubbbdan Nov 10 '19 at 16:56
  • @asafalon updated to show how to check which environment spyder is running from – dubbbdan Nov 10 '19 at 19:07
  • Hi! if i use the prompt as interpreter from prompt i get rasterio.gdal_version() == '3.0.2', but if i use spyder and define in "preferences" python interpreter from "testing " env i.e: C:\Users\shilo\Anaconda3\envs\testing\python.exe, i cannot even import rasterio or gdal (in spyder) – Alon8200 Nov 12 '19 at 14:25
  • yeah, the python path manager won't change how spyder is connecting to the python executable. All that does is allow you to import packages that aren't in the `site-packages` folder. If you have installed spyder in `testing`, you can find the executable at: `C:\Users\shilo\Anaconda3\envs\testing\Scripts\spyder.exe` – dubbbdan Nov 12 '19 at 14:30
  • Have a look at: https://github.com/spyder-ide/spyder/wiki/Working-with-packages-and-environments-in-Spyder – dubbbdan Nov 12 '19 at 14:38
  • I've never gotten the [modular approach](https://github.com/spyder-ide/spyder/wiki/Working-with-packages-and-environments-in-Spyder#the-modular-approach) to work. I just use the [naive appoach](https://github.com/spyder-ide/spyder/wiki/Working-with-packages-and-environments-in-Spyder#the-naive-approach) – dubbbdan Nov 12 '19 at 14:40
  • if this helped you, please consider accepting the answer. – dubbbdan Nov 19 '19 at 15:23
  • Hi, i tried execute C:\Users\shilo\Anaconda3\envs\testing\Scripts\spyder.exe but spyder doesn't run from it..Is there other way to run rasterio and gdal from based env, and make them upgrade? – Alon8200 Nov 20 '19 at 16:12
  • yes, it opens a prompt window for a sec, but doesn't run the spyder – Alon8200 Nov 20 '19 at 17:10
  • what a pain, how did you install spyder? – dubbbdan Nov 21 '19 at 14:27
  • I am pretty sure you can only have one instance of spyder open at a time. Make sure you dont have spyder running from base. – dubbbdan Nov 21 '19 at 14:37
  • Hi, when i'm trying to execute spyder from windows (i.e double click on spyder.exe which locate in C:\Users\shilo\Anaconda3\envs\testing\Scripts\spyder.exe ) i get propmpt window that disappear after 1 sec, i made sure no other spyder running. when i'm trying to execute from command prompt, i get: "ModuleNotFoundError: No module named 'PySide'" – Alon8200 Nov 25 '19 at 15:02
  • I installed spyder at testing env, by pip – Alon8200 Nov 25 '19 at 15:03
  • Try using `conda-forge` : `conda install -c conda-forge spyder` – dubbbdan Nov 25 '19 at 15:04
  • still the same.. sorry – Alon8200 Nov 25 '19 at 17:50
  • if you are getting module not found errors, something is wrong with the spyder install. Try creating a new envrionment and installing spyder in there. – dubbbdan Nov 25 '19 at 19:15