Ejemplo n.º 1
0
def test_vega_choropleth_map():
    vega = vega_choroplethmap(1900, 1410,
                              [-73.994092, 40.753893, -73.977588, 40.759642],
                              "blue_to_red", [2.5, 5], 1.0,
                              "EPSG:3857").build()
    vega_dict = json.loads(vega)
    assert vega_dict["width"] == 1900
    assert vega_dict["height"] == 1410
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        0] == -73.994092
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        1] == 40.753893
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        2] == -73.977588
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        3] == 40.759642
    assert vega_dict["marks"][0]["encode"]["enter"]["color_style"][
        "value"] == "blue_to_red"
    assert len(vega_dict["marks"][0]["encode"]["enter"]["ruler"]["value"]) == 2
    assert vega_dict["marks"][0]["encode"]["enter"]["ruler"]["value"][0] == 2.5
    assert vega_dict["marks"][0]["encode"]["enter"]["ruler"]["value"][1] == 5
    assert vega_dict["marks"][0]["encode"]["enter"]["opacity"]["value"] == 1.0
    assert vega_dict["marks"][0]["encode"]["enter"]["coordinate_system"][
        "value"] == "EPSG:3857"

    vega = vega_choroplethmap(1900, 1410,
                              [-73.994092, 40.753893, -73.977588, 40.759642],
                              "blue_to_red", [2.5, 5], 1.0).build()
    vega_dict = json.loads(vega)
    assert vega_dict["marks"][0]["encode"]["enter"]["coordinate_system"][
        "value"] == "EPSG:4326"
Ejemplo n.º 2
0
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")
Ejemplo n.º 3
0
def draw_choropleth_map(spark):
    start_time = time.time()
    df = spark.read.format("csv").option("header", True).option(
        "delimiter", ","
    ).schema(
        "VendorID string, tpep_pickup_datetime timestamp, tpep_dropoff_datetime timestamp, passenger_count long, trip_distance double, pickup_longitude double, pickup_latitude double, dropoff_longitude double, dropoff_latitude double, fare_amount double, tip_amount double, total_amount double, buildingid_pickup long, buildingid_dropoff long, buildingtext_pickup string, buildingtext_dropoff string"
    ).load("file:///tmp/0_5M_nyc_taxi_and_building.csv").cache()
    df.createOrReplaceTempView("nyc_taxi")

    register_funcs(spark)
    res = spark.sql(
        "select ST_GeomFromText(buildingtext_dropoff) as polygon, passenger_count as w from nyc_taxi where (buildingtext_dropoff!='')"
    )

    vega1 = vega_choroplethmap(
        1900,
        1410,
        bounding_box=[-73.994092, 40.753893, -73.977588, 40.759642],
        color_gradient=["#0000FF", "#FF0000"],
        color_bound=[2.5, 5],
        opacity=1.0,
        coordinate_system='EPSG:4326')
    res1 = choroplethmap(vega1, res)
    save_png(res1, '/tmp/choroplethmap1.png')

    spark.sql("show tables").show()
    spark.catalog.dropGlobalTempView("nyc_taxi")
    print("--- %s seconds ---" % (time.time() - start_time))
Ejemplo n.º 4
0
def test_choropleth_map():
    wkt_data = []
    count_data = []

    wkt_data.append("POLYGON (("
                    "200 200, "
                    "200 300, "
                    "300 300, "
                    "300 200, "
                    "200 200))")
    count_data.append(5.0)

    arr_wkt = pandas.Series(wkt_data)
    arr_wkb = arctern.wkt2wkb(arr_wkt)
    arr_count = pandas.Series(count_data)

    vega = vega_choroplethmap(
        1900,
        1410,
        bounding_box=[-73.994092, 40.753893, -73.977588, 40.759642],
        color_gradient=["#0000FF", "#FF0000"],
        color_bound=[2.5, 5],
        opacity=1.0,
        coordinate_system='EPSG:4326')
    choropleth_map1 = arctern.choropleth_map(vega, arr_wkb, arr_count)
    save_png(choropleth_map1, "/tmp/test_choropleth_map1.png")
