/// <summary> /// Parses a string such as "Intersects(-10,20,-8,22) distErrPct=0.025". /// </summary> /// <param name="v"></param> /// <param name="ctx"></param> /// <returns></returns> public SpatialArgs Parse(String v, SpatialContext ctx) { int idx = v.IndexOf('('); int edx = v.LastIndexOf(')'); if (idx < 0 || idx > edx) { throw new ArgumentException("missing parens: " + v); } SpatialOperation op = SpatialOperation.Get(v.Substring(0, idx).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 ArgumentException("missing body : " + v); } Shape shape = new ShapeReadWriter(ctx).ReadShape(body); var args = new SpatialArgs(op, shape); if (v.Length > (edx + 1)) { body = v.Substring(edx + 1).Trim(); if (body.Length > 0) { Dictionary<String, String> aa = ParseMap(body); args.DistErrPct = ReadDouble(aa["distErrPct"]); aa.Remove(DIST_ERR_PCT); args.DistErr = ReadDouble(aa["distErr"]); aa.Remove(DIST_ERR); if (aa.Count != 0) { throw new ArgumentException("unused parameters: " + aa); } } } args.Validate(); return args; }
/// <summary> /// /// </summary> /// <param name="geo">Establishes geo vs cartesian / Euclidean.</param> /// <param name="calculator">Optional; defaults to Haversine or cartesian depending on units.</param> /// <param name="worldBounds">Optional; defaults to GEO_WORLDBOUNDS or MAX_WORLDBOUNDS depending on units.</param> public SpatialContext(bool geo, DistanceCalculator calculator, Rectangle worldBounds) { this.geo = geo; if (calculator == null) { calculator = IsGeo() ? (DistanceCalculator)new GeodesicSphereDistCalc.Haversine() : new CartesianDistCalc(); } this.calculator = calculator; if (worldBounds == null) { worldBounds = IsGeo() ? new RectangleImpl(-180, 180, -90, 90, this) : new RectangleImpl(-Double.MaxValue, Double.MaxValue,-Double.MaxValue, Double.MaxValue, this); } else { if (IsGeo()) Debug.Assert(worldBounds.Equals(new RectangleImpl(-180, 180, -90, 90, this))); if (worldBounds.GetCrossesDateLine()) throw new ArgumentException("worldBounds shouldn't cross dateline: " + worldBounds, "worldBounds"); } //hopefully worldBounds' rect implementation is compatible this.worldBounds = new RectangleImpl(worldBounds, this); shapeReadWriter = MakeShapeReadWriter(); }
protected List<Document> getDocuments(String testDataFile) { IEnumerator<SampleData> sampleData = getSampleData(testDataFile); var documents = new List<Document>(); while (sampleData.MoveNext()) { SampleData data = sampleData.Current; var document = new Document(); document.Add(new Field("id", data.id, Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("name", data.name, Field.Store.YES, Field.Index.ANALYZED)); Shape shape = new ShapeReadWriter(ctx).ReadShape(data.shape); shape = convertShapeFromGetDocuments(shape); if (shape != null) { foreach (var f in strategy.CreateIndexableFields(shape)) { document.Add(f); } if (storeShape) document.Add(new Field(strategy.GetFieldName(), ctx.ToString(shape), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); } documents.Add(document); } return documents; }
protected void Init(DistanceUnits units, DistanceCalculator calculator, Rectangle worldBounds) { if (units == null) throw new ArgumentException("units can't be null", "units"); this.units = units; if (calculator == null) { calculator = IsGeo() ? (DistanceCalculator)new GeodesicSphereDistCalc.Haversine(units.EarthRadius()) : new CartesianDistCalc(); } this.calculator = calculator; if (worldBounds == null) { worldBounds = IsGeo() ? GEO_WORLDBOUNDS : MAX_WORLDBOUNDS; } else { if (IsGeo()) Debug.Assert(new RectangleImpl(worldBounds).Equals(GEO_WORLDBOUNDS)); if (worldBounds.GetCrossesDateLine()) throw new ArgumentException("worldBounds shouldn't cross dateline: " + worldBounds, "worldBounds"); } //copy so we can ensure we have the right implementation worldBounds = MakeRect(worldBounds.GetMinX(), worldBounds.GetMaxX(), worldBounds.GetMinY(), worldBounds.GetMaxY()); this.worldBounds = worldBounds; shapeReadWriter = MakeShapeReadWriter(); this.maxCircleDistance = IsGeo() ? calculator.DegreesToDistance(180) : (double?)null; }