Exemplo n.º 1
0
def importArcData(filename):
    """Creates a new Layer from a shapefile (<file>.shp)
    
    :param filename: filename without extension 
    :type filename: string
    :rtype: Layer (CP project)

    **Description**

    `ESRI <http://www.esri.com/>`_ shapefile is a binary file used to
    save and transport maps. During the last times it has become
    the most used format for the spatial scientists around the world.

    On clusterPy's "data_examples" folder you can find some shapefiles. To
    load a shapefile in clusterPy just follow the example bellow.

    **Example** ::

        import clusterpy
        china = clusterpy.importArcData("clusterpy/data_examples/china")

    """
    layer = Layer()
    layer.name = filename.split('/')[-1]
    print "Loading " + filename + ".dbf"
    data, fields, specs = importDBF(filename + '.dbf')
    print "Loading " + filename + ".shp"
    if fields[0] != "ID":
        fields = ["ID"] + fields
        for y in data.keys():
            data[y] = [y] + data[y]
    layer.fieldNames = fields
    layer.Y = data
    layer.areas, layer.Wqueen, layer.Wrook, layer.shpType = importShape(
        filename + '.shp')
    layer._defBbox()
    print "Done"
    return layer
Exemplo n.º 2
0
def importArcData(filename):
    """Creates a new Layer from a shapefile (<file>.shp)
    
    :param filename: filename without extension 
    :type filename: string
    :rtype: Layer (CP project)

    **Description**

    `ESRI <http://www.esri.com/>`_ shapefile is a binary file used to
    save and transport maps. During the last times it has become
    the most used format for the spatial scientists around the world.

    On clusterPy's "data_examples" folder you can find some shapefiles. To
    load a shapefile in clusterPy just follow the example bellow.

    **Example** ::

        import clusterpy
        china = clusterpy.importArcData("clusterpy/data_examples/china")

    """
    layer = Layer()
    layer.name = filename.split('/')[-1]
    print "Loading " + filename + ".dbf"
    data, fields, specs = importDBF(filename + '.dbf')
    print "Loading " + filename + ".shp"
    if fields[0] != "ID":
        fields = ["ID"] + fields
        for y in data.keys():
            data[y] = [y] + data[y]
    layer.fieldNames = fields
    layer.Y = data
    layer.areas, layer.Wqueen, layer.Wrook, layer.shpType = importShape(filename + '.shp')
    layer._defBbox()
    print "Done"
    return layer
