Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reproject_vector fails on non-UTF-8 strings #418

Open
davemfish opened this issue Dec 4, 2024 · 2 comments
Open

reproject_vector fails on non-UTF-8 strings #418

davemfish opened this issue Dec 4, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@davemfish
Copy link
Contributor

I have an example shapefile that raises this exception:

Traceback (most recent call last):
  File "C:\Users\dmf\projects\pygeoprocessing\scratch\reproject_vector.py", line 8, in <module>
    pygeoprocessing.reproject_vector(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        base_vector_path, base_wkt,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
        '../forum/ndr_setfield/copy.shp',
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        driver_name='ESRI Shapefile')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "src\\pygeoprocessing\\geoprocessing_core.pyx", line 77, in pygeoprocessing.geoprocessing_core.gdal_use_exceptions.wrapper
    with GDALUseExceptions():
  File "src\\pygeoprocessing\\geoprocessing_core.pyx", line 78, in pygeoprocessing.geoprocessing_core.gdal_use_exceptions.wrapper
    return func(*args, **kwargs)
  File "C:\Users\dmf\projects\pygeoprocessing\src\pygeoprocessing\geoprocessing.py", line 2282, in reproject_vector
    target_feature.SetField(target_index, value)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dmf\projects\pygeoprocessing\env\Lib\site-packages\osgeo\ogr.py", line 3877, in SetField
    return _ogr.Feature_SetFieldString(self, fld_index, args[1])
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: not a string or os.PathLike

I think GDAL tried to read a string assuming a UTF8 encoding during a GetField call, and then the result is not usable in SetField.

layer.TestCapability(ogr.OLCStringsAsUTF8)) returns False for this layer.

We could simply wrap the SetField call in reproject_vector in a try/except. And then maybe replace offending characters with something else.

@davemfish davemfish added the bug Something isn't working label Dec 4, 2024
@emlys
Copy link
Member

emlys commented Dec 9, 2024

to clarify, the string in question is a field name?

@davemfish
Copy link
Contributor Author

to clarify, the string in question is a field name?

It's actually the field value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants