我是靠谱客的博主 发嗲小虾米,这篇文章主要介绍geometry-api-java 学习笔记(八)分割Intersection,现在分享给大家,希望可以做个参考。

一个简单的例子

Let's look at a couple of simple examples. In the following images, the blue geometry is the intersectorand the green geometries are the input geometries. The intersector is paired with each of the inputgeometries to output the red geometries.

Intersector and InputsDimension mask = -1Dimension mask = 3Dimension mask = 6

调用Intersection 操作

这里有2种方法:

There are two ways to call the Intersection operator as there are two execute methods. Oneof the execute methods doesn't have a mask parameter. By calling this method,we are essentially setting the dimension mask equal to -1. Recall that this implies that the returned geometry willbe the lowest dimension of the intersecting pair.

If we are not using a dimension mask, the call looks like this:

复制代码
1
GeometryCursor outputGeoms = OperatorIntersection.local().execute(inputGeoms, intersector, spatialRef, null);

If we are using a dimension mask, the call looks like this:

复制代码
1
GeometryCursor outputGeoms = OperatorIntersection.local().execute(inputGeoms, intersector, spatialRef, null, mask);

where inputGeoms and intersector are of type GeometryCursor.

Then to retrieve the intersected geometries from the cursor, our code will look something like this:

复制代码
1
2
3
4
5
6
Geometry geometry = null; while ((geometry = outputGeoms.next()) != null) { ... do something }

Understanding the output

The number of output geometries for each input pair depends on the dimension mask. If the dimension mask isequal to -1, then there will be one output geometry for each input pair. If the dimension mask is greater than0, then for each input pair there will be the same number of output geometries as is specified in the dimensionmask. Some of the output geometries may be empty.

