public string GetLengthOfFeature(Map map, GeoCollection <object> args) { PointShape point = new PointShape(Convert.ToDouble(args[0]), Convert.ToDouble(args[1])); FeatureLayer worldLayer = (FeatureLayer)map.StaticOverlay.Layers["RoadLayer"]; InMemoryFeatureLayer streetLayer = (InMemoryFeatureLayer)map.DynamicOverlay.Layers["StreetLayer"]; streetLayer.InternalFeatures.Clear(); worldLayer.Open(); Collection <Feature> selectedFeatures = worldLayer.QueryTools.GetFeaturesNearestTo(point, GeographyUnit.DecimalDegree, 1, new string[1] { "FENAME" }); worldLayer.Close(); string popupContentHtml = string.Empty; if (selectedFeatures.Count > 0) { LineBaseShape lineShape = (LineBaseShape)selectedFeatures[0].GetShape(); double length = lineShape.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter); string contentHtml = "<span style='color:red'>{0}</span> has a length of <span style='color:red'>{1:N0}</span> meters."; string information = string.Format(contentHtml, selectedFeatures[0].ColumnValues["FENAME"].Trim(), length); popupContentHtml = "<div style='font-size:10px; font-family:verdana; padding:4px;'>" + information + "</div>"; streetLayer.InternalFeatures.Add("Street", new Feature(lineShape)); } return(popupContentHtml); }
private void AndroidMapMapSingleTap(object sender, Android.Views.MotionEvent e) { PointF location = new PointF(e.GetX(), e.GetY()); PointShape position = ExtentHelper.ToWorldCoordinate(androidMap.CurrentExtent, location.X, location.Y, androidMap.Width, androidMap.Height); LayerOverlay overlay = androidMap.Overlays["RoadOverlay"] as LayerOverlay; FeatureLayer roadLayer = overlay.Layers["TXlkaA40"] as FeatureLayer; LayerOverlay highlightOverlay = androidMap.Overlays["HighlightOverlay"] as LayerOverlay; InMemoryFeatureLayer highlightLayer = (InMemoryFeatureLayer)highlightOverlay.Layers["HighlightLayer"]; roadLayer.Open(); Collection <Feature> selectedFeatures = roadLayer.QueryTools.GetFeaturesNearestTo(position, GeographyUnit.DecimalDegree, 1, new string[1] { "fename" }); roadLayer.Close(); if (selectedFeatures.Count > 0) { LineBaseShape lineShape = (LineBaseShape)selectedFeatures[0].GetShape(); highlightLayer.Open(); highlightLayer.InternalFeatures.Clear(); highlightLayer.InternalFeatures.Add(new Feature(lineShape)); highlightLayer.Close(); double length = lineShape.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter); string lengthMessage = string.Format(CultureInfo.InvariantCulture, "{0} has a length of {1:F2} meters.", selectedFeatures[0].ColumnValues["fename"].Trim(), length); messageLabel.Text = lengthMessage; highlightOverlay.Refresh(); } }
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 static LineShape GetLineShape(Feature feature) { LineBaseShape road = feature.GetShape() as LineBaseShape; LineShape roadSegment = road as LineShape; if (roadSegment == null) { roadSegment = ((MultilineShape)road).Lines[0]; } roadSegment.Id = feature.Id; return(roadSegment); }
private static Collection <LineShape> ConvertLineBaseShapeToLines(LineBaseShape sourceShape) { Collection <LineShape> lines = null; LineShape lineShape = sourceShape as LineShape; if (lineShape != null) { lines = new Collection <LineShape>(); lines.Add(lineShape); } else { lines = ((MultilineShape)sourceShape).Lines; } return(lines); }
protected override RouteSegment GetRouteSegmentByFeatureIdCore(string roadId) { Feature feature = featureSource.GetFeatureById(roadId, ReturningColumnsType.NoColumns); LineBaseShape shape = feature.GetShape() as LineBaseShape; LineShape sourceShape = ConvertLineBaseShapeToLines(shape)[0]; sourceShape.Id = feature.Id; Collection <string> startIds = new Collection <string>(); Collection <string> endIds = new Collection <string>(); GetAdjacentFeataureIds(featureSource, sourceShape, startIds, endIds); PointShape startPoint = new PointShape(sourceShape.Vertices[0]); PointShape endPoint = new PointShape(sourceShape.Vertices[sourceShape.Vertices.Count - 1]); float length = (float)sourceShape.GetLength(GeographyUnit.Meter, DistanceUnit.Meter); RouteSegment road = new RouteSegment(feature.Id, 0, length, startPoint, startIds, endPoint, endIds); return(road); }
private void winformsMap1_MapClick(object sender, MapClickWinformsMapEventArgs e) { FeatureLayer worldLayer = winformsMap1.FindFeatureLayer("RoadLayer"); // Find the road the user clicked on. worldLayer.Open(); Collection <Feature> selectedFeatures = worldLayer.QueryTools.GetFeaturesNearestTo(e.WorldLocation, GeographyUnit.DecimalDegree, 1, new string[1] { "FENAME" }); worldLayer.Close(); //Determine the length of the road. if (selectedFeatures.Count > 0) { LineBaseShape lineShape = (LineBaseShape)selectedFeatures[0].GetShape(); double length = lineShape.GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter); MessageBox.Show(string.Format(CultureInfo.InvariantCulture, "{0} has a length of {1:F2} meters.", selectedFeatures[0].ColumnValues["FENAME"].Trim(), length), "Length", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0); } }
internal static Collection <LineShape> GetLineShapes(Feature feature) { Collection <LineShape> lineShapes = new Collection <LineShape>(); LineBaseShape lineBaseShape = feature.GetShape() as LineBaseShape; if (lineBaseShape is MultilineShape) { MultilineShape multiLineShape = lineBaseShape as MultilineShape; lineShapes = multiLineShape.Lines; } else if (lineBaseShape is LineShape) { lineShapes.Add((LineShape)lineBaseShape); } else { throw new NotSupportedException("Only the data which includes the line shapes is supported."); } return(lineShapes); }
private void mapView_MapClick(object sender, MapClickMapViewEventArgs e) { FeatureLayer worldLayer = mapView.FindFeatureLayer("RoadLayer"); InMemoryFeatureLayer highlightLayer = (InMemoryFeatureLayer)mapView.FindFeatureLayer("HighlightLayer"); Overlay highlightOverlay = mapView.Overlays["HighlightOverlay"]; // Find the road the user clicked on. worldLayer.Open(); Collection <Feature> selectedFeatures = worldLayer.QueryTools.GetFeaturesNearestTo(e.WorldLocation, GeographyUnit.Meter, 1, new string[1] { "FENAME" }); worldLayer.Close(); //Determine the length of the road. if (selectedFeatures.Count > 0) { LineBaseShape lineShape = (LineBaseShape)selectedFeatures[0].GetShape(); highlightLayer.Open(); highlightLayer.InternalFeatures.Clear(); highlightLayer.InternalFeatures.Add(new Feature(lineShape)); highlightLayer.Close(); ProjectionConverter project = new ProjectionConverter(3857, 4326); project.Open(); double length = ((LineBaseShape)project.ConvertToExternalProjection(lineShape)).GetLength(GeographyUnit.DecimalDegree, DistanceUnit.Meter); project.Close(); string lengthMessage = string.Format(CultureInfo.InvariantCulture, "{0} has a length of {1:F2} meters.", selectedFeatures[0].ColumnValues["FENAME"].Trim(), length); Popup popup = new Popup(e.WorldLocation); popup.Content = lengthMessage; PopupOverlay popupOverlay = (PopupOverlay)mapView.Overlays["PopupOverlay"]; popupOverlay.Popups.Clear(); popupOverlay.Popups.Add(popup); highlightOverlay.Refresh(); popupOverlay.Refresh(); } }
private void WriteFeaturesIntoQueue(Collection <Feature> features, FeatureSource featureSource) { foreach (Feature feature in features) { Collection <LineShape> processingLineShapes = GeometryHelper.GetLineShapes(feature); // Get the lineshape of the processing feature. foreach (LineShape processingLineShape in processingLineShapes) { // Define a variable to save the points where the adjacent lines intersect with current processing line. Collection <PointShape> crossingPoints = new Collection <PointShape>(); // Get all the lines in current processing shape bounds. Collection <Feature> adjacentFeatures = featureSource.GetFeaturesInsideBoundingBox(processingLineShape.GetBoundingBox(), ReturningColumnsType.NoColumns); // Loop and see if the queried shape is intersected with processing shape. foreach (Feature adjacentFeature in adjacentFeatures) { LineBaseShape adjacentLineShape = adjacentFeature.GetShape() as LineBaseShape; MultipointShape tempCrossingPoints = processingLineShape.GetCrossing(adjacentLineShape); // The queried shape is intersected with processing shape. foreach (PointShape point in tempCrossingPoints.Points) { bool hasAdded = false; foreach (var item in crossingPoints) { if (point.X == item.X && point.Y == item.Y) { hasAdded = true; break; } } if (!hasAdded) { crossingPoints.Add(point); } } } // Order the crossing points following the sequence of line vertex. Collection <FlagedVertex> vertecesOfNewLine = GeometryHelper.AddCrossingPointToLine(processingLineShape, crossingPoints); Collection <Vertex> verteces = new Collection <Vertex>(); Collection <Feature> lineFeatures = new Collection <Feature>(); foreach (var vertex in vertecesOfNewLine) { verteces.Add(vertex.Vertex); if (vertex.Flag) { if (verteces.Count >= 2) { LineShape segment = new LineShape(verteces); lineFeatures.Add(new Feature(segment, feature.ColumnValues)); verteces.RemoveAt(0); } } } if (lineFeatures.Count > 0) { queue.Enqueue(lineFeatures); } } #if DEBUG Console.WriteLine(string.Format("Done {0} in {1}", feature.Id, features.Count)); #endif } }
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); } }
private void btnCalculate_Click(object sender, RoutedEventArgs e) { //Get a reference to the results MapShapeLayer and clear it so we can display our new results. MapShapeLayer results = (MapShapeLayer)((LayerOverlay)mapView.Overlays["layerOverlay"]).Layers["Results"]; results.MapShapes.Clear(); //Get a reference to the test data layer. InMemoryFeatureLayer testDataLayer = (InMemoryFeatureLayer)((LayerOverlay)mapView.Overlays["layerOverlay"]).Layers["TestData"]; //Check to see if the test data has been edited. if (mapView.EditOverlay.EditShapesLayer.InternalFeatures.Count > 0) { //Loop through the edited test data and update the test data layer. testDataLayer.EditTools.BeginTransaction(); foreach (Feature feature in mapView.EditOverlay.EditShapesLayer.InternalFeatures) { testDataLayer.EditTools.Update(feature.CloneDeep()); } testDataLayer.EditTools.CommitTransaction(); //Clear out the EditOverlay since we are done editing. mapView.EditOverlay.EditShapesLayer.InternalFeatures.Clear(); //Since we are done editing, turn visibility back on the for the layer overlay so we can see our test data again. mapView.Overlays["layerOverlay"].IsVisible = true; } //Get the test point feature from the test data layer. Feature testPointFeature = testDataLayer.QueryTools.GetFeaturesByColumnValue("Name", "TestPoint", ReturningColumnsType.AllColumns)[0]; //Get the test line feature from the test data layer. Feature testLineFeature = testDataLayer.QueryTools.GetFeaturesByColumnValue("Name", "TestLine", ReturningColumnsType.AllColumns)[0]; //Take the test line feature and create a line shape so we can use line-specific APIs for finding the shortest line and line-on-line. LineShape testLine = new MultilineShape(testLineFeature.GetWellKnownText()).Lines[0]; //Calculate the shortest line between our test line and test point. LineShape shortestLineResult = testLine.GetShortestLineTo(testPointFeature.GetShape(), GeographyUnit.Meter).Lines[0]; //Take the result and create a MapShape so we can display the shortest line on the map in a blue color. MapShape shortestLine = new MapShape(new Feature(shortestLineResult)); shortestLine.ZoomLevels.ZoomLevel01.DefaultLineStyle = LineStyle.CreateSimpleLineStyle(GeoColors.Blue, 4, false); shortestLine.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; results.MapShapes.Add("ShortestLine", shortestLine); string message = "Length of Blue Line is: " + shortestLineResult.GetLength(GeographyUnit.Meter, DistanceUnit.Kilometer).ToString() + " KM" + '\n'; //Split the test line from the left side based on where the shortest line touches it. LineBaseShape leftSideOfLineResult = testLine.GetLineOnALine(new PointShape(testLine.Vertices[0]), new PointShape(shortestLineResult.Vertices[0])); //Make sure the split was valid and the closest point wasn't at the beginning or end of the line. if (leftSideOfLineResult.Validate(ShapeValidationMode.Simple).IsValid) { MapShape leftSideOfLine = new MapShape(new Feature(leftSideOfLineResult)); leftSideOfLine.ZoomLevels.ZoomLevel01.DefaultLineStyle = LineStyle.CreateSimpleLineStyle(GeoColors.Green, 2, false); leftSideOfLine.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; results.MapShapes.Add("LeftSideofLine", leftSideOfLine); message += "Length of Green Line is: " + leftSideOfLineResult.GetLength(GeographyUnit.Meter, DistanceUnit.Kilometer).ToString() + " KM" + '\n'; } //Split the test line from the right side based on where the shortest line touches it. LineBaseShape rightSideOfLineResult = testLine.GetLineOnALine(new PointShape(shortestLineResult.Vertices[0]), new PointShape(testLine.Vertices[testLine.Vertices.Count - 1])); //Make sure the split was valid and the closest point wasn't at the beginning or end of the line. if (rightSideOfLineResult.Validate(ShapeValidationMode.Simple).IsValid) { MapShape rightSideOfLine = new MapShape(new Feature(rightSideOfLineResult)); rightSideOfLine.ZoomLevels.ZoomLevel01.DefaultLineStyle = LineStyle.CreateSimpleLineStyle(GeoColors.Yellow, 2, false); rightSideOfLine.ZoomLevels.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; results.MapShapes.Add("RightSideofLine", rightSideOfLine); message += "Length of Yellow Line is: " + rightSideOfLineResult.GetLength(GeographyUnit.Meter, DistanceUnit.Kilometer).ToString() + " KM" + '\n'; } //Display the length of each line in kilometers. message += "Length of Red Line is: " + testLine.GetLength(GeographyUnit.Meter, DistanceUnit.Kilometer).ToString() + " KM"; mapView.Refresh(); MessageBox.Show(message, "Results"); }