Lucene.Net.Spatial.Queries.SpatialArgsParser.Parse C# (CSharp) Method

Parse() public method

Parses a string such as "Intersects(ENVELOPE(-10,-8,22,20)) distErrPct=0.025".
if the parameters don't make sense or an add-on parameter is unknown If there is a problem parsing the string When the coordinates are invalid for the shape
public Parse ( string v, SpatialContext ctx ) : SpatialArgs
v string The string to parse. Mandatory.
ctx Spatial4n.Core.Context.SpatialContext The spatial context. Mandatory.
return SpatialArgs
        public virtual SpatialArgs Parse(string v, SpatialContext ctx)
        {
            int idx = v.IndexOf('(');
            int edx = v.LastIndexOf(')');

            if (idx < 0 || idx > edx)
            {
                throw new ParseException("missing parens: " + v, -1);
            }

            SpatialOperation op = SpatialOperation.Get(v.Substring(0, idx - 0).Trim());

            //Substring in .NET is (startPosn, length), But in Java it's (startPosn, endPosn)
            //see http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#substring(int, int)
            string body = v.Substring(idx + 1, edx - (idx + 1)).Trim();
            if (body.Length < 1)
            {
                throw new ParseException("missing body : " + v, idx + 1);
            }

            var shape = ParseShape(body, ctx);
            var args = NewSpatialArgs(op, shape);

            if (v.Length > (edx + 1))
            {
                body = v.Substring(edx + 1).Trim();
                if (body.Length > 0)
                {
                    IDictionary<string, string> aa = ParseMap(body);
                    ReadNameValuePairs(args, aa);
                    if (!aa.Any())
                    {
                        throw new ArgumentException("unused parameters: " + aa);
                    }
                }
            }
            args.Validate();
            return args;
        }

Usage Example

コード例 #1
0
        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
            }
        }
All Usage Examples Of Lucene.Net.Spatial.Queries.SpatialArgsParser::Parse