Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
if self.pos > 0:
raise StopIteration
flag, n, shp, id_var = self.file.readline().strip().split()
self.shpName = shp
self.varName = id_var
id_order = None
id_type = str
try:
base = os.path.split(self.dataPath)[0]
dbf = os.path.join(base, self.shpName.replace('.shp', '') + '.dbf')
if os.path.exists(dbf):
db = FileIO.FileIO(dbf, 'r')
if id_var in db.header:
id_order = db.by_col(id_var)
id_type = type(id_order[0])
else:
warn("ID_VAR:'%s' was not in the DBF header, proceeding with unordered string ids." % (id_var), RuntimeWarning)
else:
warn("DBF relating to GWT was not found, proceeding with unordered string ids.", RuntimeWarning)
except:
warn("Exception occurred will reading DBF, proceeding with unordered string ids.", RuntimeWarning)
self.flag = flag
self.n = n
self.shp = shp
self.id_var = id_var
if id_order is None:
weights, neighbors, id_order = self._readlines(id_type, True)
else:
def __init__(self, *args, **kwargs):
self._varName = 'Unknown'
fileio.FileIO.__init__(self, *args, **kwargs)
self.file = open(self.dataPath, self.mode + 'b')
def __init__(self, *args, **kwargs):
fileio.FileIO.__init__(self, *args, **kwargs)
self.__idx = {}
self.__pos = 0
self.__open()
def __init__(self, *args, **kwargs):
self._varName = 'Unknown'
args = args[:2]
fileio.FileIO.__init__(self, *args, **kwargs)
self.file = fileio.FileIO(self.dataPath, self.mode)
Check whether or not the W object is set as an attribute
>>> wc.w_set()
True
Get the number of observations included in the W object
>>> wc.w.n
88
"""
try:
if self.inputDataFormat:
f = psopen(self.inputPath, 'r', self.inputDataFormat)
else:
f = psopen(self.inputPath, 'r')
except:
raise IOError('A problem occurred while reading the input file.')
else:
try:
self.w = f.read()
except:
raise RuntimeError('A problem occurred while creating a weights object.')
finally:
f.close()
Clean up the temporary file
>>> os.remove(fname)
"""
ext = os.path.splitext(outputPath)[1]
ext = ext.replace('.', '')
#if ext.lower() == 'gwt':
# raise TypeError, 'Currently, PySAL does not support writing a weights object into a gwt file.'
if not self.w_set():
raise RuntimeError('There is no weights object to write out.')
try:
if dataFormat:
o = psopen(outputPath, 'w', dataFormat)
else:
o = psopen(outputPath, 'w')
except:
raise IOError('A problem occurred while creating the output file.')
else:
try:
if dataFormat in ['arcgis_text', 'arcgis_dbf'] or ext == 'swm':
o.write(self.w, useIdIndex=useIdIndex)
elif dataFormat == 'stata_text':
o.write(self.w, matrix_form=matrix_form)
else:
o.write(self.w)
except:
raise RuntimeError('A problem occurred while writing out the weights object')
finally:
o.close()
else:
raise TypeError("Expected a string, got: %s" % (type(obj)))
#default is to raise "NotImplementedError"
def flush(self):
self._complain_ifclosed(self.closed)
self.fileObj.flush()
#REQUIRED
def close(self):
self.fileObj.close()
#clean up the parent class too....
FileIO.close(self)
class TemplateReaderWriter(FileIO.FileIO):
FORMATS = ['bar']
MODES = ['r', 'w']
def __init__(self, *args, **kwargs):
FileIO.__init__(self, *args, **kwargs)
self.fileObj = open(self.dataPath, self.mode)
#Notice reading is a bit different
def _filter(self, st):
def foobar(c):
if c in 'foobar':
return True
else:
return False
return list(filter(foobar, st)) # e.g. 'foobara' == filter(foobar,'my little foobar example')
spw : sparse_matrix
scipy sparse matrix in CSR format
ids : array
identifiers for rows/cols of spw
Examples
--------
>>> import pysal.lib
>>> spw = pysal.lib.weights.spw_from_gal(pysal.lib.examples.get_path("sids2.gal"))
>>> spw.sparse.nnz
462
"""
return ps_open(galfile, 'r').read(sparse=True)
-----
Rook contiguity defines as neighbors any pair of polygons that share a
common edge in their polygon definitions.
See Also
--------
:class:`pysal.lib.weights.weights.W`
:class:`pysal.lib.weights.contiguity.Rook`
"""
sparse = kwargs.pop('sparse', False)
if idVariable is not None:
ids = get_ids(filepath, idVariable)
else:
ids = None
w = cls(FileIO(filepath), ids=ids,**kwargs)
w.set_shapefile(filepath, idVariable=idVariable, full=full)
if sparse:
w = w.to_WSP()
return w