Esempio n. 1
0
def query_database_hash(hsh):
    hsh = hsh[:5]
    exact_matches5 = []
    if hsh in _dbase:
        exact_matches5 = _dbase[hsh]
    r = geohash.neighbors(hsh)
    neighbors5 = []
    for x in r:
        if x in _dbase:
            neighbors5.extend(_dbase[x])

    hsh = hsh[:4]
    exact_matches4 = []
    if hsh in _dbase:
        exact_matches4 = _dbase[hsh]
    r = geohash.neighbors(hsh)
    neighbors4 = []
    for x in r:
        if x in _dbase:
            neighbors4.extend(_dbase[x])

    hsh = hsh[:3]
    exact_matches3 = []
    if hsh in _dbase:
        exact_matches3 = _dbase[hsh]
    r = geohash.neighbors(hsh)
    neighbors3 = []
    for x in r:
        if x in _dbase:
            neighbors3.extend(_dbase[x])

    return [exact_matches5, neighbors5, exact_matches4, neighbors4, exact_matches3, neighbors3]
Esempio n. 2
0
def nearby():
	print 'geohash.bounding box'
	print geohash.bbox('tdr1w')
	print 'geohash neighbours'
	print geohash.neighbors('tdr1wxype953')
	print 'geohash expand'
	print geohash.expand('tdr1wxype953')
Esempio n. 3
0
def nearby():
    print 'geohash.bounding box'
    print geohash.bbox('tdr1w')
    print 'geohash neighbours'
    print geohash.neighbors('tdr1wxype953')
    print 'geohash expand'
    print geohash.expand('tdr1wxype953')
Esempio n. 4
0
 def test_one(self):
     self.assertEqual(set(['1', '2', '3', 'p', 'r']),
                      set(geohash.neighbors("0")))
     self.assertEqual(set(['w', 'x', 'y', '8', 'b']),
                      set(geohash.neighbors("z")))
     self.assertEqual(set(['2', '6', '1', '0', '4', '9', '8', 'd']),
                      set(geohash.neighbors("3")))
Esempio n. 5
0
def get_cells_in_circle(lat, lon, radius, precision):
  """Gets all geohash cells inside a circle, sorted approximately by distance.

  Args:
    lat: float, the latitude of the circle center.
    lon: float, the longitude of the circle center.
    radius: float, the radius of the circle in meters.
    precision: int, the precision of the geohash.

  Returns:
    list, the list of geohash cells.
  """
  # Get all cells that are in the circle (with the max_resolution).
  # Start from the center cell.
  cur_set = set([geohash.encode(lat, lon, precision)])
  all_set = set(cur_set)
  result = list(cur_set)
  while cur_set:
    # Gradually extend the found cells (all_set) layer by layer.
    new_set = set([])
    for cell in cur_set:
      for one_neighbor in geohash.neighbors(cell):
        if one_neighbor in all_set:
          continue
        (nb_lat, nb_lon) = geohash.decode(one_neighbor)
        if distance(nb_lat, nb_lon, lat, lon) < radius:
          new_set.add(one_neighbor)
    all_set.update(new_set)
    result.extend(list(new_set))
    cur_set = new_set

  return result
Esempio n. 6
0
def solve_xmin(pt1,pt2,size):
	positiondict = {'neg':[4,6,2,5,0,1],'pos':[3,7,2,5,0,1],'vert':[2,5],'zero':[0,1]}
	
	ghash = geohash.encode(pt1[1],pt1[0],size)
	
	# getting neighbors
	neighbors = geohash.neighbors(ghash)

	# setting up variables for slope determination
	y1,y2,y3,y4,xmin = get_corner_points(neighbors)

	# getting slope
	slope = get_slope(pt1,pt2)

	y = interpol_point(xmin,slope,pt1)
	if y < y1 and y > y2:
		slope = 'neg'
		tang = 'pos'
	elif y < y2 and y > y3:
		slope = 'zero'
		tang = 'vert'
	elif y < y3 and y > y4:
		slope = 'pos'
		tang = 'neg'
	elif y >= y1 or y <= y4:
		slope = 'vert'
		tang = 'zero'
	if tang == 'pos' or tang == 'neg':
		pos1,pos2,pos3,pos4,pos5,pos6 = positiondict[tang]
		return [pos1,pos2,pos3,pos4,pos5,pos6]
	pos1,pos2 = positiondict[tang]
	return [pos1,pos2]
Esempio n. 7
0
def get_moving_time(geohash1, geohash2):
    try:
        if geohash1 == None or geohash2 == None: # 바로 나오는 경우, 들어가는 경우
            return 0

        if geohash1 == '' or geohash2 == '': # 모를 경우에는 1시간을 줌
            return 60

        geohash1_parent = geohash1[:5]
        geohash2_parent = geohash2[:5]

        expanded = geohash.expand(geohash1_parent)
        expanded_depth2 = set()

        for gh in expanded:
            neighbors = geohash.neighbors(gh)
            expanded_depth2 = expanded_depth2.union(neighbors)

        expanded_depth2 = list(expanded_depth2)

        if geohash1 == geohash2:
            return 30

        elif geohash2_parent in expanded:
            return 60

        elif geohash2_parent in expanded_depth2:
            return 90
        else:
            return 120
    except Exception, e:
        print_err_detail(e)
        return 60