Ejemplo n.º 5
0
def test_vega_choropleth_map():
    vega = vega_choroplethmap(1900, 1410,
                              [-73.994092, 40.753893, -73.977588, 40.759642],
                              ["#0000FF", "#FF0000"], [2.5, 5], 1.0,
                              "EPSG:3857", "max").build()
    vega_dict = json.loads(vega)
    assert vega_dict["width"] == 1900
    assert vega_dict["height"] == 1410
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        0] == -73.994092
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        1] == 40.753893
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        2] == -73.977588
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        3] == 40.759642
    assert vega_dict["marks"][0]["encode"]["enter"]["color_gradient"]["value"][
        0] == "#0000FF"
    assert vega_dict["marks"][0]["encode"]["enter"]["color_gradient"]["value"][
        1] == "#FF0000"
    assert len(
        vega_dict["marks"][0]["encode"]["enter"]["color_bound"]["value"]) == 2
    assert vega_dict["marks"][0]["encode"]["enter"]["color_bound"]["value"][
        0] == 2.5
    assert vega_dict["marks"][0]["encode"]["enter"]["color_bound"]["value"][
        1] == 5
    assert vega_dict["marks"][0]["encode"]["enter"]["opacity"]["value"] == 1.0
    assert vega_dict["marks"][0]["encode"]["enter"]["coordinate_system"][
        "value"] == "EPSG:3857"
    assert vega_dict["marks"][0]["encode"]["enter"]["aggregation_type"][
        "value"] == "max"

    vega = vega_choroplethmap(1900, 1410,
                              [-73.994092, 40.753893, -73.977588, 40.759642],
                              ["#0000FF", "#FF0000"]).build()
    vega_dict = json.loads(vega)
    assert len(
        vega_dict["marks"][0]["encode"]["enter"]["color_bound"]["value"]) == 2
    assert vega_dict["marks"][0]["encode"]["enter"]["color_bound"]["value"][
        0] == 0
    assert vega_dict["marks"][0]["encode"]["enter"]["color_bound"]["value"][
        1] == 0
    assert vega_dict["marks"][0]["encode"]["enter"]["opacity"]["value"] == 1.0
    assert vega_dict["marks"][0]["encode"]["enter"]["coordinate_system"][
        "value"] == "EPSG:3857"
    assert vega_dict["marks"][0]["encode"]["enter"]["aggregation_type"][
        "value"] == "sum"
Ejemplo n.º 6
0
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]))
Ejemplo n.º 7
0
def test_choropleth_map():
    wkt_data = []
    count_data = []

    wkt_data.append("POLYGON (("
      "200 200, "
      "200 300, "
      "300 300, "
      "300 200, "
      "200 200))")
    count_data.append(5.0)

    arr_wkt = pandas.Series(wkt_data)
    arr_wkb = arctern.wkt2wkb(arr_wkt)
    arr_count = pandas.Series(count_data)

    vega = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], ["#0000FF", "#FF0000"], [2.5, 5], 1.0, 'EPSG:4326')
    choropleth_map1 = arctern.choropleth_map(vega, arr_wkb, arr_count)
    save_png(choropleth_map1, "/tmp/test_choropleth_map1.png")
Ejemplo n.º 8
0
def draw_choropleth_map(spark):
    df = spark.read.format("csv").option("header", True).option(
        "delimiter", ","
    ).schema(
        "VendorID string, tpep_pickup_datetime timestamp, tpep_dropoff_datetime timestamp, passenger_count long, trip_distance double, pickup_longitude double, pickup_latitude double, dropoff_longitude double, dropoff_latitude double, fare_amount double, tip_amount double, total_amount double, buildingid_pickup long, buildingid_dropoff long, buildingtext_pickup string, buildingtext_dropoff string"
    ).load("file:///tmp/0_5M_nyc_taxi_and_building.csv").cache()
    df.createOrReplaceTempView("nyc_taxi")

    res = spark.sql(
        "select buildingtext_dropoff as wkt, passenger_count as w from nyc_taxi"
    )

    vega = vega_choroplethmap(1900, 1410,
                              [-73.994092, 40.753893, -73.977588, 40.759642],
                              "blue_to_red", [2.5, 5], 1.0, 'EPSG:4326')
    res = choroplethmap(res, vega)
    save_png(res, '/tmp/choroplethmap.png')

    spark.sql("show tables").show()
    spark.catalog.dropGlobalTempView("nyc_taxi")