Exemplo n.º 3
0
def createGrid(nRows, nCols, lowerLeft=None, upperRight=None):
    """Creates a new Layer with a regular lattice
    
    :param nRows: number of rows
    :type nRows: integer
    :param nCols: number of columns
    :type nCols: integer
    :type lowerLeft: tuple or none, lower-left corner coordinates; default is (0,0) 
    :type upperRight: tuple or none, upper-right corner coordinates; default is (100,100)
    :rtype: Layer new lattice 

    **Description**

    Regular lattices are widely used in both theoretical and empirical
    applications in Regional Science. The example below shows how easy 
    the creation of this kind of maps is using clusterPy.
    
    **Examples**

    Create a grid of ten by ten points.::

        import clusterpy
        points = clusterpy.createGrid(10,10)

    Create a grid of ten by ten points on the bounding box (0,0,100,100).::

        import clusterpy
        points = clusterpy.createGrid(10, 10, lowerLeft=(0, 0), upperRight=(100, 100))
    """
    print "Creating grid"
    if lowerLeft != None and upperRight != None:
        ymin = lowerLeft[1]
        ymax = upperRight[1]
        xmin = lowerLeft[0]
        xmax = upperRight[0]
        areaHeight = float(ymax - ymin) / nRows
        areaWidth = float(xmax - xmin) / nCols
    else:
        ymin = 0
        xmin = 0
        xmax = 10 * nCols
        ymax = 10 * nRows
        areaHeight = 10
        areaWidth = 10
    nyPoints = nRows
    nxPoints = nCols
    N = nyPoints * nxPoints
    Y = {}
    acty = ymax
    actx = xmin
    map = []
    wr = {}
    wq = {}
    # Creating the wr matrix writh towrer criterium
    disAreas = [0, nxPoints - 1, (N - nxPoints), N - 1]
    wr[0] = [1, nyPoints]
    wr[nxPoints - 1] = [nxPoints - 2, 2 * nxPoints - 1]
    wr[N - nxPoints] = [N - nxPoints - nxPoints, N - nxPoints + 1]
    wr[N - 1] = [N - 2, N - 1 - nxPoints]
    wq[0] = [1, nxPoints, nxPoints + 1]
    wq[nxPoints -
       1] = [nxPoints - 2, nxPoints + nxPoints - 1, nxPoints + nxPoints - 2]
    wq[N - nxPoints] = [
        N - nxPoints - nxPoints, N - nxPoints + 1, N - nxPoints - nxPoints + 1
    ]
    wq[N - 1] = [N - 2, N - 1 - nxPoints, N - 1 - nxPoints - 1]
    verticalBorderAreas = []
    for i in range(1, nxPoints -
                   1):  #Asigning the neighborhood of the corner Areas
        wr[i * nxPoints] = [
            i * nxPoints - nxPoints, i * nxPoints + 1, i * nxPoints + nxPoints
        ]
        wr[nxPoints * i + nxPoints - 1] = [
            nxPoints * i - 1, nxPoints * i + nxPoints - 2,
            nxPoints * i + 2 * nxPoints - 1
        ]
        wq[i * nxPoints] = [
            i * nxPoints - nxPoints, i * nxPoints - nxPoints + 1,
            i * nxPoints + 1, i * nxPoints + nxPoints,
            i * nxPoints + nxPoints + 1
        ]
        wq[nxPoints * i + nxPoints - 1] = [
            nxPoints * i - 1, nxPoints * i - 2, nxPoints * i + nxPoints - 2,
            nxPoints * i + 2 * nxPoints - 1, nxPoints * i + 2 * nxPoints - 2
        ]
        disAreas = disAreas + [i * nxPoints, nxPoints * i + nxPoints - 1]
    disAreas = disAreas + range(1, nxPoints - 1) + range(
        (N - nxPoints) + 1, N - 1)
    for i in range(1, nxPoints -
                   1):  # Asigning the neighborhood of the side Areas
        wr[i] = [i - 1, i + nxPoints, i + 1]
        wq[i] = [
            i - 1, i + nxPoints - 1, i + nxPoints, i + nxPoints + 1, i + 1
        ]
    for i in range((N - nxPoints) + 1, N - 1):
        wr[i] = [i - 1, i - nxPoints, i + 1]
        wq[i] = [
            i - 1, i - nxPoints - 1, i - nxPoints, i - nxPoints + 1, i + 1
        ]
    cont = 0
    for i in range(nyPoints):  #Creating de clusterPy areas
        nexty = acty - areaHeight
        for j in range(nxPoints):
            nextx = actx + areaWidth
            x1 = tuple([actx, acty])
            x2 = tuple([nextx, acty])
            x3 = tuple([nextx, nexty])
            x4 = tuple([actx, nexty])
            x5 = tuple([actx, acty])
            area = [x1, x2, x3, x4, x5]
            map.append([area])
            actx = nextx
            if cont not in disAreas:  # Asigning the rest of the neighborhoods
                wr[cont] = [
                    cont - 1, cont - nxPoints, cont + 1, cont + nxPoints
                ]
                wq[cont] = [
                    cont - 1, cont - nxPoints - 1, cont - nxPoints,
                    cont - nxPoints + 1, cont + 1, cont + nxPoints - 1,
                    cont + nxPoints, cont + nxPoints + 1
                ]
            cont = cont + 1
        acty = nexty
        actx = xmin
    for i in range(N):
        Y[i] = [i]
    layer = Layer()
    layer.Y = Y
    layer.fieldNames = ['ID']
    layer.areas = map
    layer.Wrook = wr
    layer.Wqueen = wq
    layer.Wqueen, layer.Wrook, = weightsFromAreas(layer.areas)
    layer.shpType = 'polygon'
    layer.name = 'root'
    layer._defBbox()
    print "Done"
    return layer
Exemplo n.º 4
0
def createHexagonalGrid(nRows, nCols, lowerLeft=(0, 0), upperRight=(100, 100)):
    """Creates a new Layer with a hexagonal regular lattice
    
    :param nRows: number of rows
    :type nRows: integer
    :param nCols: number of columns
    :type nCols: integer
    :type lowerLeft: tuple or none, lower-left corner coordinates; default is (0,0) 
    :type upperRight: tuple or none, upper-right corner coordinates; default is (100,100)
    :rtype: Layer new lattice 

    **Description**

    Regular lattices are widely used in both theoretical and empirical
    applications in Regional Science. The example below shows how easy 
    the creation of this kind of maps is using clusterPy.
    
    **Examples**

    Create a grid of ten by ten points.::

        import clusterpy
        points = clusterpy.createGrid(10,10)
    
    Create a grid of ten by ten points on the bounding box (0,0,100,100).::

        import clusterpy
        points = clusterpy.createGrid(10, 10, lowerLeft=(0, 0), upperRight=(100, 100))
    """
    print "Creating grid"
    rowHeight = (upperRight[1] - lowerLeft[1]) / float(nRows)
    colStep = rowHeight / float(2)
    N = nRows * nCols
    areas = []
    for row in range(nRows):
        actx = lowerLeft[0]
        for col in range(nCols):
            if col != 0:
                actx += 2 * colStep
            if col % 2 == 1:
                y0 = lowerLeft[1] + rowHeight * row - 2 * rowHeight / float(2)
                y1 = lowerLeft[1] + rowHeight * row - rowHeight / float(2)
                y2 = lowerLeft[1] + rowHeight * row
            else:
                y0 = lowerLeft[1] + rowHeight * row - rowHeight / float(2)
                y1 = lowerLeft[1] + rowHeight * row
                y2 = lowerLeft[1] + rowHeight * row + rowHeight / float(2)
            x0 = actx
            x1 = actx + colStep
            x2 = actx + 2 * colStep
            x3 = actx + 3 * colStep
            pol = [(x0, y1), (x1, y2), (x2, y2), (x3, y1), (x2, y0), (x1, y0),
                   (x0, y1)]
            areas.append([pol])
    Y = {}
    for i in range(N):
        Y[i] = [i]
    layer = Layer()
    layer.Y = Y
    layer.fieldNames = ['ID']
    layer.areas = areas
    layer.Wqueen, layer.Wrook, = weightsFromAreas(layer.areas)
    layer.shpType = 'polygon'
    layer.name = 'root'
    layer._defBbox()
    print "Done"
    return layer
Exemplo n.º 5
0
def createPoints(nRows, nCols, lowerLeft=(0, 0), upperRight=(100, 100)):
    """Creates a new Layer with uniformly distributed points in space

    :param nRows: number of rows
    :type nRows: integer
    :param nCols: number of cols
    :type nCols: integer
    :param lowerLeft: lower-left corner coordinates; default is (0,0)
    :type lowerLeft: tuple or none 
    :param upperRight: upper-right corner coordinates; default is (100,100)
    :type upperRight: tuple or none
    :rtype: Layer (new points layer)

    **Description**

    The example below shows how to create a point-based regular grids with clusterPy. 
    
    **Examples**
    
    Creating a grid of ten by ten points.::

        import clusterpy
        points = clusterpy.createPoints(10, 10)
    


    Creating a grid of ten by ten points on the bounding box (0,0,100,100). ::

        import clusterpy
        points = clusterpy.createPoints(10, 10, lowerLeft=(0, 0), upperRight=(100, 100))

    """
    print "Creating points"
    yMin = lowerLeft[1]
    yMax = upperRight[1]
    xMin = lowerLeft[0]
    xMax = upperRight[0]
    nyPoints = nRows
    nxPoints = nCols
    areaHeight = float(yMax - yMin) / nRows
    areaWidth = float(xMax - xMin) / nCols
    N = nyPoints * nxPoints
    Y = {}
    acty = yMax
    actx = xMin
    map = []
    verticalBorderAreas = []
    cont = 0
    for i in range(N):
        Y[i] = [i]
    for i in range(nyPoints):
        nexty = acty - areaHeight
        for j in range(nxPoints):
            nextx = actx + areaWidth
            point = (actx + areaWidth / float(2), acty - areaHeight / float(2))
            area = [point]
            map.append([area])
            actx = nextx
            Y[cont].extend([point[0], point[1]])
            cont = cont + 1
        acty = nexty
        actx = xMin
    layer = Layer()
    layer.Y = Y
    layer.fieldNames = ['ID', 'X', 'Y']
    layer.areas = map
    layer.shpType = 'point'
    layer.name = 'root'
    layer._defBbox()
    print "Done"
    return layer
Exemplo n.º 6
0
def createGrid(nRows, nCols, lowerLeft=None, upperRight=None):
    """Creates a new Layer with a regular lattice
    
    :param nRows: number of rows
    :type nRows: integer
    :param nCols: number of columns
    :type nCols: integer
    :type lowerLeft: tuple or none, lower-left corner coordinates; default is (0,0) 
    :type upperRight: tuple or none, upper-right corner coordinates; default is (100,100)
    :rtype: Layer new lattice 

    **Description**

    Regular lattices are widely used in both theoretical and empirical
    applications in Regional Science. The example below shows how easy 
    the creation of this kind of maps is using clusterPy.
    
    **Examples**

    Create a grid of ten by ten points.::

        import clusterpy
        points = clusterpy.createGrid(10,10)

    Create a grid of ten by ten points on the bounding box (0,0,100,100).::

        import clusterpy
        points = clusterpy.createGrid(10, 10, lowerLeft=(0, 0), upperRight=(100, 100))
    """
    print "Creating grid"
    if lowerLeft != None and upperRight != None:
        ymin = lowerLeft[1]
        ymax = upperRight[1]
        xmin = lowerLeft[0]
        xmax = upperRight[0]
        areaHeight = float(ymax - ymin) / nRows
        areaWidth = float(xmax - xmin) / nCols
    else:
        ymin = 0
        xmin = 0
        xmax = 10*nCols
        ymax = 10*nRows
        areaHeight = 10
        areaWidth = 10
    nyPoints = nRows
    nxPoints = nCols
    N = nyPoints*nxPoints
    Y = {}
    acty = ymax
    actx = xmin
    map = []
    wr = {}
    wq = {}
    # Creating the wr matrix writh towrer criterium
    disAreas = [0, nxPoints - 1, (N-nxPoints), N - 1]
    wr[0] = [1, nyPoints]
    wr[nxPoints - 1] = [nxPoints - 2, 2 * nxPoints - 1]
    wr[N - nxPoints] = [N - nxPoints - nxPoints, N - nxPoints + 1]
    wr[N - 1] = [N - 2, N - 1 - nxPoints]
    wq[0] = [1, nxPoints, nxPoints + 1]
    wq[nxPoints - 1] = [nxPoints - 2, nxPoints + nxPoints - 1,
            nxPoints + nxPoints - 2]
    wq[N - nxPoints] = [N - nxPoints - nxPoints, N - nxPoints + 1,
            N - nxPoints - nxPoints + 1]
    wq[N - 1] = [N - 2, N - 1 - nxPoints, N - 1 - nxPoints - 1]
    verticalBorderAreas = []
    for i in range(1, nxPoints - 1): #Asigning the neighborhood of the corner Areas
        wr[i * nxPoints] = [i * nxPoints - nxPoints, i * nxPoints + 1,
                i * nxPoints + nxPoints]
        wr[nxPoints * i + nxPoints - 1] = [nxPoints * i - 1,
                nxPoints * i + nxPoints - 2, nxPoints * i + 2 * nxPoints - 1]
        wq[i * nxPoints] = [i * nxPoints - nxPoints, i * nxPoints - nxPoints + 1,
                i * nxPoints + 1,i * nxPoints + nxPoints, i * nxPoints + nxPoints + 1]
        wq[nxPoints * i + nxPoints - 1] = [nxPoints * i - 1, nxPoints * i - 2,
                nxPoints * i + nxPoints - 2, nxPoints * i + 2 * nxPoints - 1,
                nxPoints * i + 2 * nxPoints - 2]
        disAreas = disAreas + [i * nxPoints, nxPoints * i + nxPoints - 1]
    disAreas = disAreas + range(1, nxPoints - 1) + range((N - nxPoints) + 1, N - 1)
    for i in range(1, nxPoints - 1): # Asigning the neighborhood of the side Areas
        wr[i]=[i - 1, i + nxPoints, i + 1]
        wq[i]=[i - 1, i + nxPoints - 1, i + nxPoints, i + nxPoints + 1, i + 1]
    for i in  range((N - nxPoints) + 1, N - 1):
        wr[i]=[i - 1, i - nxPoints, i + 1]
        wq[i]=[i - 1, i - nxPoints - 1, i - nxPoints, i - nxPoints + 1, i + 1]
    cont = 0
    for i in range(nyPoints): #Creating de clusterPy areas
        nexty = acty - areaHeight
        for j in range(nxPoints):
            nextx = actx + areaWidth
            x1 = tuple([actx, acty])
            x2 = tuple([nextx, acty])
            x3 = tuple([nextx, nexty])
            x4 = tuple([actx, nexty])
            x5 = tuple([actx, acty])
            area = [x1, x2, x3, x4, x5]
            map.append([area])
            actx = nextx
            if cont not in disAreas: # Asigning the rest of the neighborhoods
                wr[cont]=[cont - 1, cont - nxPoints, cont + 1, cont + nxPoints]
                wq[cont]=[cont - 1, cont - nxPoints - 1, cont - nxPoints,
                        cont - nxPoints + 1, cont + 1, cont + nxPoints - 1,
                        cont + nxPoints, cont + nxPoints + 1]
            cont = cont + 1
        acty = nexty
        actx = xmin
    for i in range(N):
        Y[i]=[i]
    layer = Layer()
    layer.Y = Y
    layer.fieldNames = ['ID']
    layer.areas = map
    layer.Wrook = wr
    layer.Wqueen = wq
    layer.Wqueen, layer.Wrook, = weightsFromAreas(layer.areas)
    layer.shpType = 'polygon'
    layer.name = 'root'
    layer._defBbox()
    print "Done"
    return layer
