当前位置:   article > 正文

JTS(Geometry)表示圆形_jts polygon

jts polygon

在平时工作中会涉及一些空间范围的操作,比如两个面求交集,求一个多边形的面积等等,一般都会用到JTS。JTS支持点线面的很多操作,关键字为point、linestring、polygon。

JTS没有直接表示圆的关键字,一般用一个多边形表示圆:

1,确定边数

2,画一个环线

3,根据环线生成面

  1. public class JtsTest {
  2. private static GeometryFactory geometryFactory = new GeometryFactory();
  3. public static Polygon createCircle(double x, double y, final double RADIUS) {
  4. final int SIDES = 32; // 确定边数
  5. Coordinate coords[] = new Coordinate[SIDES + 1];
  6. for (int i = 0; i < SIDES; i++) {
  7. double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
  8. double dx = Math.cos(angle) * RADIUS;
  9. double dy = Math.sin(angle) * RADIUS;
  10. coords[i] = new Coordinate((double) x + dx, (double) y + dy);
  11. }
  12. coords[SIDES] = coords[0];
  13. LinearRing ring = geometryFactory.createLinearRing(coords); // 画一个环线
  14. Polygon polygon = geometryFactory.createPolygon(ring, null); // 生成一个面
  15. return polygon;
  16. }
  17. public static void main(String[] args) throws ParseException {
  18. Polygon p = createCircle(116.45224995263672, 39.988073630126955, 1);
  19. System.out.println(p);
  20. }
  21. }

接下来用openlayers看一下生成的polygon长什么样子。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>WKT</title>
  5. <link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
  6. <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
  7. <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
  8. <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  9. </head>
  10. <body>
  11. <div id="map" class="map"></div>
  12. <script>
  13. var raster = new ol.layer.Tile({
  14. source: new ol.source.OSM()
  15. });
  16. var wkt = '';// 把上面生成的polygon复制到这里
  17. var format = new ol.format.WKT();
  18. var feature = format.readFeature(wkt, {
  19. dataProjection: 'EPSG:4326'
  20. });
  21. var vector = new ol.layer.Vector({
  22. source: new ol.source.Vector({
  23. features: [feature]
  24. })
  25. });
  26. var map = new ol.Map({
  27. layers: [raster, vector],
  28. target: 'map',
  29. view: new ol.View({
  30. center: [108.560121,36.364153],
  31. projection: 'EPSG:4326',
  32. zoom: 4
  33. })
  34. });
  35. </script>
  36. </body>
  37. </html>

结果如下图:



说明:

1,第一段代码来自网络其他博客,出处忘记记录,抱歉。

2,第二段代码来自openlayers官网示例,出处:http://openlayers.org/en/latest/examples/wkt.html?q=wkt


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/941055
推荐阅读
相关标签
  

闽ICP备14008679号