diff --git a/README.md b/README.md index c55e631..6a677af 100644 --- a/README.md +++ b/README.md @@ -452,6 +452,7 @@ A line must have at least two points. Because of the similarities between polygon and line types it is possible to create a line shape using either the "line" or "poly" method. + >>> w = shapefile.Writer() >>> w.line(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]]) @@ -546,14 +547,12 @@ To store very large numbers you must increase the field length size to the total >>> w.save('shapefiles/test/dtype') >>> r = shapefile.Reader('shapefiles/test/dtype') - >>> r.record(0) - [1, 1.32, 1.3217328, -3.2302e-25, 1.3217328, 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L] - >>> r.record(1) - [None, None, None, None, None, None] + >>> assert r.record(0) == [1, 1.32, 1.3217328, -3.2302e-25, 1.3217328, 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] + >>> assert r.record(1) == [None, None, None, None, None, None] Finally, we can create boolean fields by setting the type to 'L'. -This field can take True or False values, or any string whose first character is one of YyTt (True) or NnFf (False). +This field can take True or False values, or 1 (True) or 0 (False). None is interpreted as missing. @@ -565,22 +564,30 @@ None is interpreted as missing. >>> w.null() >>> w.null() >>> w.record(True) - >>> w.record("Yes") + >>> w.record(1) >>> w.record(False) - >>> w.record("No") + >>> w.record(0) >>> w.record(None) + >>> w.record("Nonesense") >>> w.save('shapefiles/test/dtype') >>> r = shapefile.Reader('shapefiles/test/dtype') - >>> assert r.record(0) == [True] - >>> assert r.record(1) == [True] - >>> assert r.record(2) == [False] - >>> assert r.record(3) == [False] - >>> assert r.record(4) == [None] - + >>> r.record(0) + [True] + >>> r.record(1) + [True] + >>> r.record(2) + [False] + >>> r.record(3) + [False] + >>> r.record(4) + [None] + >>> r.record(5) + [None] You can also add attributes using keyword arguments where the keys are field names. + >>> w = shapefile.Writer() >>> w.field('FIRST_FLD','C','40') >>> w.field('SECOND_FLD','C','40') diff --git a/shapefile.py b/shapefile.py index b034cc2..626f60d 100644 --- a/shapefile.py +++ b/shapefile.py @@ -516,7 +516,7 @@ def __record(self): except ValueError: #not parseable as int, set to None value = None - elif typ == b('D'): + elif typ == 'D': # date: 8 bytes - date stored as a string in the format YYYYMMDD. if value.count(b('0')) == len(value): # QGIS NULL is all '0' chars value = None @@ -526,9 +526,9 @@ def __record(self): value = date(y, m, d) except: value = value.strip() - elif typ == b('L'): + elif typ == 'L': # logical: 1 byte - initialized to 0x20 (space) otherwise T or F. - if value == " ": + if value == b(" "): value = None # space means missing or not yet set else: if value in b('YyTt1'): @@ -536,7 +536,7 @@ def __record(self): elif value in b('NnFf0'): value = False else: - value = b('?') + value = None # unknown value is set to missing else: # anything else is forced to string/unicode value = u(value) @@ -952,9 +952,13 @@ def __dbfRecords(self): elif fieldType == 'L': # logical: 1 byte - initialized to 0x20 (space) otherwise T or F. if value in MISSING: - value = str(' ') # missing is set to space + value = b(' ') # missing is set to space + elif value in [True,1]: + value = b("T") + elif value in [False,0]: + value = b("F") else: - value = str(value)[0].upper() + value = b(' ') # unknown is set to space else: # anything else is forced to string value = str(value)[:size].ljust(size) @@ -1116,6 +1120,7 @@ def save(self, target=None, shp=None, shx=None, dbf=None): self.dbf.close() if generated: return target + class Editor(Writer): def __init__(self, shapefile=None, shapeType=POINT, autoBalance=1): self.autoBalance = autoBalance