rioxarray icon indicating copy to clipboard operation
rioxarray copied to clipboard

Open ASCII file format from String

Open sehHeiden opened this issue 3 years ago • 3 comments

I would ask for a way to open ascii files that are already opened as string/list of strings.

Background I sometimes work with ascii files for an import national agency that offers asc- files non-standard format. I have to mess abit with the file beofore the raster can sucessfully be opened. It would be nice, that I could manipulate the files and use them dirrectly from memory without the need to save the files. An alternative would be automatically be abe to read that non standard format.

The format has an extended for example header:

[header]
Datensatz_Hersteller=DWD/KU1HA
Datensatz_Version=V003
Datensatz_Erstellungsdatum=05.02.2018 13:27:45
Org_Dateiname=grids_germany_annual_radiation_global_1991.asc
Titel_1=Globalstrahlung
Titel_2=Jahressumme
Titel_3=auf die horizontale Ebene
Land=DE
Jahr=1991
Monat=
Bemerkung_1=weitere Erlaeuterungen siehe BESCHREIBUNG_gridsgermany_annual_radiation_global_de.pdf
Bemerkung_2=see DESCRIPTION_gridsgermany_annual_radiation_global_en.pdf
Bemerkung_3=
Koordinatensystem=Gauss-Krueger 3. Meridianstreifen Potsdam-Datum
Werte_Dimension=kWh/m2
Werte_keineDaten=-999
Werte_Minimum=931.00
Werte_Maximum=1274.00
Werte_Trennzeichen=Leerzeichen
ZeilenendeFormat=CrLf (Standard auf Windows Betriebssysteme)
[ASCII-Raster-Format]
NCOLS 654
NROWS 866
XLLCORNER 3280500
YLLCORNER 5237500
CELLSIZE 1000
NODATA_VALUE -999

sehHeiden avatar Jul 14 '22 10:07 sehHeiden

The reading logic is handled by GDAL. See: https://gdal.org/drivers/raster/aaigrid.html. If you would like to see this format supported, I recommend opening up an issue here: https://github.com/OSGeo/gdal

snowman2 avatar Jul 14 '22 13:07 snowman2

Also, you could look into using StringIO/BytesIO to pass in a string/binary as a file: https://docs.python.org/3/library/io.html

snowman2 avatar Jul 14 '22 13:07 snowman2

Hi I, tried StringIO before with:

    r_f = StringIO(file_text)
    r = open_rasterio(r_f)

I get the Error: AttributeError: '_GeneratorContextManager' object has no attribute 'subdatasets'. The file_text can be read after saving to:

    with open(tmp_folder / file_path.name, "w") as tmp_f:
        tmp_f.write(file_text)

    r = open_rasterio(tmp_folder / all_types.name)

sehHeiden avatar Jul 14 '22 15:07 sehHeiden

See: #562

snowman2 avatar Aug 16 '22 21:08 snowman2

Also related: https://github.com/rasterio/rasterio/issues/2550

snowman2 avatar Aug 16 '22 21:08 snowman2