Ejemplo n.º 9
0
def test_choropleth_map():
    wkt_data = []
    count_data = []

    wkt_data.append("POLYGON (("
                    "200 200, "
                    "200 300, "
                    "300 300, "
                    "300 200, "
                    "200 200))")
    count_data.append(5.0)

    arr_wkt = pandas.Series(wkt_data)
    arr_count = pandas.Series(count_data)

    vega_choropleth_map = vega_choroplethmap(
        1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642],
        "blue_to_red", [2.5, 5], 1.0, 'EPSG:4326')
    vega_json = vega_choropleth_map.build()

    arr_wkb = arctern.wkt2wkb(arr_wkt)
    choropleth_map1 = arctern.choropleth_map(arr_wkb, arr_count,
                                             vega_json.encode('utf-8'))
    save_png(choropleth_map1, "/tmp/test_choropleth_map1.png")
Ejemplo n.º 10
0
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')
Ejemplo n.º 11
0
def db_query():
    """
    /db/query handler
    """
    log.INSTANCE.info('POST /db/query: {}'.format(request.json))

    if not utils.check_json(request.json, 'id') \
            or not utils.check_json(request.json, 'query') \
            or not utils.check_json(request.json['query'], 'type') \
            or not utils.check_json(request.json['query'], 'sql'):
        return jsonify(status='error', code=-1, message='query format error')

    query_sql = request.json['query']['sql']
    query_type = request.json['query']['type']

    content = {}
    content['sql'] = query_sql
    content['err'] = False

    db_instance = db.CENTER.get(str(request.json['id']), None)
    if db_instance is None:
        return jsonify(status="error",
                       code=-1,
                       message='there is no database whose id equal to ' +
                       str(request.json['id']))

    if query_type == 'sql':
        res = db_instance.run_for_json(query_sql)
        data = []
        for row in res:
            obj = json.loads(row)
            data.append(obj)
        content['result'] = data
    else:
        if not utils.check_json(request.json['query'], 'params'):
            return jsonify(status='error',
                           code=-1,
                           message='query format error')
        query_params = request.json['query']['params']

        res = db_instance.run(query_sql)

        if query_type == 'point':
            vega = vega_pointmap(int(query_params['width']),
                                 int(query_params['height']),
                                 query_params['point']['bounding_box'],
                                 int(query_params['point']['point_size']),
                                 query_params['point']['point_color'],
                                 float(query_params['point']['opacity']),
                                 query_params['point']['coordinate_system'])
            data = pointmap(vega, res)
            content['result'] = data
        elif query_type == 'heat':
            vega = vega_heatmap(int(query_params['width']),
                                int(query_params['height']),
                                query_params['heat']['bounding_box'],
                                float(query_params['heat']['map_zoom_level']),
                                query_params['heat']['coordinate_system'],
                                query_params['heat']['aggregation_type'])
            data = heatmap(vega, res)
            content['result'] = data
        elif query_type == 'choropleth':
            vega = vega_choroplethmap(
                int(query_params['width']), int(query_params['height']),
                query_params['choropleth']['bounding_box'],
                query_params['choropleth']['color_gradient'],
                query_params['choropleth']['color_bound'],
                float(query_params['choropleth']['opacity']),
                query_params['choropleth']['coordinate_system'],
                query_params['choropleth']['aggregation_type'])
            data = choroplethmap(vega, res)
            content['result'] = data
        elif query_type == 'weighted':
            vega = vega_weighted_pointmap(
                int(query_params['width']), int(query_params['height']),
                query_params['weighted']['bounding_box'],
                query_params['weighted']['color_gradient'],
                query_params['weighted']['color_bound'],
                query_params['weighted']['size_bound'],
                float(query_params['weighted']['opacity']),
                query_params['weighted']['coordinate_system'])
            data = weighted_pointmap(vega, res)
            content['result'] = data
        elif query_type == 'icon':
            vega = vega_icon(int(query_params['width']),
                             int(query_params['height']),
                             query_params['icon']['bounding_box'],
                             query_params['icon']['icon_path'],
                             query_params['icon']['coordinate_system'])
            data = icon_viz(vega, res)
            content['result'] = data
        else:
            return jsonify(status="error",
                           code=-1,
                           message='{} not support'.format(query_type))

    return jsonify(status="success", code=200, data=content)
