public Parse ( string v, |
||
v | string | The string to parse. Mandatory. |
ctx | The spatial context. Mandatory. | |
return |
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;
}
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 } }