Exemple #1
0
def visualize(recs):
    '''
    Takes the direct output from calculation, maps the 3 recommendations
    based on their cuisine locations
    '''

    if recs != []:
        rv = []
        for idx, tup in enumerate(recs):
            lat, lon = tup[1]['coordinates']
            rv.append(["#" + str(idx + 1) + ". " + tup[1]['name'] + \
                            " (" + tup[1]['cuisine'] + ")", lat, lon])

        header = ['name', 'lat', 'lon']
        with open("form/static_files/img/visualization.csv", "w") as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow([h for h in header])
            writer.writerows(rv)

        data = geoplotlib.utils.read_csv(
            "form/static_files/img/visualization.csv")
        geoplotlib.dot(data)
        geoplotlib.labels(data, 'name', color=[0, 0, 255, 255], font_size=10, \
                                                         anchor_x='center')
        geoplotlib.savefig("form/static_files/img/visualization")
def main():
    """ Extracts latitude and longitude from the JSON feed, converts the list of dictionaries to a dataframe and then plot the coordinates via GeoPlotLib """
    url = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/1.0_month.geojson"
    json_obj = get_json(url)
    entire_report_dictionary = json_loads_to_dictionary(json_obj)
    number_of_records, id_lon_lat = extract_records(entire_report_dictionary)[
        0], extract_records(entire_report_dictionary)[1]

    print("writing to file\n")
    with open('id_coordinates.csv', 'wt') as textiowrapper:
        textiowrapper.writelines('name,lon,lat\n')
        for i in range(number_of_records):
            x = 0
            id_num = str(id_lon_lat[i][x])
            lon = str(id_lon_lat[i][x + 1])
            lat = str(id_lon_lat[i][x + 2])
            s = id_num + ',' + lon + ',' + lat
            textiowrapper.writelines(s + '\n')
            del x
    textiowrapper.close()
    print("done writing to file\n")

    print("mapping the data\n")
    data = read_csv('id_coordinates.csv')
    geoplotlib.dot(data)
    geoplotlib.show()
Exemple #3
0
def geo_dot(file):      # file must have at top: name,lat,lon
    """
    Renders a geo dot graph
    :param file: path to file
    :return: saves image to temp/map.png
    """
    data = read_csv(file)
    geoplotlib.dot(data, point_size=3)
    # geoplotlib.show()
    geoplotlib.savefig('temp/map')
Exemple #4
0
def plot(graph, tag_to_color, directed=True):
    color_to_nodes_data = defaultdict(list)
    color_to_nodes_pois = defaultdict(list)
    plot_edges = list()
    for node in graph.nodes:
        node_info = {'lon': node.coordinates[1], 'lat': node.coordinates[0]}
        color = (0, 0, 0)
        if len(node.tags):
            if node.tags[0] in tag_to_color:
                color = tag_to_color[node.tags[0]]
            else:
                continue
        if node.data_node:
            color_to_nodes_data[color].append(node_info)
        else:
            color_to_nodes_pois[color].append(node_info)
        neighbors = node.successors
        if not directed:
            neighbors = neighbors.union(node.predecessors)
        for successor in neighbors:
            #if node.coordinates[1] == successor.coordinates[1] and node.coordinates[0] == successor.coordinates[0]:
            #   print(node)
            #   print(successor)
            plot_edges.append({
                'start_lon': node.coordinates[1],
                'end_lon': successor.coordinates[1],
                'start_lat': node.coordinates[0],
                'end_lat': successor.coordinates[0]
            })

    df_edges = pd.DataFrame(plot_edges)
    ''' 
    if not df_edges.empty: 
        geoplotlib.graph(df_edges,
                        src_lat='start_lat',
                        src_lon='start_lon',
                        dest_lat='end_lat',
                        dest_lon='end_lon',
                        color='Dark2',
                        alpha=30,
                        linewidth=3)
    '''
    for color, nodes_list in color_to_nodes_pois.items():
        nodes_df = pd.DataFrame(nodes_list)
        color = list(color)
        color.append(255)
        geoplotlib.dot(nodes_df, color=color)

    for color, nodes_list in color_to_nodes_data.items():
        nodes_df = pd.DataFrame(nodes_list)
        color = list(color)
        color.append(255)
        geoplotlib.dot(nodes_df, color=color)

    geoplotlib.show()
Exemple #5
0
def DistanceVisual(lat1, lon1, venue, distance):
    locs = pd.DataFrame({
        'name':
        ['University of Michigan', venue + " " + str(distance) + " km"],
        'lat': [42.2780, lat1],
        'lon': [-83.7382, lon1]
    })
    geoplotlib.dot(locs,
                   color='b',
                   point_size=20,
                   f_tooltip=lambda r: r['name'])
    geoplotlib.show()
def displayChargers(chargers, color="red", img_name="charging_points"):
    """ Puts the charging points in a map. Saves image to disk. """
    lats, lons, powers = [], [], []
    for center in chargers:
        for charger in center:
            lats.append(charger["coords"][0])
            lons.append(charger["coords"][1])
            powers.append(charger["power"])

    thedata = pd.DataFrame({"lat": lats, "lon": lons, "powers": powers})
    geoplotlib.dot(thedata, color=color)
    geoplotlib.savefig(img_name)
    return True
Exemple #7
0
def draw_crashes_points(cards, filtred = True):
    if filtred: 
        latitude, longitude = ('lat', 'lon')
    else: 
        latitude, longitude = ('latitude', 'longitude')
        
    points = {'lon':[], 'lat':[]}
    for lat, lon in zip(cards[latitude], cards[longitude]):
        points['lon'].append(float(lon))
        points['lat'].append(float(lat))

    geoplotlib.dot(points, point_size=2, color='green')
    geoplotlib.show()
def doKMeans(df):
    #
    # INFO: Plot data with a '.' marker, with 0.3 alpha at the Longitude,
    # and Latitude locations in your dataset. Longitude = x, Latitude = y
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(df.lon, df.lat, marker='.', alpha=0.1)

    #
    # : Filter df so that you're only looking at Longitude and Latitude,
    # since the remaining columns aren't really applicable for this purpose.
    #
    sliceK = df[['lon', 'lat']]

    #
    # : Use K-Means to try and find seven cluster centers in this df.
    #

    #model = KMeans(n_clusters=10, n_init=10, init='k-means++')
    model = DBSCAN(eps=0.5,
                   min_samples=5,
                   metric="euclidean",
                   metric_params=None,
                   algorithm="auto",
                   leaf_size=30,
                   p=None,
                   n_jobs=1)
    model.fit(sliceK)

    #
    # INFO: Print and plot the centroids...
    centroids = model.cluster_centers_
    try:
        geoplotlib.dot(df, color='red', point_size=0.2)
    except:
        print("Exception")
    geoplotlib.show()

    ax.scatter(centroids[:, 0],
               centroids[:, 1],
               marker='x',
               c='red',
               alpha=0.5,
               linewidths=3,
               s=169)

    print(centroids)
Exemple #9
0
def plotTwoGroups(coords1, coords2):
    geoplotlib.dot(coords1, color=(255, 0, 0, 255))
    geoplotlib.dot(coords2, color=(0, 255, 0, 255))
    geoplotlib.show()
    
#plot each file 
    
#p v c d: (7133, 59) (307804, 35) (119931, 21) (19680, 18)


#permit

import geoplotlib

b3=b2.ix[b2['id']<7133,:]
sns.lmplot('lon', 'lat', data=b3, fit_reg=False, size=10)
#Image('detroit_area.png')


geoplotlib.dot(b3,point_size=0.8)
geoplotlib.show()

#crime

b3=b2.ix[(b2['id']>=7133) & (b2['id']<7133+307804),:]
sns.lmplot('lon', 'lat', data=b3, fit_reg=False, size=10)
#Image('detroit_area.png')


geoplotlib.dot(b3,point_size=0.8)
geoplotlib.show()
#Image('crime.png')
plt.savefig('crime.png')    

