def polygon_into_geohash(self, geo: BaseGeometry, accuracy: int = 7) -> list: """ 将多边形切割成固定精度的多个geohash块,将其按照位置输出成矩阵 Parameters ---------- geo : shapely.geometry.base.BaseGeometry 目标多边形 accuracy : int, optional Geohash的精度,默认为7 Returns ---------- list 分割出的geohash字符串列表 Examples ---------- >>> g = GeohashOperator() >>> p = Polygon([[116.40233516693117, 39.95442126877703], [116.40233516693117, 39.95744689749303], [116.4070386902313, 39.95744689749303], [116.4070386902313, 39.95442126877703]]) >>> g.polygon_into_geohash(p) [['wx4g2f1', 'wx4g2f4', 'wx4g2f5', 'wx4g2fh', 'wx4g2fj'], ['wx4g2cc', 'wx4g2cf', 'wx4g2cg', 'wx4g2cu', 'wx4g2cv'], ['wx4g2c9', 'wx4g2cd', 'wx4g2ce', 'wx4g2cs', 'wx4g2ct'], ['wx4g2c3', 'wx4g2c6', 'wx4g2c7', 'wx4g2ck', 'wx4g2cm']] See Also ---------- nearby_geohash : 求解周边的geohash块编码 geohash_to_polygon : 将Geohash字符串转成矩形 geohash_lonlac : 求geohash字符串的边界经度/维度 """ boundary = geo.bounds geo_list, line_geohash = [], [] horizontal_geohash = vertical_geohash = geohash.encode(boundary[1], boundary[0], accuracy) while True: vertical_geohash_polygon = self.geohash_to_polygon(vertical_geohash) if geo.contains(vertical_geohash_polygon) or geo.intersects(vertical_geohash_polygon): line_geohash.append(vertical_geohash) vertical_geohash = self.nearby_geohash(str(vertical_geohash), 3) elif self.geohash_lonlac(vertical_geohash, 'w') < boundary[2]: vertical_geohash = self.nearby_geohash(str(vertical_geohash), 3) else: if line_geohash: geo_list.append(line_geohash) line_geohash = [] horizontal_geohash = vertical_geohash = self.nearby_geohash(horizontal_geohash, 1) horizontal_geohash_polygon = self.geohash_to_polygon(horizontal_geohash) if not (geo.contains(horizontal_geohash_polygon) or geo.intersects(horizontal_geohash_polygon) or ( self.geohash_lonlac(horizontal_geohash, 's') < boundary[3])): return geo_list[::-1]
def __get_tmp_res(self, res: set, intersect_geohash: str, test_poly: BaseGeometry, tmp_precision: int, stop_precision: int, intersect: bool): geohash_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] tmp_precision += 1 for add_on in geohash_list: tmp_poly = self.geohash_to_polygon(intersect_geohash + add_on) if test_poly.contains(tmp_poly): res.add(intersect_geohash + add_on) elif test_poly.intersects(tmp_poly): if tmp_precision == stop_precision: if intersect is True: res.add(intersect_geohash + add_on) else: res = res.union( self.__get_tmp_res(res, intersect_geohash + add_on, test_poly, tmp_precision, stop_precision, intersect) ) return res