You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -93,6 +93,10 @@ part of your geospatial project.
93
93
94
94
# Version Changes
95
95
96
+
## 3.1.2
97
+
- Raise error in strict mode when creating field with a name, or writing strings, that ends with whole code points
98
+
that whose encoding is pad bytes.
99
+
96
100
## 3.1.1
97
101
### Unicode support made even more robust and yet another encoding bug fixed!
98
102
- When reading, only use minimum number of pad bytes to decode text successfully (fixes issue 423).
@@ -1334,8 +1338,14 @@ All logging happens under the namespace `shapefile`. So another way to suppress
1334
1338
1335
1339
PyShp supports reading and writing shapefiles in any language or character encoding, and provides several options for decoding and encoding text.
1336
1340
Most shapefiles are written in UTF-8 encoding, PyShp's default encoding, so in most cases you don't have to specify the encoding.
1341
+
#### Encoding errors
1337
1342
If you encounter an encoding error when reading a shapefile, this means the shapefile was likely written in a non-utf8 encoding.
1338
1343
For instance, when working with English language shapefiles, a common reason for encoding errors is that the shapefile was written in Latin-1 encoding.
1344
+
#### Recommendation
1345
+
*We recommend avoiding writing Shapefiles with unicode encoded as UTF-16 and UTF-32, especially UTF-16-LE (if at all possible).*
1346
+
When using other encodings, especially UTF-32 or UTF-16, a whole host of other known (and unknown) unicode related issues can be encountered. See ## Specialist Unicode Handling below for more details.
1347
+
1348
+
#### Non-utf8 encodings
1339
1349
For reading shapefiles in any non-utf8 encoding, such as Latin-1, just
1340
1350
supply the encoding option when creating the Reader class.
1341
1351
@@ -1362,7 +1372,10 @@ should give you the same unicode string you started with.
1362
1372
>>> r.close()
1363
1373
1364
1374
If you supply the wrong encoding and the string is unable to be decoded, PyShp will by default raise an
1365
-
exception. If however, on rare occasion, you are unable to find the correct encoding and want to ignore
1375
+
exception.
1376
+
1377
+
#### Custom handling of encoding errors.
1378
+
If however, on rare occasion, you are unable to find the correct encoding and want to ignore
1366
1379
or replace encoding errors, you can specify the "encodingErrors" to be used by the decode method. This
1367
1380
applies to both reading and writing.
1368
1381
@@ -1378,6 +1391,95 @@ in [CPython 3.9 - 3.14](https://docs.python.org/3/library/stdtypes.html#bytes.de
1378
1391
('xmlcharrefreplace' and 'backslashreplace' are only supported by
The simplest most robust solution to support arbitrary codec encoding as ascii bytes, is probably to encode those
1458
+
bytes as Base64 (with an alphabet excluding dbf pad bytes), and then encode that Base64 string a third time as bytes.
1459
+
PyShp tries to be a little easier to use than this. The intention is to adhere to [Postel's Law](https://en.wikipedia.org/wiki/Robustness_principle) so hopefully PyShp is still fairly lenient when decoding and reading Shapefiles, but either informative or
1460
+
stricter, when the user attempts to encoding or write invalid files.
1461
+
#### Warnings
1462
+
When possible data corruption is detected when reading or writing a Shapefile (or dbf file), by default
1463
+
PyShp will raise a warning. Supported situations are listed below under Decoding and Encoding.
1464
+
#### Strict mode
1465
+
If a Writer (or dbf version) is created with `strict=True` then an exception is raised instead of any of the above warnings
1466
+
(hopefully before corrupt data can be written at all).
1467
+
#### Decoding
1468
+
- PyShp first removes all padding bytes. If decoding fails, those padding bytes are restored one by one to the data
1469
+
until decoding succeeds. A warning is raised if any padding bytes were required, but an exception is not raised for this
1470
+
in strict mode (those pad bytes might just have been part of a valid encoding).
1471
+
- PyShp warns if there is a null character in a decoded string (but silently accepts null bytes in encoded strings). This
1472
+
warning does not become an exception in strict mode (there might simply be null characters in the data).
1473
+
#### Encoding
1474
+
- PyShp warns when trying to write data to a text field or field name that PyShp itself would not be able to decode correctly.
1475
+
- PyShp now truncates text fields the dbf field `size` or 10 byte limit for names, by if necessary, successively
1476
+
truncating the actual string, one code point at a time, until the number of encoded bytes is less than or equal
1477
+
to the maximum. If truncation was necessary, a warning is raised.
1478
+
- When trying to write text data, that ends with code points that would encode
1479
+
to pad bytes (these would be lost on decoding, otherwise all fields stored as utf-16 under the limit will end
1480
+
in ††††††††††††...).
1481
+
- PyShp warns if a field name is written containing a null character (before encoding). For users who wish to steer well clear of potential bugs.
1482
+
1381
1483
## Reading Large Shapefiles
1382
1484
1383
1485
Despite being a lightweight library, PyShp is designed to be able to read shapefiles of any size, allowing you to work with hundreds of thousands or even millions
0 commit comments