Esempio n. 8
0
def filterFromSignature(data,hmap,maxDistance=5,heatThreshold=.01):
  ndata = copy.deepcopy(data)
  geohashlength = hmap['geohashlength']
  for l in range(len(data)):
    h = geohash.encode(data[l]['lat'],data[l]['lon'],geohashlength)
    visited = set([h])
    neighbors = set([h])
    nonZeroNeighbors = [h] if (h in hmap and hmap[h]>heatThreshold) else []
    d=0
    while (len(nonZeroNeighbors)==0 and d<maxDistance):
      nneighbors = set([])
      for n in neighbors:
        nneighbors.union([h for h in geohash.neighbors(n) if h not in visited])
      neighbors = nneighbors
      for n in neighbors:
        if (n in hmap and hmap[n]>heatThreshold):
          nonZeroNeighbors.append(n)
      visited.union(neighbors)
      d+=1

    if len(nonZeroNeighbors)>0:
      if len(nonZeroNeighbors)>1:
        print h,nonZeroNeighbors
      lat,lon=0.,0.
      for n in nonZeroNeighbors:
        dlat,dlon = geohash.decode(n)
        lat += dlat
        lon += dlon
      ndata[l]= Location(lat/len(nonZeroNeighbors),
                            lon/len(nonZeroNeighbors),
                            data[l].timestamp)

  return ndata
Esempio n. 9
0
 def do_geohashtogeojson(self, geoh):
     """Build GeoJSON corresponding to geohash given as parameter.
     GEOHASHTOGEOJSON u09vej04 [NEIGHBORS 0]"""
     geoh, with_neighbors = self._match_option('NEIGHBORS', geoh)
     bbox = geohash.bbox(geoh)
     north = bbox['n']
     south = bbox['s']
     east = bbox['e']
     west = bbox['w']
     if with_neighbors != '0':
         neighbors = geohash.neighbors(geoh)
         for neighbor in neighbors:
             bbox = geohash.bbox(neighbor)
             if bbox['n'] > north:
                 north = bbox['n']
             if bbox['s'] < south:
                 south = bbox['s']
             if bbox['e'] > east:
                 east = bbox['e']
             if bbox['w'] < west:
                 west = bbox['w']
     geojson = {
         "type": "Polygon",
         "coordinates": [[
             [west, north],
             [east, north],
             [east, south],
             [west, south],
             [west, north]
         ]]
     }
     print(white(json.dumps(geojson)))
Esempio n. 10
0
    def get_sites_near_latlon(self, lat, lon):
        ghash = geohash.encode(float(lat), float(lon))

        hashes = geohash.neighbors(ghash)
        hashes.append(ghash)

        sites = rds.zrange(sitekey, 0, -1, withscores = True)

        hashmatches = {}

        for chars in range(6,3,-1):
            for sitehash, id in sites:
                for currenthash in hashes:

                    if currenthash[0:chars] == sitehash[0:chars]:
                        hashmatches[sitehash] = int(id)

            if len(hashmatches) > 0:
                break

        sites = []

        if len(hashmatches) > 0:

            for hash, id in hashmatches.items():
                site = self.fetch_id(id)
                if site:
                    sites.append(site)

        return sites
Esempio n. 11
0
    def _buil_cell_tiles_from_bbox(self, bbox_coordinates):
        '''Computes all geohash tile in the given bounding box
        :param bbox_coordinates: the bounding box coordinates of the geohashes
        :return: a list of geohashes
        '''

        checked_geohashes = set()
        geohash_stack = set()
        geohashes = []

        '''get center of bounding box, assuming the earth is flat'''
        center_longitude = (bbox_coordinates[0] + bbox_coordinates[2]) / 2
        center_latitude = (bbox_coordinates[1] + bbox_coordinates[3]) / 2

        center_geohash = self.build_cell([center_longitude, center_latitude])

        geohashes.append(center_geohash)
        geohash_stack.add(center_geohash)
        checked_geohashes.add(center_geohash)

        while len(geohash_stack) > 0:
            current_geohash = geohash_stack.pop()
            neighbors = geohash.neighbors(current_geohash)
            for neighbor in neighbors:
                if neighbor not in checked_geohashes and self._is_geohash_in_bounding_box(neighbor, bbox_coordinates):
                    geohashes.append(neighbor)
                    geohash_stack.add(neighbor)
                    checked_geohashes.add(neighbor)

        geohashes.sort()

        return geohashes
Esempio n. 12
0
def compute_geohash_tiles(bbox_coordinates):
    """Computes all geohash tile in the given bounding box

    :param bbox_coordinates: the bounding box coordinates of the geohashes
    :return: a list of geohashes
    """

    checked_geohashes = set()
    geohash_stack = set()
    geohashes = []
    # get center of bounding box, assuming the earth is flat ;)
    center_latitude = (bbox_coordinates[0] + bbox_coordinates[2]) / 2
    center_longitude = (bbox_coordinates[1] + bbox_coordinates[3]) / 2

    center_geohash = geohash.encode(center_latitude,
                                    center_longitude,
                                    precision=GEOHASH_PRECISION)
    geohashes.append(center_geohash)
    geohash_stack.add(center_geohash)
    checked_geohashes.add(center_geohash)
    while len(geohash_stack) > 0:
        current_geohash = geohash_stack.pop()
        neighbors = geohash.neighbors(current_geohash)
        for neighbor in neighbors:
            if neighbor not in checked_geohashes and is_geohash_in_bounding_box(
                    neighbor, bbox_coordinates):
                geohashes.append(neighbor)
                geohash_stack.add(neighbor)
                checked_geohashes.add(neighbor)
    return geohashes
