Repository: cogeotiff/rio-tiler

Issue:
ZeroDivisionError when using complex polygons with statistics calculation
## Issue Description
When calculating statistics on STAC items with complex polygons (high vertex count) using the `rio-tiler`/`titiler` libraries, a `ZeroDivisionError: Weights sum to zero, can't be normalized` is thrown. However, the same operation succeeds with simpler polygons covering the same area.

### Reproduction Steps
1. Create a request to `/stac/statistics` with a complex polygon (high vertex count, ~290 vertices)
2. Use the following parameters:
   ```python
   params = {
       "assets": ["red", "nir"],
       "expression": "(nir-red)/(nir+red)",  # NDVI calculation
       "rescale": "0.2,1",
       "colormap_name": "turbo",
       "asset_as_band": "True",
       "url": "https://earth-search.aws.element84.com/v1/collections/sentinel-2-l2a/items/S2B_21JXN_20210214_1_L2A"
   }
3. Observe the error: ZeroDivisionError: Weights sum to zero, can't be normalized
4. Repeat with a simplified polygon (5 vertices) covering the same area, and observe success

### Root Cause Analysis
I've conducted an investigation by analyzing the image mask and coverage array created during the processing:
Complex polygon:
 ```python
Image mask - Non-zero elements: 0/21420
Masked data valid elements: 0/21420
Coverage compressed length: 0
 ```

Simple polygon:
 ```python
Image mask - Non-zero elements: 381/21420
Masked data valid elements: 381/21420
Coverage compressed length: 381
 ```

The issue occurs because:

1. With complex polygons, the image mask created by rio-tiler contains all zeros (all pixels marked as invalid)
2. When masked_coverage = np.ma.MaskedArray(coverage_array, ~mask) is executed, all data is masked out
3. When coverage_compressed = masked_coverage.compressed() is executed, it returns an empty array
4. This empty array is passed to numpy.average(values, weights=weights) in the _weighted_stdev function
5. Since the weights sum to zero, a ZeroDivisionError is thrown

A warning during processing, Dataset has no geotransform, gcps, or rpcs. The identity matrix will be returned., suggests that the issue may be related to rasterization accuracy or lack of proper georeference information when handling complex geometries.

### Proposed Solutions

1. Improve the mask generation process to handle complex polygons correctly
2. Add defensive programming in the statistics calculation to handle zero weight sums:
```python
if len(weights) == 0 or np.sum(weights) == 0:
    # Return NaN or a reasonable default
    return np.nan  # or 0
```

3. Provide a more descriptive error message when encountering this issue, indicating the actual problem is with the mask, not the division

Discussion:
@vincentsarago: Hi @wakame1367 could you provide the polygon you're using so I can reproduce the issue? 

thanks @wakame1367: here is polygons

## simplified polygon
```json
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [-55.460645350932246, -25.396997862002205],
                        [-55.44454569626599, -25.396997862002205],
                        [-55.44454569626599, -25.385031761968126],
                        [-55.460645350932246, -25.385031761968126],
                        [-55.460645350932246, -25.396997862002205],
                    ]
                ],
            },
        }
    ],
}
```

## complex polygon

<details>

<summary>complex polygon</summary>

```geojson
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            -55.44991219357051,
                            -25.38736531418524
                        ],
                        [
                            -55.4499022679357,
                            -25.387370324159757
                        ],
                        [
                            -55.449871066230365,
                            -25.387394946320086
                        ],
                        [
                            -55.449846180566915,
                            -25.387424869300897
                        ],
                        [
                            -55.449828688529884,
                            -25.387458797400686
                        ],
                        [
                            -55.449725853236444,
                            -25.387728568945956
                        ],
                        [
                            -55.44971792743673,
                            -25.387756421718272
                        ],
                        [
                            -55.44971501659314,
                            -25.38778505967369
                        ],
                        [
                            -55.4497092419192,
                            -25.388237126712315
                        ],
                        [
                            -55.44967075343472,
                            -25.388240138312597
                        ],
                        [
                            -55.449633314381515,
                            -25.38824995677223
                        ],
                        [
                            -55.449598710946255,
                            -25.38826617818908
                        ],
                        [
                            -55.4495682699043,
                            -25.38828818059733
                        ],
                        [
                            -55.449543158437145,
                            -25.388315120374486
                        ],
                        [
                            -55.44952433938011,
                            -25.38834596458747
                        ],
                        [
                            -55.44951253430474,
                            -25.388379530597284
                        ],
                        [
                            -55.44950819585142,
                            -25.388414531403896
                        ],
                        [
                            -55.44951149037333,
                            -25.388449624992635
                        ],
                        [
                            -55.44952229155703,
                            -25.3884834657902
                        ],
                        [
                            -55.44954018526464,
                            -25.38851475625723
                        ],
                        [
                            -55.44956448541206,
                            -25.388542296639365
                        ],
                        [
                            -55.44970369037788,
                            -25.388671718319678
                        ],
                        [
                            -55.44970283198304,
                            -25.38873891493062
                        ],
                        [
                            -55.44953990807371,
                            -25.38878634671054
                        ],
                        [
                            -55.44950210670467,
                            -25.388801421690456
                        ],
                        [
                            -55.44946856397062,
                            -25.388823221457542
                        ],
                        [
                            -55.44944072470931,
                            -25.388850806998644
                        ],
                        [
                            -55.44941978808683,
                            -25.388882990080802
                        ],
                        [
                            -55.44940665594384,
                            -25.388918384433335
                        ],
                        [
                            -55.449401893948526,
                            -25.388955465460402
                        ],
                        [
                            -55.449397280236106,
                            -25.389316557310245
                        ],
                        [
                            -55.44940086263285,
                            -25.389352662448317
                        ],
                        [
                            -55.4494123858802,
                            -25.38938738537222
                        ],
                        [
                            -55.449431381926146,
                            -25.38941931568419
                        ],
                        [
                            -55.44945707918216,
                            -25.38944715641751
                        ],
                        [
                            -55.44948843386341,
                            -25.38946977671784
                        ],
                        [
                            -55.44952417238555,
                            -25.38948625777749
                        ],
                        [
                            -55.44956284309629,
                            -25.38949593015656
                        ],
                        [
                            -55.44960287523998,
                            -25.38949840097504
                        ],
                        [
                            -55.449642642760594,
                            -25.389493569871348
                        ],
                        [
                            -55.45032982340277,
                            -25.389345186698193
                        ],
                        [
                            -55.450545575982325,
                            -25.389545772558574
                        ],
                        [
                            -55.45057282065406,
                            -25.389566904162116
                        ],
                        [
                            -55.45060382375944,
                            -25.38958327094971
                        ],
                        [
                            -55.45063757679014,
                            -25.38959434052104
                        ],
                        [
                            -55.45103393564014,
                            -25.389688800732955
                        ],
                        [
                            -55.451070637093366,
                            -25.389694299797526
                        ],
                        [
                            -55.451107830240815,
                            -25.389693534302943
                        ],
                        [
                            -55.451144216410924,
                            -25.38968653097794
                        ],
                        [
                            -55.45117852510968,
                            -25.38967353435732
                        ],
                        [
                            -55.45120955838294,
                            -25.389654998243376
                        ],
                        [
                            -55.451236232645726,
                            -25.38963156986035
                        ],
                        [
                            -55.45125761651784,
                            -25.389604067255117
                        ],
                        [
                            -55.451272963344756,
                            -25.389573450733174
                        ],
                        [
                            -55.45132898690163,
                            -25.389426479375977
                        ],
                        [
                            -55.45150579998572,
                            -25.389428341130813
                        ],
                        [
                            -55.45149274073511,
                            -25.389462600751028
                        ],
                        [
                            -55.451483911458126,
                            -25.38949560854051
                        ],
                        [
                            -55.45148210677697,
                            -25.389529527274043
                        ],
                        [
                            -55.45148739102447,
                            -25.389563147935213
                        ],
                        [
                            -55.451499575851884,
                            -25.38959527213123
                        ],
                        [
                            -55.45151822694162,
                            -25.38962475480921
                        ],
                        [
                            -55.451542679487716,
                            -25.389650545071195
                        ],
                        [
                            -55.45157206189201,
                            -25.389671723633278
                        ],
                        [
                            -55.451605326831746,
                            -25.38968753559336
                        ],
                        [
                            -55.45164128859107,
                            -25.389697417339665
                        ],
                        [
                            -55.45167866532576,
                            -25.389701016640696
                        ],
                        [
                            -55.451976799031456,
                            -25.389704155035055
                        ],
                        [
                            -55.45201448359318,
                            -25.38970130692747
                        ],
                        [
                            -55.452050892259216,
                            -25.38969207291938
                        ],
                        [
                            -55.45208471148292,
                            -25.389676786154126
                        ],
                        [
                            -55.45211472114034,
                            -25.389655998145454
                        ],
                        [
                            -55.45213983855002,
                            -25.389630458879857
                        ],
                        [
                            -55.45215915753386,
                            -25.389601089758408
                        ],
                        [
                            -55.45217198110994,
                            -25.38956895035432
                        ],
                        [
                            -55.452177846637525,
                            -25.38953520018561
                        ],
                        [
                            -55.45217654250747,
                            -25.38950105688214
                        ],
                        [
                            -55.45209126664023,
                            -25.388909977892254
                        ],
                        [
                            -55.45214646124923,
                            -25.388765177389065
                        ],
                        [
                            -55.45235098520649,
                            -25.388705630579825
                        ],
                        [
                            -55.45238918580159,
                            -25.38869034775355
                        ],
                        [
                            -55.452423015011284,
                            -25.38866819936359
                        ],
                        [
                            -55.452450983157405,
                            -25.388640160721955
                        ],
                        [
                            -55.452471858658186,
                            -25.388607466519147
                        ],
                        [
                            -55.45248472226103,
                            -25.388571556453723
                        ],
                        [
                            -55.45255613054412,
                            -25.388265688521017
                        ],
                        [
                            -55.45280684034964,
                            -25.388268326130103
                        ],
                        [
                            -55.45288835929684,
                            -25.388344112470577
                        ],
                        [
                            -55.45288585282698,
                            -25.388540729992172
                        ],
                        [
                            -55.45288923020044,
                            -25.388575854396887
                        ],
                        [
                            -55.452900127070464,
                            -25.38860970879424
                        ],
                        [
                            -55.45291812474954,
                            -25.388640992386915
                        ],
                        [
                            -55.45294253171227,
                            -25.388668503155408
                        ],
                        [
                            -55.45297241016543,
                            -25.388691184043896
                        ],
                        [
                            -55.45300661208085,
                            -25.388708163576183
                        ],
                        [
                            -55.4530438233065,
                            -25.38871878934106
                        ],
                        [
                            -55.453082614061074,
                            -25.388722653060487
                        ],
                        [
                            -55.45357949992376,
                            -25.38872787845271
                        ],
                        [
                            -55.45361852010586,
                            -25.38872480779776
                        ],
                        [
                            -55.45365611973607,
                            -25.38871489505842
                        ],
                        [
                            -55.453690843566775,
                            -25.38869852389525
                        ],
                        [
                            -55.45372134765608,
                            -25.388676327934068
                        ],
                        [
                            -55.453963870155064,
                            -25.388461063701236
                        ],
                        [
                            -55.454477343705896,
                            -25.38846646038778
                        ],
                        [
                            -55.45451622961251,
                            -25.38846341218358
                        ],
                        [
                            -55.45455370991168,
                            -25.388453568407662
                        ],
                        [
                            -55.45458834401885,
                            -25.388437307413376
                        ],
                        [
                            -55.45461880074644,
                            -25.38841525420474
                        ],
                        [
                            -55.454643909469425,
                            -25.388388256413677
                        ],
                        [
                            -55.45466270511933,
                            -25.38835735172028
                        ],
                        [
                            -55.45467446527699,
                            -25.388323727968476
                        ],
                        [
                            -55.454678737938714,
                            -25.388288677510143
                        ],
                        [
                            -55.454681241303916,
                            -25.38809207306798
                        ],
                        [
                            -55.454823691412074,
                            -25.387965631263658
                        ],
                        [
                            -55.45484864108065,
                            -25.387938673224177
                        ],
                        [
                            -55.45486731273601,
                            -25.38790784709454
                        ],
                        [
                            -55.45487899371111,
                            -25.387874329466833
                        ],
                        [
                            -55.454883238165046,
                            -25.38783939966301
                        ],
                        [
                            -55.45489243223728,
                            -25.387117214033992
                        ],
                        [
                            -55.454889054307266,
                            -25.38708208966886
                        ],
                        [
                            -55.45487815700025,
                            -25.38704823541497
                        ],
                        [
                            -55.454860159031846,
                            -25.38701695206146
                        ],
                        [
                            -55.454835751946426,
                            -25.386989441614276
                        ],
                        [
                            -55.45480587354516,
                            -25.38696676111152
                        ],
                        [
                            -55.45477167185244,
                            -25.386949782009086
                        ],
                        [
                            -55.454734461005216,
                            -25.386939156696897
                        ],
                        [
                            -55.45469567076008,
                            -25.38693529343236
                        ],
                        [
                            -55.45439784276033,
                            -25.386932163968737
                        ],
                        [
                            -55.45440130066351,
                            -25.38666062734848
                        ],
                        [
                            -55.45439792287914,
                            -25.38662550297513
                        ],
                        [
                            -55.45438702574235,
                            -25.38659164868963
                        ],
                        [
                            -55.45436902796215,
                            -25.386560365282193
                        ],
                        [
                            -55.454344621075585,
                            -25.386532854760667
                        ],
                        [
                            -55.45431474287608,
                            -25.386510174165892
                        ],
                        [
                            -55.45428054138013,
                            -25.386493194957183
                        ],
                        [
                            -55.45424333071695,
                            -25.386482569528457
                        ],
                        [
                            -55.45420454063591,
                            -25.38647870614156
                        ],
                        [
                            -55.453819323617324,
                            -25.38647465707831
                        ],
                        [
                            -55.45390799861898,
                            -25.385331135802108
                        ],
                        [
                            -55.45482868691416,
                            -25.385480492957342
                        ],
                        [
                            -55.45521928663195,
                            -25.385771144936463
                        ],
                        [
                            -55.45539123781791,
                            -25.387055858374087
                        ],
                        [
                            -55.455399848368224,
                            -25.387090691642328
                        ],
                        [
                            -55.45541590262524,
                            -25.387123303086245
                        ],
                        [
                            -55.45543876843425,
                            -25.387152408581382
                        ],
                        [
                            -55.45546754542364,
                            -25.387176862054393
                        ],
                        [
                            -55.455501100457916,
                            -25.38719570061179
                        ],
                        [
                            -55.45553811225669,
                            -25.38720818245571
                        ],
                        [
                            -55.455934467982964,
                            -25.387302629299025
                        ],
                        [
                            -55.45597116893553,
                            -25.387308127108703
                        ],
                        [
                            -55.45600836131029,
                            -25.387307360344295
                        ],
                        [
                            -55.45604474646304,
                            -25.387300355778873
                        ],
                        [
                            -55.456079053935674,
                            -25.387287357990463
                        ],
                        [
                            -55.45611008581722,
                            -25.387268820822005
                        ],
                        [
                            -55.45613675857161,
                            -25.387245391534385
                        ],
                        [
                            -55.456158140871594,
                            -25.38721788820579
                        ],
                        [
                            -55.45617348611775,
                            -25.387187271166713
                        ],
                        [
                            -55.45625409712283,
                            -25.386975767327655
                        ],
                        [
                            -55.45642803947788,
                            -25.386873044308818
                        ],
                        [
                            -55.4567473480171,
                            -25.38693454925587
                        ],
                        [
                            -55.456996185416656,
                            -25.38739999984092
                        ],
                        [
                            -55.4570187215327,
                            -25.387432678343195
                        ],
                        [
                            -55.45704843981389,
                            -25.387460274918055
                        ],
                        [
                            -55.45708397849631,
                            -25.387481525019258
                        ],
                        [
                            -55.457200361779535,
                            -25.38753592114441
                        ],
                        [
                            -55.45727410428279,
                            -25.387817657597765
                        ],
                        [
                            -55.457288364636035,
                            -25.387854095605174
                        ],
                        [
                            -55.45731091224541,
                            -25.387886940144625
                        ],
                        [
                            -55.45734070605324,
                            -25.387914674724293
                        ],
                        [
                            -55.45737637042891,
                            -25.387936018787496
                        ],
                        [
                            -55.45750079835886,
                            -25.38799417437038
                        ],
                        [
                            -55.457763578689004,
                            -25.388738843124667
                        ],
                        [
                            -55.45777802858969,
                            -25.388769583125544
                        ],
                        [
                            -55.45779852009719,
                            -25.388797379765403
                        ],
                        [
                            -55.458264961020085,
                            -25.38931793020589
                        ],
                        [
                            -55.45844546016892,
                            -25.389829416186522
                        ],
                        [
                            -55.45846218757075,
                            -25.38986386564434
                        ],
                        [
                            -55.458486480768144,
                            -25.389894418382575
                        ],
                        [
                            -55.459544886595545,
                            -25.390967849467913
                        ],
                        [
                            -55.460003831528994,
                            -25.391913811291268
                        ],
                        [
                            -55.46017886390843,
                            -25.39351243379662
                        ],
                        [
                            -55.460184490000145,
                            -25.39354063213175
                        ],
                        [
                            -55.460645350932246,
                            -25.39511794388956
                        ],
                        [
                            -55.460586166162166,
                            -25.39527323469124
                        ],
                        [
                            -55.460149246760956,
                            -25.395505178108998
                        ],
                        [
                            -55.46012001295884,
                            -25.39552410032742
                        ],
                        [
                            -55.460095024021214,
                            -25.395547464511527
                        ],
                        [
                            -55.46007509523287,
                            -25.395574508387963
                        ],
                        [
                            -55.46006087678888,
                            -25.395604349630194
                        ],
                        [
                            -55.46005283258122,
                            -25.39563601464487
                        ],
                        [
                            -55.45994885664655,
                            -25.396326768922627
                        ],
                        [
                            -55.459715491102834,
                            -25.396738768166372
                        ],
                        [
                            -55.45914316421504,
                            -25.396732771715982
                        ],
                        [
                            -55.45910414161014,
                            -25.39673584370649
                        ],
                        [
                            -55.45906653992527,
                            -25.396745757700366
                        ],
                        [
                            -55.45903181447355,
                            -25.39676212999244
                        ],
                        [
                            -55.459001309249494,
                            -25.396784326919327
                        ],
                        [
                            -55.458760734477046,
                            -25.396997862002205
                        ],
                        [
                            -55.45464134899752,
                            -25.396867572782956
                        ],
                        [
                            -55.454274760875144,
                            -25.396594808571393
                        ],
                        [
                            -55.454275963761766,
                            -25.39650039924242
                        ],
                        [
                            -55.45427259683486,
                            -25.39646532774143
                        ],
                        [
                            -55.454261732340214,
                            -25.39643152041333
                        ],
                        [
                            -55.45424378646558,
                            -25.39640027230155
                        ],
                        [
                            -55.45421944666086,
                            -25.396372780413696
                        ],
                        [
                            -55.454189645303494,
                            -25.39635009786862
                        ],
                        [
                            -55.45398133659884,
                            -25.396220767616068
                        ],
                        [
                            -55.453982414628385,
                            -25.39613617244927
                        ],
                        [
                            -55.45397964116795,
                            -25.396104114857728
                        ],
                        [
                            -55.45397059323068,
                            -25.396073015678887
                        ],
                        [
                            -55.45395556000024,
                            -25.39604386886618
                        ],
                        [
                            -55.45393502195386,
                            -25.396017605973213
                        ],
                        [
                            -55.45324854721438,
                            -25.395288105331524
                        ],
                        [
                            -55.453218510310144,
                            -25.39526222125703
                        ],
                        [
                            -55.45318318493206,
                            -25.395242526239123
                        ],
                        [
                            -55.453144106629786,
                            -25.395229876394207
                        ],
                        [
                            -55.45185559052799,
                            -25.394945460692888
                        ],
                        [
                            -55.45181065111935,
                            -25.394940332554956
                        ],
                        [
                            -55.45115126183345,
                            -25.39493338861514
                        ],
                        [
                            -55.450393467113884,
                            -25.39466611574076
                        ],
                        [
                            -55.45034924705955,
                            -25.39465561077976
                        ],
                        [
                            -55.44808971125399,
                            -25.39436362996137
                        ],
                        [
                            -55.447379450239595,
                            -25.394113104253165
                        ],
                        [
                            -55.4470803131502,
                            -25.393695074650108
                        ],
                        [
                            -55.44724522819807,
                            -25.393332841140072
                        ],
                        [
                            -55.44725628258451,
                            -25.39329990192716
                        ],
                        [
                            -55.4472601890131,
                            -25.393265663565327
                        ],
                        [
                            -55.44725680443369,
                            -25.39323137988788
                        ],
                        [
                            -55.44716320034066,
                            -25.392778966466455
                        ],
                        [
                            -55.4471467797838,
                            -25.39273379864653
                        ],
                        [
                            -55.44647791147629,
                            -25.39148248486623
                        ],
                        [
                            -55.44629454608539,
                            -25.390689510428746
                        ],
                        [
                            -55.44628429780919,
                            -25.39065900604017
                        ],
                        [
                            -55.44626824948621,
                            -25.390630615221216
                        ],
                        [
                            -55.44624690524031,
                            -25.390605229802162
                        ],
                        [
                            -55.44622093555306,
                            -25.39058364720524
                        ],
                        [
                            -55.446191156201834,
                            -25.390566545395732
                        ],
                        [
                            -55.446102558459884,
                            -25.390525128422457
                        ],
                        [
                            -55.44610460661812,
                            -25.39036517921056
                        ],
                        [
                            -55.44610057016218,
                            -25.390327005499238
                        ],
                        [
                            -55.44608766785773,
                            -25.390290472413735
                        ],
                        [
                            -55.44606648619037,
                            -25.39025724057582
                        ],
                        [
                            -55.44579815524429,
                            -25.38992424534594
                        ],
                        [
                            -55.44561855400336,
                            -25.38914752206228
                        ],
                        [
                            -55.445608306007884,
                            -25.389117017635048
                        ],
                        [
                            -55.44559225803019,
                            -25.38908862675395
                        ],
                        [
                            -55.44557091418324,
                            -25.389063241251094
                        ],
                        [
                            -55.44554494493598,
                            -25.38904165855124
                        ],
                        [
                            -55.445515166051855,
                            -25.389024556622797
                        ],
                        [
                            -55.44539074365551,
                            -25.388966391219224
                        ],
                        [
                            -55.445223947175776,
                            -25.388493573161437
                        ],
                        [
                            -55.44521008141184,
                            -25.388463812594726
                        ],
                        [
                            -55.445190549157836,
                            -25.388436771305845
                        ],
                        [
                            -55.44516597989948,
                            -25.388413320776866
                        ],
                        [
                            -55.44454569626599,
                            -25.38791886163163
                        ],
                        [
                            -55.44455010365539,
                            -25.38757497241029
                        ],
                        [
                            -55.44495590944422,
                            -25.38633938073998
                        ],
                        [
                            -55.444964120613896,
                            -25.38629029913501
                        ],
                        [
                            -55.44497167519526,
                            -25.38570064136725
                        ],
                        [
                            -55.445234032863084,
                            -25.385237534648798
                        ],
                        [
                            -55.44554349308516,
                            -25.385031761968126
                        ],
                        [
                            -55.44606560230714,
                            -25.385037278769843
                        ],
                        [
                            -55.44618157362625,
                            -25.38519935657822
                        ],
                        [
                            -55.44636155935135,
                            -25.386069347521957
                        ],
                        [
                            -55.446373124096674,
                            -25.386104692914834
                        ],
                        [
                            -55.44639242086386,
                            -25.38613716649052
                        ],
                        [
                            -55.44641863936405,
                            -25.386165404644164
                        ],
                        [
                            -55.44645067865421,
                            -25.386188221620547
                        ],
                        [
                            -55.44648719336669,
                            -25.386204659305836
                        ],
                        [
                            -55.44652665020309,
                            -25.3862140274604
                        ],
                        [
                            -55.4472211190286,
                            -25.386311645766614
                        ],
                        [
                            -55.44725806114653,
                            -25.386313692949727
                        ],
                        [
                            -55.44729478432621,
                            -25.38630952704864
                        ],
                        [
                            -55.44733001938451,
                            -25.3862992920403
                        ],
                        [
                            -55.447362548569664,
                            -25.386283341654945
                        ],
                        [
                            -55.44739124764822,
                            -25.386262227150688
                        ],
                        [
                            -55.4479670136403,
                            -25.385751209364738
                        ],
                        [
                            -55.44859272928719,
                            -25.385515380849636
                        ],
                        [
                            -55.44884930260294,
                            -25.38551808684658
                        ],
                        [
                            -55.44937754786694,
                            -25.38617362030653
                        ],
                        [
                            -55.449401537434504,
                            -25.386198295879918
                        ],
                        [
                            -55.44943009345852,
                            -25.38621861923969
                        ],
                        [
                            -55.44946226386031,
                            -25.386233912788768
                        ],
                        [
                            -55.449496976053645,
                            -25.38624366662776
                        ],
                        [
                            -55.44987964167304,
                            -25.38631739441645
                        ],
                        [
                            -55.449920409103484,
                            -25.386432946053425
                        ],
                        [
                            -55.44973623101712,
                            -25.386916112037998
                        ],
                        [
                            -55.449727226922256,
                            -25.386950243540195
                        ],
                        [
                            -55.44972572900419,
                            -25.386985304911807
                        ],
                        [
                            -55.449731794273156,
                            -25.3870199618811
                        ],
                        [
                            -55.44974519191952,
                            -25.38705289556486
                        ],
                        [
                            -55.44991219357051,
                            -25.38736531418524
                        ]
                    ]
                ]
            }
        }
    ]
}
```

</details>@vincentsarago: > Improve the mask generation process to handle complex polygons correctly

@wakame1367 this has nothing to do with the complexity of the geojson. 

The `complex` geojson just doesn't intersect valid data 

<img width="653" alt="Image" src="https://github.com/user-attachments/assets/e08d635e-608f-45b5-b721-ad7bc1cf80c7" />

The errors you're seeing comes from https://github.com/cogeotiff/rio-tiler/blob/ee3d17fc168db938bf8779a5e0283da9ac64b361/rio_tiler/utils.py#L46-L65

Both will raise `ZeroDivisionError`

```
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
...
File ~/Dev/CogeoTiff/rio-tiler/rio_tiler/utils.py:62, in _weighted_stdev(values, weights)
     58 def _weighted_stdev(
     59     values: NDArray[numpy.floating],
     60     weights: NDArray[numpy.floating],
     61 ) -> float:
