Geometries

Point, Multipoint, Polyline, Polygon,和 Envelope都继承自Geometry

他们的共同特点:

几何图形是不可变的(Geometries are immutable)

大多数几何体都是在其生命周期内创建的,不会更改。

Envelope(范围)

它们可以用作图形的几何图形和许多几何图形操作,但它们不能用作要素的几何图形。通过指定最小和最大x坐标以及最小和最大y坐标以及SpatialReference来定义Envelope的新实例。

Envelope envelope = new Envelope(-123.0, 33.5, -101.0, 48.0, SpatialReferences.getWgs84());

或者使用两个 Point 对象作为参数

Envelope envelopePoints = new Envelope(pointOne, pointTwo);

Point(点)

  • 了解点的坐标系
  • 您可以使用CoordinateFormatter将纬度,经度格式的字符串直接转换为Point,还可以从现有Point返回纬度,经度格式的字符串。

多点(MultiPoint)

多点几何表示点的有序集合。它们可以用作要素和图形的几何,或者作为几何操作的输入或输出。

  • MultiPoint 为只读集合,创建示例:
PointCollection stateCapitalsPST = new PointCollection(SpatialReferences.getWgs84());
stateCapitalsPST.add(-121.491014, 38.579065); // Sacramento, CA
stateCapitalsPST.add(-122.891366, 47.039231); // Olympia, WA
stateCapitalsPST.add(-123.043814, 44.93326); // Salem, OR
stateCapitalsPST.add(-119.766999, 39.164885); // Carson City, NV
Multipoint multipoint = new Multipoint(stateCapitalsPST);

使用示例:

for (Point pt : multipoint.getPoints()) {
  Log.i(TAG, String.format("Point x=%f,y=%f, z=%f",  pt.getX(), pt.getY(), pt.getZ()));
}

折线(Polyline)

创建

PointCollection borderCAtoNV = new PointCollection(SpatialReferences.getWgs84());
borderCAtoNV.add(-119.992, 41.989);
borderCAtoNV.add(-119.994, 38.994);
borderCAtoNV.add(-114.620, 35.0);
Polyline polyline = new Polyline(borderCAtoNV);

多边形(Polygon)

创建

PointCollection coloradoCorners = new PointCollection(SpatialReferences.getWgs84());
coloradoCorners.add(-109.048, 40.998);
coloradoCorners.add(-102.047, 40.998);
coloradoCorners.add(-102.037, 36.989);
coloradoCorners.add(-109.048, 36.998);
Polygon polygon = new Polygon(coloradoCorners);

Multipart (Polygon and Polyline)

两种遍历方式

// 第一种
// iterate each Part of a Polyline
ImmutablePartCollection parts = polyline.getParts();
for  (ImmutablePart part : parts)
{
  // iterate the collection of Points representing vertices of the ImmutablePart
  for (Point pt : part.getPoints())
  {
    Log.i(TAG, String.format("Point: x=%f,y=%f, z=%f", pt.getX(), pt.getY(), pt.getZ()));
  }
  // iterate each Segment - ImmutablePart is a collection of segments
  for (Segment seg : part) {
    Log.i(TAG, String.format("Segment: (%f, %f) to (%f %f)",
        seg.getStartPoint().getX(), seg.getStartPoint().getY(),
        seg.getEndPoint().getX(), seg.getEndPoint().getY()));
  }
}
// 第二种
// get the combined set of Points from every ImmutablePart of a Polyline
for  (Point pt : polyline.getParts().getPartsAsPoints())
{
  Log.i(TAG, String.format("Point: x=%f,y=%f, z=%f",
      pt.getX(), pt.getY(), pt.getZ()));
}

Converting to and from JSON

// convert a Polygon to a JSON representation
String polygonJson = polygon.toJson();
// create a new Polygon from JSON
Polygon polygonFromJson = (Polygon) Polygon.fromJson(polygonJson);

推荐阅读更多精彩内容