corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
511 stars 81 forks source link

Open ASCII file format from String #549

Closed sehHeiden closed 2 years ago

sehHeiden commented 2 years ago

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
snowman2 commented 2 years ago

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 commented 2 years ago

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

sehHeiden commented 2 years ago

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)
snowman2 commented 2 years ago

See: #562

snowman2 commented 2 years ago

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