简体   繁体   English

如何在Java中使用Google S2库创建多边形

[英]how to create polygon using Google S2 Library in Java

I am able to create rectangle using Google S2 Library but unable to create polygon. 我可以使用Google S2库创建矩形,但无法创建多边形。 I have tried with different way but not able to solve this. 我尝试了不同的方法,但无法解决此问题。

Sharing my code and the output also : 共享我的代码和输出:

 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());

The output is : 输出为:

 ***************  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

You can see in the out that i am able to create only 6 cells but it should be more than 6, because the entire region has to be filled with cells only. 您可以看到,我只能创建6个单元格,但应该超过6个单元格,因为整个区域只需要填充单元格即可。

Appreciate your time, if you can suggest me how to do this. 如果您可以建议我该如何做,请感谢您的时间。

Thanks !!!! 谢谢 !!!!

The library has it's own internal Lat long representations. 该库具有自己的内部Lat long表示形式。 You are making a shape that covers an entirely different area than you think. 您正在制作的形状与您认为的完全不同。 If you return the levels for the 6 cells it returns I suspect you will find that they are all extremely high level(0 or 1 or 2). 如果您返回6个单元格的级别,它会返回,我怀疑您会发现它们都非常高(0或1或2)。

You need to create a S2latlong object using the S2latlong.FromDegreees(y,x) Then you can call .ToPoint() on that S2LatLng object you just made. 您需要使用S2latlong.FromDegreees(y,x)创建一个S2latlong对象,然后可以对刚刚创建的S2LatLng对象调用.ToPoint()。

The rest of it you have correct. 其余的您都正确。 Use those points to form edges and those edges to build the polygon. 使用这些点形成边缘,使用这些边缘构建多边形。 Once you do the latlong conversion, it should work for you. 一旦完成latlong转换,它便会为您工作。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google S2库将地理位置作为坐标来构造多边形 - How to construct polygon using geolocation as coordinate using Google S2 Library 在Java中,如何使用多边形选择创建一个图像,该图像是另一个图像的子图像? - In Java how can I create an image that's the subimage of another image using a polygon to select? 如何使用 Spring Boot 和 Java 库在 google 上创建操作 - How to create actions on google using Spring Boot & Java Library 如何使用Google的Guava库提取Java中的泛型类型 - How to extract a generic type in Java using Google's Guava library 应该使用java将S1的每个备用索引中的字符替换为S2 - Character in each alternate index of S1 should be replaced with S2 using java 改进我的Java方法containsSubstring(s1,s2),它查找s2是否为s1的子字符串 - Improving my Java method containsSubstring(s1, s2) which finds if s2 is a substring of s1 在连接两个String s1和s2时,生成的输出String s3将在java中创建堆或常量池吗? - on concatenating two String s1 and s2, produced output String s3 will create in heap or constant pool in java? 使用Google的Java API客户端库进行身份验证时出现IllegalArgumentException - IllegalArgumentException when authenticating using Google's Java API client library 如何使用Java绘制不完整的多边形 - How to draw an incomplete Polygon using Java 使用 JGeometry 创建多边形 - Create Polygon using JGeometry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM