jgrss / geowombat

GeoWombat: Utilities for geospatial data
https://geowombat.readthedocs.io
MIT License
184 stars 10 forks source link

extract error #75

Closed mmann1123 closed 2 years ago

mmann1123 commented 3 years ago

Hey Jordan, I am trying to extract values to a large set of polygons. Just not sure how to interpret. Any ideas? I am using the lastest git version.

 with gw.config.update(ref_image=example, ref_bounds=bounds):
            with gw.open(
                files,
                band_names=["ppt_mean", "ppt_sum"],
                stack_dim="band",
                nodata=np.nan,
            ) as ds:

                df = ds.gw.extract(
                    EAs,
                    all_touched=True,
                    band_names=["ppt_mean", "ppt_sum"],
                    time_names=[date],
                    n_jobs=1,
                )

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/Documents/github/ET_Random_James_Tasks/ET_precip_2012_2019.py in <module>
      4                    # band_names=["ppt_mean", "ppt_sum"],
      5                    # time_names=[date],
----> 6                    n_jobs=1,
      7                )

~/anaconda3/envs/geowombat2/lib/python3.7/site-packages/geowombat/core/geoxarray.py in extract(self, aoi, bands, time_names, band_names, frac, all_touched, mask, n_jobs, verbose, **kwargs)
   1217                        n_jobs=n_jobs,
   1218                        verbose=verbose,
-> 1219                        **kwargs)
   1220 
   1221     def band_mask(self, valid_bands, src_nodata=None, dst_clear_val=0, dst_mask_val=1):

~/anaconda3/envs/geowombat2/lib/python3.7/site-packages/geowombat/core/sops.py in extract(self, data, aoi, bands, time_names, band_names, frac, min_frac_area, all_touched, id_column, time_format, mask, n_jobs, verbose, n_workers, n_threads, use_client, address, total_memory, processes, pool_kwargs, **kwargs)
    655                                        n_jobs=n_jobs,
    656                                        verbose=verbose,
--> 657                                        **pool_kwargs)
    658 
    659         if df.empty:

~/anaconda3/envs/geowombat2/lib/python3.7/site-packages/geowombat/core/conversion.py in prepare_points(self, data, aoi, frac, min_frac_area, all_touched, id_column, mask, n_jobs, verbose, **kwargs)
    418                                              id_column=id_column,
    419                                              n_jobs=n_jobs,
--> 420                                              **kwargs)
    421 
    422             if not df.empty:

~/anaconda3/envs/geowombat2/lib/python3.7/site-packages/geowombat/core/conversion.py in polygons_to_points(data, df, frac, min_frac_area, all_touched, id_column, n_jobs, **kwargs)
    476                                           meta,
    477                                           frac,
--> 478                                           min_frac_area)
    479 
    480                 if not point_df.empty:

~/anaconda3/envs/geowombat2/lib/python3.7/site-packages/geowombat/core/util.py in sample_feature(df_row, id_column, df_columns, crs, res, all_touched, meta, frac, min_frac_area, feature_array)
    553 
    554     for col in other_cols:
--> 555         fea_df.loc[:, col] = df_row[col]
    556 
    557     return fea_df

~/anaconda3/envs/geowombat2/lib/python3.7/site-packages/pandas/core/indexing.py in __setitem__(self, key, value)
    690 
    691         iloc = self if self.name == "iloc" else self.obj.iloc
--> 692         iloc._setitem_with_indexer(indexer, value, self.name)
    693 
    694     def _validate_key(self, key, axis: int):

~/anaconda3/envs/geowombat2/lib/python3.7/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value, name)
   1586                             if not is_list_like_indexer(value):
   1587                                 raise ValueError(
-> 1588                                     "cannot set a frame with no "
   1589                                     "defined index and a scalar"
   1590                                 )

ValueError: cannot set a frame with no defined index and a scalar
mmann1123 commented 3 years ago

Tracked down the row that was giving me trouble:

(IPdb [3]): dfrow
R_NAME                                                   SNNP
R_CODE                                                      7
Z_NAME                                    Derashe Lyiu Wereda
Z_CODE                                                    716
W_NAME                                        Derashe Special
W_CODE                                                  71601
KK_NAME                                                  None
KK_CODE                                                     0
Remark                                                   None
T_NAME                                                   None
T_CODE                                                      0
RK_NAME                                                 Onota
RK_CODE                                            71601010.0
EA_NAME                                                010-04
UK_NAME                                                  None
UK_CODE                                                  None
EA_ID                                                    None
UK_ID                                                    None
EA_CODE                                                     4
W_code_t                                               071601
KK_cd_T                                                  None
RK_Code1                                                 None
FID                                                      4714
id                                                       4715
geometry    POLYGON ((37.49745968170024 5.653699819037462,...
Name: 4714, dtype: object

(IPdb [4]): sample_feature(dfrow,
                                          id_column,
                                          df_columns,
                                          data.crs,
                                          data.res,
                                          all_touched,
                                          meta,
                                          frac,
                                          min_frac_area)
*** ValueError: cannot set a frame with no defined index and a scalar
mmann1123 commented 3 years ago

In sample_feature1 seems likerasterize` is returning an empty array...

 feature_array = features.rasterize([geom],
                                           out_shape=geom_info.shape,
                                           fill=0,
                                           out=None,
                                           transform=geom_info.affine,
                                           all_touched=all_touched,
                                           default_value=1,
                                           dtype='int32')

feature_array
array([[0]], dtype=int32)

That creates an empty geodataframe

fea_df = gpd.GeoDataFrame(data=np.c_[fid_, np.arange(0, n_samples)],
                              geometry=gpd.points_from_xy(x_coords, y_coords),
                              crs=crs,
                              columns=[id_column, 'point'])

fea_df
Empty GeoDataFrame
Columns: [id, point, geometry]
Index: []

So then transferring column data back throws an error:

for col in other_cols:
        fea_df.loc[:, col] = df_row[col]

*** ValueError: cannot set a frame with no defined index and a scalar

One possible solution

if not fea_df.empty:
        for col in other_cols:
            fea_df.loc[:, col] = df_row[col]
    else:
        pass

Although not sure if you prefer a try except...

mmann1123 commented 3 years ago

@jgrss I am running into issues with gw.extract again. I think its related to the issue above. Just wondering if we have any thoughts on a solution

jgrss commented 2 years ago

@mmann1123 have you tested this proposed solution?

if not fea_df.empty:
        for col in other_cols:
            fea_df.loc[:, col] = df_row[col]
    else:
        pass

I would be happy with this.

mmann1123 commented 2 years ago

@mmann1123 have you tested this proposed solution?

@jgrss Yes this solves the issue for me. I've tested it in a few different scenarios