isciences / exactextract

Fast and accurate raster zonal statistics
Apache License 2.0
258 stars 33 forks source link

Fraction output for python version #123

Closed ShervanGharari closed 3 months ago

ShervanGharari commented 3 months ago

Greetings,

I am trying the Python interface of exactextract. I am simply doing the following:

from exactextract import exact_extract

rast = '/path/to/raster.tif'
polys = '/path/to/shapefile.gpkg'

output = exact_extract(rast, polys, ["frac"], include_cols=['ID'], output = 'pandas')

However, the output is sth like:

          ID                                               frac
0      78000001  [0.09155590311746499, 0.032862877479797076, 0....
1      78000002  [0.02923689025229496, 0.019298079545838166, 0....
2      78000003  [0.06110069383834066, 0.029253118458412816, 0....
3      78000004  [0.07007407540065459, 0.012513232368517771, 0....
4      78000005  [0.009211099810479408, 0.022794562426456118, 0...
...         ...                                                ...
38711  78038712           [0.1392004285351904, 0.8607995714648096]
...         ...                                                ...

which does not have a column name for each class fraction, while its length is also varying (perhaps depending on the intersecting class with a shape or feature). The same issue is also present for JSON output (no column name or description for an array of fractions). I'd like to know if I am doing sth wrong here. Thank you in advance for your time to look into this.

ShervanGharari commented 3 months ago

I can output the unique values of the raster intersecting with each shape. However, it is not clear to me if the order of the unique values array and fraction array are the same. A clarification on that or other solutions would be highly appreciated. Thank you very much in advance for your time.

    ID              frac                                                    unique
0   78000001    [0.09155590311746499, 0.032862877479797076, 0....   [14, 8, 5, 10, 6, 1]
1   78000002    [0.02923689025229496, 0.019298079545838166, 0....   [18, 14, 19, 8, 16, 5, 10, 6, 12, 1]
2   78000003    [0.06110069383834066, 0.029253118458412816, 0....   [10, 14, 1, 6]
3   78000004    [0.07007407540065459, 0.012513232368517771, 0....   [14, 10, 6, 1]
4   78000005    [0.009211099810479408, 0.022794562426456118, 0...   [10, 13, 14, 16, 5, 8, 6, 12, 1]
... ... ... ...
38711   78038712    [0.1392004285351904, 0.8607995714648096]            [1, 0]
... ... ... ...
dbaston commented 3 months ago

Yes, the order of unique and frac is consistent.

ShervanGharari commented 3 months ago

Thank you very much for the confirmation. I close this issue.