TransWikia.com

Library to convert polygon to geohash

Geographic Information Systems Asked by kaboom on January 9, 2021

There are plenty of Python libraries that convert Point to geohash. I need to convert an area, enclosed in Polygon to do that. I can’t just go around the perimeter points, I’ll have a hole inside.

Googling did not reveal any such functionality.

4 Answers

geohash-poly, a javascript library. From the web site - Transform a GeoJSON (Multi)Polygon to a list of geohashes that cover it. There are 3 geohash filtering options - inside, extent or intersect.

Correct answer by klewis on January 9, 2021

Java ones:https://github.com/xlvecle/geohash-poly
it can be used in java/kotlin/scala

Answered by user1252362 on January 9, 2021

I realize this is an old question, but I hope this function helps future users.

def bbox_geohashes_shapely(bbox_pts, accuracy=5):
        """
        Given a list of lat/lon points marking the bounding box, return all geohashes within the box.
        Bounding box can be an arbitrary simple polygon (i.e., any number of sides are okay, but inclusions are not).

        To see geohashes overlaid on map, visit http://geohash.gofreerange.com/

        All bounding points are checked for in/out.  After converting bounding points to geohashes, all neighbors
        are checked for presence in/out of bounding box.  Neighbors of those points still inside are also checked,
        until no more points remain.

        Parameters:
        -----------
            bbox_latlon   : list of [lat,lon] points
            accuracy      : length of geohash

        Returns:
        --------
            inside  : set of hashes contained within bounding box

        """    
        import geohash
        import shapely
        from shapely.geometry import Point

        unchecked = set()
        inside = set()
        outside = set()

        for pt in bbox_pts:
            tst_gh = geohash.encode(pt[0], pt[1], accuracy)
            unchecked.add(tst_gh)

        bbox = shapely.geometry.Polygon(bbox_pts)
        while unchecked:
            this = unchecked.pop()

            if bbox.contains(Point(geohash.decode(this))):
                inside.add(this)

                for gh in geohash.neighbors(this):
                    if (gh not in inside) & (gh not in outside) & (gh not in unchecked):
                        unchecked.add(gh)

            else:
                outside.add(this)

        return inside

Answered by Dan Parshall on January 9, 2021

use: pip install python-geohash instead of pip install geohash

Answered by CVAS on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP