Exemplo n.º 1
0
def output_json(regions, options):
    """Write a GeoJSON collection of `regions` with their discrepancy."""
    discrepancies = [v[0] for v in regions]
    colormap = mpl.cm.ScalarMappable(sps.mcolor.Normalize(min(discrepancies),
                                                          max(discrepancies)),
                                     'YlOrBr')
    schema = {'geometry': 'Polygon', 'properties': [('discrepancy', 'float'),
                                                    ('color', 'str'),
                                                    ('photos', 'float'),
                                                    ('checkins', 'float')]}
    get_color = lambda v: sps.mcolor.rgb2hex(colormap.to_rgba(v))
    city = options['city']
    photos_as_background = options['photos_background']
    prefix = '_only' if options['only'] else ''
    ratio = options.pop('ratio', 0)
    if photos_as_background:
        photos_idx, checkins_idx = 2, 3
        name = city+prefix+'_checkins_d.json'
        options['photos_ratio'] = ratio
    else:
        photos_idx, checkins_idx = 3, 2
        name = city+prefix+'_photos_d.json'
        options['checkins_ratio'] = ratio
    polys = [{'geometry': sps.mapping(r[1]), 'properties':
              {'discrepancy': r[0], 'color': get_color(r[0]),
               'photos': r[photos_idx], 'checkins': r[checkins_idx]}}
             for r in regions]
    name = os.path.join('maps', name)
    write_collection(polys, name, schema)
    options['city'] = '"{}"'.format(options['city'])
    with open(os.path.join('maps', city+'.js'), 'a') as f:
        f.write('\n'.join(['var {} = {};'.format(var, str(val).lower())
                           for var, val in options.iteritems()]))
    options['city'] = city
Exemplo n.º 2
0
def full_disc_json(lratio, nz, city='paris'):
    it = np.nditer(lratio, flags=['f_index'])
    colormap = mpl.cm.ScalarMappable(sps.mcolor.Normalize(nz.min(), nz.max()),
                                     'coolwarm')
    schema = {
        'geometry': 'Polygon',
        'properties': [('ratio', 'float'), ('color', 'str')]
    }
    get_color = lambda v: sps.mcolor.rgb2hex(colormap.to_rgba(v))
    polys = []
    box = lambda i: sps.shape(sps.bbox_to_polygon(sps.index_to_rect(i), False))
    while not it.finished:
        idx, val = it.index, it[0]
        if not np.isinf(val):
            val = float(val)
            polys.append({
                'geometry': sps.mapping(box(idx)),
                'properties': {
                    'ratio': val,
                    'color': get_color(val)
                }
            })
        it.iternext()
    print(polys[0])
    name = 'maps/' + city + '_full_d.json'
    write_collection(polys, name, schema)
Exemplo n.º 3
0
def full_disc_json(lratio, nz, city='paris'):
    it = np.nditer(lratio, flags=['f_index'])
    colormap = mpl.cm.ScalarMappable(sps.mcolor.Normalize(nz.min(), nz.max()),
                                     'coolwarm')
    schema = {'geometry': 'Polygon', 'properties': [('ratio', 'float'),
                                                    ('color', 'str')]}
    get_color = lambda v: sps.mcolor.rgb2hex(colormap.to_rgba(v))
    polys = []
    box = lambda i: sps.shape(sps.bbox_to_polygon(sps.index_to_rect(i), False))
    while not it.finished:
        idx, val = it.index, it[0]
        if not np.isinf(val):
            val = float(val)
            polys.append({'geometry': sps.mapping(box(idx)), 'properties':
                          {'ratio': val, 'color': get_color(val)}})
        it.iternext()
    print(polys[0])
    name = 'maps/'+city+'_full_d.json'
    write_collection(polys, name, schema)
Exemplo n.º 4
0
def output_json(regions, options):
    """Write a GeoJSON collection of `regions` with their discrepancy."""
    discrepancies = [v[0] for v in regions]
    colormap = mpl.cm.ScalarMappable(
        sps.mcolor.Normalize(min(discrepancies), max(discrepancies)), 'YlOrBr')
    schema = {
        'geometry':
        'Polygon',
        'properties': [('discrepancy', 'float'), ('color', 'str'),
                       ('photos', 'float'), ('checkins', 'float')]
    }
    get_color = lambda v: sps.mcolor.rgb2hex(colormap.to_rgba(v))
    city = options['city']
    photos_as_background = options['photos_background']
    prefix = '_only' if options['only'] else ''
    ratio = options.pop('ratio', 0)
    if photos_as_background:
        photos_idx, checkins_idx = 2, 3
        name = city + prefix + '_checkins_d.json'
        options['photos_ratio'] = ratio
    else:
        photos_idx, checkins_idx = 3, 2
        name = city + prefix + '_photos_d.json'
        options['checkins_ratio'] = ratio
    polys = [{
        'geometry': sps.mapping(r[1]),
        'properties': {
            'discrepancy': r[0],
            'color': get_color(r[0]),
            'photos': r[photos_idx],
            'checkins': r[checkins_idx]
        }
    } for r in regions]
    name = os.path.join('maps', name)
    write_collection(polys, name, schema)
    options['city'] = '"{}"'.format(options['city'])
    with open(os.path.join('maps', city + '.js'), 'a') as f:
        f.write('\n'.join([
            'var {} = {};'.format(var,
                                  str(val).lower())
            for var, val in options.iteritems()
        ]))
    options['city'] = city