|
3 | 3 | Provides read and write support for ESRI Shapefiles.
|
4 | 4 | author: jlawhead<at>geospatialpython.com
|
5 | 5 | date: 2017/04/29
|
6 |
| -version: 1.2.11 |
| 6 | +version: 1.2.12-dev |
7 | 7 | Compatible with Python versions 2.7-3.x
|
8 | 8 | """
|
9 | 9 |
|
10 |
| -__version__ = "1.2.11" |
| 10 | +__version__ = "1.2.12-dev" |
11 | 11 |
|
12 | 12 | from struct import pack, unpack, calcsize, error, Struct
|
13 | 13 | import os
|
@@ -522,11 +522,19 @@ def __record(self):
|
522 | 522 | #not parseable as float, set to None
|
523 | 523 | value = None
|
524 | 524 | else:
|
| 525 | + # force to int |
525 | 526 | try:
|
526 |
| - value = int(value) |
| 527 | + # first try to force directly to int. |
| 528 | + # forcing a large int to float and back to int |
| 529 | + # will lose information and result in wrong nr. |
| 530 | + value = int(value) |
527 | 531 | except ValueError:
|
528 |
| - #not parseable as int, set to None |
529 |
| - value = None |
| 532 | + # forcing directly to int failed, so was probably a float. |
| 533 | + try: |
| 534 | + value = int(float(value)) |
| 535 | + except ValueError: |
| 536 | + #not parseable as int, set to None |
| 537 | + value = None |
530 | 538 | elif typ == 'D':
|
531 | 539 | # date: 8 bytes - date stored as a string in the format YYYYMMDD.
|
532 | 540 | if value.count(b('0')) == len(value): # QGIS NULL is all '0' chars
|
@@ -945,8 +953,18 @@ def __dbfRecords(self):
|
945 | 953 | if value in MISSING:
|
946 | 954 | value = str("*"*size) # QGIS NULL
|
947 | 955 | elif not deci:
|
| 956 | + # force to int |
| 957 | + try: |
| 958 | + # first try to force directly to int. |
| 959 | + # forcing a large int to float and back to int |
| 960 | + # will lose information and result in wrong nr. |
| 961 | + value = int(value) |
| 962 | + except ValueError: |
| 963 | + # forcing directly to int failed, so was probably a float. |
| 964 | + value = int(float(value)) |
948 | 965 | value = format(value, "d")[:size].rjust(size) # caps the size if exceeds the field size
|
949 | 966 | else:
|
| 967 | + value = float(value) |
950 | 968 | value = format(value, ".%sf"%deci)[:size].rjust(size) # caps the size if exceeds the field size
|
951 | 969 | elif fieldType == "D":
|
952 | 970 | # date: 8 bytes - date stored as a string in the format YYYYMMDD.
|
|
0 commit comments