lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Randall Tidd>
Subject "Point in polygon" search with Lucene / Spatial4j / JTS
Date Sat, 04 Jun 2016 16:34:29 GMT

I have what I think is a relatively simple use case that I’d like to use Lucene to solve.
 We have a database of 100,000’s of data points each of which has a latitude and longitude.
 I’d like to index these and then be able to search for them with an arbitrary polygon.
 For example I would define an irregular polygon roughly encompassing northern San Francisco
and search for all points that are within that polygon.

I see some examples that are close to what I need but have some questions.  For example there
is this:

But 1) this is based on Lucene 4.x while the latest version is 6.x, and 2) it searches for
points within a circle, not a polygon. 

I would be happy to use Lucene 4.x if that provides the best support but that version is getting
old now and I wonder if there is better, easier support in later versions.

Doing some reading I see that Spatial4j and JTS provide support for polygons, but I can’t
figure out how to define a polygon that can be used with SpatialArgs.  I found com.vividsolutions.jts.geom.GeometryFactory.createPolygon()
but am not sure how to get that into a JTS Shape which can be used with SpatialArgs.  Basically
I’m getting lost in the 3 sets of API’s (Lucene, Spatial4j, and JTS) wondering how they
go together and can’t find an example.

I have also tried parsing arguments to create a polygon like this:

SpatialArgs args = new SpatialArgsParser().parse("IsWithin(POLYGON-122.515193 37.781561, -122.472924
37.809958, -122.383509 37.808795))", ctx);

But I get "java.text.ParseException: Unknown Shape definition” from,
evidently it is not correctly using JTS to create the polygon.  JTS is on my class path so
I’m not sure what is wrong there.

I see mention of some Solr and ElasticSearch solutions, which I believe would just use this
Lucene functionality underneath.  I’d be happy to use those if it were easier but seems
like I should be able to do this with just Lucene.

Here is the combination of toolkits I’m trying to use:

compile group: 'org.apache.lucene', name: 'lucene-core', version: '4.10.4'
compile group: 'org.apache.lucene', name: 'lucene-analyzers-common', version: '4.10.4'
compile group: 'org.apache.lucene', name: 'lucene-spatial', version: '4.10.4'
compile group: 'com.spatial4j', name: 'spatial4j', version: '0.5'
compile group: 'com.vividsolutions', name: 'jts', version: ‘1.13'

I’ve spent more time figuring this out than I thought I’d have to and think I must be
missing something obvious, and am wondering if someone can help me out.


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message