Skip to content

pygmt v0.5.0 UnicodeDecodeError with fig.plot() when plotting shapefiles #1616

Closed

Description

@mdtanker

Description of the problem
I recently updated to pygmt v0.5.0 (cloned my v.0.4.0 environment then ran: conda update pygmt) and now I'm having an issue with pygmt.plot when trying to plot shapefiles. Below is an example of a script which works fine in my v.0.4.0 env but produces the below error with v0.5.0.

This error happens with any shapefiles I've tried. Here's a link to this specific one: https://doi.pangaea.de/10.1594/PANGAEA.819147
Full code that generated the error

import pygmt

figheight=180 # in mm

# for Ross Ice Shelf
ris_xl=-660_000
ris_yl=-1_500_000
ris_xh=470_000
ris_yh=-400_000

# Calculate the figure width and map scale
figwidth=figheight*(ris_xh-ris_xl)/(ris_yh-ris_yl)
risratio = (ris_yh - ris_yl) / (figheight/1000)
# Make a GMT region string and projection strings in both PS71 and Lon/Lat
risreg = str(ris_xl) + '/' + str(ris_xh) + '/' + str(ris_yl) + '/' + str(ris_yh) #W/E/S/N
risproj = "x1:" + str(risratio)
risproj_ll = "s0/-90/-71/1:" + str(risratio)

fig = pygmt.Figure()

# Plot Shapefiles
fig.plot(data='../PyGMT/Venturelli2020/scripps_antarctica_polygons_v1.shp', region = risreg, projection = risproj, pen = '0.5p,black')

fig.show()

Full error message

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-8-1bd9f90ba3c5> in <module>
     20 
     21 # Plot Shapefiles
---> 22 fig.plot(data='../PyGMT/Venturelli2020/scripps_antarctica_polygons_v1.shp', region = risreg, projection = risproj, pen = '0.5p,black')
     23 
     24 fig.show()

~\Anaconda3\envs\pygmt05\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
    803                 warnings.warn(msg, category=FutureWarning, stacklevel=2)
    804                 kwargs[newname] = kwargs.pop(oldname)
--> 805             return module_func(*args, **kwargs)
    806 
    807         return new_module

~\Anaconda3\envs\pygmt05\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
    803                 warnings.warn(msg, category=FutureWarning, stacklevel=2)
    804                 kwargs[newname] = kwargs.pop(oldname)
--> 805             return module_func(*args, **kwargs)
    806 
    807         return new_module

~\Anaconda3\envs\pygmt05\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
    871                 )
    872                 warnings.warn(msg, category=FutureWarning, stacklevel=2)
--> 873             return module_func(*args, **kwargs)
    874 
    875         return new_module

~\Anaconda3\envs\pygmt05\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
    584                     )
    585                     warnings.warn(msg, category=SyntaxWarning, stacklevel=2)
--> 586             return module_func(*args, **kwargs)
    587 
    588         new_module.aliases = aliases

~\Anaconda3\envs\pygmt05\lib\site-packages\pygmt\helpers\decorators.py in new_module(*args, **kwargs)
    724                         kwargs[arg] = separators[fmt].join(f"{item}" for item in value)
    725             # Execute the original function and return its output
--> 726             return module_func(*args, **kwargs)
    727 
    728         return new_module

~\Anaconda3\envs\pygmt05\lib\site-packages\pygmt\src\plot.py in plot(self, data, x, y, size, direction, **kwargs)
    235         try:
    236             with open(which(data), mode="r", encoding="utf8") as file:
