Polygons

  • API Reference

A polygon is defined by a collection of rings. Each ring is a collection of contiguous line segments suchthat the start point and the end point are the same.

看下图,多边形的起点和终点相等。

Ring

Boundary and Rings

The boundary of a polygon is the collection of rings by which the polygon is defined. The boundarycontains one or more outer rings and zero or more inner rings. An outer ring is oriented clockwise whilean inner ring is oriented counterclockwise. Imagine walking clockwise along an outer ring. The area toyour immediate right is the interior of the polygon and to your left is the exterior.

Outer Ring

Similarly, if you were to walk counter-clockwise along an inner ring, the area to your immediateright is the interior of the polygon and to your left is the exterior.

Outer & Inner Ring

It is important to understand the boundary, interior and exterior of a geometry when using the variousoperators. The relational operators, for example, rely heavily on these concepts.

If a polygon has an inner ring, the inner ring looks like a hole. If the hole containsanother outer ring, that outer ring looks like an island.

1 outer ring, no inner rings 4 outer rings, no inner rings 1 outer ring, 1 inner ring 2 outer rings, 1 inner ring

Valid polygons

A valid polygon has no overlapping rings, no self-intersections except possiblyat vertices, no dangling segments and, in general, an arbitrary point can always beclassified unambiguously as either in the exterior, in the interior or on theboundary of the polygon. A valid polygon is said to be simple.See the Simplify operator for a more detailed specificationof simple polygons. Note that a simple polygon may not be OGC compliant. See the Simplify operator with OGC restrictions.

Examples

Let's look at some examples of non-simple vs. simple polygons. The green circles are the vertices of the polygon,and the lavender colored area represents the interior of the polygon.

Non-simple polygons
Self-intersection Self-intersection Dangling segment Dangling segment Overlapping rings
Simple polygons
No self-intersection Self-intersection at vertex No dangling segment No dangling segment No overlapping rings

When drawing a polygon, use the even-odd fill rule. The even-odd fill rule will guarantee that the polygonwill draw correctly even if the ring orientation is not as described above for simple polygons.

JSON format

A polygon can be represented as a JSON string. A polygon in JSON format contains an array of ringsand an optional spatialReference. A polygon can also have boolean-valued hasM and hasZ fields. The default value is falsefor both the hasM and hasZ fields.

Each ring is represented by an array of points. Exterior rings are oriented clockwise, while interior ringsare oriented counter-clockwise. The order of the rings is irrelevant. The first point of each ring is always the same as the last point.Each point in a ring is represented as an array of numbers. See the description ofJSON multipoints for details on the interpretation of the point arrays.

An empty polygon is represented with an empty array for the rings field.

Syntax

{"hasZ" : true | false,"hasM" : true | false,"rings": [[[<x11>,<y11>,<z11>,<m11>],[<x12>,<y12>,<z12>,<m12>], ... ,[<x1j>,<y1j>,<z1j>,<m1j>]],... ,[[<xn1>,<yn1>,<zn1>,<mn1>],[<xn2>,<yn2>,<zn2>,<mn2>], ... ,[<xnk>,<ynk>,<znk>,<mnk>]]],"spatialReference" : {"wkid" : <wkid>}
}

2D Polygon

{"rings": [[[6453,16815],[10653,16423],[14549,5204],[-7003,6939],[6453,16815]],[[914,7992],[3140,11429],[1510,10525],[914,7992]]],"spatialReference" : {"wkid" : 54004}
}

3D Polygon with Ms

Note that the third point does not have a z-value, and the second ring does not have any m-values.

{"hasZ" : true,"hasM" : true,"rings": [[[6453,16815,35,1],[10653,16423,36,2],[14549,5204,null,3],[-7003,6939,37,4],[6453,16815,35,1]],[[914,7992,30],[3140,11429,29],[1510,10525,28],[914,7992,30]]],"spatialReference" : {"wkid" : 54004}
}

Empty Polygon

{"rings": []}

Creating a polygon

To create a polygon, we can use the Polygon class methods or one of the import operators.

Each of the code samples below creates a polygon with two outer rings and one inner ring.
The polygon looks like this:

Create this polygon

Polygon class methods

When using the Polygon class methods to create a polygon, the order in which the rings are createddoesn't matter. What matters is the ring orientation. Remember, clockwise implies an outer ring whereascounter-clockwise implies an inner ring.

To begin each ring, we call the startPath method and then successive calls to thelineTo method. We don't need to repeat the start point as the end point.

这里同样用到startPath和lineTo方法,不过不用重复把起点去做终点。

public static Polygon createPolygon1()
{Polygon poly = new Polygon();// clockwise => outer ringpoly.startPath(0, 0);poly.lineTo(-0.5, 0.5);poly.lineTo(0.5, 1);poly.lineTo(1, 0.5);poly.lineTo(0.5, 0);// holepoly.startPath(0.5, 0.2);poly.lineTo(0.6, 0.5);poly.lineTo(0.2, 0.9);poly.lineTo(-0.2, 0.5);poly.lineTo(0.1, 0.2);poly.lineTo(0.2, 0.3);// islandpoly.startPath(0.1, 0.7);poly.lineTo(0.3, 0.7);poly.lineTo(0.3, 0.4);poly.lineTo(0.1, 0.4);return poly;
}

Import from JSON

As with the Polygon class methods, when using OperatorImportFromJsonto create a polygon the order in which the rings are created doesn't matter. What matters is thering orientation. Unlike the Polygon class methods, the start point of each ring mustbe repeated to specify the end point.

The code shown below creates the same polygon as before, but notice that the inner ring that forms the holeis given before the outer ring. This was done just to drive home the point that the order of the ringsdoesn't matter when the polygon is in JSON format.

static Polygon createPolygonFromJson() throws JsonParseException, IOException
{String jsonString = "{\"rings\":[[[0.5,0.2],[0.6,0.5],[0.2,0.9],[-0.2,0.5],[0.1,0.2],[0.2,0.3],[0.5,0.2]],"+ "[[0.0,0.0],[-0.5,0.5],[0.0,1.0],[0.5,1.0],[1.0,0.5],[0.5,0.0],[0.0,0.0]],"+ "[[0.1,0.7],[0.3,0.7],[0.3,0.4],[0.1,0.4],[0.1,0.7]]],"+ " \"spatialReference\":{\"wkid\":4326}}";MapGeometry mapGeom = OperatorImportFromJson.local().execute(Geometry.Type.Polygon, jsonString);return (Polygon)mapGeom.getGeometry();
}

Import from GeoJSON

Unlike the Polygon class methods and OperatorImportFromJson, when usingOperatorImportFromGeoJson to create a polygon the order in which the rings are givendoes matter. Within an array of rings, the outer ring is always first followed by zero or moreinner rings. However, the order of the arrays of rings doesn't matter. The start point of eachring must be repeated to specify the end point.

The code shown below creates the same polygon as in the previous examples.

static Polygon createPolygonFromGeoJson() throws JsonParseException, IOException
{String geoJsonString = "{\"type\":\"MultiPolygon\","+ "\"coordinates\":[[[[0.0,0.0],[-0.5,0.5],[0.0,1.0],[0.5,1.0],[1.0,0.5],[0.5,0.0],[0.0,0.0]],"+ "[[0.5,0.2],[0.6,0.5],[0.2,0.9],[-0.2,0.5],[0.1,0.2],[0.2,0.3],[0.5,0.2]]],"+ "[[[0.1,0.7],[0.3,0.7],[0.3,0.4],[0.1,0.4],[0.1,0.7]]]],"+ "\"crs\":\"EPSG:4326\"}";MapGeometry mapGeom = OperatorImportFromGeoJson.local().execute(GeoJsonImportFlags.geoJsonImportDefaults, Geometry.Type.Polygon, geoJsonString, null);return (Polygon)mapGeom.getGeometry();
}

Import from WKT

As with OperatorImportFromGeoJson, when using OperatorImportFromWktto create a polygon the order in which the rings are given does matter. Within an array of rings,the outer ring is always first followed by zero or more inner rings. However, the order of the arraysof rings doesn't matter. The start point of each ring must be repeated to specify the end point.

The code shown below creates the same polygon as in the previous examples, but notice that the outer ringthat forms the island is given first. This was done for illustrative purposes. It could have been given last,which probably would be more understandable to the reader.

static Polygon createPolygonFromWKT() throws JsonParseException, IOException
{String wktString = "MULTIPOLYGON (((0.1 0.7, 0.1 0.4, 0.3 0.4, 0.3 0.7, 0.1 0.7)),"+ "((0 0, 0.5 0, 1 0.5, 0.5 1, 0 1, -0.5 0.5, 0 0),"+ "(0.5 0.2, 0.2 0.3, 0.1 0.2, -0.2 0.5, 0.2 0.9, 0.6 0.5, 0.5 0.2)))";Geometry geom = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Polygon, wktString, null);return (Polygon)geom;
}

Extracting Outer Rings

Many workflows require getting only the outer rings of a polygon.

The method getAllOuterRings extracts all the outer rings of a polygon. The call toOperatorSimplifyOGC makes sure there are no self-intersections, all outer rings areclockwise, and holes are counterclockwise. You can skip the call to OperatorSimplifyOGCif you trust your input polygon.

static List<Polygon> getAllOuterRings(Polygon polygon)
{Polygon simplePolygon = (Polygon)OperatorSimplifyOGC.local().execute(polygon, null, true, null);ArrayList<Polygon> rings = new ArrayList<Polygon>();int n = simplePolygon.getPathCount();for (int i = 0; i < n; i++){if (simplePolygon.calculateRingArea2D(i) <= 0)continue;Polygon ring = new Polygon();ring.addPath(simplePolygon, i, true);rings.add(ring);}return rings;
}

The method getOutermostRings extracts the outermost rings, that is,only rings that are not contained inside of any hole.