Esempio n. 13
0
def make_neighbors(geohashlist,firstlast=False):
	if firstlast == True:
		newlist = []
		for row in geohashlist:
			add = geohash.neighbors(row)
			newlist += add
		newlist = np.unique(newlist).tolist()
		return newlist

	first = geohashlist[0]
	newlist = [first]
	last = geohashlist[-1]
	for ghash in geohashlist[1:-1]:
		add = geohash.neighbors(ghash)
		newlist += add
	newlist.append(last)
	newlist = np.unique(newlist).tolist()
	return newlist
Esempio n. 14
0
def generate_tangs(geohashlist,positions):
	geohashlist = [i[:-1] for i in geohashlist]
	geohashlist = flatten_nonsorted(geohashlist)
	geohashlist = [geohash.neighbors(i) for i in geohashlist]
	if len(positions) == 6:
		geohashlist = [[i[positions[0]],i[positions[1]],i[positions[2]],i[positions[3]],i[positions[4]],i[positions[5]]] for i in geohashlist]
	else:
		geohashlist = [[i[positions[0]],i[positions[1]]] for i in geohashlist]
	return sum(geohashlist,[])
Esempio n. 15
0
    def get_candidate_points(self, latitude, longitude):
        code = geohash.encode(latitude, longitude)[:self.precision]
        candidates = OrderedDict()

        candidates.update([(k, None) for k in self.index.get(code, [])])

        for neighbor in geohash.neighbors(code):
            candidates.update([(k, None) for k in self.index.get(neighbor, [])])

        return candidates.keys()
Esempio n. 16
0
 def geo_aliases(cls, total_docs_by_geo, min_doc_count=1000):
     keep_geos = total_docs_by_geo.filter(
         lambda geo_count: geo_count[1] >= min_doc_count)
     alias_geos = total_docs_by_geo.subtract(keep_geos)
     return list(alias_geos.keys()) \
                      .flatMap(lambda key: [(neighbor, key) for neighbor in geohash.neighbors(key)]) \
                      .join(keep_geos) \
                      .map(lambda neighbor_key_count: (neighbor_key_count[1][0], (neighbor_key_count[0], neighbor_key_count[1][1]))) \
                      .groupByKey() \
                      .map(lambda key_values: (key_values[0], sorted(key_values[1], key_values[0]=operator.itemgetter(1), reverse=True)[0][0]))
Esempio n. 17
0
 def geo_aliases(cls, total_docs_by_geo, min_doc_count=1000):
     keep_geos = total_docs_by_geo.filter(
         lambda (geo, count): count >= min_doc_count)
     alias_geos = total_docs_by_geo.subtract(keep_geos)
     return alias_geos.keys() \
                      .flatMap(lambda key: [(neighbor, key) for neighbor in geohash.neighbors(key)]) \
                      .join(keep_geos) \
                      .map(lambda (neighbor, (key, count)): (key, (neighbor, count))) \
                      .groupByKey() \
                      .map(lambda (key, values): (key, sorted(values, key=operator.itemgetter(1), reverse=True)[0][0]))
Esempio n. 18
0
def generate_neighbors(latitude, longitude, precision=5):
    """
    Generates neighbors of given coordinates and includes the coordinate itself.
    """

    latitude, longitude = map(float, [latitude, longitude])
    room = geohash.encode(latitude, longitude, precision=precision)
    neighbors = geohash.neighbors(room)
    neighbors.append(room)

    return neighbors
Esempio n. 19
0
    def get_candidate_points(self, latitude, longitude):
        code = geohash.encode(latitude, longitude)[:self.precision]
        candidates = OrderedDict()

        candidates.update([(k, None) for k in self.index.get(code, [])])

        for neighbor in geohash.neighbors(code):
            candidates.update([(k, None)
                               for k in self.index.get(neighbor, [])])

        return candidates.keys()
Esempio n. 20
0
    def polygon_to_multi_length_geohashes(self, polygon: Polygon, precision: int) -> tuple:
        """
        将目标几何图形切割成geohash,并输出包含与相交两个geohash字符串列表

        Parameters
        ----------
        polygon : shapely.geometry.Polygon
            目标几何图形
        precision : int, optional
            所求geohash经度

        Returns
        ----------
        tuple
            被目标几何图形包括的geohash字符串列表,与目标几何图形相交的geohash字符串列表

        Examples
        ----------
        >>> g = GeohashOperator()
        >>> p = Polygon([[116.40233516693117, 39.95442126877703], [116.40233516693117, 39.95744689749303],
        >>> [116.4070386902313, 39.95744689749303], [116.4070386902313, 39.95442126877703]])
        >>> g.polygon_to_multi_length_geohashes(p, 7)
        (
            {'wx4g2cd', 'wx4g2ce', 'wx4g2cf', 'wx4g2cg', 'wx4g2cs', 'wx4g2cu'},
            {'wx4g2c3', 'wx4g2c6', 'wx4g2c7', 'wx4g2c9', 'wx4g2cc', 'wx4g2ck', 'wx4g2cm', 'wx4g2ct', 'wx4g2cv',
            'wx4g2f1', 'wx4g2f4', 'wx4g2f5', 'wx4g2fh','wx4g2fj'}
        )

        See Also
        ----------
        geohash_to_polygon : 将Geohash字符串转成矩形
        """
        inner_geohashes = set()
        outer_geohashes = set()
        intersect_geohashes = set()
        testing_geohashes = queue.Queue()
        testing_geohashes.put(geohash.encode(polygon.exterior.xy[1][0], polygon.exterior.xy[0][0], precision))
        while not testing_geohashes.empty():
            current_geohash = testing_geohashes.get()
            if current_geohash not in inner_geohashes and current_geohash not in outer_geohashes:
                current_polygon = self.geohash_to_polygon(current_geohash)
                condition = polygon.intersects(current_polygon)
                if condition:
                    if polygon.contains(current_polygon):
                        inner_geohashes.add(current_geohash)
                    elif polygon.intersects(current_polygon):
                        intersect_geohashes.add(current_geohash)
                        outer_geohashes.add(current_geohash)
                    else:
                        outer_geohashes.add(current_geohash)
                    for neighbor in geohash.neighbors(current_geohash):
                        if neighbor not in inner_geohashes and neighbor not in outer_geohashes:
                            testing_geohashes.put(neighbor)
        return inner_geohashes, intersect_geohashes
