ArcGIS API for Silverlight use geometryservice to solve the intersection of line and line (2)

Source: Internet
Author: User
Tags polyline

In the previous blog, the intersection of line and plane is solved. The method used is to use the simplify + intersect service of geometryservice to obtain the intersection of line and plane, indirectly, the intersection between the line and the plane is obtained through the intersection point of the line. The process is as follows:

However, as we mentioned above, intersect in geometry cannot get the intersection of line and line. Even if we use intersect to find the intersection between line and line, we can return the result of intersection, however, the intersection cannot be obtained, because the Intersect returns a line element whose extent is null, and we cannot express it on the map.

So what should we do here?

Here we use another geometry service: trimextend (trim extension)

For specific examples, refer:

Http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#TrimExtend

Trimextend functions:

We found that after trimextend is used, the grid horizontal line is derived in the right direction and intersecting with the line elements, while the grid located below the river is cut out.

You can use the curveextension attribute to set the direction of cropping and extending (for example, extending to the right, extending to the left, or extending to the two sides, for more information about curveextension attributes, see the official documentation:

Http://help.arcgis.com/en/webapi/silverlight/apiref/api_start.htm

It should be noted that the result returned by trimextend is a line element, and one end of the return line element is the intersection of the line element and the line element (only when the line element is intersection, we will find that elements that do not have intersections are also returned, so further filtering is required here, which will be described later)

Through the above analysis, we know that we can get the intersection result as long as we get the endpoint of the return line element.

The procedure is as follows:

1. Build a grid (here you can also use the line elements of a layer. In this article, you can build a grid by yourself to find the nodes where the grid and line elements intersect)

Key code:

A. Construct a straight line based on two points

Public void createline (mappoint P1, mappoint P2) {ESRI. arcGIS. client. geometry. polyline = new ESRI. arcGIS. client. geometry. polyline (); ESRI. arcGIS. client. geometry. pointcollection Pc = new ESRI. arcGIS. client. geometry. pointcollection (); PC. add (P1); PC. add (P2); polyline. paths. add (PC); // remember to assign values to the space coordinate system; otherwise, the simplify operation will fail. Polyline. spatialreference = map1.spatialreference; addlinegraphic (polyline );}

Addlinegraphic is a custom method used to add a line element to a layer. The sample code is as follows:

  public void AddLineGraphic(ESRI.ArcGIS.Client.Geometry.Polyline polyline)        {            Graphic g = new Graphic()            {                Geometry = polyline,                Symbol = LayoutRoot.Resources["LineSymbol"] as SimpleLineSymbol            };                      glayer.Graphics.Add(g);        }

Here, you can customize the mesh step to build a mesh based on the drawn rectangle or polygon range.

Public void creategrid (double xmin, double xmax, double ymin, double Ymax, double xstep, double ystep) {// define two points to determine a straight line mappoint MP1; mappoint MP2; // determine the number of parts divided by step. Int Nx = convert. toint32 (xmax-xmin)/xstep); int ny = convert. toint32 (Ymax-ymin)/ystep); // construct the vertical line for (INT I = 0; I <NX + 1; I ++) {MP1 = createmappoint (xmin + I * xstep, ymin); // MP2 = createmappoint (xmin + I * xstep, Ymax); MP2 = createmappoint (xmin + I * xstep, ymin + ny * ystep); createline (MP1, MP2);} // construct the horizontal line for (INT I = 0; I <ny + 1; I ++) {MP1 = createmappoint (xmin, ymin + I * ystep); MP2 = createmappoint (xmin + NX * xstep, ymin + I * ystep); // MP2 = createmappoint (xmax, ymin + I * ystep); createline (MP1, MP2);} // rectextent is a custom struct, recextent = new rectextent () {xmin = convert. todouble (xmin ). tostring ("#0.000"), xmax = convert. todouble (xmin + NX * xstep ). tostring ("#0.000"), ymin = convert. todouble (ymin ). tostring ("#0.000"), Ymax = convert. todouble (ymin + ny * ystep ). tostring ("#0.000 "))};}

Effect:

The following uses the trimextend service to obtain the intersection point.

2. Call the trimextend Function

Private void intersectcenterlinebutton_click (Object sender, routedeventargs e) {// list of storage line elements <ESRI. arcGIS. client. geometry. polyline> polylinelistgrid = new list <ESRI. arcGIS. client. geometry. polyline> (); List <ESRI. arcGIS. client. geometry. polyline> polylinelistriver = new list <ESRI. arcGIS. client. geometry. polyline> (); If (glayer. graphics. count = 0 | flayer. graphics. count = 0) {MessageBox. show ("confirm that the input line element is not empty"); return;} // traverse the line element foreach (Graphic g in glayer. graphics) {polylinelistgrid. add (G. geometry as ESRI. arcGIS. client. geometry. polyline);} foreach (Graphic g in flayer. graphics) {polylinelistriver. add (G. geometry as ESRI. arcGIS. client. geometry. polyline );} /************************************ intersection layer (in this article, there is only one element, so the index is 0) * The first parameter represents the intersection elements, such as the grid, so it is a set * The second parameter represents the intersection elements, such as a river, therefore, it is a line element, not a set of elements * The third parameter represents an extension (CUT) *************************************/ geometryservice. trimextendasync (polylinelistgrid, polylinelistriver [0], curveextension. noextendatfrom );}

The next step is to process the results. As we have said above, one endpoint of all traffic lines is the intersection point of a straight line. Returns the result of a straight line (the red line segment is the result ).

When the trimextend function is used, the curveextension attribute is set to noextendatfrom. In this way, the grid searches for the grid line intersection from the start point to the right. If there is no intersection, the grid is extended to the right until it is intersecting.

(1) The length of the intersection must be smaller than the mesh drawn at the initial time, that is

Horizontal line length <| xmax-xmin |, xmax, and xmin are the maximum and minimum values of the X axis of the grid.

Vertical Line Length <| Ymax-ymin |, Ymax, and ymin are the maximum and minimum values of the grid Y axis.

(2) for horizontal and vertical grids, the coordinate of the intersection is always the minimum value (when it is a diagonal line, you can obtain the intersection slope and then obtain the intersection)

With these two points in mind, we can achieve it easily. The following code gets the result:

Void geometryservice_trimextendcompleted (Object sender, graphicseventargs e) {glayer. graphics. clear (); mappoint intersectpoint = NULL; foreach (Graphic g in E. results) {G. symbol = layoutroot. resources ["resultslinesymbol"] as ESRI. arcGIS. client. symbols. symbol; glayer. graphics. add (g); If (G. geometry. extent! = NULL) {/* The recextent struct stores the range of the rectangle to be drawn * here, we can compare the length of the line segment in the returned results to determine whether it is a line break * at the same time, the mesh drawn in this article is vertical and horizontal, * The length of the line must be less than the range of the rectangle to be drawn (you only need to determine the horizontal direction here) * The Method for Determining the diagonal line is similar to */If (G. geometry. extent. xmax-G. geometry. extent. xmin) <(recextent. xmax-recextent. xmin) {intersectpoint = createmappoint (G. geometry. extent. xmin, G. geometry. extent. ymin); addpointgraphic (intersectpoint );}}}}

The createmappoint method constructs a vertex through the X and Y coordinates, and addpointgraphic indicates adding the vertex to the map for display.

// Construct a public mappoint createmappoint (Double X, Double Y) by X and Y {return New mappoint (x, y );} // Add a vertex to the layer public void addpointgraphic (ESRI. arcGIS. client. geometry. mappoint point) {graphic G = new graphic () {geometry = point, symbol = layoutroot. resources ["pointsymbol"] As simplemarkersymbol,}; glayer. graphics. add (g );}

In this way, the intersection of the line and the line is obtained.

Example:

A. gridlines constructed

 

B. Intersection

Summary:

This article uses trimextend to solve the intersection. The process is to obtain the line segments returned by the trimextend function, filter out the intersection, and obtain the intersection endpoint, that is, the intersection. This article explains the intersection of water and vertical grids. When a line segment is irregular, the intersection can be determined by the slope of the line.

Next, we will explain how to construct a diagonal line mesh to calculate the intersection point.

(All Rights Reserved. For details, refer to the source)

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.