---> 62     average = numpy.average(values, weights=weights)
     63     variance = numpy.average((values - average) ** 2, weights=weights)
     64     return float(math.sqrt(variance))

File ~/Dev/venv/py312/lib/python3.12/site-packages/numpy/lib/_function_base_impl.py:565, in average(a, axis, weights, returned, keepdims)
    563     scl = wgt.sum(axis=axis, dtype=result_dtype, **keepdims_kw)
    564     if np.any(scl == 0.0):
--> 565         raise ZeroDivisionError(
    566             "Weights sum to zero, can't be normalized")
    568     avg = avg_as_array = np.multiply(a, wgt,
    569                       dtype=result_dtype).sum(axis, **keepdims_kw) / scl
    571 if returned:

ZeroDivisionError: Weights sum to zero, can't be normalized
```
(Note: it would have been nice to have this traceback, if you had it)

PR is coming to solve this 🙏 

Solution patch:
diff --git a/CHANGES.md b/CHANGES.md
index b4c2caa..ca5ea15 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,6 +1,7 @@
 
 # Unreleased
 
+* fix `utils.get_array_statistics` method to avoid `ZeroDivisionError` when there is no valid pixel
 * use `GDAL_MEM_ENABLE_OPEN=TRUE` when opening a numpy array with rasterio
 
 # 7.5.0 (2025-02-26)
diff --git a/rio_tiler/utils.py b/rio_tiler/utils.py
index c2534be..9564a05 100644
--- a/rio_tiler/utils.py
+++ b/rio_tiler/utils.py
@@ -59,8 +59,8 @@ def _weighted_stdev(
     values: NDArray[numpy.floating],
     weights: NDArray[numpy.floating],
 ) -> float:
-    average = numpy.average(values, weights=weights)
-    variance = numpy.average((values - average) ** 2, weights=weights)
+    average = numpy.ma.average(values, weights=weights)
+    variance = numpy.ma.average((values - average) ** 2, weights=weights)
     return float(math.sqrt(variance))
 
 
@@ -174,35 +174,44 @@ def get_array_statistics(
                 _weighted_quantiles(data_comp, masked_coverage.compressed(), pp / 100.0)
                 for pp in percentiles
             ]
-        else:
-            percentiles_values = [numpy.nan] * len(percentiles_names)
-
-        if valid_pixels:
             majority = float(keys[counts.tolist().index(counts.max())].tolist())
             minority = float(keys[counts.tolist().index(counts.min())].tolist())
+            std = _weighted_stdev(data_comp, masked_coverage.compressed())
+            med = _weighted_quantiles(data_comp, masked_coverage.compressed())
+            _min = float(data[b].min())
+            _max = float(data[b].max())
+            _mean = float(data_cov.sum() / masked_coverage.sum())
+            _count = float(masked_coverage.sum())
+            _sum = float(data_cov.sum())
+
         else:
+            percentiles_values = [numpy.nan] * len(percentiles_names)
             majority = numpy.nan
             minority = numpy.nan
-
-        _count = masked_coverage.sum()
-        _sum = data_cov.sum()
+            std = numpy.nan
+            med = numpy.nan
+            _min = numpy.nan
+            _max = numpy.nan
+            _count = 0
+            _sum = 0
+            _mean = numpy.nan
 
         output.append(
             {
                 # Minimum value, not taking coverage fractions into account.
-                "min": float(data[b].min()),
+                "min": _min,
                 # Maximum value, not taking coverage fractions into account.
-                "max": float(data[b].max()),
+                "max": _max,
                 # Mean value, weighted by the percent of each cell that is covered.
-                "mean": float(_sum / _count),
+                "mean": _mean,
                 # Sum of all non-masked cell coverage fractions.
-                "count": float(_count),
+                "count": _count,
                 # Sum of values, weighted by their coverage fractions.
-                "sum": float(_sum),
+                "sum": _sum,
                 # Population standard deviation of cell values, taking into account coverage fraction.
-                "std": _weighted_stdev(data_comp, masked_coverage.compressed()),
+                "std": std,
                 # Median value of cells, weighted by the percent of each cell that is covered.
-                "median": _weighted_quantiles(data_comp, masked_coverage.compressed()),
+                "median": med,
                 # The value occupying the greatest number of cells.
                 "majority": majority,
                 # The value occupying the least number of cells.


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index bead8ef..0c93f00 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -559,6 +559,39 @@ def test_get_array_statistics():
     assert not math.isnan(stats[0]["max"])
     assert not math.isnan(stats[0]["max"])
 
+    # Totally Masked Array
+    arr = np.ma.MaskedArray(data=np.zeros((1, 256, 256), dtype="uint8"), mask=True)
+    stats = utils.get_array_statistics(arr)
+    assert len(stats) == 1
+    assert list(stats[0]) == [
+        "min",
+        "max",
+        "mean",
+        "count",
+        "sum",
+        "std",
+        "median",
+        "majority",
+        "minority",
+        "unique",
+        "percentile_2",
+        "percentile_98",
+        "histogram",
+        "valid_pixels",
+        "masked_pixels",
+        "valid_percent",
+    ]
+    # Make sure the statistics object are JSON serializable
+    assert json.dumps(stats[0])
+    assert math.isnan(stats[0]["min"])
+    assert math.isnan(stats[0]["max"])
+    assert math.isnan(stats[0]["max"])
+    assert math.isnan(stats[0]["mean"])
+    assert stats[0]["count"] == 0
+    assert stats[0]["sum"] == 0
+    assert math.isnan(stats[0]["std"])
+    assert math.isnan(stats[0]["median"])
+
 
 def test_resize_array():
     """make sure we resize well."""