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()
def Make_Map(green_data, zips): sizes = [] for tup in green_data.values(): sizes.append(tup[0]) max_size = max(sizes) colors = [] for tup in green_data.values(): colors.append(tup[1]) max_color = max(colors) for element in green_data.keys(): if element in zips.keys(): #create the data to make a dot for data = {'zip':zips[element][0], 'lat': [(zips[element][1])], 'lon': [(zips[element][2])], \ 'color': [int(255*(green_data[element][1]/max_color)), 255, int(255*(green_data[element][1]/max_color))], \ 'size':50*(int(green_data[element][0])/max_size)} #add the dot for the zipcode geoplotlib.add_layer(dd(data, data['color'], data['size'], f_tooltip=None)) #add labels for zipcode geoplotlib.labels(data, 'zip' , color='k', font_name= 'helvetica', \ font_size=8, anchor_x='left', anchor_y='top') geoplotlib.show()
def getHistogramOfMatches(self): #self.updateLocationDataFile() data = read_csv(self.location_data) print(data) geoplotlib.hist(data, colorscale='sqrt', binsize=8) geoplotlib.show()
def getHeatmapOfMatches(self): #self.updateLocationDataFile() data = read_csv(self.location_data) print(data) geoplotlib.kde(data, bw=[5, 5]) geoplotlib.show()
def getDelaunayTriangulation(self): #self.updateLocationDataFile() data = read_csv(self.location_data) print(data) geoplotlib.delaunay(data, cmap='hot_r') geoplotlib.show()
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()
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 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 draw_grid(nodes, unfiltered=None): """ Draw grid using computed nodes. Args: nodes (numpy.ndarray): Data points to plot unfiltered (numpy.ndarray): Unfiltered data points. If not None, plot using different color. """ # Layer for plotting the nodes class PointsLayer(BaseLayer): def __init__(self, data, color, point_size): self.data = data self.color = color self.point_size = point_size def invalidate(self, proj): x, y = proj.lonlat_to_screen(self.data['lon'], self.data['lat']) self.painter = BatchPainter() self.painter.set_color(self.color) self.painter.points(x, y, point_size=self.point_size, rounded=True) def draw(self, proj, mouse_x, mouse_y, ui_manager): self.painter.batch_draw() # Get grid node data into dict format. data_grid = {'lat': nodes[:, 0], 'lon': nodes[:, 1]} # If unfiltered nodes specified, get data into dict format. if unfiltered is not None: data_unfiltered = {'lat': unfiltered[:, 0], 'lon': unfiltered[:, 1]} # If unfiltered nodes specified, plot on layer. if unfiltered is not None: geoplotlib.add_layer( PointsLayer(data_unfiltered, color=[255, 0, 0], point_size=4)) # Plot grid nodes. geoplotlib.add_layer( PointsLayer(data_grid, color=[0, 0, 255], point_size=7)) # Set bounding box and show. geoplotlib.set_bbox( BoundingBox(north=40.897994, west=-73.199040, south=40.595581, east=-74.55040)) geoplotlib.show()
def visualize_heatmap_for_data(self, df): """ Normally visualize accidents distributions across borough :param df: Dataset :return: None """ geo_data_for_plotting = {"lat": df["LATITUDE"], "lon": df["LONGITUDE"]} geoplotlib.kde(geo_data_for_plotting, 1) east = max(df["LONGITUDE"]) west = min(df["LONGITUDE"]) south = max(df["LATITUDE"]) north = min(df["LATITUDE"]) bbox = BoundingBox(north=north, west=west, south=south, east=east) geoplotlib.set_bbox(bbox) geoplotlib.tiles_provider('toner-lite') 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)
def test_graph(): import geoplotlib from geoplotlib.utils import read_csv movement_data = read_csv('./data/graph_movement.csv') geoplotlib.graph(movement_data, src_lat='SourceLat', src_lon='SourceLon', dest_lat='TargetLat', dest_lon='TargetLon', alpha=80, linewidth=2, color='inferno', color_by='Weight', levels=10) geoplotlib.show()
def __init__(self, year, grade): self.scale = 18 self.start = 18 self.year = year self.grade = grade cur.execute( 'select date, state, county, {} from counties where date like "%{}%"' .format(self.grade, self.year)) print("connected") self.counties = cur.fetchall() with open('GEOIDs.txt', 'r') as f: reader = csv.reader(f) csv_list = list(reader) self.test = [] for i in self.counties: for x in csv_list: if i[1] == x[0] and (i[2] in x[3]): geoid = x[1] + x[2] try: self.test.append([geoid, float(i[3])]) except TypeError: self.test.append([geoid, 0]) self.test.sort(key=lambda j: float(j[1])) self.ranger(self.test) self.unemployment = {t[0]: t[1] for t in self.test} print('starting map') self.cmap = ColorMap('plasma', alpha=255, levels=10) geoplotlib.geojson( 'data/gz_2010_us_050_00_20m.json', fill=True, color=self.get_color, f_tooltip=(lambda properties: properties['NAME'] + ' ' + str( self.unemployment.get( str(properties['STATE']) + properties['COUNTY'])))) geoplotlib.geojson('data/gz_2010_us_050_00_20m.json', fill=False, color=[255, 255, 255, 64]) geoplotlib.set_bbox(BoundingBox.USA) geoplotlib.show()
def _draw_hot(day, hour, minute): """ geoplotlib热力图 :param day: :param hour, minute: :param tp: :return: """ data = da.loc_state(day, hour, minute) bbox1 = BoundingBox(north=32, south=30.7, west=122.2, east=120.8) gp.set_bbox(bbox1) # gp.shapefiles('F:\\road_datas\\ShangHai\\boundary', shape_type='empty') gp.kde(data, bw=[0.5, 0.5], cmap='jet', scaling='wjk') gp.savefig('001') gp.show()
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]
#!/usr/bin/env python2 import geoplotlib from geoplotlib.utils import read_csv data = read_csv('ui_network_density.csv') geoplotlib.dot(data) geoplotlib.show()
def plotTwoGroups(coords1, coords2): geoplotlib.dot(coords1, color=(255, 0, 0, 255)) geoplotlib.dot(coords2, color=(0, 255, 0, 255)) geoplotlib.show()
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()
def draw_geo(): data = geoplotlib.utils.read_csv('../data/des.csv') geoplotlib.add_layer(KMeansLayer(data)) geoplotlib.set_smoothing(True) geoplotlib.show()
def show_map(self): self.load_points() gp.show()
def show_geoplot(results, mbc): data=DataAccessObject.from_dataframe(results[["lat","lon"]]) gpl.hist(data, colorscale='sqrt', binsize=4) gpl.kde(data, bw=5, cut_below=1e-3) gpl.set_bbox(BoundingBox(mbc[0],mbc[1],mbc[2],mbc[3])) gpl.show()
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) gp.dot(geoPoltData, point_size=plotPointSize, color=crimeColor) gp.show() #2. Only streetlights, wifi, and violent crime locations (no differentiation between night/day) - felonies 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)