def background_and_pixels(latitudes, longitudes, size, maptype): """Queries the proper background map and translate geo coordinated into pixel locations on this map. :param pandas.Series latitudes: series of sample latitudes :param pandas.Series longitudes: series of sample longitudes :param int size: target size of the map, in pixels :param string maptype: type of maps, see GoogleStaticMapsAPI docs for more info :return: map and pixels :rtype: (PIL.Image, pandas.DataFrame) """ # From lat/long to pixels, zoom and position in the tile center_lat = (latitudes.max() + latitudes.min()) / 2 center_long = (longitudes.max() + longitudes.min()) / 2 zoom = GoogleStaticMapsAPI.get_zoom(latitudes, longitudes, size, SCALE) pixels = GoogleStaticMapsAPI.to_tile_coordinates(latitudes, longitudes, center_lat, center_long, zoom, size, SCALE) # Google Map img = GoogleStaticMapsAPI.map( center=(center_lat, center_long), zoom=zoom, scale=SCALE, size=(size, size), maptype=maptype, ) return img, pixels
def plot_markers(markers, maptype=MAPTYPE): """Plot markers on a map. :param pandas.DataFrame markers: DataFrame with at least 'latitude' and 'longitude' columns, and optionally * 'color' column, see GoogleStaticMapsAPI docs for more info * 'label' column, see GoogleStaticMapsAPI docs for more info * 'size' column, see GoogleStaticMapsAPI docs for more info :param string maptype: type of maps, see GoogleStaticMapsAPI docs for more info :return: None """ # Checking input columns fields = markers.columns.intersection(['latitude', 'longitude', 'color', 'label', 'size']) if len(fields) == 0 or 'latitude' not in fields or 'longitude' not in fields: msg = 'Input dataframe should contain at least colums \'latitude\' and \'longitude\' ' msg += '(and columns \'color\', \'label\', \'size\' optionally).' raise KeyError(msg) # Checking NaN input nans = (markers.latitude.isnull() | markers.longitude.isnull()) if nans.sum() > 0: warnings.warn('Ignoring {} example(s) containing NaN latitude or longitude.'.format(nans.sum())) # Querying map img = GoogleStaticMapsAPI.map( scale=SCALE, markers=markers[fields].loc[~nans].T.to_dict().values(), maptype=maptype, ) plt.figure(figsize=(10, 10)) plt.imshow(np.array(img)) plt.tight_layout() plt.axis('off') plt.show()
def plot_markers(markers, maptype=MAPTYPE): """Plot markers on a map. :param pandas.DataFrame markers: DataFrame with at least 'latitude' and 'longitude' columnns, and optionally * 'color' column, see GoogleStaticMapsAPI docs for more info * 'label' column, see GoogleStaticMapsAPI docs for more info * 'size' column, see GoogleStaticMapsAPI docs for more info :param string maptype: type of maps, see GoogleStaticMapsAPI docs for more info :return: None """ img = GoogleStaticMapsAPI.map(scale=SCALE, markers=markers.T.to_dict().values(), maptype=maptype) plt.figure(figsize=(10, 10)) plt.imshow(np.array(img)) plt.tight_layout() plt.axis('off') plt.show()