19 using System.Collections.Generic;
21 using Spatial4n.Core.Context;
22 using Spatial4n.Core.Io;
23 using Spatial4n.Core.Shapes;
25 namespace Lucene.Net.Spatial.Queries
29 public const String DIST_ERR_PCT =
"distErrPct";
30 public const String DIST_ERR =
"distErr";
37 public static String WriteSpatialArgs(SpatialArgs args)
39 var str =
new StringBuilder();
40 str.Append(args.Operation.GetName());
42 str.Append(args.Shape);
43 if (args.DistErrPct != null)
44 str.Append(
" distErrPct=").Append(String.Format(
"{0:0.00}%", args.DistErrPct*100d));
45 if (args.DistErr != null)
46 str.Append(
" distErr=").Append(args.DistErr);
48 return str.ToString();
57 public SpatialArgs Parse(String v, SpatialContext ctx)
59 int idx = v.IndexOf(
'(');
60 int edx = v.LastIndexOf(
')');
62 if (idx < 0 || idx > edx)
64 throw new ArgumentException(
"missing parens: " + v);
71 String body = v.Substring(idx + 1, edx - (idx + 1)).Trim();
74 throw new ArgumentException(
"missing body : " + v);
77 Shape shape =
new ShapeReadWriter(ctx).ReadShape(body);
78 var args =
new SpatialArgs(op, shape);
80 if (v.Length > (edx + 1))
82 body = v.Substring(edx + 1).Trim();
85 Dictionary<String, String> aa = ParseMap(body);
86 args.DistErrPct = ReadDouble(aa[
"distErrPct"]); aa.Remove(DIST_ERR_PCT);
87 args.DistErr = ReadDouble(aa[
"distErr"]); aa.Remove(DIST_ERR);
90 throw new ArgumentException(
"unused parameters: " + aa);
98 protected static double? ReadDouble(String v)
101 return double.TryParse(v, out val) ? val : (
double?)null;
104 protected static bool ReadBool(String v,
bool defaultValue)
107 return bool.TryParse(v, out ret) ? ret : defaultValue;
116 protected static Dictionary<String, String> ParseMap(String body)
118 var map =
new Dictionary<String, String>();
120 var st = body.Split(
new[] {
' ',
'\n',
'\t'}, StringSplitOptions.RemoveEmptyEntries);
121 while (tokenPos < st.Length)
123 String a = st[tokenPos++];
124 int idx = a.IndexOf(
'=');
127 String k = a.Substring(0, idx);
128 String v = a.Substring(idx + 1);