Skip to content

Updated docs, tests, and type detection error to fix Travis CI failures #94

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

Merged
merged 1 commit into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]]])
Expand Down Expand Up @@ -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.


Expand All @@ -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')
Expand Down
17 changes: 11 additions & 6 deletions shapefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -526,17 +526,17 @@ 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'):
value = True
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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down