--> 237                 line = file.readline()
    238             if (
    239                 "@GMULTIPOINT" in line or "@GPOINT" in line

~\Anaconda3\envs\pygmt05\lib\codecs.py in decode(self, input, final)
    320         # decode input (taking the buffer into account)
    321         data = self.buffer + input
--> 322         (result, consumed) = self._buffer_decode(data, self.errors, final)
    323         # keep undecoded input until the next call
    324         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8d in position 26: invalid start byte

System information

Please paste the output of python -c "import pygmt; pygmt.show_versions()":

PyGMT information:
  version: v0.5.0
System information:
  python: 3.8.10 | packaged by conda-forge | (default, May 11 2021, 06:25:23) [MSC v.1916 64 bit (AMD64)]
  executable: C:\Users\matthewt\Anaconda3\envs\pygmt05\python.exe
  machine: Windows-10-10.0.17134-SP0
Dependency information:
  numpy: 1.20.3
  pandas: 1.2.4
  xarray: 0.18.2
  netCDF4: 1.5.7
  packaging: 20.9
  ghostscript: 9.54.0
  gmt: 6.2.0
GMT library information:
  binary dir: C:/Users/matthewt/Anaconda3/envs/pygmt05
  cores: 8
  grid layout: rows
  library path: C:/Users/matthewt/Anaconda3/envs/pygmt05/Library/bin/gmt.dll
  padding: 2
  plugin dir: C:/Users/matthewt/Anaconda3/envs/pygmt05/Library/bin/gmt_plugins
  share dir: C:/Users/matthewt/Anaconda3/envs/pygmt05/Library/share/gmt
  version: 6.2.0

Activity

  1. added
    bug
    on Nov 7, 2021
  2. welcome commented on Nov 7, 2021

    @welcome

    👋 Thanks for opening your first issue here! Please make sure you filled out the template with as much detail as possible. You might also want to take a look at our contributing guidelines and code of conduct.

  3. mdtanker commented on Nov 7, 2021

    @mdtanker
    ContributorAuthor

    A workaround is to load the shapefile with geopandas and plot the geodataframe, thanks to @weiji14

  4. weiji14 commented on Nov 7, 2021

    @weiji14
    Member

    The utf8 decode bug was introduced at 0881d0f#diff-20f52ad15c87b542274352e34c1520aa1c9376e2bab7148df2a6e9f479c6e1c9R222, might be a Windows specific issue.

    @mdtanker, another workaround is to explicitly set the style/-S parameter to bypass that check in the if-then block. I.e. use something like fig.plot(data="file.shp", style="c0.2c", ...).

  5. added this to the 0.6.0 milestone on Nov 8, 2021
  6. seisman commented on Nov 8, 2021

    @seisman
    Member

    might be a Windows specific issue.

    I think it's because the .shp file is not an plaintext file, so it makes no sense to open it using mode="r". It happens on macOS, too.

  7. weiji14 commented on Nov 8, 2021

    @weiji14
    Member

    might be a Windows specific issue.

    I think it's because the .shp file is not an plaintext file, so it makes no sense to open it using mode="r". It happens on macOS, too.

    Ah you're right. I thought we set a condition that only files which end in .gmt have the default styling, but clearly not. So need to add an extra check that datafile.endswith(".gmt") or something.

  8. weiji14 commented on Nov 9, 2021

    @weiji14
    Member

    @GenericMappingTools/pygmt-maintainers, do you think this bug is critical enough to warrant a PyGMT v0.5.1 release? My feeling is that plotting a shapefile via fig.plot(data="file.shp", ...) is something that is quite common (even though we don't advertise it much). There's also a few documentation related things (e.g. the tutorial reorganization, Liam's ROSES 2021 video, etc) that shouldn't take too long to get in by next week.

    Of course, there is a workaround for this, and the team might have other priorities (e.g. GMT 6.3), so maybe we can vote 👍 to do a PyGMT v0.5.1, and 👎 to just jump straight to PyGMT v0.6.0 for December/January. Will decide by end of the weekday 12 Nov (UTC time).

  9. willschlitzer commented on Nov 10, 2021

    @willschlitzer
    Contributor

    @GenericMappingTools/pygmt-maintainers, do you think this bug is critical enough to warrant a PyGMT v0.5.1 release? My feeling is that plotting a shapefile via fig.plot(data="file.shp", ...) is something that is quite common (even though we don't advertise it much). There's also a few documentation related things (e.g. the tutorial reorganization, Liam's ROSES 2021 video, etc) that shouldn't take too long to get in by next week.

    Of course, there is a workaround for this, and the team might have other priorities (e.g. GMT 6.3), so maybe we can vote +1 to do a PyGMT v0.5.1, and -1 to just jump straight to PyGMT v0.6.0 for December/January. Will decide by end of the weekday 12 Nov (UTC time).

    I don't have any experience working with shapefiles, but I think two releases (or three, to include the contributors who work on GMT as well) in the coming months is a lot to ask with it being the holiday season for the contributors in North America and Europe. Since there is a workaround using geopandas, I don't think it is that pressing for the functionality to be added in the next week or two instead of the coming months.

  10. changed the title [-]pygmt v0.5.0 UnicodeDecodeError with fig.plot()[/-] [+]pygmt v0.5.0 UnicodeDecodeError with fig.plot() when plotting shapefiles[/+] on Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions