Exemplo n.º 1
0
    def calcWeightMatrix(self, get_wm=0):

        wm = None
        if self.geotype == 'Polygon':
            parser = WKTParser()
            shape_l = MyList([parser.fromWKT(s) for s in self.listWKT])
            neighbour_data = ContiguityWeights(shape_l).w
            wm = WeightMatrix(neighbour_data, id_order=self.ids)
        elif self.geotype == 'Point':
            parser = WKTParser()
            shape_l = MyList([parser.fromWKT(s) for s in self.listWKT])
            #wm = pysal.knnW_from_array(np.array(self.listWKT), k=5, ids=self.ids)
            wm = pysal.knnW_from_array(np.array(shape_l), k=5, ids=self.ids)
        if get_wm == 1:
            return wm
        warray = self.dict2array(wm, self.N)
        return warray
 def __open(self):
     self.dataObj = open(self.dataPath, self.mode)
     self.wkt = WKTParser()
class WKTReader(FileIO.FileIO):
    """
    Parameters
    ----------
    Reads Well-Known Text
    Returns a list of PySAL Polygon objects

    Examples
    --------
    Read in WKT-formatted file

    >>> import pysal
    >>> f = pysal.open(pysal.examples.get_path('stl_hom.wkt'), 'r')

    Convert wkt to pysal polygons

    >>> polys = f.read()

    Check length

    >>> len(polys)
    78

    Return centroid of polygon at index 1

    >>> polys[1].centroid
    (-91.19578469430738, 39.990883050220845)

    Type dir(polys[1]) at the python interpreter to get a list of supported methods

    """
    MODES = ['r']
    FORMATS = ['wkt']

    def __init__(self, *args, **kwargs):
        FileIO.FileIO.__init__(self, *args, **kwargs)
        self.__idx = {}
        self.__pos = 0
        self.__open()

    def open(self):
        self.__open()

    def __open(self):
        self.dataObj = open(self.dataPath, self.mode)
        self.wkt = WKTParser()

    def _read(self):
        FileIO.FileIO._complain_ifclosed(self.closed)
        if self.__pos not in self.__idx:
            self.__idx[self.__pos] = self.dataObj.tell()
        line = self.dataObj.readline()
        if line:
            shape = self.wkt.fromWKT(line)
            shape.id = self.pos
            self.__pos += 1
            self.pos += 1
            return shape
        else:
            self.seek(0)
            return None

    def seek(self, n):
        FileIO.FileIO.seek(self, n)
        pos = self.pos
        if pos in self.__idx:
            self.dataObj.seek(self.__idx[pos])
            self.__pos = pos
        else:
            while pos not in self.__idx:
                s = self._read()
                if not s:
                    raise IndexError("%d not in range(0,%d)" %
                                     (pos, max(self.__idx.keys())))
            self.pos = pos
            self.__pos = pos
            self.dataObj.seek(self.__idx[pos])

    def close(self):
        self.dataObj.close()
        FileIO.FileIO.close(self)
Exemplo n.º 4
0
Arquivo: wkt.py Projeto: CartoDB/pysal
 def __open(self):
     self.dataObj = open(self.dataPath, self.mode)
     self.wkt = WKTParser()
Exemplo n.º 5
0
Arquivo: wkt.py Projeto: CartoDB/pysal
class WKTReader(FileIO.FileIO):
    """
    Parameters
    ----------
    Reads Well-Known Text
    Returns a list of PySAL Polygon objects

    Examples
    --------
    Read in WKT-formatted file

    >>> import pysal
    >>> f = pysal.open(pysal.examples.get_path('stl_hom.wkt'), 'r')

    Convert wkt to pysal polygons

    >>> polys = f.read()

    Check length

    >>> len(polys)
    78

    Return centroid of polygon at index 1

    >>> polys[1].centroid
    (-91.19578469430738, 39.990883050220845)

    Type dir(polys[1]) at the python interpreter to get a list of supported methods

    """
    MODES = ['r']
    FORMATS = ['wkt']

    def __init__(self, *args, **kwargs):
        FileIO.FileIO.__init__(self, *args, **kwargs)
        self.__idx = {}
        self.__pos = 0
        self.__open()

    def open(self):
        self.__open()

    def __open(self):
        self.dataObj = open(self.dataPath, self.mode)
        self.wkt = WKTParser()

    def _read(self):
        FileIO.FileIO._complain_ifclosed(self.closed)
        if self.__pos not in self.__idx:
            self.__idx[self.__pos] = self.dataObj.tell()
        line = self.dataObj.readline()
        if line:
            shape = self.wkt.fromWKT(line)
            shape.id = self.pos
            self.__pos += 1
            self.pos += 1
            return shape
        else:
            self.seek(0)
            return None

    def seek(self, n):
        FileIO.FileIO.seek(self, n)
        pos = self.pos
        if pos in self.__idx:
            self.dataObj.seek(self.__idx[pos])
            self.__pos = pos
        else:
            while pos not in self.__idx:
                s = self._read()
                if not s:
                    raise IndexError("%d not in range(0,%d)" % (
                        pos, max(self.__idx.keys())))
            self.pos = pos
            self.__pos = pos
            self.dataObj.seek(self.__idx[pos])

    def close(self):
        self.dataObj.close()
        FileIO.FileIO.close(self)