don t
don t

Reputation: 87

Solr4 spatial troubleshooting polygons

I am trying to troubleshoot Solr4 and its spatial capabilities. I have a document indexed with a field “geo”.

In it I have the following

Latitude -34.7413

Longitude 149.7102

Stored as <geo>-34.741328,149.710238</>

I have also tried indexing the same document as

<geo> 149.710238 -34.741328 

which is suggested if you are not comma separating the values (reverse the lat/long order and put a space between the values).

The docs index just fine.

I am attempting to send it a polygon search. The polygon is a triangle shape and if you can imagine the below 3 points of the triangle are in clockwise order. It’s a very large triangle with the target coordinate in the middle somewhere. I chose a triangle in the hopes of a simple debug, I intend to have much more complicate shapes drawn for these searches

(-34.580136904223494, 149.6165823974609)

(-34.84203933395146, 150.0175833740234)

(-34.84654761634415, 149.4627738037109)

I have sent Solr the following queries and get errors.

fq=geo:"Intersects(POLYGON(-34.5801 149.6165, -34.8420 150.0175, -34.8465 149.4627))" 

My attempt at a triangle.

fq=geo:"Intersects(POLYGON(-34.5801 149.6165, -34.8420 150.0175, -34.8465 149.4627, -34.5801 149.6165))"

My attempt at a triangle adding the last co ordinate again in order to “close” the polygon. The above 2 queries return no results only the following error.

ERROR 500 Unable to read: POLYGON(-34.5801 149.6165, -34.8420 150.0175, -34.8465 149.4627, -34.5801 149.6165)

My question I guess is where to first look? I have JTS library installed. Solr Log records this.

19:11:50

SEVERE

SolrDispatchFilternull:com.spatial4j.core.exception.InvalidShapeException: Unable to read: POLYGON((-34.5801 149.6165, -34.8420 150.0175, -34.8465 149.4627))

Thanks for any pointers anyone can provide. I think the problem is either a solr config issue, somehow something isn’t installed right, the way im sending it coordinates and shapes, or something else I am missing.

Upvotes: 3

Views: 2407

Answers (2)

don t
don t

Reputation: 87

OK, so i think after days on this i fixed it with the help of David Smiley who pops up all over the internet helping myself and others with their Solr Spatial issues - If we ever meet I will pour many a beer into you - thanks for your help.

YOU must reverse the lat/long of each co-ord (long/lat instead) and it worked. Straight away. So.Simple.

fq=geo:"Intersects(POLYGON((149.4023 -34.6072, 149.4023 -34.8690, 149.9022 -34.8690, 149.9022 -34.6072, 149.4023 -34.6072)))" 

instead of this

fq=geo:"Intersects(POLYGON((-34.6072 149.4023, -34.8690 149.4023, -34.8690 149.9022, -34.6072 149.9022, -34.6072 149.4023)))" 

Hopefully this thread helps others.

When in doubt the things I tried to debug this where as follows. 1. Unpack the solr.war file and save it as a folder instead of a file. There are many ways to do this depending on your server. For amateurs like me, configserver explorer, a whm plugin, is your friend :). 2. Download the jts plugin, unzip it, inside the /lib/ folder there was a file jts-1.12.jar - this it the money file. 3. Upload this to your solrinstall/solr.war/WEB-INF/lib/ location - you know that your in the right place when you see many other .jar files in there 4. If you want you can repack the war file, or it should work unpacked like this. 5. Make sure your schema is EXACTLY as it says in the solr wiki here. It is not there by default. Check and check again. Add this

<fieldType name="location_rpt"   class="solr.SpatialRecursivePrefixTreeFieldType"
           spatialContextFactory="com.spatial4j.core.context.jts.JtsSpatialContextFactory"
           distErrPct="0.025"
           maxDistErr="0.000009"
           units="degrees"
        />

and add your field "geo" in the following manner.

<field name="geo"  type="location_rpt"  indexed="true" stored="true"  multiValued="true" />

Index your data. My data is in a Lat/long order and looks like this. geo : -34.741328,149.710238 Remember Lat=horizontal, Long=Vertical lines. 6. Test your search by using the Lucene method of searching. There is a very simple rectangular search you can do. Find your target documents GEO point, then find a co-ordinate on the bottom left hand corner, and the top right hand corner. All you need for a rectangle is 2 points and you simply do a range search. For me it looks like this.

fq=geo:[-34.8690,149.4023 TO -34.6072,149.9022]

If that works you have basic GEO searching working.

  1. Now lets make a polygon out of those co-ordinates. To make a polygon (rectangle shape in this case) we need 5 co-ords, the starting one, 3 other points of the rectangle, and then the starting co-ord again in order to close the shape(polygon). For whatever reason, polygon searches use a different lat/long layout. You must reverse this. You put the LONGITUDE FIRST. You must also construct this shape in COUNTER-CLOCKWISE format. So a polygon search using points constructed of the above co-ordinates looks like this.

    fq=geo:"Intersects(POLYGON((149.4023 -34.6072, 149.4023 -34.8690, 149.9022 -34.8690, 149.9022 -34.6072, 149.4023 -34.6072)))"

As you can see, the lat/long is long/lat, each long/lat is seperated by a space, and each co-ord pair is seperated by a comma ",".

Hopefully this thread helps others.

Upvotes: 3

David Smiley
David Smiley

Reputation: 4150

fq=geo:"Intersects(POLYGON(-34.5801 149.6165, -34.8420 150.0175, -34.8465 149.4627, -34.5801 149.6165))"

You almost had it right. Yes you need to close the polygon, and you did here. But you're missing an extra pair of parenthesis around the coordinate list:

fq=geo:"Intersects(POLYGON((-34.5801 149.6165, -34.8420 150.0175, -34.8465 149.4627, -34.5801 149.6165)))"

The extra parenthesis is because there could be multiple lists of coordinates. The first list is the outer ring, the subsequent lists are inner rings, AKA holes. This is standard WKT.

Upvotes: 2

Related Questions