Issue on using imdlib package with rasterstats to calculate zonal statistics

61 views
Skip to first unread message

Dammalapati Sai Krishna

unread,
Sep 7, 2023, 6:58:15 AM9/7/23
to datameet
Hi,

This question is for people who have worked on Python's imdlib package.

The rasters downloaded using imdlib has the following affine transformation:
| 0.25, 0.00, 66.38|
| 0.00, 0.25, 6.38|
| 0.00, 0.00, 1.00|

If I'm not wrong, the general standard is to define the pixel height in negative with top-left bounding box coordinates as X,Y. At least, the images downloaded from GEE follow that format.

But in the array above you can see (in bold blue) that the pixel height is specified in the positive. And the bottom-left bounding box coordinates are specified (bold red).

The issue I faced with this is that when I try to calculate zonal statistics using rasterstats, the positive height becomes a problem (negative dimensions error). I changed the transformation matrix and flipped the array to be able to run zonal stats.

Want to know if there is a standard in defining affine transformation matrix and if imdlib is not using that.

Thanks!

Nikhil VJ

unread,
Sep 7, 2023, 10:35:04 AM9/7/23
to data...@googlegroups.com
Hi Dammalapati,

I had used imdlib python package a year back, but the data fetched using it was vector data, not raster. It had latitude, longitude, date and value of either rainfall in mm or temperature in degrees celsius. If you're fetching a dataset what is different from rainfall or temperature data, then pls specify.

Here's a few lines of code that fetches the yearly data of 1 year, converts it to a simpler flat table in pandas dataframe and saves to CSV file:

imdlib.get_data('rain', 2000, 2000, 'yearwise')
rain1 = imdlib.open_data('rain', 2000, 2000, 'yearwise').get_xarray().to_dataframe()
rain1.to_csv('rain1.csv')


Note: there were junk data placeholders for indicating null value (ie, no value available for that location and year). Temperature: >99, Rainfall: < -100 . 

--
Cheers,
Nikhil VJ
https://nikhilvj.co.in


--
Datameet is a community of Data Science enthusiasts in India. Know more about us by visiting http://datameet.org
---
You received this message because you are subscribed to the Google Groups "datameet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datameet+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/datameet/98c9162b-e45e-4bba-9095-be37683b7e41n%40googlegroups.com.

Dammalapati Sai Krishna

unread,
Sep 7, 2023, 11:31:40 AM9/7/23
to data...@googlegroups.com
Hi Nikhil!

I remember having a correspondence with you about imdlib last year.

I am fetching the rainfall data - but in the raster formal.

data = imd.get_data(
        var_type='rain',
        start_yr=year,
        end_yr=year,
        fn_format='yearwise',
        file_dir=DATA_FOLDER
    )

This downloads the rainfall data in .grd format. Later I convert it into .tif
data.to_geotiff(
        '{}.tif'.format(year),
        TIFF_DATA_FOLDER
    )

Faced issues with the affine transformation of this raster. Solved it for my need. But curious to know if there is a standard or not in defining affine transformation. Never faced this issue with other raster images.

Best,
Dammalapati Sai Krishna

Reply all
Reply to author
Forward
0 new messages