kungfoo / geohash-java

Implementation of GeoHashes in java. We try to be/stay compliant to the spec, as far as possible.
Other
981 stars 310 forks source link

Will support polygons? #30

Closed someorz closed 6 years ago

kungfoo commented 6 years ago

I don't quite understand. What do you mean?

kungfoo commented 6 years ago

Do you mean polygon queries or polygon hashes? One might work by covering the entire polygon with hashes, the other won't since the bounding boxes are bound (no pun intended) to be rectangular.

someorz commented 6 years ago

yes,i mean polygon hashes,To determine whether a point is in a polygon

kungfoo commented 6 years ago

The hashes represent subdivisions of a rectangular grid, so they are by definition rectangular. Do you know of any library that has polygon support? One was to easily (and no very smartly) do it, might be to use a big enough bounding box to include all the points of the polygon. Then use that as a query.

dsmiley commented 6 years ago

Using Apache Lucene's spatial-extras module, which depends on LocationTech Spatial4j & JTS dependencies, you can do this with code like this (I tried it successfully):

  public void testGeoHashes() throws IOException, ParseException {
    SpatialContext ctx = JtsSpatialContext.GEO;
    GeohashPrefixTree grid = new GeohashPrefixTree(ctx, 11);//< 1 meter == 11 maxLevels
    Shape shape = ctx.getFormats().getWktReader().read("POLYGON((-93.18100824442227 45.25676372469945," +
        "-93.23182001200654 45.21421290799412," +
        "-93.16315546122038 45.23742639412364," +
        "-93.18100824442227 45.25676372469945))");
    double distErr = SpatialArgs.calcDistanceFromErrPct(shape, 0.10, ctx);
    int detailLevel = grid.getLevelForDistance(distErr);
    CellIterator cellIterator = grid.getTreeCellIterator(shape, detailLevel);
    BytesRef bytesRef = new BytesRef();
    while (cellIterator.hasNext()) {
      Cell next = cellIterator.next();
      if (!next.isLeaf()) {
        continue;
      }
      System.out.println(next.getTokenBytesNoLeaf(bytesRef).utf8ToString());
    }

  }

The spatial-extras module of Lucene uses these grids for spatial indexes on terms.

kungfoo commented 6 years ago

You would have to write a small class that basically covers the polygon with rectangles (hashes) and then use these as a query. As of now, I don't have the time or resources to do that, but I'm always. open to contributions.