03_数据查询-iObjects Python with JupyterHub for K8s

数据查询-iObjects Python with JupyterHub for K8s

这里演示如何在JupyterHub中使用iObjects python进行空间数据的查询操作,从而提取出自己关心的信息。

查询结果写入到另外的数据集中,然后可以使用其他的工具(如iDesktop/iClient等)打开。

from iobjectspy import (Point2D,
                        QueryParameter,
                        open_datasource,
                        create_datasource,
                        SpatialQueryMode)
import os
# 设置示例数据路径
#example_data_dir = ''
example_data_dir = '/home/jovyan/data/smdata/'

# 设置结果输出路径
out_dir = os.path.join(example_data_dir, 'out')

if not os.path.exists(out_dir):
    os.makedirs(out_dir)
def _write_result_recordset(recordset):
    """将查询的结果记录集写入数据源中"""

    record_out_path = os.path.join(out_dir, 'out_query_data.udb')
    if not os.path.exists(record_out_path):
        ds = create_datasource(record_out_path)
    else:
        ds = open_datasource(record_out_path)
    assert ds is not None, '打开结果数据源失败'

    result_dt = ds.write_recordset(recordset, out_dataset_name=recordset.dataset.name + '_query')

    if result_dt is not None:
        print('写入查询结果记录集到数据集 ' + ds.connection_info.server + '|' + result_dt.name + ' 成功')
    else:
        print('写入查询结果记录集失败')
    ds.close()


def attribute_filter_query_test():
    """对数据集 landr 进行属性查询,并将查询结果输出到结果数据源中"""

    ds = open_datasource(os.path.join(example_data_dir, 'example_data.udb'))
    assert ds is not None, '打开数据源失败'

    dataset = ds['landr']
    assert dataset is not None, '获取数据集失败'

    recordset = dataset.query_with_filter('R_AREA > 500 and R_AREA < 5000', cursor_type='STATIC')
    assert recordset is not None, '查询记录集失败'

    print('属性查询结果记录数为: ' + str(recordset.get_record_count()))

    _write_result_recordset(recordset)

    ds.close()


def bounds_query_test():
    """对数据集 landr 进行地理范围查询,并将查询结果输出到结果数据源中"""

    ds = open_datasource(os.path.join(example_data_dir, 'example_data.udb'))
    assert ds is not None, '打开数据源失败'

    dataset = ds['landr']
    assert dataset is not None, '获取数据集失败'

    rc = ds['adminR'].get_geometries('SmID == 2')[0].bounds
    recordset = dataset.query_with_bounds(rc, cursor_type='STATIC')
    assert recordset is not None, '查询记录集失败'

    print('范围查询结果记录数为: ' + str(recordset.get_record_count()))

    _write_result_recordset(recordset)

    ds.close()


def distance_query_test():
    """对数据集 landp 进行距离查询, 查询距离为100米, 并将查询结果输出到结果数据源中"""

    ds = open_datasource(os.path.join(example_data_dir, 'example_data.udb'))
    assert ds is not None, '打开数据源失败'

    dataset = ds['landp']
    assert dataset is not None, '获取数据集失败'

    recordset = dataset.query_with_distance(Point2D(315.782892179537, 260.119529494306), 100, unit='Meter',
                                            cursor_type='STATIC')
    assert recordset is not None, '查询记录集失败'

    print('距离查询结果记录数为: ' + str(recordset.get_record_count()))

    _write_result_recordset(recordset)

    ds.close()


def spatial_query_test():
    """对数据集 landp 进行空间查询查询, 并将查询结果输出到结果数据源中"""

    ds = open_datasource(os.path.join(example_data_dir, 'example_data.udb'))
    assert ds is not None, '打开数据源失败'

    dataset = ds['landp']
    assert dataset is not None, '获取数据集失败'

    query_geo = ds['adminR'].get_geometries('SmID == 2')[0]
    query_parameter = (QueryParameter().set_spatial_query_mode(SpatialQueryMode.CONTAIN).
                       set_spatial_query_object(query_geo).set_cursor_type('static'))

    recordset = dataset.query(query_parameter)

    assert recordset is not None, '查询记录集失败'

    print('空间查询结果记录数为: ' + str(recordset.get_record_count()))

    _write_result_recordset(recordset)

    ds.close()
if __name__ == '__main__':
    # 属性查询
    attribute_filter_query_test()

    # 范围查询
    bounds_query_test()

    # 距离查询
    distance_query_test()

    # 空间查询
    spatial_query_test()
java -cp /opt/conda/lib/python3.6/site-packages/iobjectspy/_jsuperpy/jars/com.supermap.jsuperpy-9.1.1.jar com.supermap.jsuperpy.ApplicationExample 127.0.0.1 49289
[iObjectsPy]: Connection gateway-service successful, Python callback port bind 39229

属性查询结果记录数为: 319
写入查询结果记录集到数据集 /home/jovyan/data/smdata/out/out_query_data.udb|landr_query 成功
范围查询结果记录数为: 96
写入查询结果记录集到数据集 /home/jovyan/data/smdata/out/out_query_data.udb|landr_query_1 成功
距离查询结果记录数为: 458
写入查询结果记录集到数据集 /home/jovyan/data/smdata/out/out_query_data.udb|landp_query 成功
空间查询结果记录数为: 49
写入查询结果记录集到数据集 /home/jovyan/data/smdata/out/out_query_data.udb|landp_query_1 成功

转载于:https://my.oschina.net/u/2306127/blog/3084776

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值