def show_google_map(paths, API_key, region): lines = [] for f in pbar()(paths.fragments): flines = [] for l in f: line_coords = np.r_[list(l.coords.xy)].T for i in range(len(line_coords) - 1): flines.append( gmaps.Line(start=tuple(line_coords[i][::-1]), end=tuple(line_coords[i + 1][::-1]))) lines.append(flines) lines = flatten(lines) print "found", len(lines), "line segments" markers = [] for o, f in pbar()(zip(flatten(paths.resampled_orientations), flatten(paths.resampled_fragments))): coords = np.r_[list(f.xy)].T markers.append([ gmaps.Marker((coords[i][1], coords[i][0]), info_box_content=str(o[i])) for i in range(len(coords)) ]) markers = flatten(markers) print "found", len(markers), "sampling locations" gmaps.configure(api_key=API_key) gmap_b = gmaps.Polygon([(i[1], i[0]) for i in region]) fig = gmaps.figure(center=tuple(region.mean(axis=0)[::-1]), zoom_level=16) fig.add_layer(gmaps.drawing_layer(features=[gmap_b] + lines + markers)) return fig
def plot_route_map(self, freq): station_pairs = [sorted(i) for i in (self.route_dis.index)] bs = self.bikesystem fig = gmaps.figure(center=tuple(bs.locate(bs.mp.index[0])), zoom_level=11) layer = gmaps.drawing_layer(features=[ gmaps.Line(start=tuple(bs.locate(i)), end=tuple(bs.locate(j))) for i, j in self.highfreq_routes(freq) ]) fig.add_layer(layer) return fig
def predictSingle(self, imgFile, dataDir, ployGrid=None): ''' Predicts softmax ouput by trained model for single image and plots it imgFile has to look like: # eg: <gridNo>+<lat,long>+<imageNo_date>.jpg # eg: 60+48.4271513,-110.5611851+0_2009-06.jpg ''' xx,yy = self.readData([imgFile], dataDir) yp = self.model.predict(xx)[0] yn = list(map(lambda x:x/max(yp), yp)) dist, start, end = self.gridDist(ployGrid[np.argmax(yy[0])],ployGrid[np.argmax(yp)]) if ployGrid: mx = max(yn) mn = min(yn) plt.plot([start[1],end[1]], [start[0],end[0]], color='black', label="Distance: {} miles".format(round(dist,3))) for k,i in ployGrid.items(): if k==np.argmax(yy[0]): plt.plot(i[:,1],i[:,0],color='blue',label="Actual Grid", alpha=1) else: plt.plot(i[:,1],i[:,0],color='black', alpha=0.7) plt.fill(i[:,1],i[:,0],color='red', alpha=yn[k]) plt.legend(loc="lower left") plt.show() gPoly = [] gLine = gmaps.Line( start=start, end=end, stroke_color = 'blue' ) for grid, polygon in ployGrid.items(): gPoly.append(gmaps.Polygon( list(polygon), stroke_color='black', fill_color='red', fill_opacity=float(yn[grid]) )) fig = gmaps.figure(center=(39.50,-98.35), zoom_level=4) fig.add_layer(gmaps.drawing_layer(features=gPoly)) fig.add_layer(gmaps.drawing_layer(features=[gLine])) fig.add_layer(gmaps.symbol_layer([start], scale=3, fill_color='green',stroke_color='green', info_box_content='Expected')) fig.add_layer(gmaps.symbol_layer([end], scale=3, fill_color='yellow', stroke_color='yellow', info_box_content='Predicted: {}'.format(dist))) embed_minimal_html('gmap.html', views=fig) webbrowser.open('gmap.html',new=1) return dist
def map_plot(idx, d): line_coords = list( map(lambda i: polylines[i[0]][i[1]], list(zip(idx[:-1], idx[1:])))) # Features to draw on the map lines = [] markers = [] for points in line_coords: for p1, p2 in zip(points[:-1], points[1:]): lines.append(gmaps.Line(start=p1, end=p2, stroke_weight=3.0)) for i, p in enumerate(map(lambda x: candidate_stops[x], idx)): markers.append(gmaps.Marker(p)) d.features = lines + markers
def route(start, end, fig, color, stroke_weight_line, mode='walking'): directions_result = google_maps.directions(start, end, mode=mode) polyline_directions = polyline.decode( directions_result[0]['overview_polyline']['points']) polyline_result = [ (a, b) for a, b in zip(polyline_directions, polyline_directions[1:]) ] for i in range(len(polyline_result)): line_drawing = gmaps.Line(start=polyline_result[i][0], end=polyline_result[i][1], stroke_weight=stroke_weight_line, stroke_color=color, stroke_opacity=1.0) drawing = gmaps.drawing_layer(features=[line_drawing], show_controls=False) fig.add_layer(drawing)
def predictSingle(self, imgFile, dataDir, ployGrid): ''' Predicts softmax ouput by trained model for single image and plots it mgFiles: String that contains test location image triplet folder name. String has to look like: # <gridNo>+<lat,long> # 60+48.4271513,-110.5611851 dataDir: Directory that stores combined image files eg: "/dataCombinedSamples/" polyGrid: List of polygons that contain make up the USA split into grids. It can be loaded from eg: "infoExtraction/usaPolyGrid.pkl" ''' # read image triplets from single file xx, yy = self.readData([imgFile], dataDir) # predict single image triplet yp = self.model.predict(xx)[0] # normalize prediction for better visualization yn = list(map(lambda x: x / max(yp), yp)) # evaluate distance for single point dist, start, end = self.gridDist(ployGrid[np.argmax(yy[0])], ployGrid[np.argmax(yp)]) mx = max(yn) mn = min(yn) # plot result using matplotlib plt.plot([start[1], end[1]], [start[0], end[0]], color='black', label="Distance: {} miles".format(round(dist, 3))) for k, i in ployGrid.items(): if k == np.argmax(yy[0]): plt.plot(i[:, 1], i[:, 0], color='blue', label="Actual Grid", alpha=1) else: plt.plot(i[:, 1], i[:, 0], color='black', alpha=0.7) plt.fill(i[:, 1], i[:, 0], color='red', alpha=yn[k]) plt.legend(loc="lower left") plt.show() # plot result using google maps API gPoly = [] gLine = gmaps.Line(start=start, end=end, stroke_color='blue') for grid, polygon in ployGrid.items(): gPoly.append( gmaps.Polygon(list(polygon), stroke_color='black', fill_color='red', fill_opacity=float(yn[grid]))) fig = gmaps.figure(center=(39.50, -98.35), zoom_level=4) fig.add_layer(gmaps.drawing_layer(features=gPoly)) fig.add_layer(gmaps.drawing_layer(features=[gLine])) fig.add_layer( gmaps.symbol_layer([start], scale=3, fill_color='green', stroke_color='green', info_box_content='Expected')) fig.add_layer( gmaps.symbol_layer([end], scale=3, fill_color='yellow', stroke_color='yellow', info_box_content='Predicted: {}'.format(dist))) # save and display html page containing google maps API embed_minimal_html('gmap.html', views=fig) webbrowser.open('gmap.html', new=1) return dist
#%% # Charting Stuff on a Map with open("./secrets.json") as f: data = f.read() api_key = json.loads(data)['key'] gmaps.configure(api_key=api_key) fig = gmaps.figure() fig.add_layer(gmaps.heatmap_layer(ems[['Location Latitude', 'Location Longitude']], weights=ems['Total Patients'])) fig #%% fig = gmaps.figure() drawing = gmaps.drawing_layer(features=[ gmaps.Line((46.23, 5.86), (46.44, 5.24), stroke_weight=3.0), gmaps.Marker((46.88, 5.45), label='D'), gmaps.Polygon( [(46.72, 6.06), (46.48, 6.49), (46.79, 6.91)], fill_color='red' ) ]) fig.add_layer(drawing) fig #%% gmaps.__version__ #%%