Skip to content

Commit

Permalink
remove tabs, unused code, extra whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit committed Jul 19, 2011
1 parent d0a1811 commit be229f2
Showing 1 changed file with 17 additions and 139 deletions.
156 changes: 17 additions & 139 deletions lib/mpl_toolkits/basemap/shapefile.py
Expand Up @@ -496,7 +496,7 @@ def __shapefileHeader(self, fileObj, headerType='shp'):
f.write(pack(">i", int(self.__shpFileLength())))
elif headerType == 'shx':
f.write(pack('>i', int(((100 + (len(self._shapes) * 8)) / 2))))
# Version, Shape type
# Version, Shape type
f.write(pack("<2i", 1000, self.shapeType))
# The shapefile's bounding box (lower left, upper right)
if self.shapeType != 0:
Expand Down Expand Up @@ -643,22 +643,22 @@ def __shxRecords(self):
f.write(pack(">i", int(self._lengths[i])))

def __dbfRecords(self):
"""Writes the dbf records."""
f = self.__getFileObj(self.dbf)
for record in self.records:
if not self.fields[0][0].startswith("Deletion"):
f.write(pack('c', ' ')) # deletion flag
for (fieldName, fieldType, size, decimal), value in zip(self.fields, record):
fieldType = fieldType.upper()
size = int(size)
if fieldType.upper() == "N":
value = value.rjust(size)
elif fieldType == 'L':
value = value[0].upper()
else:
value = value[:size].ljust(size)
assert len(value) == size
f.write(pack('%ss' % len(value), value))
"""Writes the dbf records."""
f = self.__getFileObj(self.dbf)
for record in self.records:
if not self.fields[0][0].startswith("Deletion"):
f.write(pack('c', ' ')) # deletion flag
for (fieldName, fieldType, size, decimal), value in zip(self.fields, record):
fieldType = fieldType.upper()
size = int(size)
if fieldType.upper() == "N":
value = value.rjust(size)
elif fieldType == 'L':
value = value[0].upper()
else:
value = value[:size].ljust(size)
assert len(value) == size
f.write(pack('%ss' % len(value), value))

def null(self):
"""Creates a null shape."""
Expand Down Expand Up @@ -771,125 +771,3 @@ def save(self, target=""):
self.saveShp(target)
self.saveShx(target)
self.saveDbf(target)

class Editor(Writer):
"""This class is not currently functional in Python 3 however it is basically a
convienience class to wrap the Reader and Writer classes. You can still
accomplish the same tasks using the Reader and Writer class as per the
documentation."""
def __init__(self, shapefile=None, shapeType=POINT, autoBalance=1):
self.autoBalance = autoBalance
if not shapefile:
Writer.__init__(self, shapeType)
elif isinstance(shapefile, str):
base = os.path.splitext(shapefile)[0]
# os.path.isopen doesn't exist in Python 3
#if os.path.isopen("%s.shp" % base):
r = Reader(base)
Writer.__init__(self, shapeType=r.shapeType)
self._shapes = r.shapes()
self.fields = r.fields
self.records = r.records()

def select(self, expr):
"""Select one or more shapes (to be implemented)"""
# TODO: Implement expressions to select shapes.
pass

def delete(self, shape=None, part=None, point=None):
"""Deletes the specified part of any shape by specifying a shape
number, part number, or point number."""
# shape, part, point
if shape and part and point:
del self._shapes[shape][part][point]
# shape, part
elif shape and part and not point:
del self._shapes[shape][part]
# shape
elif shape and not part and not point:
del self._shapes[shape]
# point
elif not shape and not part and point:
for s in self._shapes:
if s.shapeType == 1:
del self._shapes[point]
else:
for part in s.parts:
del s[part][point]
# part, point
elif not shape and part and point:
for s in self._shapes:
del s[part][point]
# part
elif not shape and part and not point:
for s in self._shapes:
del s[part]

def point(self, x=None, y=None, z=None, m=None, shape=None, part=None, point=None, addr=None):
"""Creates/updates a point shape. The arguments allows
you to update a specific point by shape, part, point of any
shape type."""
# shape, part, point
if shape and part and point:
try: self._shapes[shape]
except IndexError: self._shapes.append([])
try: self._shapes[shape][part]
except IndexError: self._shapes[shape].append([])
try: self._shapes[shape][part][point]
except IndexError: self._shapes[shape][part].append([])
p = self._shapes[shape][part][point]
if x: p[0] = x
if y: p[1] = y
if z: p[2] = z
if m: p[3] = m
self._shapes[shape][part][point] = p
# shape, part
elif shape and part and not point:
try: self._shapes[shape]
except IndexError: self._shapes.append([])
try: self._shapes[shape][part]
except IndexError: self._shapes[shape].append([])
points = self._shapes[shape][part]
for i in range(len(points)):
p = points[i]
if x: p[0] = x
if y: p[1] = y
if z: p[2] = z
if m: p[3] = m
self._shapes[shape][part][i] = p
# shape
elif shape and not part and not point:
try: self._shapes[shape]
except IndexError: self._shapes.append([])

# point
# part
if addr:
shape, part, point = addr
self._shapes[shape][part][point] = [x, y, z, m]
else:
Writer.point(self, x, y, z, m)
if self.autoBalance:
self.balance()

def validate(self):
"""An optional method to try and validate the shapefile
as much as possible before writing it (not implemented)."""
#TODO: Implement validation method
pass

def balance(self):
"""Adds a corresponding empty attribute or null geometry record depending
on which type of record was created to make sure all three files
are in synch."""
if len(self.records) > len(self._shapes):
self.null()
elif len(self.records) < len(self._shapes):
self.record()

def __fieldNorm(self, fieldName):
"""Normalizes a dbf field name to fit within the spec and the
expectations of certain ESRI software."""
if len(fieldName) > 11: fieldName = fieldName[:11]
fieldName = fieldName.upper()
fieldName.replace(' ', '_')

0 comments on commit be229f2

Please sign in to comment.