def test_choropleth_map(): wkt_data = [] count_data = [] wkt_data.append("POLYGON ((" "-73.97324 40.73747, " "-73.96524 40.74507, " "-73.96118 40.75890, " "-73.95556 40.77654, " "-73.97324 40.73747))") count_data.append(5.0) arr_wkt = pandas.Series(wkt_data) arr_wkb = arctern.ST_GeomFromText(arr_wkt) arr_count = pandas.Series(count_data) vega = vega_choroplethmap( 1024, 896, bounding_box=[-73.998427, 40.730309, -73.954348, 40.780816], color_gradient=["#0000FF", "#FF0000"], color_bound=[2.5, 5], opacity=1.0, coordinate_system='EPSG:4326') choropleth_map1 = arctern.choropleth_map_layer(vega, arr_wkb, arr_count) save_png(choropleth_map1, "/tmp/test_choropleth_map1.png")
def choroplethmap(ax, region_boundaries, weights, bounding_box, color_gradient, color_bound=None, opacity=1.0, coordinate_system='EPSG:3857', aggregation_type='max', **extra_contextily_params): """ :type ax: AxesSubplot :param ax: Matplotlib axes object on which to add the basemap. :type points: Series(dtype: object) :param points: Points in WKB form :type bounding_box: (float, float, float, float) :param bounding_box: The bounding rectangle, as a [left, upper, right, lower]-tuple. value should be of :coordinate_system: :type coordinate_system: str :param coordinate_system: either 'EPSG:4326' or 'EPSG:3857' :type extra_contextily_params: dict :param extra_contextily_params: extra parameters for contextily.add_basemap. See https://contextily.readthedocs.io/en/latest/reference.html """ 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_choroplethmap(w, h, bounding_box=bounding_box, color_gradient=color_gradient, color_bound=color_bound, opacity=opacity, aggregation_type=aggregation_type, coordinate_system=coordinate_system) hexstr = arctern.choropleth_map_layer(vega, region_boundaries, 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 choroplethmap(ax, region_boundaries, weights, bounding_box, color_gradient, color_bound=None, opacity=1.0, coordinate_system='EPSG:3857', aggregation_type='max', **extra_contextily_params): """ Render region boundaries in matplotlib :type ax: AxesSubplot :param ax: Matplotlib axes object on which to add the basemap. :type region_boundaries: GeoSeries :param region_boundaries: Sequence of polygons, as region boundaries to plot. :type weights: Series(dtype: float|int64) :param weights: Color weights for polygons :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 polygons, or ["hex_color1", "hex_color2"] to specify a color gradient ranging from "hex_color1" to "hex_color2" :type color_bound: list :param color_bound: Specify weight range [w1, w2] binding to color_gradient. Needed only when color_gradient has two value ["color1", "color2"]. Bind w1 to "color1", and w2 to "color2". When weight < w1 or weight > w2, truncate to w1/w2 accordingly. Default as None :type opacity: float :param opacity: Opacity of polygons, 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 'max' :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}) >>> input = df[pd.notna(df['region_boundaries'])].groupby(['region_boundaries']).mean().reset_index() >>> polygon = arctern.GeoSeries(input['region_boundaries']) >>> # plot choroplethmap >>> fig, ax = plt.subplots(figsize=(10, 6), dpi=200) >>> arctern.plot.choroplethmap(ax, polygon, input['color_weights'], bounding_box=[-74.01124953254566,40.73413446570038,-73.96238859103838,40.766161712662296], color_gradient=["#115f9a","#d0f400"], color_bound=[5,18], opacity=1.0, coordinate_system='EPSG:4326', aggregation_type="mean") >>> 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_choroplethmap(w, h, bounding_box=bounding_box, color_gradient=color_gradient, color_bound=color_bound, opacity=opacity, aggregation_type=aggregation_type, coordinate_system=coordinate_system) hexstr = arctern.choropleth_map_layer(vega, region_boundaries, 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 choroplethmap(ax, region_boundaries, weights, bounding_box, color_gradient, color_bound=None, opacity=1.0, coordinate_system='EPSG:3857', aggregation_type='max', **extra_contextily_params): """ Plots a choropleth map in matplotlib. Parameters ---------- ax : matplotlib.axes.Axes Axes where geometries will be plotted. region_boundaries : GeoSeries Sequence of polygons, as region boundaries to plot. weights : Series Color weights for polygons bounding_box : list Bounding box of the map. For example, [west, south, east, north]. color_gradient : list Range of color gradient. 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". color_bound : list, optional Weight range [w1, w2] of ``color_gradient``. Needed only when ``color_gradient`` has two values ["color1", "color2"]. Binds w1 to "color1", and w2 to "color2". When weight < w1 or weight > w2, the weight will be truncated to w1 or w2 accordingly. opacity : float, optional Opacity of polygons, 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 'max'. **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}) >>> input = df[pd.notna(df['region_boundaries'])].groupby(['region_boundaries']).mean().reset_index() >>> polygon = arctern.GeoSeries(input['region_boundaries']) >>> # plot choroplethmap >>> fig, ax = plt.subplots(figsize=(10, 6), dpi=200) >>> arctern.plot.choroplethmap(ax, polygon, input['color_weights'], bounding_box=[-74.01124953254566,40.73413446570038,-73.96238859103838,40.766161712662296], color_gradient=["#115f9a","#d0f400"], color_bound=[5,18], opacity=1.0, coordinate_system='EPSG:4326', aggregation_type="mean") >>> 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_choroplethmap(w, h, bounding_box=bounding_box, color_gradient=color_gradient, color_bound=color_bound, opacity=opacity, aggregation_type=aggregation_type, coordinate_system=coordinate_system) hexstr = arctern.choropleth_map_layer(vega, region_boundaries, 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 choroplethmap_wkb(wkb, w, conf=vega): from arctern import choropleth_map_layer return choropleth_map_layer(conf, wkb, w, False)