To better understand the output, we will look at the output generated by the application shown below.The application creates the geometries we looked at previously and calls intersection with variousdimension masks. It prints out the JSON format of the output geometries.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package geometryapp; import com.esri.core.geometry.Geometry; import com.esri.core.geometry.GeometryCursor; import com.esri.core.geometry.OperatorExportToJson; import com.esri.core.geometry.OperatorIntersection; import com.esri.core.geometry.Polygon; import com.esri.core.geometry.Polyline; import com.esri.core.geometry.SimpleGeometryCursor; import com.esri.core.geometry.SpatialReference; import java.util.ArrayList; /* * This program creates an intersector polygon, two input polylines and two input * polygons. It then calls the Intersection operator with three different * dimension masks. Each time the Intersection operator is called, it pairs up * the intersector polygon with each of the input geometries. For each pair the * dimension of the output is determined by the given dimension mask. * -1 => lowest dimension of input pair * 3 => output multipoints (1) and polylines (2) (1 + 2 = 3) * 6 => polylines (2) and polygons (4) (2 + 4 = 6) */ public class IntersectionApp { public static void main(String[] args) { // Create intersector. double coords[][] = {{0,4},{2,10},{8,12},{12,6},{10,2},{4,0}}; Polygon poly = createPolygon(coords); // Create the input polylines. ArrayList<Geometry> geomList = createAllInputs(); // Create a spatial reference object for GCS_WGS_1984. SpatialReference sr = SpatialReference.create(4326); System.out.println("Intersector"); printJSONGeometry(sr, poly); System.out.println("Input Geometries"); for (Geometry geom : geomList) { printJSONGeometry(sr, geom); } // Let's try it with different dimension masks. int mask[] = {-1, 3, 6}; for (int i = 0; i < 3; i++) { SimpleGeometryCursor inGeoms = new SimpleGeometryCursor(geomList); SimpleGeometryCursor intersector = new SimpleGeometryCursor(poly); GeometryCursor outGeoms = OperatorIntersection.local().execute(inGeoms, intersector, sr, null, mask[i]); System.out.println("*******Dim mask: " + Integer.toString(mask[i]) + "*******"); // Get the geometries from the cursor and print them in JSON format. Geometry geom = null; while((geom = outGeoms.next()) != null) { printJSONGeometry(sr, geom); } } } public static ArrayList<Geometry> createAllInputs() { // Create a list of input geometries. ArrayList<Geometry> geomList = new ArrayList<Geometry>(4); // Polyline 1 double coords[][] = {{1,15},{3.5,10.5},{6.5,11.5},{18,11.5}}; geomList.add(createPolyline(coords)); // Polyline 2 double coords2[][] = {{-2,10},{1,7}}; geomList.add(createPolyline(coords2)); // Polygon 3 double coords3[][] = {{8,8},{10,10},{14,10},{16,8},{16,4},{14,2},{10,2},{8,4}}; geomList.add(createPolygon(coords3)); // Polygon 4 double coords4[][] = {{1,3},{3,1},{0,0}}; geomList.add(createPolygon(coords4)); return geomList; } public static Polyline createPolyline(double[][] pts) { Polyline line = new Polyline(); line.startPath(pts[0][0], pts[0][1]); for (int i = 1; i < pts.length; i++) line.lineTo(pts[i][0], pts[i][1]); return line; } public static Polygon createPolygon(double[][] pts) { Polygon poly = new Polygon(); poly.startPath(pts[0][0], pts[0][1]); for (int i = 1; i < pts.length; i++) poly.lineTo(pts[i][0], pts[i][1]); return poly; } public static void printJSONGeometry(SpatialReference spatialRef, Geometry geometry) { System.out.println("Type: " + geometry.getType()); // Export the geometry to JSON format to print it out. String jsonString = OperatorExportToJson.local().execute(spatialRef, geometry); System.out.println(jsonString); System.out.println(); } }

Now let's look at the output a bit at a time.

First, we just print the intersector and the input geometries so we can see the coordinates.

Intersector and Inputs

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Intersector Type: Polygon {"rings":[[[0.0,4.0],[2.0,10.0],[8.0,12.0],[12.0,6.0],[10.0,2.0],[4.0,0.0],[0.0,4.0]]],"spatialReference":{"wkid":4326}} Input Geometries Type: Polyline {"paths":[[[1.0,15.0],[3.5,10.5],[6.5,11.5],[18.0,11.5]]],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[[[-2.0,10.0],[1.0,7.0]]],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[[[8.0,8.0],[10.0,10.0],[14.0,10.0],[16.0,8.0],[16.0,4.0],[14.0,2.0],[10.0,2.0],[8.0,4.0],[8.0,8.0]]],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[[[1.0,3.0],[3.0,1.0],[0.0,0.0],[1.0,3.0]]],"spatialReference":{"wkid":4326}}

The first dimension mask is equal to -1. Because the intersector is a polygon, if the input is a polygon, thenthe output will be a polygon. If the input is a polyline, then the output will be a polyline.There will be one output for each input geometry.

Notice that the second output polyline is empty. This is because the intersection of the intersector polygon andPolyline 2 is a point, not a polyline. Similarly, the second output polygon is empty because theintersection of the intersector polygon and Polygon 2 is a polyline, not a polygon.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
*******Dim mask: -1******* Type: Polyline {"paths":[[[3.5,10.5],[6.5,11.5],[8.333333333333334,11.5]]],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[[[10.0,2.0],[8.0,4.0],[8.0,8.0],[9.6,9.6],[12.0,6.0],[10.0,2.0]]],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[],"spatialReference":{"wkid":4326}}

Next, we use a dimension mask equal to 3. This signifies that we want to find point/multipoint and polylineintersections. Therefore, we will have two output geometries for each input geometry.

Notice that the only multipoint that isn't empty is that which was output as the intersection of the intersectorand Polyline 2.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
*******Dim mask: 3******* Type: MultiPoint {"points":[],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[[[3.5,10.5],[6.5,11.5],[8.333333333333334,11.5]]],"spatialReference":{"wkid":4326}} Type: MultiPoint {"points":[[1.0,7.0]],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[],"spatialReference":{"wkid":4326}} Type: MultiPoint {"points":[],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[],"spatialReference":{"wkid":4326}} Type: MultiPoint {"points":[],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[[[3.0,1.0],[0.9999999999999999,3.0]]],"spatialReference":{"wkid":4326}}

Finally, we examine the results when the dimension mask is equal to 6. This time we want intersections thatare polylines and polygons.

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
*******Dim mask: 6******* Type: Polyline {"paths":[[[3.5,10.5],[6.5,11.5],[8.333333333333334,11.5]]],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[[[10.0,2.0],[8.0,4.0],[8.0,8.0],[9.6,9.6],[12.0,6.0],[10.0,2.0]]],"spatialReference":{"wkid":4326}} Type: Polyline {"paths":[[[3.0,1.0],[0.9999999999999999,3.0]]],"spatialReference":{"wkid":4326}} Type: Polygon {"rings":[],"spatialReference":{"wkid":4326}}

最后

以上就是发嗲小虾米最近收集整理的关于geometry-api-java 学习笔记(八)分割Intersection的全部内容,更多相关geometry-api-java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部