manaakiwhenua / vector2dggs

DGGS indexer for vector data
https://pypi.org/project/vector2dggs/
GNU Lesser General Public License v3.0
6 stars 0 forks source link

Errors on CRS issue when input dataset is empty #11

Open alpha-beta-soup opened 1 year ago

alpha-beta-soup commented 1 year ago

Using this in a workflow, one input (Postgres table) was empty (which is valid). Instead of doing nothing and exiting gracefully, I get the following traceback:

Traceback (most recent call last):
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/bin/vector2dggs", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/vector2dggs/cli.py", line 25, in main
    cli()
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/vector2dggs/h3.py", line 304, in h3
    _index(
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/vector2dggs/h3.py", line 102, in _index
    df = df.to_crs(cut_crs)
         ^^^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/geopandas/geodataframe.py", line 1364, in to_crs
    geom = df.geometry.to_crs(crs=crs, epsg=epsg)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/geopandas/geoseries.py", line 1124, in to_crs
    self.values.to_crs(crs=crs, epsg=epsg), index=self.index, name=self.name
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/richard/landcare/luis/.snakemake/conda/ce91d2d98a6110cf88fbd80a788421dc_/lib/python3.11/site-packages/geopandas/array.py", line 762, in to_crs
    raise ValueError(
ValueError: Cannot transform naive geometries.  Please set a crs on the object first.
alpha-beta-soup commented 1 year ago

I think correct behaviour in this case should be a non-zero exit, but with a more explict message that the input is empty.

alpha-beta-soup commented 1 year ago

One current workaround

FEATURE_COUNT=$(ogrinfo -so {datasource} {layer} | sed -n 's/Feature Count: //p');
if [ "$FEATURE_COUNT" -eq '0' ]; then
    echo "{layer} is empty, skipping" && mkdir -p {output}
else
    vector2dggs h3 ...
fi;