public virtual void TestArgsParser() { SpatialArgsParser parser = new SpatialArgsParser(); String arg = SpatialOperation.IsWithin + "(Envelope(-10, 10, 20, -20))"; SpatialArgs @out = parser.Parse(arg, ctx); assertEquals(SpatialOperation.IsWithin, @out.Operation); IRectangle bounds = (IRectangle)@out.Shape; assertEquals(-10.0, bounds.MinX, 0D); assertEquals(10.0, bounds.MaxX, 0D); // Disjoint should not be scored arg = SpatialOperation.IsDisjointTo + " (Envelope(-10,-20,20,10))"; @out = parser.Parse(arg, ctx); assertEquals(SpatialOperation.IsDisjointTo, @out.Operation); try { parser.Parse(SpatialOperation.IsDisjointTo + "[ ]", ctx); fail("spatial operations need args"); } catch (Exception ex) {//expected } try { parser.Parse("XXXX(Envelope(-10, 10, 20, -20))", ctx); fail("unknown operation!"); } catch (Exception ex) {//expected } }
public virtual void TestNGramPrefixGridLosAngeles() { SpatialContext ctx = SpatialContext.GEO; TermQueryPrefixTreeStrategy prefixGridStrategy = new TermQueryPrefixTreeStrategy(new QuadPrefixTree(ctx), "geo"); Spatial4n.Core.Shapes.IShape point = ctx.MakePoint(-118.243680, 34.052230); Document losAngeles = new Document(); losAngeles.Add(new StringField("name", "Los Angeles", Field.Store.YES)); foreach (IndexableField field in prefixGridStrategy.CreateIndexableFields(point)) { losAngeles.Add(field); } losAngeles.Add(new StoredField(prefixGridStrategy.FieldName, point.toString()));//just for diagnostics addDocumentsAndCommit(Arrays.AsList(losAngeles)); // This won't work with simple spatial context... SpatialArgsParser spatialArgsParser = new SpatialArgsParser(); // TODO... use a non polygon query // SpatialArgs spatialArgs = spatialArgsParser.parse( // "Intersects(POLYGON((-127.00390625 39.8125,-112.765625 39.98828125,-111.53515625 31.375,-125.94921875 30.14453125,-127.00390625 39.8125)))", // new SimpleSpatialContext()); // Query query = prefixGridStrategy.makeQuery(spatialArgs, fieldInfo); // SearchResults searchResults = executeQuery(query, 1); // assertEquals(1, searchResults.numFound); }
public void testNGramPrefixGridLosAngeles() { SpatialContext ctx = SpatialContext.GEO; TermQueryPrefixTreeStrategy prefixGridStrategy = new TermQueryPrefixTreeStrategy(new QuadPrefixTree(ctx), "geo"); Shape point = ctx.MakePoint(-118.243680, 34.052230); Document losAngeles = new Document(); losAngeles.Add(new Field("name", "Los Angeles", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); foreach (var indexableField in prefixGridStrategy.CreateIndexableFields(point)) { losAngeles.Add(indexableField); } losAngeles.Add(new Field(prefixGridStrategy.GetFieldName(), ctx.ToString(point), Field.Store.YES, Field.Index.NO)); addDocumentsAndCommit(new List<Document> { losAngeles }); // This won't work with simple spatial context... SpatialArgsParser spatialArgsParser = new SpatialArgsParser(); // TODO... use a non polygon query // SpatialArgs spatialArgs = spatialArgsParser.parse( // "Intersects(POLYGON((-127.00390625 39.8125,-112.765625 39.98828125,-111.53515625 31.375,-125.94921875 30.14453125,-127.00390625 39.8125)))", // new SimpleSpatialContext()); // Query query = prefixGridStrategy.makeQuery(spatialArgs, fieldInfo); // SearchResults searchResults = executeQuery(query, 1); // assertEquals(1, searchResults.numFound); }
/** * Get Test Queries. The InputStream is closed. */ public static IEnumerator<SpatialTestQuery> GetTestQueries( SpatialArgsParser parser, SpatialContext ctx, string name, Stream @in) { List<SpatialTestQuery> results = new List<SpatialTestQuery>(); TextReader bufInput = new StreamReader(@in, Encoding.UTF8); try { String line; for (int lineNumber = 1; (line = bufInput.ReadLine()) != null; lineNumber++) { SpatialTestQuery test = new SpatialTestQuery(); test.line = line; test.lineNumber = lineNumber; try { // skip a comment if (line.StartsWith("[", StringComparison.Ordinal)) { int idx2 = line.IndexOf(']'); if (idx2 > 0) { line = line.Substring(idx2 + 1); } } int idx = line.IndexOf('@'); StringTokenizer st = new StringTokenizer(line.Substring(0, idx - 0)); while (st.HasMoreTokens()) { test.ids.Add(st.NextToken().Trim()); } test.args = parser.Parse(line.Substring(idx + 1).Trim(), ctx); results.Add(test); } catch (Exception ex) { throw new ApplicationException("invalid query line: " + test.line, ex); } } } finally { bufInput.Dispose(); } return results.GetEnumerator(); }
public virtual void TestArgsParser() { SpatialArgsParser parser = new SpatialArgsParser(); String arg = SpatialOperation.IsWithin + "(Envelope(-10, 10, 20, -20))"; SpatialArgs @out = parser.Parse(arg, ctx); assertEquals(SpatialOperation.IsWithin, @out.Operation); IRectangle bounds = (IRectangle)@out.Shape; assertEquals(-10.0, bounds.MinX, 0D); assertEquals(10.0, bounds.MaxX, 0D); // Disjoint should not be scored arg = SpatialOperation.IsDisjointTo + " (Envelope(-10,-20,20,10))"; @out = parser.Parse(arg, ctx); assertEquals(SpatialOperation.IsDisjointTo, @out.Operation); try { parser.Parse(SpatialOperation.IsDisjointTo + "[ ]", ctx); fail("spatial operations need args"); } #pragma warning disable 168 catch (Exception ex) #pragma warning restore 168 {//expected } try { parser.Parse("XXXX(Envelope(-10, 10, 20, -20))", ctx); fail("unknown operation!"); } #pragma warning disable 168 catch (Exception ex) #pragma warning restore 168 {//expected } }
public void TestArgParser() { SpatialArgsParser parser = new SpatialArgsParser(); String arg = SpatialOperation.IsWithin + "(-10 -20 10 20)"; SpatialArgs outValue = parser.Parse(arg, ctx); Assert.AreEqual(SpatialOperation.IsWithin, outValue.Operation); Rectangle bounds = (Rectangle)outValue.Shape; Assert.AreEqual(-10.0, bounds.GetMinX(), 0D); Assert.AreEqual(10.0, bounds.GetMaxX(), 0D); // Disjoint should not be scored arg = SpatialOperation.IsDisjointTo + " (-10 10 -20 20)"; outValue = parser.Parse(arg, ctx); Assert.AreEqual(SpatialOperation.IsDisjointTo, outValue.Operation); try { parser.Parse(SpatialOperation.IsDisjointTo + "[ ]", ctx); Assert.True(false, "spatial operations need args"); } catch (Exception) { //expected } try { parser.Parse("XXXX(-10 10 -20 20)", ctx); Assert.True(false, "unknown operation!"); } catch (Exception) { //expected } }
public override string ToString() { return(SpatialArgsParser.WriteSpatialArgs(this)); }
private void Search() { IndexReader indexReader = DirectoryReader.Open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Sort idSort = new Sort(new SortField("id", SortField.Type_e.INT)); //--Filter by circle (<= distance from a point) { //Search with circle //note: SpatialArgs can be parsed from a string SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.MakeCircle(-80.0, 33.0, DistanceUtils.Dist2Degrees(200, DistanceUtils.EARTH_MEAN_RADIUS_KM))); Filter filter = strategy.MakeFilter(args); TopDocs docs = indexSearcher.Search(new MatchAllDocsQuery(), filter, 10, idSort); AssertDocMatchedIds(indexSearcher, docs, 2); //Now, lets get the distance for the 1st doc via computing from stored point value: // (this computation is usually not redundant) Document doc1 = indexSearcher.Doc(docs.ScoreDocs[0].Doc); String doc1Str = doc1.GetField(strategy.FieldName).StringValue; //assume doc1Str is "x y" as written in newSampleDocument() int spaceIdx = doc1Str.IndexOf(' '); double x = double.Parse(doc1Str.Substring(0, spaceIdx - 0), CultureInfo.InvariantCulture); double y = double.Parse(doc1Str.Substring(spaceIdx + 1), CultureInfo.InvariantCulture); double doc1DistDEG = ctx.CalcDistance(args.Shape.Center, x, y); assertEquals(121.6d, DistanceUtils.Degrees2Dist(doc1DistDEG, DistanceUtils.EARTH_MEAN_RADIUS_KM), 0.1); //or more simply: assertEquals(121.6d, doc1DistDEG * DistanceUtils.DEG_TO_KM, 0.1); } //--Match all, order by distance ascending { IPoint pt = ctx.MakePoint(60, -50); ValueSource valueSource = strategy.MakeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM);//the distance (in km) Sort distSort = new Sort(valueSource.GetSortField(false)).Rewrite(indexSearcher);//false=asc dist TopDocs docs = indexSearcher.Search(new MatchAllDocsQuery(), 10, distSort); AssertDocMatchedIds(indexSearcher, docs, 4, 20, 2); //To get the distance, we could compute from stored values like earlier. // However in this example we sorted on it, and the distance will get // computed redundantly. If the distance is only needed for the top-X // search results then that's not a big deal. Alternatively, try wrapping // the ValueSource with CachingDoubleValueSource then retrieve the value // from the ValueSource now. See LUCENE-4541 for an example. } //demo arg parsing { SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.MakeCircle(-80.0, 33.0, 1)); SpatialArgs args2 = new SpatialArgsParser().Parse("Intersects(BUFFER(POINT(-80 33),1))", ctx); assertEquals(args.toString(), args2.toString()); } indexReader.Dispose(); }