Esempio n. 21
0
def generate_neighbors(latitude, longitude, precision=5):
    """
    Generates neighbors of given coordinates and includes the coordinate itself.
    """

    latitude, longitude = map(float, [latitude, longitude])
    room = geohash.encode(latitude, longitude, precision=precision)
    neighbors = geohash.neighbors(room)
    neighbors.append(room)

    return neighbors
Esempio n. 22
0
    def execute(self, current_location: Location) -> Optional[Location]:
        """Execution of the Movement Evaluation Policy"""

        if random.random() <= settings.COURIER_MOVEMENT_PROBABILITY:
            current_geohash = geohash.encode(*current_location.coordinates, precision=6)
            geohash_neighbors = geohash.neighbors(current_geohash)
            destination_geohash = random.choice(geohash_neighbors)
            destination_coordinates = geohash.decode(destination_geohash)

            return Location(lat=destination_coordinates[0], lng=destination_coordinates[1])

        return None
Esempio n. 23
0
    def _add_neighbors(self, cell):
        if cell in self._computed_cells:
            return

        n = geohash.neighbors(cell)

        def cond(c): return (c in self._computed_cells) or (c in self._cells)

        n[:] = list(filterfalse(cond, n))

        self._next_batch.update(n)
        self._computed_cells.add(cell)
Esempio n. 24
0
    def _build_cell_tiles_from_shapefile(self, shpfile):
        '''Computes all geohash tiles in the given shapefile
        :param shapefile: shapefile
        :return: a list of geohashes
        '''

        ''' open shapefile'''
        sf = shapefile.Reader(shpfile)

        '''get features'''
        shapes = sf.shapes()
        if len(shapes) > 1:
            print("More than one feature was found. Only first will be selected.")
            input("Press Enter to continue...")
        '''only use first feature'''
        first_shp = shapes[0]

        ''' get shape type. only if shapetype is polygon'''
        shape_type = first_shp.shapeType
        if shape_type != 5:
            handle_error(msg='Shapefile feature be a polygon')

        ''' get points coordinates for each point in the shape '''
        points = first_shp.points
        polygon = Polygon(points)

        checked_geohashes = set()
        geohash_stack = set()
        geohashes = []

        '''get center of bounding box, assuming the earth is flat'''
        center_latitude = polygon.centroid.coords[0][1]
        center_longitude = polygon.centroid.coords[0][0]

        center_geohash = self.build_cell([center_longitude, center_latitude])

        geohashes.append(center_geohash)
        geohash_stack.add(center_geohash)
        checked_geohashes.add(center_geohash)

        while len(geohash_stack) > 0:
            current_geohash = geohash_stack.pop()
            neighbors = geohash.neighbors(current_geohash)
            for neighbor in neighbors:
                point = Point(geohash.decode(neighbor)[::-1])
                if neighbor not in checked_geohashes and polygon.contains(point):
                    geohashes.append(neighbor)
                    geohash_stack.add(neighbor)
                    checked_geohashes.add(neighbor)

        geohashes.sort()

        return geohashes
Esempio n. 25
0
File: mobi.py Progetto: m329/mobi
def wishlist_nearby():
	
	gh = get_user_geohash()
	
	neighbors = geohash.neighbors(gh)
	neighbors.append(gh)
	neighborhood = "('"+"','".join(neighbors)+"')"
		
	db = get_db()
	cur = db.execute("select I.itm_name, I.prc, U.usr as owner, I.itm_id from items as I join users as U on I.usr=U.usr join (select * from wishlists as WI join users as US on WI.usr = US.usr) as W where U.usr!=? and W.usr=? and I.itm_name like '%'||W.wishstr||'%' and U.geohash in "+neighborhood+";",[g.user.id, g.user.id])
	items = cur.fetchall()
	return render_template("wishlist_nearby.html", user=g.user, items=items)
Esempio n. 26
0
def lochasher(lines):
	hashDict = {}
	for line in lines:
		if type(line[1]) != tuple:
			hashed = Geohash.encode(round(float(line[0][0]),6),round(float(line[0][1]),6),7)
			hashDict.setdefault(hashed,[]).append(line)
		else:
			hashed = Geohash.encode(round(float(line[0][1]),6),round(float(line[0][0]),6),7)
			hashedneighbors = geohash.neighbors(hashed)
			hashedneighbors.append(hashed)
			hashDict.setdefault((' '.join(hashedneighbors),line[1]),[]).append(line)
	return hashDict
Esempio n. 27
0
    def _add_neighbors(self, cell):
        if cell in self._computed_cells:
            return

        n = geohash.neighbors(cell)

        def cond(c):
            return (c in self._computed_cells) or (c in self._cells)

        n[:] = list(filterfalse(cond, n))

        self._next_batch.update(n)
        self._computed_cells.add(cell)
