On 3/10/14, 6:45 AM, "Javi" <javiersangra...@mitula.com> wrote:

>Hi all.
>
>I need your help! I have read every post about Spatial in Solr because I
>need to check if a point (latitude,longitude) is inside a Polygon.
>
>/**************/
>/* 1. library */
>/**************/
>
>(1) I use "jts-1.13.jar" and "spatial4j-0.4.1.jar"
>(I think they are the latest version)

You should only need to add JTS; spatial4j is included in Solr.  Where
exactly did you put it?

>
>/*****************/
>/* 2. schema.xml */
>/*****************/
><fieldType name="locationSolr"
>class="solr.SpatialRecursivePrefixTreeFieldType"               
>spatialContextFactory="com.spatial4j.core.context.jts.JtsSpatialContextFac
>to
>ry" distErrPct="0.025" maxDistErr="0.000009" units="degrees" />
>
>(I omit geo="true" because it is the default)
>
>...
>
><field name="LOCATION" type="locationSolr" indexed="true" stored="true"/>
>
>(Here I dont know what means if I add multiValued="true")

How many points might there be in this field for a given document?  0 or
1?  Don’t set multiValued=true but if you expect possibly more than 1 then
set it to true.

>
>/*********************/
>/* Document contents */
>/*********************/
>I have tried with 3 different content for my documents (lat-lon refers to
>Madrid, Spain):

Um…. Just to be absolutely sure, are you adding the data in Solr’s XML
format, which is this?:

XML Formatted Index Updates
<https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index
+Handlers#UploadingDatawithIndexHandlers-XMLFormattedIndexUpdates>


The examples you give below are the *output* XML format which is not the
same as the input format.  In particular you don’t give arrays of values
to Solr; you simply give more than one field element that has the same
name.

>
>
>a) As it is WKT format, I tried "longitude latitude" (x y)
><doc>
>    <arr name="LOCATION">
>      <str>-3.69278 40.442179</str>
>    </arr>
></doc>

That should work but I don’t recommend that, as a matter of taste, if all
your data is in latitude & longitude, as opposed to projected data or any
other spatial data.

>
>b) As it is WKT format, I tried "POINT(longitude latitude) (x y)
>
><doc>
>    <arr name="LOCATION">
>      <str>POINT(-3.69278 40.442179)</str>
>    </arr>
></doc>
>
>And

Again, that should work but see my comment above.

>
>c) I tried no WKT format by adding a comma and using "longitude,latitude"
>
><doc>
>    <arr name="LOCATION">
>      <str>40.442179,-3.69278</str>
>    </arr>
></doc>

That is *wrong*.  Remove the comma and it will then be okay.  But again,
see my earlier advise on lat & lon data.

>
>d) I tried no WKT format by adding a comma and using "latitude,longitude"
>
><doc>
>    <arr name="LOCATION">
>      <str>-3.69278,40.442179</str>
>    </arr>
></doc>

But that isn’t latitude then longitude of Madrid; you have it reversed.
“latitude,longitude” of Madrid is “40.442719,-3.69278”.

>
>/*****************/
>/* My solr query */
>/*****************/
>
>a) 
>_Description: This POLYGON (in WKT format, so "longitude latitude") is a
>triangle that cover Madrid at all, so my point would be inside them.
>_Result: Query return 0 documents (which is wrong).
> 
>http://localhost:8983/solr/pisos22/select?q=*%3A*&;
>fl=LOCATION&
>wt=xml&
>indent=true&
>fq=LOCATION:"IsWithin(POLYGON((
>-3.732605 40.531415,
>-3.856201 40.336993,
>-3.493652 40.332806,
>-3.732605 40.531415
>))) distErrPct=0"
>
>b) 
>_Descripcion: This POLYGON (in WKT format, so "longitude latitude") is a
>rectangle out of Madrid, so my point would not be inside them.
>_Result: Query return 0 documents (which is correct).
>
>http://localhost:8983/solr/pisos22/select?q=*%3A*&;
>fl=LOCATION&
>wt=xml&
>indent=true&
>fq=LOCATION:"IsWithin(POLYGON((
>-4.0594 40.8708,
>-4.0621 40.7211 ,
>-3.8095 40.7127,
>-3.8232 40.8687,
>-4.0594 40.8708
>))) distErrPct=0"
>
>***I also tried modifying the order of lat/lon but I am not able to find
>out 
>the solution to make it work.

The “x y” order looks good.  “IsWithin” should work but if all your
indexed data is points then use “Intersects” which is much faster.

As a sanity check can you simply do a {!geofilt} query with the “pt” set
to madrid and a hundred kilometers or whatever?

~ David

Reply via email to