def write(self, obj, useIdIndex=False): """ Writes a spatial weights matrix data file in swm format. Parameters ---------- .write(weightsObject) accepts a weights object Returns ------- an ArcGIS swm file write a weights object to the opened swm file. Examples -------- >>> import tempfile, pysal, os >>> testfile = pysal.open(pysal.examples.get_path('ohio.swm'),'r') >>> w = testfile.read() Create a temporary file for this example >>> f = tempfile.NamedTemporaryFile(suffix='.swm') Reassign to new var >>> fname = f.name Close the temporary named file >>> f.close() Open the new file in write mode >>> o = pysal.open(fname,'w') Write the Weights object into the open file >>> o.write(w) >>> o.close() Read in the newly created text file >>> wnew = pysal.open(fname,'r').read() Compare values from old to new >>> wnew.pct_nonzero == w.pct_nonzero True Clean up temporary file created for this example >>> os.remove(fname) """ self._complain_ifclosed(self.closed) if issubclass(type(obj), W): if not (type(obj.id_order[0]) in (np.int32, np.int64, int)) and not useIdIndex: raise TypeError("ArcGIS SWM files support only integer IDs") if useIdIndex: id2i = obj.id2i obj = remap_ids(obj, id2i) unk = str('%s;Unknown\n' % self.varName).encode() self.file.write(unk) self.file.write(pack('<l', obj.n)) self.file.write(pack('<l', obj.transform.upper() == 'R')) for obs in obj.weights: self.file.write(pack('<l', obs)) no_nghs = len(obj.weights[obs]) self.file.write(pack('<l', no_nghs)) self.file.write(pack('<%il' % no_nghs, *obj.neighbors[obs])) self.file.write(pack('<%id' % no_nghs, *obj.weights[obs])) self.file.write(pack('<d', sum(obj.weights[obs]))) self.pos += 1 else: raise TypeError("Expected a pysal weights object, got: %s" % (type(obj)))
def write(self, obj, useIdIndex=False): """ Parameters ---------- .write(weightsObject) accepts a weights object Returns ------ an ArcGIS dbf file write a weights object to the opened dbf file. Examples -------- >>> import tempfile, pysal, os >>> testfile = pysal.open(pysal.examples.get_path('arcgis_ohio.dbf'),'r','arcgis_dbf') >>> w = testfile.read() Create a temporary file for this example >>> f = tempfile.NamedTemporaryFile(suffix='.dbf') Reassign to new var >>> fname = f.name Close the temporary named file >>> f.close() Open the new file in write mode >>> o = pysal.open(fname,'w','arcgis_dbf') Write the Weights object into the open file >>> o.write(w) >>> o.close() Read in the newly created text file >>> wnew = pysal.open(fname,'r','arcgis_dbf').read() Compare values from old to new >>> wnew.pct_nonzero == w.pct_nonzero True Clean up temporary file created for this example >>> os.remove(fname) """ self._complain_ifclosed(self.closed) if issubclass(type(obj), W): self.file.header = [self.varName, 'NID', 'WEIGHT'] id_type = type(obj.id_order[0]) if id_type is not int and not useIdIndex: raise TypeError( "ArcGIS DBF weight files support only integer IDs") if useIdIndex: id2i = obj.id2i obj = remap_ids(obj, id2i) id_spec = ('N', len(str(max(obj.id_order))), 0) self.file.field_spec = [id_spec, id_spec, ('N', 13, 6)] for id in obj.id_order: neighbors = zip(obj.neighbors[id], obj.weights[id]) for neighbor, weight in neighbors: self.file.write([id, neighbor, weight]) self.pos = self.file.pos else: raise TypeError("Expected a pysal weights object, got: %s" % (type(obj)))
def write(self, obj, useIdIndex=False): """ Parameters ---------- .write(weightsObject) accepts a weights object Returns ------ an ArcGIS dbf file write a weights object to the opened dbf file. Examples -------- >>> import tempfile, pysal, os >>> testfile = pysal.open(pysal.examples.get_path('arcgis_ohio.dbf'),'r','arcgis_dbf') >>> w = testfile.read() Create a temporary file for this example >>> f = tempfile.NamedTemporaryFile(suffix='.dbf') Reassign to new var >>> fname = f.name Close the temporary named file >>> f.close() Open the new file in write mode >>> o = pysal.open(fname,'w','arcgis_dbf') Write the Weights object into the open file >>> o.write(w) >>> o.close() Read in the newly created text file >>> wnew = pysal.open(fname,'r','arcgis_dbf').read() Compare values from old to new >>> wnew.pct_nonzero == w.pct_nonzero True Clean up temporary file created for this example >>> os.remove(fname) """ self._complain_ifclosed(self.closed) if issubclass(type(obj), W): self.file.header = [self.varName, "NID", "WEIGHT"] id_type = type(obj.id_order[0]) if id_type is not int and not useIdIndex: raise TypeError("ArcGIS DBF weight files support only integer IDs") if useIdIndex: id2i = obj.id2i obj = remap_ids(obj, id2i) id_spec = ("N", len(str(max(obj.id_order))), 0) self.file.field_spec = [id_spec, id_spec, ("N", 13, 6)] for id in obj.id_order: neighbors = zip(obj.neighbors[id], obj.weights[id]) for neighbor, weight in neighbors: self.file.write([id, neighbor, weight]) self.pos = self.file.pos else: raise TypeError("Expected a pysal weights object, got: %s" % (type(obj)))
def write(self, obj, useIdIndex=False): """ Parameters ---------- .write(weightsObject) accepts a weights object Returns ------ an ArcGIS text file write a weights object to the opened text file. Examples -------- >>> import tempfile, pysal, os >>> testfile = pysal.open(pysal.examples.get_path('arcgis_txt.txt'),'r','arcgis_text') >>> w = testfile.read() Create a temporary file for this example >>> f = tempfile.NamedTemporaryFile(suffix='.txt') Reassign to new var >>> fname = f.name Close the temporary named file >>> f.close() Open the new file in write mode >>> o = pysal.open(fname,'w','arcgis_text') Write the Weights object into the open file >>> o.write(w) >>> o.close() Read in the newly created text file >>> wnew = pysal.open(fname,'r','arcgis_text').read() Compare values from old to new >>> wnew.pct_nonzero == w.pct_nonzero True Clean up temporary file created for this example >>> os.remove(fname) """ self._complain_ifclosed(self.closed) if issubclass(type(obj), W): id_type = type(obj.id_order[0]) if id_type is not int and not useIdIndex: raise TypeError("ArcGIS TEXT weight files support only integer IDs") if useIdIndex: id2i = obj.id2i obj = remap_ids(obj, id2i) header = '%s\n' % self.varName self.file.write(header) self._writelines(obj) else: raise TypeError("Expected a pysal weights object, got: %s" % ( type(obj)))
def write(self, obj, useIdIndex=False): """ Writes a spatial weights matrix data file in swm format. Parameters ---------- .write(weightsObject) accepts a weights object Returns ------- an ArcGIS swm file write a weights object to the opened swm file. Examples -------- >>> import tempfile, pysal, os >>> testfile = pysal.open(pysal.examples.get_path('ohio.swm'),'r') >>> w = testfile.read() Create a temporary file for this example >>> f = tempfile.NamedTemporaryFile(suffix='.swm') Reassign to new var >>> fname = f.name Close the temporary named file >>> f.close() Open the new file in write mode >>> o = pysal.open(fname,'w') Add properities to the file to write >>> o.varName = testfile.varName >>> o.srs = testfile.srs Write the Weights object into the open file >>> o.write(w) >>> o.close() Read in the newly created text file >>> wnew = pysal.open(fname,'r').read() Compare values from old to new >>> wnew.pct_nonzero == w.pct_nonzero True Clean up temporary file created for this example >>> os.remove(fname) """ self._complain_ifclosed(self.closed) if not issubclass(type(obj), W): raise TypeError("Expected a pysal weights object, got: %s" % ( type(obj))) if not (type(obj.id_order[0]) in (np.int32, np.int64, int)) and not useIdIndex: raise TypeError("ArcGIS SWM files support only integer IDs") if useIdIndex: id2i = obj.id2i obj = remap_ids(obj, id2i) unk = str('%s;%s\n' % (self.varName, self.srs)).encode() self.file.write(unk) self.file.write(pack('<l', obj.n)) self.file.write(pack('<l', obj.transform.upper() == 'R')) for obs in obj.weights: self.file.write(pack('<l', obs)) no_nghs = len(obj.weights[obs]) self.file.write(pack('<l', no_nghs)) self.file.write(pack('<%il' % no_nghs, *obj.neighbors[obs])) self.file.write(pack('<%id' % no_nghs, *obj.weights[obs])) self.file.write(pack('<d', sum(obj.weights[obs]))) self.pos += 1