IBM Cloud Docs
地理空间索引函数

地理空间索引函数

利用时空库,您可以使用函数对区域内的点、包含点的区域以及半径内的点建立索引,以便在位置分析期间快速查询该数据。

请执行下列步骤,以创建并使用空间索引。 此示例显示如何利用空间索引来查找美国的县,以用于某些位置的给定查询。

  1. 获取县边界。 为此,请使用 geojson_reader 将包含美国县边界的 GeoJSON 文件读取到 pandas DataFrame 中,并显示前三行内容:

    county_df = stc.geojson_reader().read('http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_20m.json')
    county_df.head(3)
    
  2. 创建县空间索引。 pyst 提供了多种空间索引算法,包括 grid_indexr_star_tree_indextessellation_index。 此示例显示如何创建曲面细分索引。

    要创建曲面细分空间索引,需要设置两个参数:

  • 边界框: 定义空间索引的边界。 如果您确切知道几何图形所在的位置并且能够定义包含所有这些几何图形的边界,那么应该将此边界框信息传递给函数,因为这将减少需要创建的 磁贴 数量并提高性能。 但是,如果您不知道几何图形,或者您希望安全地执行操作以不排除任何可能落在给定边界框之外的几何图形 (这两种情况都是非常常见的情况) ,那么只需将 bbox 参数设置保留在 None(这是缺省值) 即可使用整个地球作为边界框。

  • 磁贴大小: 定义 tessellation 索引中的磁贴大小。 该值通过瓦片长度(以米为单位)指定。 您应提供接近于几何图形大小的瓦片大小,以提高性能。 例如,如果几何图形尺寸为 100 km2(即 10^8 m2),那么 10^4 m 可能是理想的瓦片大小。

    有了边界框和瓦片大小值,现在可以创建空间索引,以及将几何图形导入到空间索引中。 使用 from_df 函数将几何图形从 pandas DataFrame 移动到空间索引。 为此,您只需指定几何图形标识列名和几何图形列名。 将控制日志处理的第三个参数 verbosity设置为 error ,以便仅显示摘要和故障日志。

    >>> tile_size = 100000
    >>> si = stc.tessellation_index(tile_size=tile_size) # we leave bbox as None to use full earth as boundingbox
    >>> si.from_df(county_df, 'NAME', 'geometry', verbosity='error')
    3221 entries processed, 3221 entries successfully added
    
  1. 执行空间索引查询。 对于查询空间索引, pyst 提供了以下不同的查询 API: contained_incontained_in_with_infocontainingcontaining_with_infointersectsintersects_with_infowithin_distancewithin_distance_with_infonearest_neighborsnearest_neighbors_with_info

    样本查询:

    • White Plains Hospital 位于哪个县? 哪个县的多边形包含 White Plains Hospital 的点位置?
      >>> white_plains_hospital = stc.point(41.026132, -73.769585)
      >>> si.containing(white_plains_hospital)
      ['Westchester']
      
    • 哪个县是 White Plains 所在的城市? 哪个县的多边形包含 White Plains 的多边形?
      >>> white_plains_WKT = 'POLYGON((-73.792 41.024,-73.794 41.031,-73.779 41.046,-73.78 41.049,-73.779 41.052,-73.776 41.054,-73.775 41.057,-73.767 41.058,-73.769 41.062,-73.768 41.067,-73.762 41.073,-73.759 41.074,-73.748 41.069,-73.746 41.056,-73.742 41.056,-73.74 41.053,-73.74 41.049,-73.749 41.04,-73.748 41.035,-73.739 41.034,-73.729 41.029,-73.725 41.025,-73.72 41.016,-73.717 41.015,-73.716 41.006,-73.718 41.002,-73.732 40.988,-73.732 40.985,-73.739 40.979,-73.745 40.978,-73.749 40.981,-73.749 40.986,-73.751 40.986,-73.756 40.991,-73.759 40.991,-73.76 40.993,-73.765 40.994,-73.769 40.997,-73.774 41.002,-73.775 41.006,-73.788 41.018,-73.792 41.024))'
      >>> wkt_reader = stc.wkt_reader()
      >>> white_plains = wkt_reader.read(white_plains_WKT)
      >>> si.containing(white_plains)
      ['Westchester']
      
    • 哪个县是 White Plains 所在的城市? 结果是包含值、几何图形和距离的元组列表。
      >>> si.containing_with_info(white_plains)
      [('Westchester',
        MultiPolygon(Polygon: Boundary: Ring(LineSegment(Point(40.886299, -73.767176), Point(40.886899, -73.767276)), LineSegment(Point(40.886899, -73.767276), Point(40.887599, -73.768276)), LineSegment(Point(40.887599, -73.768276), Point(40.888399, -73.770576)), ...) Interiors: , Polygon: Boundary: Ring(LineSegment(Point(41.198434, -73.514617), Point(41.200814, -73.509487)), LineSegment(Point(41.200814, -73.509487), Point(41.21276, -73.482709)), LineSegment(Point(41.21276, -73.482709), Point(41.295422, -73.550961)), ...) Interiors: ),
        0.0)]
      
    • 哪 3 个县离 White Plains Hospital 最近? 结果包括其距离。
      >>> counties = si.nearest_neighbors_with_info(white_plains_hospital, 3)
      >>> for county in counties:
      ...     print(county[0], county[2])
      Westchester 0.0
      Fairfield 7320.602641166855
      Rockland 10132.182241119823
      
    • 在距离 White Plains Hospital 的 20 公里范围内有哪些县? 结果按其距离排序。
      >>> counties = si.within_distance_with_info(white_plains_hospital, 20000)
      >>> counties.sort(key=lambda tup: tup[2])
      >>> for county in counties:
      ...     print(county[0], county[2])
      Westchester 0.0
      Fairfield 7320.602641166855
      Rockland 10132.182241119823
      Bergen 10934.1691335908
      Bronx 15683.400292349625
      Nassau 17994.425235412604