Coder Social home page Coder Social logo

polygon-geohasher's Introduction

polygon-geohasher

Polygon Geohasher is an open source Python package for converting Shapely's polygons into a set of geohashes. It obtains the set of geohashes inside a polygon or geohashes that touch (intersect) the polygon. This library uses python-geohash and shapely.

Requirements

Polygon Geohasher requires:

  • Python >= 3.x.
  • GEOS >= 3.3 (due to shapely).

Installing

Linux users can get Polygon Geohasher from the Python Package Index with pip (8+):

$ pip install polygon-geohasher

Usage

Here are some simple examples:

from polygon_geohasher.polygon_geohasher import polygon_to_geohashes, geohashes_to_polygon
from shapely import geometry

polygon = geometry.Polygon([(-99.1795917, 19.432134), (-99.1656847, 19.429034),
                            (-99.1776492, 19.414236), (-99.1795917, 19.432134)])
inner_geohashes_polygon = geohashes_to_polygon(polygon_to_geohashes(polygon, 7))
outer_geohashes_polygon = geohashes_to_polygon(polygon_to_geohashes(polygon, 7, False))

geohash_to_polygon(geohash):

This function receives a geohash and returns a Shapely's Polygon.

geohashes_to_polygon(geohashes):

This function receives a set of geohashes and returns a Shapely's Polygon or MultiPolygon.

polygon_to_geohashes(polygon, precision[, inner=True]):

This function receives a Shapely's Polygon and the precision of geohashes to be used to create a polygon and returns a set of geohashes (strings) that covers said polygon. It also receives an optional parameter inner that defines the way in which those polygons will be created. If an inner parameter is given, then only contained geohashes will be used; otherwise, intersected geohashes will be used.

See geohashed polygons resulting from both options (with and without inner) in the following example:

Example

polygon-geohasher's People

Contributors

astefanz avatar bonsanto avatar fpiedrah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

polygon-geohasher's Issues

Convert a set of points into polygons and search

I have a set of points defined in terms of latitudes and longitudes. These are clustered on the surface of a sphere. Can I use your package to convert them into a polygon by constructing geohashes ?

Then I need to search for other points in that polygon using Shapely. Possible ?

Set of geohashes returned for polygon with narrow sections does not cover the full polygon

Hi! I have been using this library to convert administrative polygons into sets of geohashes. Before I describe my issue, I want to thank you for creating/contributing to this library - I am grateful that I don't have to write these kinds of functions from scratch for the work I'm doing!

I noticed that the set of level 7 geohashes I received for a polygon of the city of Boston, MA, omits a significant section of the city. The polygon is in blue, and the outline of the resulting level 7 geohashes is in orange.

image

I was wondering if the algorithm for converting polygons to geohashes is thrown off by the fact that there is a very narrow stretch of the polygon where it's possible that no level 7 geohashes fit. When I set inner=False, it worked as expected and gets me all the geohashes that cover the polygon. However, I would prefer to have the set of geohashes that are specifically within the polygon, even if there are no level 7 geohashes covering that one narrow stretch of polygon. In other words, it would be nice to have all geohashes within the polygon, even if they aren't all contiguous with other.

Attached below is a text file with the WKT string of the polygon I'm using, so that people can replicate the issue if needed. I am thinking of forking this repo and playing with the code myself once I have more time, so any response pointing out where the issues in the code the issue might be would be helpful. Even if other people don't have the capacity to debug this issue themselves, that would help me test some different approaches myself. I'll update this issue if I get the chance to work on this issue myself.

boston_wkt.txt

Create CI pipeline

  • Test every time a PR is submitted

  • Generate an automated way to deploy into PyPi

Unable to pip install package

I am trying to install this package with pip in a python 3.7 conda env with pip version 20.1.1 on a MacOS. The env has a few other basic spatial packages (rtree, geopandas, shapely, and their dependencies) but is otherwise a base env. When I run pip install polygon-geohasher I get this error:

Processing /Users/chloehampton/Library/Caches/pip/wheels/a4/28/b6/71c5ec824b0350905a69e57bc3f43cdc87000049f2d390a4ca/polygon_geohasher-0.0.1-py3-none-any.whl
Requirement already satisfied: shapely in /Users/chloehampton/anaconda3/envs/global_car_detection/lib/python3.7/site-packages (from polygon-geohasher) (1.7.0)
Collecting python-geohash
  Using cached python-geohash-0.8.5.tar.gz (17 kB)