static List<Polygon> getOutermostRings(Polygon polygon)
{List<Polygon> allOuterRings = getAllOuterRings(polygon);GeometryCursor outerRingsCursor = new SimpleGeometryCursor(allOuterRings);GeometryCursor unionCursor = OperatorUnion.local().execute(outerRingsCursor, null, null);Geometry unionPoly = unionCursor.next();// Holes could have been produced during the union, so rerun just in case.return getAllOuterRings((Polygon)unionPoly);
}

geometry-api-java 学习笔记(五)多边形 Polygons相关推荐

  1. Java学习笔记(五):一张图总结完JVM8基础概念

    Java学习笔记(五):一张图总结完JVM8基础概念 引文 最近在学习JVM的相关内容,好不容易把基础概念全部都学了一遍,却发现知识网络是零零散散的.迫不得已,只好再来一次总的归纳总结.为了更好的理解 ...

  2. JAVA学习笔记五---函数

    JAVA学习笔记五---函数 5.1 方法的学习 编写一个程序,求圆的周长和面积. package practice; /*** 编写一个程序,求圆的周长和面积.* @author iszhangyo ...

  3. java学习笔记(五)----super用法,final用法,抽象类,接口

    子类不会继承父类的构造方法,在子类的构造方法中可使用super(参数列表)调用父类的构造方法. class Person { String name;   int age;   public Pers ...

  4. 【Java学习笔记五】Java异常处理

    异常通常分为三类: 程序可控制的异常:一般是可预见的错误,不是致命的.例如:除数为0,数组下标越界. 程序不可控制的的异常:这种异常往往是致命的,但是系统可以预见的.例如:系统栈溢出. 人为异常 当系 ...

  5. JAVA学习笔记(五十七)- 常用设计模式

    单例模式 /** 单例模式* 应用场合:有些对象只需要一个,此时可以使用单例模式* 作用:保证整个应用中某个实例有且只有一个* 类型:饿汉模式.懒汉模式*/ public class Test04 { ...

  6. Java之多线程学习笔记五 —— 多线程模拟龟兔赛跑

    Java之多线程学习笔记五 -- 多线程模拟龟兔赛跑 参考教程B站狂神https://www.bilibili.com/video/BV1V4411p7EF package pers.ylw.less ...

  7. java学习笔记16--I/O流和文件

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note16.html,转载请注明源地址. IO(Input  Output)流 IO流用来处理 ...

  8. motan学习笔记 五 opentracing学习入门

    motan学习笔记 一 微博轻量级RPC框架Motan motan学习笔记 二 motan架构分析 motan学习笔记 三 motan Demo 分析 motan学习笔记 四 motan Demo 之 ...

  9. 《Java学习笔记(第8版)》学习指导

    <Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...

  10. Java学习笔记-Day64 Spring 框架(二)

    Java学习笔记-Day64 Spring 框架(二) 一.控制反转IOC和依赖注入DI 1.控制反转IOC 2.依赖注入DI 3.Spring IOC容器 3.1.简介 3.2.实现容器 3.2.获 ...

最新文章

  1. 浅析Mysql Join语法以及性能优化
  2. 解决phpmyadmin 遇见的问题
  3. python将字典导入excel_python将字典列表导出为Excel文件的方法
  4. Visual Studio怎么使用中文帮助文档
  5. 使用 Github Actions artifact 在 workflow job 之间共享数据
  6. 程序员教你如何用 13 种模型预测天气预报 | 原力计划
  7. 网课时代,在线教育的新机遇在哪?
  8. sqlserver存储过程加锁后怎么解锁_MySQL 的加锁处理,你都了解的一清二楚了吗?...
  9. 电脑不香吗?我在手机上装Python我图什么?
  10. 我的软件开发生涯 (10年开发经验总结和爆栈人生)
  11. VS中使用ankhSVN
  12. celery报错 NotImplementedError: No result backend is configured
  13. word页眉的横线怎么居中
  14. 表格操作系列——字段名与字段别名的获取
  15. Dense biased networks with deep priori anatomy and hard region adaptation: Semi-supervised learning
  16. 数字孪生智慧城市信息化平台建设
  17. React从零到一Demo演练(上)
  18. NOIP切题注意事项
  19. 计算机主板i3 i5区别,8代i3、i5、i7处理器的用途有哪些区别吗?如何配搭主板?...
  20. 【Spring】SpringBoot 配置 log4j2 日志

热门文章

  1. 万进制——蓝桥杯|ACM 大数阶乘——21行代码AC
  2. 浅谈万进制算法与大数定理的结合(高精度乘除法,阶乘)
  3. 剑指0ffer04.二维数组中的查找
  4. Rsync下行同步+inotify实时同步介绍和部署
  5. 数据结构与算法常见笔试题 .
  6. python中异常和错误是一个概念_Python的异常概念介绍以及处理
  7. 计算机专业PS模版,计算机专业PS范例十一
  8. mysql 排序 删除_是否可以删除mysql表排序规则?
  9. thinkphp项目mysql类关系_ThinkPHP数据库与模型
  10. java ftp下载文件 慢_java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题...