Beispiel #1
0
def map_city(geodf):
    '''
    map the a chicago heatmap

    Input:
        geodf: geom dataframe after link with the neighborhoods
        nbhd: geom dataframe of 

    return plot of heatmap
    '''
    nbhd = nbhds.import_geometries(nbhds.NEIGHS_ID)
    first_col = geodf.columns[0]
    fig, ax = plt.subplots(1)
    fig.set_size_inches(20, 13)
    heat = geodf.dissolve(by='pri_neigh', aggfunc='count')
    heat = nbhd.merge(heat, on='pri_neigh', how='left').fillna(0)
    heat.plot(ax=ax, cmap='coolwarm', column=first_col, linewidth=0.8,
              linestyle='-')
    ax.axis('off')
    ax.set_title('Chicago ' + 'crime' + ' Heat Map')
    n_min = min(heat[first_col])
    n_max = max(heat[first_col])
    leg = mpl.cm.ScalarMappable(cmap='coolwarm', norm=mpl.colors.Normalize(
        vmin=n_min, vmax=n_max))
    leg._A = []
    colorbar = fig.colorbar(leg)
    return plt
def go_permits(parameters):
    '''
    Runs the program for the tickets dataset given the specified parameters

    Inputs:
    parameters (dictonary): dictionary mapping strings of parameter names to
        strings with parameter values
    '''
    if set(parameters.keys()) - set(PERMIT_COLUMNS.keys()):
        print('Error: Invalid parameter for permits dataset!')
    else:
        print('Loading the permits dataset...')
        pers = data_loader.get_permits('07-13-2015')
        pers = filter_input(pers, parameters, PERMIT_COLUMNS, 'permits')
        
        print()
        print('Building the map...')
        nbhd = nbhds.import_geometries(nbhds.NEIGHS_ID)
        pers = link_with_neighborhoods(pers, 'longitude', 'latitude')
    
    location_bool = ('location' in parameters) or ('neighborhood' in parameters)


    project_onto_chicago(pers, nbhd, location_bool, 'permits', 
                         parameters.get('neighborhood', ""))
def go_tickets(parameters):
    '''
    Runs the program for the tickets dataset given the specified parameters

    Inputs:
    parameters (dictonary): dictionary mapping strings of parameter names to
        strings with parameter values
    '''
    if set(parameters.keys()) - set(TICKET_COLUMNS.keys()):
        print('Error: Invalid parameter for tickets dataset!')
    else:
        print('Loading the tickets dataset...')
        tickets = data_loader.import_tickets(data_loader.TICKETS_FILEPATH,
                                             data_loader.VIOLATIONS_FILEPATH)
        tickets = filter_input(tickets, parameters, TICKET_COLUMNS, 'tickets')
        
        print()
        print('Building the map...')
        nbhd = nbhds.import_geometries(nbhds.NEIGHS_ID)
        tickets = link_with_neighborhoods(tickets, 'geocoded_lng', 'geocoded_lat')
    location_bool = ('location' in parameters) or ('neighborhood' in parameters)


    project_onto_chicago(tickets, nbhd, location_bool, 'tickets',
    	                 parameters.get('neighborhood', ""))
Beispiel #4
0
def link_with_neighborhoods(dataframe, lng_col, lat_col):
    '''
    Helper function to get a geocoded dataframe

    Input:
        df: dataframe with latitude and longtitude columns
    Return:
        A dataframe that is geocoded
    '''
    nbhd = nbhds.import_geometries(nbhds.NEIGHS_ID)
    geodf = nbhds.convert_to_geodf(dataframe, lng_col, lat_col)
    return nbhds.find_neighborhoods(geodf, nbhd)
def go_linked(parameters):
    '''
    Runs the program for the tickets dataset given the specified parameters

    Inputs:
    parameters (dictonary): dictionary mapping strings of parameter names to
        strings with parameter values
    '''
    if set(parameters.keys())- (set(PERMIT_COLUMNS.keys()) | 
                                set(TICKET_COLUMNS.keys())):
        print('Error: Invalid parameter for linked dataset!')
    else:
        print('Loading the tickets dataset...')
        tickets = data_loader.import_tickets(data_loader.TICKETS_FILEPATH,
                                             data_loader.VIOLATIONS_FILEPATH)
        tickets = filter_input(tickets, parameters, TICKET_COLUMNS, 'tickets')

        print()
        print('Loading the permits dataset...')
        pers = data_loader.get_permits('07-13-2015')
        pers = filter_input(pers, parameters, PERMIT_COLUMNS, 'permits')

        print()
        if not (pers.empty or tickets.empty):
            print('Linking permits to tickets...')
            linked = link_permits_tickets(pers, tickets)
            nbhd = nbhds.import_geometries(nbhds.NEIGHS_ID)
            linked = link_with_neighborhoods(linked, 'geocoded_lng',
                                             'geocoded_lat')
            
            print()
            print('Building the map...')
            location_bool = ('location' in parameters) or \
                            ('neighborhood' in parameters)

            project_onto_chicago(linked, nbhd, location_bool, 'linked', \
                parameters.get('neighborhood', ""))
        else:
            print('Datasets cannot be linked because your search yields either', 
                   '0 tickets, 0 permits, or both.')