#violation
 def load_points(self):
     gp.dot(self.dataframe, )
Exemple #12
0
import geoplotlib
from geoplotlib.colors import colorbrewer
from geoplotlib.utils import epoch_to_str, BoundingBox, read_csv

data = read_csv('./data/metro.csv')
geoplotlib.dot(data, 'r')
geoplotlib.labels(data,
                  'name',
                  color=[0, 0, 255, 255],
                  font_size=10,
                  anchor_x='center')
geoplotlib.set_bbox(BoundingBox.KBH)
geoplotlib.show()
Exemple #13
0
def origin(id):
    return filter_by_id(id).loc[[0]]
        
# The last time a vehicle id registered an action in the simulation
def destiny(id):
    return filter_by_id(id).iloc[[-1]]

tool_tip = lambda x: 'time: ' + str(x["time"]) + ', lat: ' + str(x["lat"]) + ', lon: ' + str(x["lon"])

import geoplotlib

points_df = filter_by_id('4858_52').reset_index()
start = origin('4858_52').reset_index()
end = destiny('4858_52').reset_index()

geoplotlib.dot(start, color="green", point_size=5, f_tooltip=tool_tip)
geoplotlib.dot(end, color="black", point_size=5, f_tooltip=tool_tip)

# Custom layer
from geoplotlib.layers import BaseLayer
from geoplotlib.layers import HotspotManager
from geoplotlib.utils import BoundingBox
from geoplotlib.core import BatchPainter
import random
import time

class DotDensityLayer(BaseLayer):

    def __init__(self, data, color=None, point_size=2, f_tooltip=None):
        """Create a dot density map
        :param data: data access object
Exemple #14
0
#!/usr/bin/env python2

import geoplotlib
from geoplotlib.utils import read_csv

data = read_csv('ui_network_density.csv')
geoplotlib.dot(data)
geoplotlib.show()
Exemple #15
0
#!/usr/bin/env python2
import geoplotlib
from geoplotlib.utils import read_csv, BoundingBox, DataAccessObject

data = read_csv('filtered_lonlat.csv')

# http://andreacuttone.com/geoplotlib/api.html#module-geoplotlib
geoplotlib.dot(data, color=[0,0,0], point_size=1.5)
geoplotlib.kde(data, bw=10, cmap='PuBuGn', cut_below=1e-4, clip_above=1e-2, alpha=180)
geoplotlib.graph(read_csv('group0.csvgraph.csv'), src_lat='flat', src_lon='flon',
        dest_lat='tlat', dest_lon='tlon', color=[0,0,0], linewidth=2)
geoplotlib.graph(read_csv('group1.csvgraph.csv'), src_lat='flat', src_lon='flon',
        dest_lat='tlat', dest_lon='tlon', color=[0,255,0], linewidth=2)
geoplotlib.graph(read_csv('group2.csvgraph.csv'), src_lat='flat', src_lon='flon',
        dest_lat='tlat', dest_lon='tlon', color=[128,0,128], linewidth=2)
geoplotlib.kde(read_csv('chokepoints.csv'), bw=10, cmap='hot',
        cut_below=1e-4, clip_above=1e-2, alpha=180)

bbox = BoundingBox(north=25.7188,west=-80.280,south=25.711,east=-80.280)
geoplotlib.set_bbox(bbox)
geoplotlib.set_window_size(1400, 1600)
#geoplotlib.set_window_size(700, 800)
geoplotlib.tiles_provider('toner')
geoplotlib.set_smoothing(True)
geoplotlib.savefig('output')
#geoplotlib.show()
Exemple #16
0
schools = df.School

data = [go.Bar(x=df.School, y=df.Gap)]

py.iplot(data, filename='jupyter-basic_bar')

# # geoplotlib

# In[ ]:

import geoplotlib
from geoplotlib.utils import read_csv

data = read_csv('bus.csv')
geoplotlib.dot(data)
geoplotlib.show()

# # Direct plotting

# In[116]:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(200, 6),
                  index=pd.date_range('1/9/2009', periods=200),
                  columns=list('ABCDEF'))

df.plot(figsize=(20, 10)).legend(bbox_to_anchor=(1, 1))
#Shape of passed values is (10, 200), indices imply (4, 10)
lon2L= []
lat3L= []
lon3L= []
colorl = []

# color the dots based on price range
for idx in range(len(star_ratingL)):
    if star_ratingL[idx] == 1:
        lat1L.append(latL[idx])
        lon1L.append(lonL[idx])
    elif star_ratingL[idx] == 2:
        lat2L.append(latL[idx])
        lon2L.append(lonL[idx])
    elif star_ratingL[idx] == 3:
        lat3L.append(latL[idx])
        lon3L.append(lonL[idx])


# add new dot layer
data1 = {'lat':lat1L, 'lon':lon1L}
geoplotlib.dot(data1, color = "blue")

data2 = {'lat':lat2L, 'lon':lon2L}
geoplotlib.dot(data2, color = "green")

data3 = {'lat':lat3L, 'lon':lon3L}
geoplotlib.dot(data3, color = "red")

# show the plot
geoplotlib.show()
Exemple #18
0
sns.lmplot('lon', 'lat', data=b2, fit_reg=False, size=10)
#Image('detroit_area.png')

#plot each file

#p v c d: (7133, 59) (307804, 35) (119931, 21) (19680, 18)

#permit

import geoplotlib

b3 = b2.ix[b2['id'] < 7133, :]
sns.lmplot('lon', 'lat', data=b3, fit_reg=False, size=10)
#Image('detroit_area.png')

geoplotlib.dot(b3, point_size=0.8)
geoplotlib.show()

#crime

b3 = b2.ix[(b2['id'] >= 7133) & (b2['id'] < 7133 + 307804), :]
sns.lmplot('lon', 'lat', data=b3, fit_reg=False, size=10)
#Image('detroit_area.png')

geoplotlib.dot(b3, point_size=0.8)
geoplotlib.show()
#Image('crime.png')
plt.savefig('crime.png')

#violation
f = open("VLOCs.csv", "w+")
f.close()

#write csv to be read by geoplotlib
with open('VLOCs.csv', mode='w', newline='') as VLOCs:
    VLOCs = csv.writer(VLOCs, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    VLOCs.writerow(['name', 'S_lat', 'S_lon', 'D_lat', 'D_lon'])
    for station in Locations:
        VLOCs.writerow([station[0], station[1], station[2], station[3], station[4]])

#empty SLOCs.csv
f = open("SLOCs.csv", "w+")
f.close()

#write csv to be read by geoplotlib
with open('SLOCs.csv', mode='w', newline='') as SLOCs:
    SLOCs = csv.writer(SLOCs, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    SLOCs.writerow(['name', 'lat', 'lon'])
    for station in Locations:
        SLOCs.writerow([station[0], station[1], station[2]])

#plot stations
Plotdata1 = read_csv('VLOCs.csv')
Plotdata2 = read_csv('SLOCs.csv')
gp.set_bbox(BoundingBox(north=9, west=110, south=1, east=95))
gp.graph(Plotdata1, 'S_lat', 'S_lon', 'D_lat', 'D_lon', linewidth=2, color='Blues')
gp.dot(Plotdata2, color='blue', point_size=3)
gp.labels(Plotdata2, 'name', color='black', font_size=8, anchor_x='center')
gp.tiles_provider('positron')

gp.show()
        #convert list with [phi, lambda, h] to [name, lat, lon]
        Locations.append([Sname, deg(series[0].pos[0]), deg(series[0].pos[1])])

#path to SLOCs.csv
os.chdir(os.path.dirname(os.path.realpath(__file__)))

#empty SLOCs.csv
f = open("SLOCs.csv", "w+")
f.close()

#write csv to be read by geoplotlib
with open('SLOCs.csv', mode='w', newline='') as SLOCs:
    SLOCs = csv.writer(SLOCs,
                       delimiter=',',
                       quotechar='"',
                       quoting=csv.QUOTE_MINIMAL)
    SLOCs.writerow(['name', 'lat', 'lon'])
    for station in Locations:
        SLOCs.writerow([station[0], station[1], station[2]])

#plot stations
Plotdata = read_csv('SLOCs.csv')
gp.set_bbox(BoundingBox(north=9, west=110, south=1, east=95))
gp.dot(Plotdata, color='red', point_size=2)
gp.labels(Plotdata, 'name', color='black', font_size=8, anchor_x='center')
gp.tiles_provider('positron')

gp.show()

'https://maps-for-free.com/layer/relief/z{Z}/row{Y}/{Z}_{X}-{Y}.jpg'
#ToDo: change tile to map from the web (url above)
Exemple #21
0
transpose = np.transpose(extracted_cols)
# Write the latitude and longitude columns to a csv
with open('lat_lon_prices.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(transpose)
with open('lat_lon_prices2.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(extracted_cols)

# In[190]:

#Visualization 1: A map that depicts geographical distribution of listings
from geoplotlib.colors import ColorMap
from geoplotlib.colors import colorbrewer
from geoplotlib.utils import epoch_to_str, BoundingBox, read_csv
geoplotlib.dot(listings2, color='blue')
geoplotlib.show()

# In[180]:

# Second Visualization: Distribution of Prices
plt.hist(prices, bins=1000, range=(0, 1000))
plt.title(
    "Distribution of sub-$1000 Airbnb Listings by Price in San Francisco")
plt.xlabel("Price (USD)")
plt.ylabel("Number of Listings")
plt.show()

# In[182]:

# Third Visualization: Distribution of Customer Ratings
def s_meanshif(filepath,labelpath="../../data/label.csv", label_despath="../../data/label_des.csv"):
    #datas = deal_des(filepath)
    #提取目的地坐标
    #des = []
    #for it in datas:
    #    des.append(it[1])

    #从des文件中读取目的地信息
    des = []
    tripid = []
    file = open("../../data/des.csv","r")
    for line in file:
        try:
            if len(line) > 1:
                data = line.strip("\n").split(" ")
                x,y = float(data[1]),float(data[2])
                tripid.append(data[0])
                des.append([x, y])
        except:
            #print line
            pass
    print len(des),len(tripid)
    #将list转换为numpy的矩阵格式
    des = np.array(des)
    # The following bandwidth can be automatically detected using
    #得到meanshift所需的bandwidth
    bandwidth = estimate_bandwidth(des, quantile=0.2, n_samples=100000)
    #print bandwidth
    bandwidth = 0.008
    #得到meanShift模型并进行训练
    ms = MeanShift(bandwidth=bandwidth, bin_seeding=True, min_bin_freq=5)
    ms.fit(des)

    #得到训练的标签以及类别中心点坐标
    labels = ms.labels_
    cluster_centers = ms.cluster_centers_
    print "样本个数:",len(labels)
    #存储每个类别对应的中心坐标
    out = open(labelpath,"w")
    i = 0
    ress = []
    for cluster_center in cluster_centers:
        #print cluster_center
        out.write(str(i)+" "+str(cluster_center[0])+" "+str(cluster_center[1])+"\n")
        i += 1
    out.close()

    #存储目的地对应的类别
    label_des = open(label_despath, "w")
    i = 0
    for label in labels:
        #print tripid[i],label
        label_des.write(str(tripid[i])+" "+str(label)+" "+str(cluster_centers[int(label)][0])+" "+str(cluster_centers[int(label)][1])+"\n")
        i += 1

    label_des.close()
    #print ress
    labels_unique = np.unique(labels)
    n_clusters_ = len(labels_unique)
    print 'cluster num:',n_clusters_

    #对聚类效果进行可视化
    geo_data = {'lat':[],'lon':[]}
    for xy in cluster_centers:
        x, y = xy[0],xy[1]
        geo_data['lon'].append(x)
        geo_data['lat'].append(y)

    #print geo_data
    geoplotlib.dot(geo_data)
    geoplotlib.show()
import plotly
import psycopg2
import geoplotlib

try:
    conn = psycopg2.connect(
        "dbname='zillowdb' user='******' host='localhost' password='******'")
except:
    print "I am unable to connect to the database."

cur = conn.cursor()
try:
    cur.execute(
        """SELECT latitude, longitude FROM homes WHERE house_id<100;""")

except (Exception, psycopg2.DatabaseError) as error:
    print(error)

coords = cur.fetchall()

thedata = {
    'lat': [float(i[0]) / 1000000 for i in coords],
    'lon': [float(j[1]) / 1000000 for j in coords]
}

# close communication with the PostgreSQL database server
cur.close()

# display geographical map
geoplotlib.dot(thedata)
geoplotlib.show()
streetLightColor = [103, 191, 92]
wifiColor = [0, 122, 255]
crimeColor = [237, 102, 93, alphaCrime]
gp.clear()
plotPointSize = 2

gp.set_window_size(800, 800)
gp.tiles_provider('darkmatter')

#Street light plot
geoPoltData = streetlight[["lat_s", "lon_s"]].rename(columns={
    'lat_s': 'lat',
    'lon_s': 'lon'
},
                                                     inplace=False)
gp.dot(geoPoltData, point_size=plotPointSize, color=streetLightColor)

#Wifi plot
geoPoltData = wifi[["lat_w", "lon_w"]].rename(columns={
    'lat_w': 'lat',
    'lon_w': 'lon'
},
                                              inplace=False)
gp.dot(geoPoltData, point_size=plotPointSize, color=wifiColor)

#Crime plot
geoPoltData = NYPD_filter[["lat_c", "lon_c"]].rename(columns={
    'lat_c': 'lat',
    'lon_c': 'lon'
},
                                                     inplace=False)
Exemple #25
0
import sys
import numpy
import geoplotlib
from geoplotlib.utils import read_csv, BoundingBox
from geoplotlib.colors import ColorMap

data = read_csv('data/out_FL.csv')
#TODO: apply filtering here rather than when making the csv
bb = BoundingBox.from_points(lons=data['lon'], lats=data['lat'])

#TODO: utilize layers
#mark the station locations
geoplotlib.dot(data, color=[0, 0, 0, 255])

#TODO: show based on zoom level
#geoplotlib.labels(data,'Station Name (LEA)', color=[150,150,190,255], font_size=9, anchor_x='center')

#geoplotlib.voronoi(data, cmap='Blues_r', max_area=8e3, alpha=200, f_tooltip=lambda d:d['Station Name (LEA)'] )

geoplotlib.kde(data, cmap='Blues_r', bw=10, cut_below=1e-4, scaling='lin')

#geoplotlib.delaunay(data,cmap='hot_r')

##post
geoplotlib.set_bbox(bb)
geoplotlib.set_smoothing(True)
geoplotlib.show()
Exemple #26
0
import geoplotlib
from geoplotlib.colors import colorbrewer
from geoplotlib.utils import epoch_to_str, BoundingBox, read_csv


data = read_csv('./data/metro.csv')
geoplotlib.dot(data, 'r')
geoplotlib.labels(data, 'name', color=[0,0,255,255], font_size=10, anchor_x='center')
geoplotlib.set_bbox(BoundingBox.KBH)
geoplotlib.show()
Exemple #27
0
import geoplotlib
from geoplotlib.utils import read_csv
import pandas as pd

df = pd.read_csv('directory.csv')
lat = df.lat
lon = df.lon

geoplotlib.dot(lat[0], lon[0])
geoplotlib.show()

print lat[0]
print lon[0]
Exemple #28
0
import geoplotlib
from geoplotlib.colors import colorbrewer
from geoplotlib.utils import epoch_to_str, BoundingBox, read_csv


data = read_csv('agentTest.csv')
plot points
geoplotlib.dot(data, 'b')
# plot the heatmap
#geoplotlib.hist(data, colorscale='sqrt', binsize=4)
# geoplotlib.show()