def fishnetmap(ax, points, weights, bounding_box, color_gradient=["#0000FF", "#FF0000"], cell_size=4, cell_spacing=1, opacity=1.0, coordinate_system='epsg:3857', aggregation_type='sum', **extra_contextily_params): from matplotlib import pyplot as plt import contextily as cx bbox = _transform_bbox(bounding_box, coordinate_system, 'epsg:3857') w, h = _get_recom_size(bbox[2] - bbox[0], bbox[3] - bbox[1]) vega = vega_fishnetmap(w, h, bounding_box=bounding_box, color_gradient=color_gradient, cell_size=cell_size, cell_spacing=cell_spacing, opacity=opacity, coordinate_system=coordinate_system, aggregation_type=aggregation_type) hexstr = arctern.fishnet_map_layer(vega, points, weights) f = io.BytesIO(base64.b64decode(hexstr)) img = plt.imread(f) ax.set(xlim=(bbox[0], bbox[2]), ylim=(bbox[1], bbox[3])) cx.add_basemap(ax, **extra_contextily_params) ax.imshow(img, alpha=img[:, :, 3], extent=(bbox[0], bbox[2], bbox[1], bbox[3]))
def test_fishnet_map(): x_data = [] y_data = [] c_data = [] x_data.append(-73.96524) x_data.append(-73.96118) x_data.append(-73.97324) x_data.append(-73.98456) y_data.append(40.73747) y_data.append(40.74507) y_data.append(40.75890) y_data.append(40.77654) c_data.append(10) c_data.append(20) c_data.append(30) c_data.append(40) arr_x = pandas.Series(x_data) arr_y = pandas.Series(y_data) arr_c = pandas.Series(c_data) points = arctern.ST_Point(arr_x, arr_y) vega = vega_fishnetmap( 1024, 896, bounding_box=[-73.998427, 40.730309, -73.954348, 40.780816], color_gradient=["#0000FF", "#FF0000"], cell_size=4, cell_spacing=1, opacity=1.0, coordinate_system='EPSG:4326') heat_map1 = arctern.fishnet_map_layer(vega, points, arr_c) save_png(heat_map1, "/tmp/test_fishnetmap.png")
def fishnetmap(ax, points, weights, bounding_box, color_gradient=["#0000FF", "#FF0000"], cell_size=4, cell_spacing=1, opacity=1.0, coordinate_system='epsg:3857', aggregation_type='sum', **extra_contextily_params): """ Plot fishnetmap in Matplotlib :type ax: AxesSubplot :param ax: Matplotlib axes object on which to add the basemap. :type points: GeoSeries :param points: Sequence of Points :type weights: Series(dtype: float|int64) :param weights: Color weight of points :type bounding_box: list :param bounding_box: Specify the bounding rectangle [west, south, east, north]. :type color_gradient: list :param color_gradient: Specify range of color gradient. Either use ["hex_color"] to specify a same color for all points, or ["hex_color1", "hex_color2"] to specify a color gradient ranging from "hex_color1" to "hex_color2" Current only default value ["#0000FF", "#FF0000"] is supported :type cell_size: int :param cell_size: Side length of fishnet cells, default as 4 :type cell_spacing: int :param cell_spacing: Margin between adjacent fishnet cells, default as 1 :type opacity: float :param opacity: Opacity of fishnet, ranged from 0.0 to 1.0, default as 1.0 :type coordinate_system: str :param coordinate_system: Coordinate Reference System of the geometry objects. Must be SRID formed, e.g. 'EPSG:4326' or 'EPSG:3857' Default as 'EPSG:3857' :type aggregation_type: str :param aggregation_type: Aggregation type of data processing. Default as 'sum' :type extra_contextily_params: dict :param extra_contextily_params: Extra parameters will be passed to contextily.add_basemap See https://contextily.readthedocs.io/en/latest/reference.html for details :example: >>> import pandas as pd >>> import numpy as np >>> import arctern >>> import matplotlib.pyplot as plt >>> # read from test_data.csv >>> # Download link: https://raw.githubusercontent.com/arctern-io/arctern-resources/benchmarks/benchmarks/dataset/layer_rendering_test_data/test_data.csv >>> df = pd.read_csv("/path/to/test_data.csv", dtype={'longitude':np.float64, 'latitude':np.float64, 'color_weights':np.float64, 'size_weights':np.float64, 'region_boundaries':np.object}) >>> points = arctern.GeoSeries.point(df['longitude'], df['latitude']) >>> # render fishnet >>> fig, ax = plt.subplots(figsize=(10, 6), dpi=200) >>> arctern.plot.fishnetmap(ax, points=points, weights=df['color_weights'], bounding_box=[-74.01424568752932, 40.72759334104623, -73.96056823889673, 40.76721122683304], cell_size=8, cell_spacing=2, opacity=1.0, coordinate_system="EPSG:4326") >>> plt.show() """ from matplotlib import pyplot as plt import contextily as cx bbox = _transform_bbox(bounding_box, coordinate_system, 'epsg:3857') w, h = _get_recom_size(bbox[2] - bbox[0], bbox[3] - bbox[1]) vega = vega_fishnetmap(w, h, bounding_box=bounding_box, color_gradient=color_gradient, cell_size=cell_size, cell_spacing=cell_spacing, opacity=opacity, coordinate_system=coordinate_system, aggregation_type=aggregation_type) hexstr = arctern.fishnet_map_layer(vega, points, weights) f = io.BytesIO(base64.b64decode(hexstr)) img = plt.imread(f) ax.set(xlim=(bbox[0], bbox[2]), ylim=(bbox[1], bbox[3])) cx.add_basemap(ax, **extra_contextily_params) ax.imshow(img, alpha=img[:, :, 3], extent=(bbox[0], bbox[2], bbox[1], bbox[3])) ax.axis('off')
def fishnetmap(ax, points, weights, bounding_box, color_gradient=["#0000FF", "#FF0000"], cell_size=4, cell_spacing=1, opacity=1.0, coordinate_system='epsg:3857', aggregation_type='sum', **extra_contextily_params): """ Plots a fishnet map in Matplotlib. Parameters ---------- ax : matplotlib.axes.Axes Axes where geometries will be plotted. points : GeoSeries Sequence of points. weights : Series Color weights of polygons. bounding_box : list Bounding box of the map. For example, [west, south, east, north]. color_gradient : list, optional Range of color gradient, by default ["#0000FF", "#FF0000"]. Either use ["hex_color"] to specify a same color for all geometries, or ["hex_color1", "hex_color2"] to specify a color gradient ranging from "hex_color1" to "hex_color2". cell_size : int, optional Side length of fishnet cells, by default 4. cell_spacing : int, optional Margin between adjacent fishnet cells, by default 1. opacity : float, optional Opacity of the fishnet, ranged from 0.0 to 1.0, by default 1.0. coordinate_system : str, optional The Coordinate Reference System (CRS) set to all geometries, by default 'EPSG:3857'. Only supports SRID as a WKT representation of CRS by now. aggregation_type : str, optional Aggregation type, by default 'sum'. **extra_contextily_params: dict Extra parameters passed to `contextily.add_basemap. <https://contextily.readthedocs.io/en/latest/reference.html>`_ Examples ------- >>> import pandas as pd >>> import numpy as np >>> import arctern >>> import matplotlib.pyplot as plt >>> # read from test_data.csv >>> # Download link: https://raw.githubusercontent.com/arctern-io/arctern-resources/benchmarks/benchmarks/dataset/layer_rendering_test_data/test_data.csv >>> df = pd.read_csv("/path/to/test_data.csv", dtype={'longitude':np.float64, 'latitude':np.float64, 'color_weights':np.float64, 'size_weights':np.float64, 'region_boundaries':np.object}) >>> points = arctern.GeoSeries.point(df['longitude'], df['latitude']) >>> # render fishnet >>> fig, ax = plt.subplots(figsize=(10, 6), dpi=200) >>> arctern.plot.fishnetmap(ax, points=points, weights=df['color_weights'], bounding_box=[-74.01424568752932, 40.72759334104623, -73.96056823889673, 40.76721122683304], cell_size=8, cell_spacing=2, opacity=1.0, coordinate_system="EPSG:4326") >>> plt.show() """ from matplotlib import pyplot as plt import contextily as cx bbox = _transform_bbox(bounding_box, coordinate_system, 'epsg:3857') w, h = _get_recom_size(bbox[2] - bbox[0], bbox[3] - bbox[1]) vega = vega_fishnetmap(w, h, bounding_box=bounding_box, color_gradient=color_gradient, cell_size=cell_size, cell_spacing=cell_spacing, opacity=opacity, coordinate_system=coordinate_system, aggregation_type=aggregation_type) hexstr = arctern.fishnet_map_layer(vega, points, weights) f = io.BytesIO(base64.b64decode(hexstr)) img = plt.imread(f) ax.set(xlim=(bbox[0], bbox[2]), ylim=(bbox[1], bbox[3])) cx.add_basemap(ax, **extra_contextily_params) ax.imshow(img, alpha=img[:, :, 3], extent=(bbox[0], bbox[2], bbox[1], bbox[3])) ax.axis('off')