google / s2-geometry-library-java

Automatically exported from code.google.com/p/s2-geometry-library-java
Apache License 2.0
533 stars 230 forks source link

S2CellCreation #11

Open sivalearn12 opened 6 years ago

sivalearn12 commented 6 years ago

Hi,

i want to create the cell using lat and long by mentioning level. Once created, need to loop through the cell to get the nearest cell information. Please guide me for coding

Thanks.

matthias-mueller commented 6 years ago

Hi,

if you create a new cell from a point, it is always a leaf cell:

S2Cell cell = new S2Cell(S2LatLng.fromDegrees(0.5, 14.3).toPoint());

If you need a particular level, ask for the desired parent cell level (ex: level=5):

S2CellId cellId = cell.id().parent(5);

I am not sure what you mean by "looping".

sivalearn12 commented 6 years ago

Hi,

Thanks for quick response.

double fromLat = -37.79954720; double fromLng = 144.76014430;

        double fromLat1 = -37.69772770;
        double fromLng1 = 144.85909800;

        double fromLat2 = -37.69772786;
        double fromLng2 = 144.85909800;

        double fromLat3 = -37.69772776;
        double fromLng3 = 144.85909800;

        Map<Double,Double> latLon = new HashMap<Double, Double>();

        latLon.put(fromLat, fromLng);
        latLon.put(fromLat1, fromLng1);
        latLon.put(fromLat2, fromLng2);
        latLon.put(fromLat3, fromLng3);

        List<S2CellId> s2CellIdList = new ArrayList<S2CellId>();

        List<S2CellId> s2CellIdListNew = new ArrayList<S2CellId>();

        S2CellId s2CellId = null;
        Set<Map.Entry<Double, Double>> set = latLon.entrySet();
         //creation of cell
        for (Map.Entry<Double, Double> me : set) {
               S2LatLng s2LatLng =    S2LatLng.fromDegrees(me.getKey(), me.getValue());
              s2CellId = S2CellId.fromLatLng(s2LatLng);
              System.out.println("Level -->" + s2CellId.level());  // level always coming as 30. i want to set the level to 12

               System.out.println("Id -->" + s2CellId.id());
               s2CellIdList.add(s2CellId);
         }
        //get only the cellid  within 5km.

         for (S2CellId tempCellId : s2CellIdList) {

               tempCellId.getAllNeighbors(1, s2CellIdListNew);

               for (S2CellId tempNearCellId : s2CellIdListNew) {

                    System.out.println("tempNearCellId-->"+ tempNearCellId.id());
                    if(tempNearCellId.isValid())
                          System.out.println("valid -->"+ tempNearCellId.isValid());

               }
              s2CellIdListNew.clear();
              System.out.println("Completed for cell id -->"+tempCellId.id() );

         }

I want to get only cellid’s within 5km.

ryantheleach commented 6 years ago

I'm not a developer on this ticket tracker, but it's widely considered bad etiquette to

  1. Open multiple issues https://github.com/google/s2-geometry-library-java/issues/12 https://github.com/google/s2-geometry-library-java/issues/13

  2. Open issues regarding support, as opposed to bugs.

In addition, Matthias offered a solution above, use parent(n) to get a parent cellid at a given level.

If this were my Repo, and behavior didn't change after being warned, I'd be seriously considering moderation actions against you.

Now to elaborate slightly since I'm not a total asshole.

assuming that S2CellId's are immutable, and that their equality contract is value based, or (their instances are multitons, based on their id)

You could use a Set instead of a list, and use set.add(cell.id().parent(12));

Adding multiple id's to the same set should be ok, as the unique constraint on Sets should ensure you only have 1 level 12 cell if there happens to be duplicates.

Just curious, what application are you using the s2-geometry library for?

More context may provide clues to whether you are using it correctly.