Ejemplo n.º 12
0
save_png(png, "/tmp/arctern_weighted_pointmap_pandas.png")

vega = vega_heatmap(1024,
                    384,
                    bounding_box=[pos1[0], pos1[1], pos2[0], pos2[1]],
                    map_zoom_level=13.0,
                    coordinate_system="EPSG:4326")
png = heat_map_layer(
    vega, ST_Point(pickup_df.pickup_longitude, pickup_df.pickup_latitude),
    df.head(limit_num).fare_amount)
save_png(png, "/tmp/arctern_heatmap_pandas.png")

vega = vega_choroplethmap(1024,
                          384,
                          bounding_box=[pos1[0], pos1[1], pos2[0], pos2[1]],
                          color_gradient=["#115f9a", "#d0f400"],
                          color_bound=[2.5, 5],
                          opacity=1.0,
                          coordinate_system="EPSG:4326")
png = choropleth_map_layer(vega,
                           ST_GeomFromText(pickup_df.buildingtext_pickup),
                           df.head(limit_num).fare_amount)
save_png(png, "/tmp/arctern_choroplethmap_pandas.png")

vega = vega_icon(1024,
                 384,
                 bounding_box=[pos1[0], pos1[1], pos2[0], pos2[1]],
                 icon_path='/path/to/arctern-color.png',
                 coordinate_system="EPSG:4326")