Esempio n. 28
0
    def _get_adjoining_hashes(self, hashcode, precision):
        """
        Given a hash, find the nearest eight boxes (four adjacent,
        four diagonal) to the box.

        :param hashcode: Initial full geohash
        :param precision: Level of precision, integer
        :return:
        """
        if precision > len(hashcode):
            raise ValueError('Precision greater than hashcode size.')
        reduced_hash = hashcode[:precision]

        return geohash.neighbors(reduced_hash)
Esempio n. 29
0
    def _get_adjoining_hashes(self, hashcode, precision):
        """
        Given a hash, find the nearest eight boxes (four adjacent,
        four diagonal) to the box.

        :param hashcode: Initial full geohash
        :param precision: Level of precision, integer
        :return:
        """
        if precision > len(hashcode):
            raise ValueError('Precision greater than hashcode size.')
        reduced_hash = hashcode[:precision]

        return geohash.neighbors(reduced_hash)
Esempio n. 30
0
def region_neighbors(data, user_region=True):
    region_pairs = set()
    regions = set(data['region'])
    if user_region:
        regions = regions.union(set(data['user_region']))
    for region in regions:
        neighbors = geohash.neighbors(region)
        for neighbor in neighbors:
            if neighbor in regions:
                if neighbor < region:
                    region_pairs.add((neighbor, region))
                else:
                    region_pairs.add((region, neighbor))
    return regions, region_pairs
Esempio n. 31
0
 def get_geohashes(self):
     """Get all the geohashes from this song."""
     song_tags = self.get_stripped_tags()
     geohashes = [
         t.split(':')[1] for t in song_tags if t.startswith('geohash:')
     ]
     if GEOHASH:
         for ghash in geohashes[:]:
             try:
                 geohashes.extend(geohash.neighbors(ghash))
             except ValueError:
                 # invalid geohash
                 print("Invalid geohash: %s in %s - %s" %
                       (ghash, self.get_artist(), self.get_title()))
     return geohashes
Esempio n. 32
0
 def get_geohashes(self):
     """Get all the geohashes from this song."""
     song_tags = self.get_stripped_tags()
     geohashes = [
         t.split(':')[1] for t in song_tags if t.startswith('geohash:')]
     if GEOHASH:
         for ghash in geohashes[:]:
             try:
                 geohashes.extend(geohash.neighbors(ghash))
             except ValueError:
                 # invalid geohash
                 print(
                     "Invalid geohash: %s in %s - %s" % (
                         ghash, self.get_artist(), self.get_title()))
     return geohashes
Esempio n. 33
0
 def expand(bbox, geoh, depth):
     neighbors = geohash.neighbors(geoh)
     for neighbor in neighbors:
         other = geohash.bbox(neighbor)
         if with_neighbors > depth:
             expand(bbox, neighbor, depth + 1)
         else:
             if other['n'] > bbox['n']:
                 bbox['n'] = other['n']
             if other['s'] < bbox['s']:
                 bbox['s'] = other['s']
             if other['e'] > bbox['e']:
                 bbox['e'] = other['e']
             if other['w'] < bbox['w']:
                 bbox['w'] = other['w']
Esempio n. 34
0
 def expand(bbox, geoh, depth):
     neighbors = geohash.neighbors(geoh)
     for neighbor in neighbors:
         other = geohash.bbox(neighbor)
         if with_neighbors > depth:
             expand(bbox, neighbor, depth + 1)
         else:
             if other['n'] > bbox['n']:
                 bbox['n'] = other['n']
             if other['s'] < bbox['s']:
                 bbox['s'] = other['s']
             if other['e'] > bbox['e']:
                 bbox['e'] = other['e']
             if other['w'] < bbox['w']:
                 bbox['w'] = other['w']
Esempio n. 35
0
    def near_dupe_hashes(cls,
                         address,
                         geohash_precision=DEFAULT_GEOHASH_PRECISION,
                         use_latlon=True,
                         use_city=False,
                         use_postal_code=False):
        address_expansions = cls.component_expansions(address)

        lat = address.get(Coordinates.LATITUDE)
        lon = address.get(Coordinates.LONGITUDE)
        postcode = safe_decode(address.get(AddressComponents.POSTAL_CODE,
                                           u'')).strip()
        city = safe_decode(address.get(AddressComponents.CITY, u'')).strip()

        if not any(address_expansions):
            return

        if lat and lon and use_latlon and not (
            (isclose(lat, 0.0) and isclose(lon, 0.0)) or lat >= 90.0
                or lat <= -90.0):
            geo = geohash.encode(lat, lon)[:geohash_precision]
            geohash_neighbors = [geo] + geohash.neighbors(geo)

            base_key = cls.GEOHASH_KEY_PREFIX

            for keys in six.itertools.product(geohash_neighbors,
                                              *address_expansions):
                yield u'{}|{}'.format(base_key, u'|'.join(keys))

        if postcode and use_postal_code:
            postcode_expansions = expand_address(
                postcode, address_components=ADDRESS_POSTAL_CODE)

            base_key = cls.POSTCODE_KEY_PREFIX

            for keys in six.itertools.product(postcode_expansions,
                                              *address_expansions):
                yield u'{}|{}'.format(base_key, u'|'.join(keys))

        if city and use_city:
            city_expansions = expand_address(
                city, address_components=ADDRESS_TOPONYM)

            base_key = cls.CITY_KEY_PREFIX

            for keys in six.itertools.product(city_expansions,
                                              *address_expansions):
                yield u'{}|{}'.format(base_key, u'|'.join(keys))
    def _build_cell_tiles_from_geojson(self, jsonfile):
        '''Computes all geohash tiles in the given geojson file
        :param jsonfile: geojson (polygon)
        :return: a list of geohashes
        '''
        with open(jsonfile) as f:
            try:
                data = json.load(f)

                polygon = shape(data["geometry"])
                geom_type = polygon.geom_type

                if geom_type != 'Polygon':
                    handle_error(
                        'SyntaxError',
                        'Invalid GEOJSON format: Must be a Polygon type')

                checked_geohashes = set()
                geohash_stack = set()
                geohashes = []
                '''get center of bounding box, assuming the earth is flat'''
                center_longitude = polygon.centroid.coords[0][0]
                center_latitude = polygon.centroid.coords[0][1]

                center_geohash = self.build_cell(
                    [center_longitude, center_latitude])

                geohashes.append(center_geohash)
                geohash_stack.add(center_geohash)
                checked_geohashes.add(center_geohash)

                while len(geohash_stack) > 0:
                    current_geohash = geohash_stack.pop()
                    neighbors = geohash.neighbors(current_geohash)
                    for neighbor in neighbors:
                        point = Point(geohash.decode(neighbor)[::-1])
                        if neighbor not in checked_geohashes and polygon.contains(
                                point):
                            geohashes.append(neighbor)
                            geohash_stack.add(neighbor)
                            checked_geohashes.add(neighbor)

                geohashes.sort()

                return geohashes

            except ValueError as e:
                handle_error(msg='Invalid GEOJSON format')
Esempio n. 37
0
 def test_neighbors(self):
     test = '9p'
     expect = {
         's': '9n',
         'e': '9r',
         'w': '8z',
         'nw': 'bb',
         'sw': '8y',
         'ne': 'c2',
         'se': '9q',
         'n': 'c0',
         'c': '9p'
     }
     result = geohash.neighbors(test)
     for k in expect:
         assert expect[k] == result[k]
Esempio n. 38
0
    def process_signals(self, signals, input_id='default'):
        output_signals = []

        for signal in signals:
            precision = self.precision()
            lat = self.lat(signal)
            lng = self.lng(signal)

            loc = geohash.encode(lat, lng, precision)
            setattr(signal, 'results', loc)
            print(signal)
            if self.adj():
                neighbors = geohash.neighbors(loc)
                setattr(signal, 'neighbors', neighbors)

        self.notify_signals(signals)
Esempio n. 39
0
def polygon_to_geohashes(polygon, precision, inner=True):
    """
    :param polygon: shapely polygon.
    :param precision: int. Geohashes' precision that form resulting polygon.
    :param inner: bool, default 'True'. If false, geohashes that are completely outside from the polygon are ignored.
    :return: set. Set of geohashes that form the polygon.
    """
    inner_geohashes = set()
    outer_geohashes = set()

    envelope = polygon.envelope
    centroid = polygon.centroid

    testing_geohashes = queue.Queue()
    testing_geohashes.put(geohash.encode(centroid.y, centroid.x, precision))

    while not testing_geohashes.empty():
        current_geohash = testing_geohashes.get()

        if current_geohash not in inner_geohashes and current_geohash not in outer_geohashes:
            current_polygon = geohash_to_polygon(current_geohash)

            condition = envelope.contains(
                current_polygon) if inner else envelope.intersects(
                    current_polygon) and not envelope.touches(current_polygon)

            if condition:
                if inner:
                    if polygon.contains(current_polygon):
                        inner_geohashes.add(current_geohash)
                    else:
                        outer_geohashes.add(current_geohash)
                else:
                    if polygon.intersects(
                            current_polygon
                    ) and not polygon.touches(current_polygon):
                        inner_geohashes.add(current_geohash)
                    else:
                        outer_geohashes.add(current_geohash)
                for neighbor in geohash.neighbors(current_geohash):
                    if neighbor not in inner_geohashes and neighbor not in outer_geohashes:
                        testing_geohashes.put(neighbor)

    return inner_geohashes
Esempio n. 40
0
def geohashes(geojson={}, precision=6, start_precision=2):
  """
    Return the list of geohashes that interects the geojson.
  """
  polygon = _polygon_from_geojson(geojson)
  if not polygon:
    return []

  p = min(start_precision, precision)
  center_geohash = get_center_geohash(polygon, precision=p)
  _geohashes = [center_geohash] + gh.neighbors(center_geohash)
  _geohashes = geohashes_polygon_intersection(polygon, _geohashes)

  while p < precision:
    _geohashes = _generate_inner_geohashes_for_geohashes(_geohashes)
    _geohashes = geohashes_polygon_intersection(polygon, _geohashes)
    p += 1

  return _geohashes
Esempio n. 41
0
def get_near_loc():
    result_path = cache_path + 'near_loc.hdf'
    if os.path.exists(result_path):
        result = pd.read_hdf(result_path, 'w')
    else:
        import geohash
        train = pd.read_csv(train_path)
        test = pd.read_csv(test_path)
        loc_list = train['geohashed_start_loc'].tolist() \
                   + train['geohashed_end_loc'].tolist() \
                   + test['geohashed_start_loc'].tolist()
        loc_list = np.unique(loc_list)
        result = []
        for loc in loc_list:
            nlocs = geohash.neighbors(loc)
            nlocs.append(loc)
            for nloc in nlocs:
                result.append([loc, nloc])
        result = pd.DataFrame(result, columns=['loc', 'near_loc'])
        result.to_hdf(result_path, 'w', complib='blosc', complevel=5)
    return result
