Home » Java » how to create polygon using Google S2 Library in Java

how to create polygon using Google S2 Library in Java

Posted by: admin October 26, 2017 Leave a comment

Questions:

I am able to create rectangle using Google S2 Library but unable to create polygon. 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.

Appreciate your time, if you can suggest me how to do this.

Thanks !!!!

Answers: