示例#1
0
def labelpolygon(m, plt, sf, field, **kwargs):
    '''
    fstyle  = ['normal', 'italic', 'oblique']
    fweight = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
    '''

    from shapely.geometry import Polygon
    from mapping_tools import get_field_index
    import matplotlib as mpl

    mpl.rcParams['pdf.fonttype'] = 42

    xoff = 0.
    yoff = 0.
    fsize = 10
    col = 'k'
    fstyle = 'normal'
    fweight = 'normal'
    value = None
    for key in ('xoff', 'yoff', 'fsize', 'fweight', 'col', 'fstyle', 'value'):
        if key in kwargs:
            if key == 'xoff':
                xoff = kwargs[key]
            if key == 'yoff':
                yoff = kwargs[key]
            if key == 'fsize':
                fsize = kwargs[key]
            if key == 'fweight':
                fweight = kwargs[key]
            if key == 'fstyle':
                fstyle = kwargs[key]
            if key == 'col':
                col = kwargs[key]
            if key == 'value':
                value = kwargs[key]

    shapes = sf.shapes()
    recs = sf.records()

    # get field index
    findex = get_field_index(sf, field)

    for i, shape in enumerate(shapes):
        centroid = Polygon(shape.points).centroid.wkt
        centroid = centroid.strip('PIONT').replace('(',
                                                   ' ').replace(')',
                                                                ' ').split()
        tx, ty = m(float(centroid[0]), float(centroid[1]))
        if tx > m.xmin and tx < m.xmax and ty > m.ymin and ty < m.ymax:
            if value == None or value == recs[i][findex]:
                plt.text(tx + xoff, ty + yoff, recs[i][findex], size=fsize, \
                         weight=fweight, color=col, style=fstyle, va='center', ha='center')
        '''
示例#2
0
def getshapecolour(sf, field, colmap, ncolours, **kwargs):
    import matplotlib.cm as cm
    from numpy import arange, isnan, nan

    # if assigned, set kwargs
    zmin = nan
    zmax = nan
    for key in ('zmin', 'zmax'):
        if key in kwargs:
            # min value
            if key == 'zmin':
                zmin = kwargs[key]

            # set scaling relation
            if key == 'zmax':
                zmax = kwargs[key]

    # get field index
    findex = get_field_index(sf, field)

    # get max/min value in field
    fvect = []
    recs = sf.records()
    for rec in recs:
        fvect.append(float(rec[findex]))

    if isnan(zmin):
        zmin = min(fvect)
    if isnan(zmax):
        zmax = max(fvect)
    zrng = zmax - zmin

    # make colourmap
    cmap = cm.get_cmap(colmap, ncolours)
    cs = (cmap(arange(ncolours)))

    # get colour index
    ci = []
    for f in fvect:
        print(f)
        ci.append(round((ncolours - 1.) / zrng * (f - zmin)))

    return cs, ci, cmap, zmin, zmax
示例#3
0
def get_field_data(sf, field, datatype):
    '''
    datatype = float, str
    '''
    from misc_tools import checkfloat, checkint
    #from numpy import array

    # get index
    index = get_field_index(sf, field)

    # get records
    recs = sf.records()

    # now loop thru recs and get data
    data = []
    for rec in recs:
        if datatype == 'str':
            data.append(rec[index])
        elif datatype == 'float':
            data.append(checkfloat(rec[index]))
        elif datatype == 'int':
            data.append(checkint(rec[index]))

    return data
示例#4
0
def drawoneshapepoly(m, plt, sf, field, value, **kwargs):
    from numpy import arange

    # get kwargs
    ncolours = 256
    col = 'k'
    lw = 1.0
    ls = '-'
    fillshape = False
    polyline = False  # do not close polygon

    for key in ('col', 'cindex', 'cmap', 'ncolours', 'lw', 'ls', 'fillshape',
                'polyline'):
        if key in kwargs:
            if key == 'col':
                col = kwargs[key]

            if key == 'cindex':
                cindex = kwargs[key]

            if key == 'cmap':
                cmap = kwargs[key]

            if key == 'ncolours':
                ncolours = kwargs[key]

            if key == 'ls':
                ls = kwargs[key]

            if key == 'lw':
                lw = kwargs[key]

            if key == 'fillshape':
                fillshape = kwargs[key]

            if key == 'polyline':
                polyline = kwargs[key]

    shapes = sf.shapes()
    recs = sf.records()

    # get field index
    findex = get_field_index(sf, field)
    #print('findex', findex

    for i, shape in enumerate(shapes):
        #print('i', i
        if recs[i][findex] == value:
            # get colour
            try:
                cs = (cmap(arange(ncolours)))
                col = [
                    cs[int(cindex[i])][0], cs[int(cindex[i])][1],
                    cs[int(cindex[i])][2]
                ]
            except:
                try:
                    col = col
                except:
                    col = 'k'

            if fillshape == True:
                fillcol = col
                linecol = 'k'
            else:
                linecol = col

            # get xy coords
            x = []
            y = []

            p = 0
            parts = shape.parts
            parts.append(len(shape.points) - 1)

            for prt in range(0, len(parts) - 1):
                while p <= parts[prt + 1]:
                    x.append(shape.points[p][0])
                    y.append(shape.points[p][1])
                    p += 1

                # close polygon if not polyline
                if polyline == False:
                    if x[0] != x[-1] or y[0] != y[-1]:
                        x.append(x[0])

                # plot each polygon
                xx, yy = m(x, y)
                if fillshape == True:
                    plt.fill(xx, yy, color=fillcol)
                m.plot(xx,
                       yy,
                       linewidth=lw,
                       color=linecol,
                       linestyle=ls,
                       zorder=1)

                x = []
                y = []
    '''
示例#5
0
# make array of output field
fields = [x[0] for x in w.fields]

# loop through original records
i = 0
for record, shape in zip(records, shapes):

    # set shape polygon
    w.line(parts=[shape.points], shapeType=shapefile.POLYGON)

    # loop thru fields and match with original shapefile
    for j, field in enumerate(fields):

        # get field index from old shpfile
        idx = get_field_index(sf, field)

        # make record tuple from input shapefile
        if j == 0:
            #newrec = (record[idx],)
            newrec = [record[idx]]
        else:
            newrec.append(record[idx])

    # write new records
    print 'N0', src_n0[i], new_n0_b[i]
    if src_n0[i] != new_n0_b[i]:
        w.record(newrec[0], newrec[1], newrec[2], newrec[3], newrec[4], newrec[5], newrec[6], \
                 newrec[7], newrec[8], newrec[9], newrec[10], newrec[11], \
                 new_n0_b[i], new_n0_l[i], new_n0_u[i], new_bval_b[i], new_bval_l[i], new_bval_u[i], \
                 newrec[18], newrec[19], newrec[20], newrec[21], newrec[22], newrec[23], newrec[24], newrec[25])