【问题标题】:how to create polygon using Google S2 Library in Java如何在 Java 中使用 Google S2 库创建多边形
【发布时间】:2018-04-07 16:26:49
【问题描述】:

我可以使用 Google S2 库创建矩形,但无法创建多边形。我尝试了不同的方法,但无法解决这个问题。

也分享我的代码和输出:

 Double point1_lat= 12.9751;
    Double point1_long = 77.5599;
    Double point2_lat= 12.9952;
    Double point2_long = 77.5954;
    Double point3_lat=  12.9741;
    Double point3_long = 77.6246;
    Double point4_lat= 12.9495;
    Double point4_long = 77.59336;
    Double point5_lat= 12.9482;
    Double point5_long = 77.5630;

    S2Point point1 = new S2Point(point1_lat, point1_long, 0);
    S2Point point2 = new S2Point(point2_lat, point2_long, 0);
    S2Point point3 = new S2Point(point3_lat, point3_long, 0);
    S2Point point4 = new S2Point(point4_lat, point4_long, 0);
    S2Point point5 = new S2Point(point5_lat, point5_long, 0);

    S2PolygonBuilder polygonBuilder = new S2PolygonBuilder();

    polygonBuilder.addEdge(point1, point2);
    polygonBuilder.addEdge(point2, point3);
    polygonBuilder.addEdge(point3, point4);
    polygonBuilder.addEdge(point4, point5);
    polygonBuilder.addEdge(point5, point1);

    S2Polygon polygon = polygonBuilder.assemblePolygon();

    System.out.println("***************  debug point 1 ****************" + polygon.toString());

    S2RegionCoverer coverer = new S2RegionCoverer();
    coverer.setMinLevel(5);
    coverer.setMaxLevel(10);
   coverer.setMaxCells(100);

    System.out.println("***************  debug point 2 ****************");


    S2CellUnion union = coverer.getCovering(polygon);

    System.out.println("***************  debug point 3 ****************");


    System.out.println("cells inside the region : "+union.cellIds().size());

输出是:

 ***************  debug point 1 ****************Polygon: (1) loops:
   loop <
   (0.0, 80.52526814804811)
   (0.0, 80.52256106366895)
   (0.0, 80.50285721964708)
   (0.0, 80.4926857166327)
   (0.0, 80.51134710630562)
    >

   ***************  debug point 2 ****************
   finally checking for loop end2047
   ***************  debug point 3 ****************
   cells inside the region : 6

您可以在外面看到我只能创建 6 个单元格,但应该超过 6 个,因为整个区域只能用单元格填充。

感谢您的时间,如果您能建议我如何做到这一点。

谢谢!!!!

【问题讨论】:

    标签: java android python google-maps google-maps-api-3


    【解决方案1】:

    库有它自己的内部经纬度表示。您正在制作的形状覆盖的区域与您想象的完全不同。如果您返回它返回的 6 个单元格的级别,我怀疑您会发现它们都是非常高的级别(0 或 1 或 2)。

    您需要使用 S2latlong.FromDegreees(y,x) 创建一个 S2latlong 对象,然后您可以在刚刚创建的 S2LatLng 对象上调用 .ToPoint()。

    其余的你都正确。使用这些点形成边,并使用这些边来构建多边形。一旦你进行了 latlong 转换,它应该适合你。

    S2Point p = S2LatLng.FromDegrees(y, x).ToPoint();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-08
      • 2019-10-14
      • 2019-07-20
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多