Esempio n. 42
0
def get_near_loc():
    result_path = cache_path + 'near_loc.hdf'
    if os.path.exists(result_path):
        result = pd.read_hdf(result_path, 'w')
    else:
        import geohash
        prop = pd.read_csv(prop_path)
        parcel_geohash_dict = get_parcel_geohash_dict(7)
        prop['geohash'] = prop['parcelid'].map(parcel_geohash_dict)
        loc_list = prop['geohashed_start_loc'].tolist()
        loc_list = np.unique(loc_list)
        result = []
        for loc in loc_list:
            if loc is np.nan:
                continue
            nlocs = geohash.neighbors(loc)
            nlocs.append(loc)
            for nloc in nlocs:
                result.append([loc, nloc])
        result = pd.DataFrame(result, columns=['loc', 'near_loc'])
        result.to_hdf(result_path, 'w', complib='blosc', complevel=5)
    return result
Esempio n. 43
0
def geohashes(geojson={}, precision=6, start_precision=2):
    """
        Return the list of geohashes that interects the geojson.
    """
    polygon = _polygon_from_geojson(geojson)
    if not polygon:
        return []

    precision_init = precision
    if precision_init >= 9:
        precision -= 1

    p = min(start_precision, precision)

    center_geohash = get_center_geohash(polygon, precision=p)
    _geohashes = [center_geohash] + gh.neighbors(center_geohash)
    _geohashes = geohashes_polygon_intersection(polygon, _geohashes)

    while p < precision:
        _geohashes = _generate_inner_geohashes_for_geohashes(_geohashes)
        _geohashes = geohashes_polygon_intersection(polygon, _geohashes)
        p += 1

    if precision_init < 9:
        return _geohashes

    inside_geohashes_coarse = geohashes_polygon_within(polygon, _geohashes)
    inside_geohashes = _generate_inner_geohashes_for_geohashes(
        inside_geohashes_coarse)

    intersect_geohashes_coarse = list(
        set(_geohashes) - set(inside_geohashes_coarse))
    intersect_geohashes = _generate_inner_geohashes_for_geohashes(
        intersect_geohashes_coarse)
    intersect_geohashes = geohashes_polygon_intersection(
        polygon, intersect_geohashes)

    return inside_geohashes + intersect_geohashes
Esempio n. 44
0
def _compute_geohash_tiles(bbox_coordinates, geohash_precision):
    """
    Computes all geohash tile in the given bounding box
    (modified from https://blog.tafkas.net/2018/09/28/creating-a-grid-based-on-geohashes/)

    Args:
        bbox_coordinates: the bounding box coordinates of the geohashes (minx, miny, maxx, maxy)
        geohash_precision (int): geohash precision (level) of output geohashes

    Returns:
        list: geohashes

    """

    checked_geohashes = set()
    geohash_stack = set()
    geohashes = []
    # get center of bounding box, assuming the earth is flat ;)
    center_latitude = (bbox_coordinates[1] + bbox_coordinates[3]) / 2
    center_longitude = (bbox_coordinates[0] + bbox_coordinates[2]) / 2

    center_geohash = geohash.encode(center_latitude,
                                    center_longitude,
                                    precision=geohash_precision)
    geohashes.append(center_geohash)
    geohash_stack.add(center_geohash)
    checked_geohashes.add(center_geohash)
    while len(geohash_stack) > 0:
        current_geohash = geohash_stack.pop()
        neighbors = geohash.neighbors(current_geohash)
        for neighbor in neighbors:
            if neighbor not in checked_geohashes and _is_geohash_in_bounding_box(
                    neighbor, bbox_coordinates):
                geohashes.append(neighbor)
                geohash_stack.add(neighbor)
                checked_geohashes.add(neighbor)
    return geohashes
Esempio n. 45
0
# write on 2017/07/03 by chuan.sun
import itertools

import geohash
import pandas as pd

# import python-geohash
# from geohash import encode,decode,adjacent, neighbors, neighborsfit
# geohash.neighbors("abx1")

df_geohash = pd.read_excel('D:/kfc.xlsx')
df_geohash['neighbors'] = 0
j = 0
for i in df_geohash.iloc[:, 2]:
    # list_temp.append(geohash.neighbors(i))
    df_geohash.iloc[j, 3] = str(geohash.neighbors(i))
    j = j + 1

# print(df_geohash.head(10))
#
# a = [1, 2, 2, 2, 2]
# b = tuple(a)
# print(b)
#
# j = 0
# for i in df_geohash.iloc[:, 0]:
#     nine_list = geohash.neighbors(i[0:5])
#     list_temp = list()
#     for x in nine_list:
#         list_temp.append(geohash.neighbors(x))
#     list_temp = list(itertools.chain.from_iterable(list_temp))
	def test_one(self):
		self.assertEqual(set(['1', '2', '3', 'p', 'r']), set(geohash.neighbors("0")))
		self.assertEqual(set(['w', 'x', 'y', '8', 'b']), set(geohash.neighbors("z")))
		self.assertEqual(set(['2', '6', '1', '0', '4', '9', '8', 'd']), set(geohash.neighbors("3")))
Esempio n. 47
0
    def index_point(self, lat, lon):
        code = geohash.encode(lat, lon)[:self.precision]

        for key in [code] + geohash.neighbors(code):
            self.index[key].append(self.i)
        self.points.extend([lat, lon])