Building wheels for collected packages: python-geohash
  Building wheel for python-geohash (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /Users/chloehampton/anaconda3/envs/global_car_detection/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/setup.py'"'"'; __file__='"'"'/private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-wheel-simstzwp
       cwd: /private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/
  Complete output (16 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.9-x86_64-3.7
  copying geohash.py -> build/lib.macosx-10.9-x86_64-3.7
  copying quadtree.py -> build/lib.macosx-10.9-x86_64-3.7
  copying jpgrid.py -> build/lib.macosx-10.9-x86_64-3.7
  copying jpiarea.py -> build/lib.macosx-10.9-x86_64-3.7
  running build_ext
  building '_geohash' extension
  creating build/temp.macosx-10.9-x86_64-3.7
  creating build/temp.macosx-10.9-x86_64-3.7/src
  gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/chloehampton/anaconda3/envs/global_car_detection/include -arch x86_64 -I/Users/chloehampton/anaconda3/envs/global_car_detection/include -arch x86_64 -DPYTHON_MODULE=1 -I/Users/chloehampton/anaconda3/envs/global_car_detection/include/python3.7m -c src/geohash.cpp -o build/temp.macosx-10.9-x86_64-3.7/src/geohash.o
  xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
  error: command 'gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for python-geohash
  Running setup.py clean for python-geohash
Failed to build python-geohash
Installing collected packages: python-geohash, polygon-geohasher
    Running setup.py install for python-geohash ... error
    ERROR: Command errored out with exit status 1:
     command: /Users/chloehampton/anaconda3/envs/global_car_detection/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/setup.py'"'"'; __file__='"'"'/private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-record-x1oo3izy/install-record.txt --single-version-externally-managed --compile --install-headers /Users/chloehampton/anaconda3/envs/global_car_detection/include/python3.7m/python-geohash
         cwd: /private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/
    Complete output (16 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.9-x86_64-3.7
    copying geohash.py -> build/lib.macosx-10.9-x86_64-3.7
    copying quadtree.py -> build/lib.macosx-10.9-x86_64-3.7
    copying jpgrid.py -> build/lib.macosx-10.9-x86_64-3.7
    copying jpiarea.py -> build/lib.macosx-10.9-x86_64-3.7
    running build_ext
    building '_geohash' extension
    creating build/temp.macosx-10.9-x86_64-3.7
    creating build/temp.macosx-10.9-x86_64-3.7/src
    gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/chloehampton/anaconda3/envs/global_car_detection/include -arch x86_64 -I/Users/chloehampton/anaconda3/envs/global_car_detection/include -arch x86_64 -DPYTHON_MODULE=1 -I/Users/chloehampton/anaconda3/envs/global_car_detection/include/python3.7m -c src/geohash.cpp -o build/temp.macosx-10.9-x86_64-3.7/src/geohash.o
    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/chloehampton/anaconda3/envs/global_car_detection/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/setup.py'"'"'; __file__='"'"'/private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-install-nb4i8350/python-geohash/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/r1/wqr_7mvx7k76jkj_n5byt6tr0000gv/T/pip-record-x1oo3izy/install-record.txt --single-version-externally-managed --compile --install-headers /Users/chloehampton/anaconda3/envs/global_car_detection/include/python3.7m/python-geohash Check the logs for full command output. 

I would love to use this package, so any help is appreciated!

Black formatter

Make all library scripts black compliant and add black to the contributing guidelines.

polygon_to_geohashes order

Excuse me if this has been asked, do the geohashes returned by polygon_to_geohashes have any order?

I have field-sized polygons, and if one polygon crosses the border of two geohashes, I would like to see both (and I do now), but I would also like to know which one contains the majority percentage of my polygon. Is there currently a way to do that?

Thanks

ShapelyDeprecationWarning for use of cascaded_union

I get the following warning message when using polygon_geohasher.polygon_geohasher.geohashes_to_polygon:

.../python3.9/site-packages/polygon_geohasher/polygon_geohasher.py:56: ShapelyDeprecationWarning: The 'cascaded_union()' function is deprecated. Use 'unary_union()' instead.
    return cascaded_union([geohash_to_polygon(g) for g in geohashes])

I can suppress these using warnings.simplefilter but it might be good to switch to using unary_union as recommended in the warning message.

I'm happy to put in a PR for this.

cannot install in mac

ERROR: Command errored out with exit status 1:
Collecting polygon-geohasher
Using cached polygon_geohasher-0.0.1-py3-none-any.whl
Collecting python-geohash
Using cached python-geohash-0.8.5.tar.gz (17 kB)
Requirement already satisfied: shapely in /usr/local/lib/python3.9/site-packages (from polygon-geohasher) (1.7.1)
Building wheels for collected packages: python-geohash
Building wheel for python-geohash (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /usr/local/opt/[email protected]/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"'; file='"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-wheel-lvddbu3m
cwd: /private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/
Complete output (16 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-10.15-x86_64-3.9
copying geohash.py -> build/lib.macosx-10.15-x86_64-3.9
copying quadtree.py -> build/lib.macosx-10.15-x86_64-3.9
copying jpgrid.py -> build/lib.macosx-10.15-x86_64-3.9
copying jpiarea.py -> build/lib.macosx-10.15-x86_64-3.9
running build_ext
building '_geohash' extension
creating build/temp.macosx-10.15-x86_64-3.9
creating build/temp.macosx-10.15-x86_64-3.9/src
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -I/usr/local/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -DPYTHON_MODULE=1 -I/usr/local/include -I/usr/local/opt/[email protected]/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/tcl-tk/include -I/usr/local/Cellar/[email protected]/3.9.1_7/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c src/geohash.cpp -o build/temp.macosx-10.15-x86_64-3.9/src/geohash.o
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
error: command '/usr/bin/clang' failed with exit code 1

ERROR: Failed building wheel for python-geohash
Running setup.py clean for python-geohash
Failed to build python-geohash
Installing collected packages: python-geohash, polygon-geohasher
Running setup.py install for python-geohash ... error
ERROR: Command errored out with exit status 1:
command: /usr/local/opt/[email protected]/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"'; file='"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-record-g206bq0l/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.9/python-geohash
cwd: /private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/
Complete output (16 lines):
running install
running build
running build_py
creating build
creating build/lib.macosx-10.15-x86_64-3.9
copying geohash.py -> build/lib.macosx-10.15-x86_64-3.9
copying quadtree.py -> build/lib.macosx-10.15-x86_64-3.9
copying jpgrid.py -> build/lib.macosx-10.15-x86_64-3.9
copying jpiarea.py -> build/lib.macosx-10.15-x86_64-3.9
running build_ext
building '_geohash' extension
creating build/temp.macosx-10.15-x86_64-3.9
creating build/temp.macosx-10.15-x86_64-3.9/src
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -I/usr/local/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include -DPYTHON_MODULE=1 -I/usr/local/include -I/usr/local/opt/[email protected]/include -I/usr/local/opt/sqlite/include -I/usr/local/opt/tcl-tk/include -I/usr/local/Cellar/[email protected]/3.9.1_7/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c src/geohash.cpp -o build/temp.macosx-10.15-x86_64-3.9/src/geohash.o
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
error: command '/usr/bin/clang' failed with exit code 1
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/opt/[email protected]/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"'; file='"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-record-g206bq0l/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.9/python-geohash Check the logs for full command output.
WARNING: You are using pip version 21.0.1; however, version 21.1.2 is available.
You should consider upgrading via the '/usr/local/opt/[email protected]/bin/python3.9 -m pip install --upgrade pip' command.
bytedance@C02F65Q8MD6R ~ % ERROR: Command errored out with exit status 1: /usr/local/opt/[email protected]/bin/python3.9 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"'; file='"'"'/private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-install-g5rrrsfz/python-geohash_874e58bf73d4448eb8d54c2d3f0be4dd/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /private/var/folders/0d/9kppky_56vz1nf_1twn744d80000gp/T/pip-record-g206bq0l/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.9/python-geohash Check the logs for full command output
zsh: command not found: ERROR:

dependencies in requirements.txt are quite rigid

The dependencies in requirements.txt are given with exact version numbers.

This makes this package difficult to combine with others as versions move forward. Perhaps the requirements.txt could be changed to use ">=" rather than "=="

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.