Пример #1
0
def test_vega_icon():
    vega = vega_icon(1900, 1410,
                     [-73.998427, 40.730309, -73.954348, 40.780816],
                     "icon_path", "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.998427
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        1] == 40.730309
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        2] == -73.954348
    assert vega_dict["marks"][0]["encode"]["enter"]["bounding_box"]["value"][
        3] == 40.780816
    assert vega_dict["marks"][0]["encode"]["enter"]["icon_path"][
        "value"] == "icon_path"
    assert vega_dict["marks"][0]["encode"]["enter"]["coordinate_system"][
        "value"] == "EPSG:3857"

    vega = vega_icon(1900, 1410,
                     [-73.998427, 40.730309, -73.954348, 40.780816],
                     "icon_path").build()
    vega_dict = json.loads(vega)
    assert vega_dict["marks"][0]["encode"]["enter"]["coordinate_system"][
        "value"] == "EPSG:3857"
Пример #2
0
def test_icon_viz():
    x_data = []
    y_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)

    arr_x = pandas.Series(x_data)
    arr_y = pandas.Series(y_data)
    points = arctern.ST_Point(arr_x, arr_y)

    import os
    dir_path = os.path.dirname(os.path.realpath(__file__))
    png_path = dir_path + "/../images/taxi.png"

    vega = vega_icon(
        1024,
        896,
        bounding_box=[-73.998427, 40.730309, -73.954348, 40.780816],
        icon_path=png_path,
        coordinate_system="EPSG:4326")

    icon_buf = arctern.icon_viz_layer(vega, points)
    save_png(icon_buf, "/tmp/test_icon_viz.png")
Пример #3
0
def iconviz(ax,
            points,
            bounding_box,
            icon_path,
            coordinate_system='EPSG:3857',
            **extra_contextily_params):
    """
    Plots an icon map in Matplotlib.

    Parameters
    ----------
    ax : matplotlib.axes.Axes
        Axes where geometries will be plotted.
    points : GeoSeries
        Sequence of points.
    bounding_box : list
        Bounding box of the map. For example, [west, south, east, north].
    icon_path : str
        Absolute path to icon file.
    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.
    **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}, nrows=10)
    >>> points = arctern.GeoSeries.point(df['longitude'], df['latitude'])
    >>> # plot icon visualization
    >>> # Download icon-viz.png :  https://raw.githubusercontent.com/arctern-io/arctern-docs/master/img/icon/icon-viz.png
    >>> fig, ax = plt.subplots(figsize=(10, 6), dpi=200)
    >>> arctern.plot.iconviz(ax, points, bounding_box=[-74.01424568752932, 40.72759334104623, -73.96056823889673, 40.76721122683304], icon_path='/path/to/icon-viz.png', 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_icon(w,
                     h,
                     bounding_box=bounding_box,
                     icon_path=icon_path,
                     coordinate_system=coordinate_system)
    hexstr = arctern.icon_viz_layer(vega, points)
    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')
Пример #4
0
def draw_icon_viz(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_Point(pickup_longitude, pickup_latitude) as point from nyc_taxi where ST_Within(ST_Point(pickup_longitude, pickup_latitude), ST_GeomFromText('POLYGON ((-73.998427 40.730309, -73.954348 40.730309, -73.954348 40.780816 ,-73.998427 40.780816, -73.998427 40.730309))'))")

    icon_path = "/tmp/taxi.png"
    vega = vega_icon(1024, 896, [-73.998427, 40.730309, -73.954348, 40.780816], icon_path, "EPSG:4326")
    res = icon_viz(vega, res)
    save_png(res, '/tmp/icon_viz.png')

    spark.sql("show tables").show()
    spark.catalog.dropGlobalTempView("nyc_taxi")
    print("--- %s seconds ---" % (time.time() - start_time))
Пример #5
0
def test_icon_viz():
    x_data = []
    y_data = []

    for i in range(5):
        x_data.append(i * 100)
        y_data.append(i * 100)

    arr_x = pandas.Series(x_data)
    arr_y = pandas.Series(y_data)
    points = arctern.ST_Point(arr_x, arr_y)

    import os
    dir_path = os.path.dirname(os.path.realpath(__file__))
    png_path = dir_path + "/../images/taxi.png"

    vega = vega_icon(800, 600, [-73.998427, 40.730309, -73.954348, 40.780816],
                     png_path, "EPSG:43")

    icon_buf = arctern.icon_viz(vega, points)
    save_png(icon_buf, "/tmp/test_icon_viz.png")
Пример #6
0
def iconviz(ax,
            points,
            bounding_box,
            icon_path,
            coordinate_system='EPSG:3857',
            **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_icon(w,
                     h,
                     bounding_box=bounding_box,
                     icon_path=icon_path,
                     coordinate_system=coordinate_system)
    hexstr = arctern.icon_viz_layer(vega, points)
    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]))
Пример #7
0
def iconviz(ax,
            points,
            bounding_box,
            icon_path,
            coordinate_system='EPSG:3857',
            **extra_contextily_params):
    """
    Plot points as icons on map 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 bounding_box: list
    :param bounding_box: Specify the bounding rectangle [west, south, east, north].

    :type icon_path: str
    :param icon_path: Absolute path to icon file

    :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 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}, nrows=10)
    >>> points = arctern.GeoSeries.point(df['longitude'], df['latitude'])
    >>> # plot icon visualization
    >>> # Download icon-viz.png :  https://raw.githubusercontent.com/arctern-io/arctern-docs/master/img/icon/icon-viz.png
    >>> fig, ax = plt.subplots(figsize=(10, 6), dpi=200)
    >>> arctern.plot.iconviz(ax, points, bounding_box=[-74.01424568752932, 40.72759334104623, -73.96056823889673, 40.76721122683304], icon_path='/path/to/icon-viz.png', 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_icon(w,
                     h,
                     bounding_box=bounding_box,
                     icon_path=icon_path,
                     coordinate_system=coordinate_system)
    hexstr = arctern.icon_viz_layer(vega, points)
    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')
Пример #8
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)
Пример #9
0
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,
    ST_Point(
        pickup_df.head(25).pickup_longitude,
        pickup_df.head(25).pickup_latitude))
save_png(png, "/tmp/arctern_iconviz_pandas.png")

vega = vega_fishnetmap(1024,
                       384,
                       bounding_box=[pos1[0], pos1[1], pos2[0], pos2[1]],
                       cell_size=8,
                       cell_spacing=1,
                       opacity=1.0,