protected override Road GetRoadByIdCore(string roadId) { Feature feature = featureSource.GetFeatureById(roadId, ReturningColumnsType.NoColumns); LineBaseShape sourceShape = (LineBaseShape)feature.GetShape(); Collection <LineShape> lines = ConvertLineBaseShapeToLines(sourceShape); List <string> adjacentIDs = new List <string>(); foreach (LineShape line in lines) { Collection <Feature> features = featureSource.GetFeaturesInsideBoundingBox(line.GetBoundingBox(), ReturningColumnsType.NoColumns); foreach (Feature tempFeature in features) { BaseShape tempShape = tempFeature.GetShape(); if (feature.Id != tempFeature.Id && line.Intersects(tempShape)) { adjacentIDs.Add(tempFeature.Id); } } } //adjacentIDs.Sort(); PointShape center = sourceShape.GetCenterPoint(); float centerX = (float)center.X; float centerY = (float)center.Y; float length = (float)sourceShape.GetLength(GeographyUnit.Meter, DistanceUnit.Meter); Road road = new Road(roadId, RoadType.LocalRoad, length, adjacentIDs, centerX, centerY, roadId); return(road); }
private IEnumerable <Feature> InverseClip(FeatureSource featureSource, IEnumerable <Feature> clippingFeatures) { lock (featureSource) { if (!featureSource.IsOpen) { featureSource.Open(); } Collection <Feature> results = featureSource.GetFeaturesOutsideBoundingBox(ExtentHelper.GetBoundingBoxOfItems(clippingFeatures), ReturningColumnsType.AllColumns); Collection <Feature> sourceFeatures = new Collection <Feature>(); ShapeFileType shapeFileType = ((ShapeFileFeatureSource)featureSource).GetShapeFileType(); int index = 1; if (shapeFileType == ShapeFileType.Point || shapeFileType == ShapeFileType.Multipoint) { featureSource.Open(); Collection <Feature> allFeatures = featureSource.GetAllFeatures(ReturningColumnsType.AllColumns); featureSource.Close(); foreach (Feature f in results) { allFeatures.Remove(f); } foreach (var f in InverseClipPoints(allFeatures, clippingFeatures, shapeFileType)) { results.Add(f); } } else if (shapeFileType == ShapeFileType.Polyline) { bool isOpen = false; Projection tmpProjection = null; MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures)); if (featureSource.Projection != null && featureSource.Projection is GISEditorManagedProj4Projection && ((GISEditorManagedProj4Projection)featureSource.Projection).IsProjectionParametersEqual) { tmpProjection = featureSource.Projection; if (featureSource.IsOpen) { featureSource.Close(); featureSource.Projection.Close(); isOpen = true; } featureSource.Projection = null; } featureSource.Open(); featureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), ReturningColumnsType.AllColumns).ForEach(f => { if (!areaBaseShape.Contains(f)) { sourceFeatures.Add(f); } }); int count = sourceFeatures.Count; if (tmpProjection != null) { featureSource.Projection = tmpProjection; if (isOpen) { featureSource.Open(); } } if (featureSource.IsOpen) { featureSource.Close(); } foreach (var feature in sourceFeatures) { ReportProgress(index * 100 / count); index++; try { if (areaBaseShape.IsDisjointed(feature)) { results.Add(feature); } else { MultilineShape multiLine = (MultilineShape)feature.GetShape(); MultilineShape resultShape = new MultilineShape(); foreach (LineShape lineShape in multiLine.Lines) { if (areaBaseShape.IsDisjointed(lineShape)) { resultShape.Lines.Add(lineShape); } else { Collection <PointShape> points = new Collection <PointShape>(); points.Add(new PointShape(lineShape.Vertices[0])); lineShape.GetIntersection(areaBaseShape).Lines.ForEach(l => { PointShape p1 = new PointShape(l.Vertices[0]); if (points.Count(p => p.X == p1.X && p.Y == p1.Y && p.Z == p1.Z) <= 0) { points.Add(p1); } PointShape p2 = new PointShape(l.Vertices[l.Vertices.Count - 1]); if (points.Count(p => p.X == p2.X && p.Y == p2.Y && p.Z == p2.Z) <= 0) { points.Add(p2); } }); PointShape endPoint = new PointShape(lineShape.Vertices[lineShape.Vertices.Count - 1]); if (points.Count(p => p.X == endPoint.X && p.Y == endPoint.Y && p.Z == endPoint.Z) <= 0) { points.Add(endPoint); } for (int i = 0; i < points.Count; i++) { if (i != points.Count - 1) { LineBaseShape lineBaseShape = lineShape.GetLineOnALine(points[i], points[i + 1]); if (!areaBaseShape.Intersects(lineBaseShape.GetCenterPoint())) { resultShape.Lines.Add((LineShape)lineBaseShape); } } } } } if (resultShape != null && resultShape.Lines.Count > 0) { results.Add(new Feature(resultShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues)); } } } catch (Exception ex) { HandleExceptionFromInvalidFeature(feature.Id, ex.Message); } } } else if (shapeFileType == ShapeFileType.Polygon) { MultipolygonShape areaBaseShape = AreaBaseShape.Union(GetValidFeatures(clippingFeatures)); bool isOpen = false; Projection tmpProjection = null; if (featureSource.Projection != null && featureSource.Projection is GISEditorManagedProj4Projection && ((GISEditorManagedProj4Projection)featureSource.Projection).IsProjectionParametersEqual) { tmpProjection = featureSource.Projection; if (featureSource.IsOpen) { featureSource.Close(); featureSource.Projection.Close(); isOpen = true; } featureSource.Projection = null; } if (!featureSource.IsOpen) { featureSource.Open(); } featureSource.GetFeaturesInsideBoundingBox(areaBaseShape.GetBoundingBox(), ReturningColumnsType.AllColumns).ForEach(f => { if (!areaBaseShape.IsDisjointed(f)) { sourceFeatures.Add(f); } }); if (featureSource.IsOpen) { featureSource.Close(); } if (tmpProjection != null) { featureSource.Projection = tmpProjection; if (isOpen) { featureSource.Open(); } } int count = sourceFeatures.Count; foreach (var feature in sourceFeatures) { ReportProgress(index * 100 / count); index++; try { if (areaBaseShape.IsDisjointed(feature)) { results.Add(feature); } else { var clippedShape = ((AreaBaseShape)feature.GetShape()).GetDifference(areaBaseShape); if (clippedShape != null && clippedShape.Polygons.Count > 0) { results.Add(new Feature(clippedShape.GetWellKnownBinary(), feature.Id, feature.ColumnValues)); } } } catch (Exception ex) { HandleExceptionFromInvalidFeature(feature.Id, ex.Message); } } } else { throw new NotSupportedException("The ShapeFileType is not supported."); } return(results); } }