4

I'd like to totally move on elasticsearch to filter elements currently handled by PostGreSQL with the PostGIS extension.

For this, I have to select every result within a given distance around (multi-)polygons.
Those polygons may be complex (hundreds of points).

I saw that a year ago, elasticsearch was not able to handle such filters.
In a year, ES has considerably evolved (including a major version), so is that kind of filtering supported by now ? If not, are there any plans ?


To explain, this is a simple usecase :
Consider this polygon as roughly describing Paris "Ile de la cité" borders:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          2.339546203568933,
          48.857710541463412
        ],
        [
          2.345168113663295,
          48.856750530470009
        ],
        [
          2.350618362380806,
          48.855056348509926
        ],
        [
          2.352077484084764,
          48.853785674412812
        ],
        [
          2.352592468215498,
          48.851611335037532
        ],
        [
          2.348730087234629,
          48.852543206332619
        ],
        [
          2.343194007828603,
          48.855084585345963
        ],
        [
          2.339546203568933,
          48.857710541463412
        ]
      ]
    ]
  ]
}

Consider a location at the opposite of the other island (so, outside the polygon):

{
  "type": "Point",
  "coordinates": [
    2.359378457022193,
    48.850358223189644
  ]
}

Here are some example queries using Django ORM (I can provide the SQL equivalent but I believe this isn't relevant):

>>> Location.objects.filter(point__within=(area.polygon))
[]
>>> Location.objects.filter(point__distance_lte=(area.polygon, D(m=500)))
[]
>>> Location.objects.filter(point__distance_lte=(area.polygon, D(m=550)))
[<Location: Ile saint louis>]

The __distance lookup uses PostGIS ST_distance_sphere.

Community
  • 1
  • 1
Léo
  • 469
  • 4
  • 13
  • You mean you have to find every geometry that intersects a buffer of some other geometries (mutlipolygons)? Could you give an example query? – John Powell Aug 09 '14 at 06:24
  • Thanks John, that's exactly that. I've just updated the question to include a simple usecase. – Léo Aug 09 '14 at 15:36

1 Answers1

5

The answer is yes, see the docs particularly geo-shape filter. Note, polygonal search requires that spatial4j and Java Topology Suite (JTS) dependencies are added.

There is one caveat, however, and that is around the licensing of JTS, which is LGPL. I quote from the Manning book, Solr in Action, page 531, which is relevant as Solr, like ElasticSearch, is built on top of Apache Lucene, and so the same restrictions apply.

Apache Solr is open source and licensed under the Apache 2.0 License, which grants you the right to use the code and software in any system, including proprietary systems, without any legal responsibility to share your code or pay any license fees. The decision was made to allow “integration with” JTS, which is licensed under a much less permissive LGPL (lesser general public license). The JTS library (JAR file) is not included in the Solr distribution along with Spatial4j, leaving JTS as an optional dependency not required to use basic shapes (point, rectangle, and circle) in Solr. If you want to make use of JTS in Solr, you’ll have to add the dependency yourself. As long as you do not make changes to the code in JTS, it’s generally considered safe to reference the JTS libraries without subjecting your application to exposure to the LGPL license (which would require you to open source your application if it’s a derivative work). Referencing any LGPL library is a legal decision, however, which should be made in consultation with your company’s policies and/or legal counsel. If you only need support for points, circles, and squares, you should leave out the optional JTS spatialContextFactory attribute when defining your geography field type.

JTS provides extremely powerful spatial functionality, in fact its C++ port, GEOS, forms a large part of the spatial functionalities of Postgis. So, if you have no issue with the licensing, you can do more or less anything once you have added JTS and spatial4J.

I have heard it stated at conferences, and seen discussion on the JTS mailing list, that JTS may eventually switch to a more liberal license, such as BSD, which would open it up for being added to many other NoSQL solutions as core, rather than optional add ons.

John Powell
  • 12,253
  • 6
  • 59
  • 67
  • Thanks John. I took a look to `geoshape` prior to ask here but as I said, i'd like to filter in a range above the given shape. I've just looked again, more deeply and I didn't found any `distance` parameter to extend the shape. The `geo_distance` used to be great but it only accepts a point... – Léo Aug 10 '14 at 18:22
  • Apologies if I misunderstood. The general point is that you might have to write some yourself, but that with JTS, elasticsearch/solr can now do fairly complex spatial operations. – John Powell Aug 10 '14 at 18:34
  • No worries John. `geo_shape` seems to be the key, but for now, obviously it doesn't support a distance factor. I'll have to grow the shape before querying but, as you can expect, it will considerably add points to the polygon(s), even when simplifying using the Douglas-Peucker algorithm. Anyway, thank you for your time and the given details. – Léo Aug 10 '14 at 19:17
  • 1
    Just as an aside, have you seen http://www.mapshaper.org/. There is an algorithm there, visvalingham, that does polygon simplification differently to Douglas-Peucker. See also, http://bost.ocks.org/mike/simplify/. Very interesting. – John Powell Aug 10 '14 at 19:20