png = icon_viz_layer(
    vega,
Ejemplo n.º 13
0
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')
Ejemplo n.º 14
0
def db_query():
    """
    /db/query handler
    """
    if not utils.check_json(request.json, 'id') \
            or not utils.check_json(request.json, 'query') \
            or not utils.check_json(request.json['query'], 'type') \
            or not utils.check_json(request.json['query'], 'sql'):
        return jsonify(status='error', code=-1, message='query format error')

    query_sql = request.json['query']['sql']
    query_type = request.json['query']['type']

    content = {}
    content['sql'] = query_sql
    content['err'] = False

    if query_type == 'sql':
        res = spark.Spark.run_for_json(query_sql)
        data = []
        for row in res:
            obj = json.loads(row)
            data.append(obj)
        content['result'] = data
    else:
        if not utils.check_json(request.json['query'], 'params'):
            return jsonify(status='error',
                           code=-1,
                           message='query format error')
        query_params = request.json['query']['params']

        res = spark.Spark.run(query_sql)

        if query_type == 'point':
            vega = vega_pointmap(int(query_params['width']),
                                 int(query_params['height']),
                                 query_params['point']['bounding_box'],
                                 int(query_params['point']['stroke_width']),
                                 query_params['point']['stroke'],
                                 float(query_params['point']['opacity']),
                                 query_params['point']['coordinate'])
            data = pointmap(res, vega)
            content['result'] = data
        elif query_type == 'heat':
            vega = vega_heatmap(int(query_params['width']),
                                int(query_params['height']),
                                float(query_params['heat']['map_scale']),
                                query_params['heat']['bounding_box'],
                                query_params['heat']['coordinate'])
            data = heatmap(res, vega)
            content['result'] = data
        elif query_type == 'choropleth':
            vega = vega_choroplethmap(
                int(query_params['width']), int(query_params['height']),
                query_params['choropleth']['bounding_box'],
                query_params['choropleth']['color_style'],
                query_params['choropleth']['rule'],
                float(query_params['choropleth']['opacity']),
                query_params['choropleth']['coordinate'])
            data = choroplethmap(res, vega)
            content['result'] = data
        else:
            return jsonify(status="error",
                           code=-1,
                           message='{} not support'.format(query_type))
    return jsonify(status="success", code=200, data=content)
Ejemplo n.º 15
0
def run_test_choropleth_map(spark):
    df = spark.read.format("csv").option("header", True).option("delimiter", ",").schema(
        "VendorID string, tpep_pickup_datetime timestamp, tpep_dropoff_datetime timestamp, passenger_count long, "
        "trip_distance double, pickup_longitude double, pickup_latitude double, dropoff_longitude double, "
        "dropoff_latitude double, fare_amount double, tip_amount double, total_amount double, buildingid_pickup long, "
        "buildingid_dropoff long, buildingtext_pickup string, buildingtext_dropoff string").load(
        file_path).cache()
    df.createOrReplaceTempView("nyc_taxi")

    res = spark.sql("select buildingtext_dropoff as wkt, passenger_count as w from nyc_taxi")

    # 1-9 test color_style
    # 1 blue_to_red
    vega_1 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "blue_to_red",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline1 = choroplethmap(res, vega_1)
    choropleth_map1_1 = choroplethmap(res, vega_1)
    choropleth_map1_2 = choroplethmap(res, vega_1)

    baseline_png1 = png_path + "choropleth_map_nyc_1.png"
    save_png(baseline1, baseline_png1)
    save_png(choropleth_map1_1, png_path + "test_choropleth_map_nyc_1-1.png")
    save_png(choropleth_map1_2, png_path + "test_choropleth_map_nyc_1-2.png")

    # 2 green_yellow_red
    vega_2 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "green_yellow_red",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline2 = choroplethmap(res, vega_2)
    choropleth_map2_1 = choroplethmap(res, vega_2)
    choropleth_map2_2 = choroplethmap(res, vega_2)

    baseline_png2 = png_path + "choropleth_map_nyc_2.png"
    save_png(baseline2, baseline_png2)
    save_png(choropleth_map2_1, png_path + "test_choropleth_map_nyc_2-1.png")
    save_png(choropleth_map2_2, png_path + "test_choropleth_map_nyc_2-2.png")

    # 3 blue_white_red
    vega_3 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "blue_white_red",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline3 = choroplethmap(res, vega_3)
    choropleth_map3_1 = choroplethmap(res, vega_3)
    choropleth_map3_2 = choroplethmap(res, vega_3)

    baseline_png3 = png_path + "choropleth_map_nyc_3.png"
    save_png(baseline3, baseline_png3)
    save_png(choropleth_map3_1, png_path + "test_choropleth_map_nyc_3-1.png")
    save_png(choropleth_map3_2, png_path + "test_choropleth_map_nyc_3-2.png")

    # 4 skyblue_to_white
    vega_4 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "skyblue_to_white",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline4 = choroplethmap(res, vega_4)
    choropleth_map4_1 = choroplethmap(res, vega_4)
    choropleth_map4_2 = choroplethmap(res, vega_4)

    baseline_png4 = png_path + "choropleth_map_nyc_4.png"
    save_png(baseline4, baseline_png4)
    save_png(choropleth_map4_1, png_path + "test_choropleth_map_nyc_4-1.png")
    save_png(choropleth_map4_2, png_path + "test_choropleth_map_nyc_4-2.png")

    # 5 purple_to_yellow
    vega_5 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline5 = choroplethmap(res, vega_5)
    choropleth_map5_1 = choroplethmap(res, vega_5)
    choropleth_map5_2 = choroplethmap(res, vega_5)

    baseline_png5 = png_path + "choropleth_map_nyc_5.png"
    save_png(baseline5, baseline_png5)
    save_png(choropleth_map5_1, png_path + "test_choropleth_map_nyc_5-1.png")
    save_png(choropleth_map5_2, png_path + "test_choropleth_map_nyc_5-2.png")

    # 6 red_transparency
    vega_6 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "red_transparency",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline6 = choroplethmap(res, vega_6)
    choropleth_map6_1 = choroplethmap(res, vega_6)
    choropleth_map6_2 = choroplethmap(res, vega_6)

    baseline_png6 = png_path + "choropleth_map_nyc_6.png"
    save_png(baseline6, baseline_png6)
    save_png(choropleth_map6_1, png_path + "test_choropleth_map_nyc_6-1.png")
    save_png(choropleth_map6_2, png_path + "test_choropleth_map_nyc_6-2.png")

    # 7 blue_transparency
    vega_7 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "blue_transparency",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline7 = choroplethmap(res, vega_7)
    choropleth_map7_1 = choroplethmap(res, vega_7)
    choropleth_map7_2 = choroplethmap(res, vega_7)

    baseline_png7 = png_path + "choropleth_map_nyc_7.png"
    save_png(baseline7, baseline_png7)
    save_png(choropleth_map7_1, png_path + "test_choropleth_map_nyc_7-1.png")
    save_png(choropleth_map7_2, png_path + "test_choropleth_map_nyc_7-2.png")

    # 8 blue_green_yellow
    vega_8 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "blue_green_yellow",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline8 = choroplethmap(res, vega_8)
    choropleth_map8_1 = choroplethmap(res, vega_8)
    choropleth_map8_2 = choroplethmap(res, vega_8)

    baseline_png8 = png_path + "choropleth_map_nyc_8.png"
    save_png(baseline8, baseline_png8)
    save_png(choropleth_map8_1, png_path + "test_choropleth_map_nyc_8-1.png")
    save_png(choropleth_map8_2, png_path + "test_choropleth_map_nyc_8-2.png")

    # 9 white_blue
    vega_9 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "white_blue",
                                [2.5, 5], 1.0, 'EPSG:4326')
    baseline9 = choroplethmap(res, vega_9)
    choropleth_map9_1 = choroplethmap(res, vega_9)
    choropleth_map9_2 = choroplethmap(res, vega_9)

    baseline_png9 = png_path + "choropleth_map_nyc_9.png"
    save_png(baseline9, baseline_png9)
    save_png(choropleth_map9_1, png_path + "test_choropleth_map_nyc_9-1.png")
    save_png(choropleth_map9_2, png_path + "test_choropleth_map_nyc_9-2.png")

    # 10-12 test ruler
    # 10 ruler: [1, 500]
    vega_10 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "blue_to_red",
                                 [1, 500], 1.0, 'EPSG:4326')
    baseline10 = choroplethmap(res, vega_10)
    choropleth_map10_1 = choroplethmap(res, vega_10)
    choropleth_map10_2 = choroplethmap(res, vega_10)

    baseline_png10 = png_path + "choropleth_map_nyc_10.png"
    save_png(baseline10, baseline_png10)
    save_png(choropleth_map10_1, png_path + "test_choropleth_map_nyc_10-1.png")
    save_png(choropleth_map10_2, png_path + "test_choropleth_map_nyc_10-2.png")

    # 11 ruler: [1, 10000]
    vega_11 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "blue_to_red",
                                 [1, 10000], 1.0, 'EPSG:4326')
    baseline11 = choroplethmap(res, vega_11)
    choropleth_map11_1 = choroplethmap(res, vega_11)
    choropleth_map11_2 = choroplethmap(res, vega_11)

    baseline_png11 = png_path + "choropleth_map_nyc_11.png"
    save_png(baseline11, baseline_png11)
    save_png(choropleth_map11_1, png_path + "test_choropleth_map_nyc_11-1.png")
    save_png(choropleth_map11_2, png_path + "test_choropleth_map_nyc_11-2.png")

    # 12 ruler: [0, 2.5]
    vega_12 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "blue_to_red",
                                 [0, 2.5], 1.0, 'EPSG:4326')
    baseline12 = choroplethmap(res, vega_12)
    choropleth_map12_1 = choroplethmap(res, vega_12)
    choropleth_map12_2 = choroplethmap(res, vega_12)

    baseline_png12 = png_path + "choropleth_map_nyc_12.png"
    save_png(baseline12, baseline_png12)
    save_png(choropleth_map12_1, png_path + "test_choropleth_map_nyc_12-1.png")
    save_png(choropleth_map12_2, png_path + "test_choropleth_map_nyc_12-2.png")

    # 13-15 test opacity
    # 13 opacity: 0.0
    vega_13 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                 [2.5, 5], 0.0, 'EPSG:4326')
    baseline13 = choroplethmap(res, vega_13)
    choropleth_map13_1 = choroplethmap(res, vega_13)
    choropleth_map13_2 = choroplethmap(res, vega_13)

    baseline_png13 = png_path + "choropleth_map_nyc_13.png"
    save_png(baseline13, baseline_png13)
    save_png(choropleth_map13_1, png_path + "test_choropleth_map_nyc_13-1.png")
    save_png(choropleth_map13_2, png_path + "test_choropleth_map_nyc_13-2.png")

    # 14 opacity: 1.0
    vega_14 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                 [2.5, 5], 1.0, 'EPSG:4326')
    baseline14 = choroplethmap(res, vega_14)
    choropleth_map14_1 = choroplethmap(res, vega_14)
    choropleth_map14_2 = choroplethmap(res, vega_14)

    baseline_png14 = png_path + "choropleth_map_nyc_14.png"
    save_png(baseline14, baseline_png14)
    save_png(choropleth_map14_1, png_path + "test_choropleth_map_nyc_14-1.png")
    save_png(choropleth_map14_2, png_path + "test_choropleth_map_nyc_14-2.png")

    # 15 opacity: 0.5
    vega_15 = vega_choroplethmap(1900, 1410, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                 [2.5, 5], 0.5, 'EPSG:4326')
    baseline15 = choroplethmap(res, vega_15)
    choropleth_map15_1 = choroplethmap(res, vega_15)
    choropleth_map15_2 = choroplethmap(res, vega_15)

    baseline_png15 = png_path + "choropleth_map_nyc_15.png"
    save_png(baseline15, baseline_png15)
    save_png(choropleth_map15_1, png_path + "test_choropleth_map_nyc_15-1.png")
    save_png(choropleth_map15_2, png_path + "test_choropleth_map_nyc_15-2.png")

    # 16-18 test size
    # 16 width: 256, height: 256
    vega_16 = vega_choroplethmap(256, 256, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                 [2.5, 5], 1.0, 'EPSG:4326')
    baseline16 = choroplethmap(res, vega_16)
    choropleth_map16_1 = choroplethmap(res, vega_16)
    choropleth_map16_2 = choroplethmap(res, vega_16)

    baseline_png16 = png_path + "choropleth_map_nyc_16.png"
    save_png(baseline16, baseline_png16)
    save_png(choropleth_map16_1, png_path + "test_choropleth_map_nyc_16-1.png")
    save_png(choropleth_map16_2, png_path + "test_choropleth_map_nyc_16-2.png")

    # 17 width: 200, height: 200
    vega_17 = vega_choroplethmap(200, 200, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                 [2.5, 5], 1.0, 'EPSG:4326')
    baseline17 = choroplethmap(res, vega_17)
    choropleth_map17_1 = choroplethmap(res, vega_17)
    choropleth_map17_2 = choroplethmap(res, vega_17)

    baseline_png17 = png_path + "choropleth_map_nyc_17.png"
    save_png(baseline17, baseline_png17)
    save_png(choropleth_map17_1, png_path + "test_choropleth_map_nyc_17-1.png")
    save_png(choropleth_map17_2, png_path + "test_choropleth_map_nyc_17-2.png")

    # 18 width: 500, height: 200
    vega_18 = vega_choroplethmap(500, 200, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                 [2.5, 5], 1.0, 'EPSG:4326')
    baseline18 = choroplethmap(res, vega_18)
    choropleth_map18_1 = choroplethmap(res, vega_18)
    choropleth_map18_2 = choroplethmap(res, vega_18)

    baseline_png18 = png_path + "choropleth_map_nyc_18.png"
    save_png(baseline18, baseline_png18)
    save_png(choropleth_map18_1, png_path + "test_choropleth_map_nyc_18-1.png")
    save_png(choropleth_map18_2, png_path + "test_choropleth_map_nyc_18-2.png")

    # 19 width: 10, height: 10
    vega_19 = vega_choroplethmap(10, 10, [-73.994092, 40.753893, -73.977588, 40.759642], "purple_to_yellow",
                                 [2.5, 5], 1.0, 'EPSG:4326')
    baseline19 = choroplethmap(res, vega_19)
    choropleth_map19_1 = choroplethmap(res, vega_19)
    choropleth_map19_2 = choroplethmap(res, vega_19)

    baseline_png19 = png_path + "choropleth_map_nyc_19.png"
    save_png(baseline19, baseline_png19)
    save_png(choropleth_map19_1, png_path + "test_choropleth_map_nyc_19-1.png")
    save_png(choropleth_map19_2, png_path + "test_choropleth_map_nyc_19-2.png")

    spark.catalog.dropGlobalTempView("nyc_taxi")

    assert run_diff_png(baseline_png1, png_path + "test_choropleth_map_nyc_1-1.png")
    assert run_diff_png(baseline_png1, png_path + "test_choropleth_map_nyc_1-2.png")
    assert run_diff_png(baseline_png2, png_path + "test_choropleth_map_nyc_2-1.png")
    assert run_diff_png(baseline_png2, png_path + "test_choropleth_map_nyc_2-2.png")
    assert run_diff_png(baseline_png3, png_path + "test_choropleth_map_nyc_3-1.png")
    assert run_diff_png(baseline_png3, png_path + "test_choropleth_map_nyc_3-2.png")
    assert run_diff_png(baseline_png4, png_path + "test_choropleth_map_nyc_4-1.png")
    assert run_diff_png(baseline_png4, png_path + "test_choropleth_map_nyc_4-2.png")
    assert run_diff_png(baseline_png5, png_path + "test_choropleth_map_nyc_5-1.png")
    assert run_diff_png(baseline_png5, png_path + "test_choropleth_map_nyc_5-2.png")
    assert run_diff_png(baseline_png6, png_path + "test_choropleth_map_nyc_6-1.png")
    assert run_diff_png(baseline_png6, png_path + "test_choropleth_map_nyc_6-2.png")
    assert run_diff_png(baseline_png7, png_path + "test_choropleth_map_nyc_7-1.png")
    assert run_diff_png(baseline_png7, png_path + "test_choropleth_map_nyc_7-2.png")
    assert run_diff_png(baseline_png8, png_path + "test_choropleth_map_nyc_8-1.png")
    assert run_diff_png(baseline_png8, png_path + "test_choropleth_map_nyc_8-2.png")
    assert run_diff_png(baseline_png9, png_path + "test_choropleth_map_nyc_9-1.png")
    assert run_diff_png(baseline_png9, png_path + "test_choropleth_map_nyc_9-2.png")
    assert run_diff_png(baseline_png10, png_path + "test_choropleth_map_nyc_10-1.png")
    assert run_diff_png(baseline_png10, png_path + "test_choropleth_map_nyc_10-2.png")
    assert run_diff_png(baseline_png11, png_path + "test_choropleth_map_nyc_11-1.png")
    assert run_diff_png(baseline_png11, png_path + "test_choropleth_map_nyc_11-2.png")
    assert run_diff_png(baseline_png12, png_path + "test_choropleth_map_nyc_12-1.png")
    assert run_diff_png(baseline_png12, png_path + "test_choropleth_map_nyc_12-2.png")
    assert run_diff_png(baseline_png13, png_path + "test_choropleth_map_nyc_13-1.png")
    assert run_diff_png(baseline_png13, png_path + "test_choropleth_map_nyc_13-2.png")
    assert run_diff_png(baseline_png14, png_path + "test_choropleth_map_nyc_14-1.png")
    assert run_diff_png(baseline_png14, png_path + "test_choropleth_map_nyc_14-2.png")
    assert run_diff_png(baseline_png15, png_path + "test_choropleth_map_nyc_15-1.png")
    assert run_diff_png(baseline_png15, png_path + "test_choropleth_map_nyc_15-2.png")
    assert run_diff_png(baseline_png16, png_path + "test_choropleth_map_nyc_16-1.png")
    assert run_diff_png(baseline_png16, png_path + "test_choropleth_map_nyc_16-2.png")
    assert run_diff_png(baseline_png17, png_path + "test_choropleth_map_nyc_17-1.png")
    assert run_diff_png(baseline_png17, png_path + "test_choropleth_map_nyc_17-2.png")
    assert run_diff_png(baseline_png18, png_path + "test_choropleth_map_nyc_18-1.png")
    assert run_diff_png(baseline_png18, png_path + "test_choropleth_map_nyc_18-2.png")
    assert run_diff_png(baseline_png19, png_path + "test_choropleth_map_nyc_19-1.png")
    assert run_diff_png(baseline_png19, png_path + "test_choropleth_map_nyc_19-2.png")