Exemplo n.º 7
0
def createHexagonalGrid(nRows, nCols, lowerLeft=(0,0), upperRight=(100,100)):
    """Creates a new Layer with a hexagonal regular lattice
    
    :param nRows: number of rows
    :type nRows: integer
    :param nCols: number of columns
    :type nCols: integer
    :type lowerLeft: tuple or none, lower-left corner coordinates; default is (0,0) 
    :type upperRight: tuple or none, upper-right corner coordinates; default is (100,100)
    :rtype: Layer new lattice 

    **Description**

    Regular lattices are widely used in both theoretical and empirical
    applications in Regional Science. The example below shows how easy 
    the creation of this kind of maps is using clusterPy.
    
    **Examples**

    Create a grid of ten by ten points.::

        import clusterpy
        points = clusterpy.createGrid(10,10)
    
    Create a grid of ten by ten points on the bounding box (0,0,100,100).::

        import clusterpy
        points = clusterpy.createGrid(10, 10, lowerLeft=(0, 0), upperRight=(100, 100))
    """
    print "Creating grid"
    rowHeight = (upperRight[1] - lowerLeft[1])/float(nRows)
    colStep = rowHeight/float(2)
    N = nRows*nCols
    areas = []
    for row in range(nRows):
        actx = lowerLeft[0]
        for col in range(nCols):
            if col != 0:
                actx += 2*colStep
            if col%2 == 1:
                y0 = lowerLeft[1] + rowHeight*row - 2*rowHeight/float(2)
                y1 = lowerLeft[1] + rowHeight*row - rowHeight/float(2)
                y2 = lowerLeft[1] + rowHeight*row
            else:
                y0 = lowerLeft[1] + rowHeight*row - rowHeight/float(2)
                y1 = lowerLeft[1] + rowHeight*row
                y2 = lowerLeft[1] + rowHeight*row + rowHeight/float(2)
            x0 = actx
            x1 = actx + colStep
            x2 = actx + 2*colStep
            x3 = actx + 3*colStep
            pol = [(x0,y1),(x1,y2),(x2,y2),
                   (x3,y1),(x2,y0),(x1,y0),
                   (x0,y1)]
            areas.append([pol])
    Y = {}
    for i in range(N):
        Y[i]=[i]
    layer = Layer()
    layer.Y = Y
    layer.fieldNames = ['ID']
    layer.areas = areas
    layer.Wqueen, layer.Wrook, = weightsFromAreas(layer.areas)
    layer.shpType = 'polygon'
    layer.name = 'root'
    layer._defBbox()
    print "Done"
    return layer
Exemplo n.º 8
0
def createPoints(nRows, nCols, lowerLeft=(0,0), upperRight=(100,100)):
    """Creates a new Layer with uniformly distributed points in space

    :param nRows: number of rows
    :type nRows: integer
    :param nCols: number of cols
    :type nCols: integer
    :param lowerLeft: lower-left corner coordinates; default is (0,0)
    :type lowerLeft: tuple or none 
    :param upperRight: upper-right corner coordinates; default is (100,100)
    :type upperRight: tuple or none
    :rtype: Layer (new points layer)

    **Description**

    The example below shows how to create a point-based regular grids with clusterPy. 
    
    **Examples**
    
    Creating a grid of ten by ten points.::

        import clusterpy
        points = clusterpy.createPoints(10, 10)
    


    Creating a grid of ten by ten points on the bounding box (0,0,100,100). ::

        import clusterpy
        points = clusterpy.createPoints(10, 10, lowerLeft=(0, 0), upperRight=(100, 100))

    """
    print "Creating points"
    yMin = lowerLeft[1]
    yMax = upperRight[1]
    xMin = lowerLeft[0]
    xMax = upperRight[0]
    nyPoints = nRows
    nxPoints = nCols
    areaHeight = float(yMax - yMin) / nRows
    areaWidth = float(xMax - xMin) / nCols
    N = nyPoints*nxPoints
    Y = {}
    acty = yMax
    actx = xMin
    map = []
    verticalBorderAreas = []
    cont = 0
    for i in range(N):
        Y[i] = [i]
    for i in range(nyPoints):
        nexty = acty - areaHeight
        for j in range(nxPoints):
            nextx = actx + areaWidth
            point = (actx + areaWidth / float(2), acty - areaHeight / float(2))
            area = [point]
            map.append([area])
            actx = nextx
            Y[cont].extend([point[0],point[1]])
            cont = cont + 1
        acty = nexty
        actx = xMin
    layer = Layer()
    layer.Y = Y
    layer.fieldNames = ['ID','X','Y']
    layer.areas = map
    layer.shpType = 'point'
    layer.name = 'root'
    layer._defBbox()
    print "Done"
    return layer