Esempio n. 48
0
def make_csvs(addgeohashs,csvfilenamealignment,csvfilenameneighbor,split,total,size):
	aligns = pd.DataFrame(addgeohashs,columns=['GEOHASH','TEXT'])
	
	print('Process %s: [%s/%s] dfsize created: %s' % (split,total,size,len(addgeohashs)))
	
	addgeohashs = []
	aligns = aligns.loc[np.unique(aligns['GEOHASH'],return_index=True)[1]]

	# aggregating text
	aligns['a'] = 'a'
	totalneighbors = pd.DataFrame(str.split(aligns.groupby('a')['GEOHASH'].apply(lambda x: '|'.join(['|'.join(['|'.join(['%s,%s' % (ii,i) for i in geohash.neighbors(ii)])]) for ii in x.values]))['a'],'|'),columns=['TEXT'])

	# slicing the text field to get the appropriate geohashs
	totalneighbors['NEIGHBORS'] = totalneighbors['TEXT'].str[10:]

	# doing total neighbors
	totalneighbors = totalneighbors.loc[np.unique(totalneighbors['NEIGHBORS'],return_index=True)[1]]

	# mapping the text to the neighboriing geohashs
	totalneighbors['TEXT'] = aligns.set_index('GEOHASH').loc[totalneighbors['TEXT'].str[:9]]['TEXT'].values
	aligns['TEXT'] = aligns['TEXT'].str[10:]
	totalneighbors['TEXT'] = totalneighbors['TEXT'].str[10:]

	# renaming neighbors headers
	totalneighbors = totalneighbors[['NEIGHBORS','TEXT']]
	totalneighbors.columns = ['GEOHASH','TEXT']

	# exporting / appending both types to csv
	with open(csvfilenameneighbor,'a') as f:
		f.write('\n'+totalneighbors[['GEOHASH','TEXT']].to_csv(index=False,mode=str,header=False))
	
	# exporting / appending to csv
	with open(csvfilenamealignment,'a') as f:
		f.write('\n'+aligns[['GEOHASH','TEXT']].to_csv(index=False,mode=str,header=False))	
Esempio n. 49
0
 def test_one(self):
     self.assertEqual(set(["1", "2", "3", "p", "r"]), set(geohash.neighbors("0")))
     self.assertEqual(set(["w", "x", "y", "8", "b"]), set(geohash.neighbors("z")))
     self.assertEqual(set(["2", "6", "1", "0", "4", "9", "8", "d"]), set(geohash.neighbors("3")))
Esempio n. 50
0
 def test_empty(self):
     self.assertEqual([], geohash.neighbors(""))
Esempio n. 51
0
 def test_neighbors(self):
   test = '9p'
   expect = {'s': '9n', 'e': '9r', 'w': '8z', 'nw': 'bb', 'sw': '8y', 'ne': 'c2', 'se': '9q', 'n': 'c0', 'c':'9p'}
   result = geohash.neighbors(test)
   for k in expect:
     assert expect[k] == result[k]
Esempio n. 52
0
def neighbors_func(ghash):
	nei = geohash.neighbors(ghash)
	return '%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s' % (ghash,nei[0],ghash,nei[1],ghash,nei[2],ghash,nei[3],ghash,nei[4],ghash,nei[5],ghash,nei[6],ghash,nei[7])
Esempio n. 53
0
    def index_point(self, lat, lon, geohash_level):
        code = geohash.encode(lat, lon)[:geohash_level]

        for key in [code] + geohash.neighbors(code):
            self.index[key].append(self.i)
Esempio n. 54
0
 def test_empty(self):
     self.assertEqual([], geohash.neighbors(""))
Esempio n. 55
0
def fetch_id(id):

    key = "site:%s" % id
    site = rds.get(key)
    site = json.loads(site)

    return site



if __name__ == '__main__':

    lat, lon = sys.argv[1], sys.argv[2]
    ghash = geohash.encode(float(lat), float(lon))

    hashes = geohash.neighbors(ghash)
    hashes.append(ghash)

    sites = rds.zrange(sitekey, 0, -1, withscores = True)

    hashmatches = {}

    for chars in range(6,3,-1):
        for sitehash, id in sites:
            for currenthash in hashes:

                if currenthash[0:chars] == sitehash[0:chars]:
                    hashmatches[sitehash] = int(id)

        if len(hashmatches) > 0:
            break
 def geohash(cls, address):
     geo = geohash.encode(address.latitude, address.longitude, cls.geohash_precision)
     neighbors = geohash.neighbors(geo)
     all_geo = [geo] + neighbors
     return all_geo
import geohash
import json

coll = { "type": "FeatureCollection", "features": []}

h = geohash.encode(48.862004, 2.33734, precision=5)
cells = [h] + geohash.neighbors(h)

for cell in cells:
	b = geohash.bbox(cell)
	coordinates = []

	coordinates.append( [ b['e'], b['s'] ])	
	coordinates.append( [ b['e'], b['n'] ])	
	coordinates.append( [ b['w'], b['n'] ])	
	coordinates.append( [ b['w'], b['s'] ])	
	coordinates.append( [ b['e'], b['s'] ])	

	feat = {
		"type": "Feature", 
		"properties": {"name": cell}, 
		"geometry": { 
			"type": "Polygon",
			"coordinates": [ coordinates ]
			}
		}

	coll["features"].append(feat)

print json.dumps(coll)