/// <summary> /// Adds an index to this class internally to compute spatial relations faster. In NTS this /// is called a <see cref="IPreparedGeometry"/>. This /// isn't done by default because it takes some time to do the optimization, and it uses more /// memory. Calling this method isn't thread-safe so be careful when this is done. If it was /// already indexed then nothing happens. /// </summary> public virtual void Index() { if (preparedGeometry == null) { preparedGeometry = PreparedGeometryFactory.Prepare(geom); } }
private IPolygon GetResult() { IGeometryFactory gf = _poly.Factory; ILinearRing shell = (ILinearRing)_poly.ExteriorRing; IPreparedGeometry shellPrep = PreparedGeometryFactory.Prepare(gf.CreatePolygon(shell)); IList <IGeometry> holes = new List <IGeometry>(); for (int i = 0; i < _poly.NumInteriorRings; i++) { IGeometry hole = _poly.GetInteriorRingN(i); if (shellPrep.Covers(hole)) { holes.Add(hole); } } // all holes valid, so return original if (holes.Count == _poly.NumInteriorRings) { return(_poly); } // return new polygon with covered holes only ILinearRing[] arr = GeometryFactory.ToLinearRingArray(holes); IPolygon result = gf.CreatePolygon(shell, arr); return(result); }
/// <summary> /// Returns all geometries in the index that intersects with the given geometry /// </summary> /// <param name="geom"></param> /// <returns></returns> public IEnumerable <IndexEntry <T> > STIntersects(Geometry geom, T exclude = default) { var(trieMapSearchResult, _) = _geohashSpatialIndex.Query(geom, exclude: exclude); var preparedGeom = PreparedGeometryFactory.Prepare(geom); return(trieMapSearchResult.Where(ie => preparedGeom.Intersects(ie.Geom)).ToList()); }
///<inheritdoc/> public IEnumerable <IndexEntry <T> > STContainsProperly(Geometry geom, T exclude = default) { var(trieMapSearchResult, _) = _geohashSpatialIndex.Query(geom, minimumHits: 0, exclude: exclude); var preparedGeom = PreparedGeometryFactory.Prepare(geom); return(trieMapSearchResult.Where(ie => preparedGeom.ContainsProperly(ie.Geom)).ToList()); }
public override void Setup() { var sinePoly = CreateSineStar(new Coordinate(0, 0), 100000.0, nPts); _preparedGeometry = PreparedGeometryFactory.Prepare(sinePoly); WaitHandles = new WaitHandle[ThreadTestRunner.DefaultThreadCount]; }
private static void CheckAllPrepOps(IGeometry g1, IGeometry g2) { var prepGeom = PreparedGeometryFactory.Prepare(g1); CheckIntersects(prepGeom, g2); CheckContains(prepGeom, g2); CheckContainsProperly(prepGeom, g2); CheckCovers(prepGeom, g2); }
private static bool CheckContains(IGeometry target, IGeometry test) { bool expectedResult = target.Contains(test); PreparedGeometryFactory pgFact = new PreparedGeometryFactory(); IPreparedGeometry prepGeom = pgFact.Create(target); bool prepResult = prepGeom.Contains(test); if (prepResult != expectedResult) { return(false); } return(true); }
private static bool CheckIntersects(IGeometry target, IGeometry test) { bool expectedResult = target.Intersects(test); var pgFact = new PreparedGeometryFactory(); var prepGeom = pgFact.Create(target); bool prepResult = prepGeom.Intersects(test); if (prepResult != expectedResult) { return(false); } return(true); }
public static List <IGeometry> GetNearGeometries(IGeometry me, Quadtree <IGeometry> others, double buffer) { var bufferedGeom = me.Buffer(0); var preparedGeometry = PreparedGeometryFactory.Prepare(bufferedGeom); var geometries = new List <IGeometry>(); foreach (var geom in others.Query(bufferedGeom.EnvelopeInternal)) { var poly = geom as IPolygon; if (poly != null && preparedGeometry.Overlaps(poly.ExteriorRing)) { geometries.Add(poly); } } return(geometries); }
public void TestResultsEqual(IGeometry g, ILineString line) { bool slowIntersects = g.Intersects(line); var pgFact = new PreparedGeometryFactory(); var prepGeom = pgFact.Create(g); bool fastIntersects = prepGeom.Intersects(line); if (slowIntersects != fastIntersects) { Console.WriteLine(line); Console.WriteLine("Slow = " + slowIntersects + ", Fast = " + fastIntersects); throw new Exception("Different results found for intersects() !"); } }
private static int TestPrepGeomCached(IGeometry g, IEnumerable <IGeometry> lines) { Console.WriteLine("Using cached Prepared Geometry"); var pgFact = new PreparedGeometryFactory(); var prepGeom = pgFact.Create(g); int count = 0; foreach (var line in lines) { if (prepGeom.Intersects(line)) { count++; } } return(count); }
private void AddParishNames(List <Feature> features, string fieldname) { int featureNum = 0; int originalToSearch = 0; int lastSaved = int.MaxValue; int featuresCount = features.Count(); foreach (Feature f in features) { featureNum++; if (f.Attributes[fieldname] == null || f.Attributes[fieldname].ToString().Length == 0) { Console.WriteLine("Parish is null?? type_code:" + f.Attributes["TYPE_CODE"]); // f.Attributes["TYPE_CODE"] = "FA" } IPreparedGeometry geom = PreparedGeometryFactory.Prepare(f.Geometry); int count = 0; IEnumerable <OS50kGazetteer> toSearch = OS50k.Where(x => x.ParishName == null || x.ParishName.Length == 0); if (originalToSearch == 0) { originalToSearch = toSearch.Count(); } foreach (OS50kGazetteer os50k in toSearch) { if (geom.Intersects(os50k.BufferedPoint)) { os50k.ParishName = (string)f.Attributes[fieldname]; if (os50k.ParishName.EndsWith(" CP")) { os50k.ParishName = os50k.ParishName.Substring(0, os50k.ParishName.Length - 3); } count++; } } int left = toSearch.Count() - count; textBox1.AppendText("Set " + count + " entries for parish: " + f.Attributes[fieldname] + " number " + featureNum + " / " + featuresCount + " leaving " + left + " of " + originalToSearch + " to search\n"); if (lastSaved - 2000 > left) { lastSaved = left; SaveOS50kGazetteer(); } Application.DoEvents(); } }
/// <summary> /// Tests using PreparedGeometry, but creating a new /// PreparedGeometry object each time. /// This tests whether there is a penalty for using /// the PG algorithm as a complete replacement for /// the original algorithm. /// </summary> /// <param name="g">The polygonal test geometry</param> /// <param name="lines">The lines to test for intersection with <paramref name="g"/></param> /// <returns>The count</returns> private static int TestPrepGeomNotCached(IGeometry g, IEnumerable <IGeometry> lines) { Console.WriteLine("Using NON-CACHED Prepared Geometry"); var pgFact = new PreparedGeometryFactory(); // PreparedGeometry prepGeom = pgFact.create(g); int count = 0; foreach (var line in lines) { // test performance of creating the prepared geometry each time var prepGeom = pgFact.Create(g); if (prepGeom.Intersects(line)) { count++; } } return(count); }
public static bool Intersects(IGeometry g1, IGeometry g2) { var prepGeom = PreparedGeometryFactory.Prepare(g1); return(prepGeom.Intersects(g2)); }
public static bool Covers(Geometry g1, Geometry g2) { var prepGeom = PreparedGeometryFactory.Prepare(g1); return(prepGeom.Covers(g2)); }
public static bool ContainsProperly(Geometry g1, Geometry g2) { var prepGeom = PreparedGeometryFactory.Prepare(g1); return(prepGeom.ContainsProperly(g2)); }
public IEnumerable <LineString> GetResult() { while (_processedIndex < _inputLines.Count) { var line = _inputLines[_processedIndex]; if (line != null) { Envelope e; Coordinate pt; // StartPoint pt = line.StartPoint.Coordinate; e = new Envelope(pt); e.ExpandBy(EnvelopeBuffer); var candidates = _tree.Query(e); if (candidates.Count > 0) { var p = PreparedGeometryFactory.Prepare(line.Factory.ToGeometry(e)); foreach (var candidate in candidates) { if (candidate == line) { continue; } if (candidate.UserData == null) { continue; } if (!p.Intersects(candidate)) { continue; } NodeAt(candidate, pt); } } // EndPoint pt = line.EndPoint.Coordinate; e = new Envelope(pt); e.ExpandBy(EnvelopeBuffer); candidates = _tree.Query(e); if (candidates.Count > 0) { var p = PreparedGeometryFactory.Prepare(line.Factory.ToGeometry(e)); foreach (var candidate in candidates) { if (candidate == line) { continue; } if (candidate.UserData == null) { continue; } if (!p.Intersects(candidate)) { continue; } NodeAt(candidate, pt); } } } // Increment _processedIndex++; } foreach (var lineString in _inputLines) { if (lineString != null) { yield return(lineString); } } }