Example usage for com.google.common.geometry S2Polygon contains

List of usage examples for com.google.common.geometry S2Polygon contains

Introduction

In this page you can find the example usage for com.google.common.geometry S2Polygon contains.

Prototype

public boolean contains(S2Point p) 

Source Link

Document

The point 'p' does not need to be normalized.

Usage

From source file:org.esa.beam.occci.S2EoProduct.java

@Override
public boolean contains(double lon, double lat) {
    S2Polygon poly = getPolygon();
    if (poly != null) {
        S2Point s2Point = S2LatLng.fromDegrees(lat, lon).toPoint();
        return poly.contains(s2Point);
    }/*from   w  w  w . ja va2s.c o m*/
    return false;
}

From source file:org.esa.beam.occci.ReverseMatcher.java

public Set<Integer> matchInsitu(List<SimpleRecord> insituRecords, long maxTimeDifference) {
    Map<Integer, List<S2Point>> candidatesMap = new HashMap<>();

    try (StopWatch sw = new StopWatch("  >>test for time and cell")) {
        for (SimpleRecord insituRecord : insituRecords) {
            final long referenceTime = insituRecord.getTime();
            final long windowStartTime;
            final long windowEndTime;
            if (referenceTime == -1) {
                windowStartTime = globalStartTime;
                windowEndTime = globalEndTime;
            } else {
                windowStartTime = referenceTime - maxTimeDifference;
                windowEndTime = referenceTime + maxTimeDifference;
            }/*from w  ww  .  j a  v a  2s  .  co m*/

            Point2D.Float location = insituRecord.getLocation();
            final double lon = location.getX();
            final double lat = location.getY();
            S2LatLng s2LatLng = S2LatLng.fromDegrees(lat, lon);
            S2Point s2Point = s2LatLng.toPoint();
            S2CellId s2CellId = S2CellId.fromPoint(s2Point);
            final int cellInt = S2CellIdInteger.asInt(s2CellId.parent(3));

            List<Integer> productIndices = reverseProductDB.findInsitu(cellInt, windowStartTime, windowEndTime);
            for (Integer productIndex : productIndices) {
                List<S2Point> candidateProducts = candidatesMap.get(productIndex);
                if (candidateProducts == null) {
                    candidateProducts = new ArrayList<>();
                    candidatesMap.put(productIndex, candidateProducts);
                }
                candidateProducts.add(s2Point);
            }
        }
        System.out.println("candidatesMap = " + candidatesMap.size());
    }

    List<Integer> uniqueProductList = new ArrayList<>(candidatesMap.size());
    uniqueProductList.addAll(candidatesMap.keySet());

    Set<Integer> matches = new HashSet<>();
    try (StopWatch sw = new StopWatch("  >>load and test polygons")) {
        Collections.sort(uniqueProductList);
        try (DataInputStream dis = new DataInputStream(
                new BufferedInputStream(new FileInputStream(polygonFile)))) {
            int streamPID = 0;
            for (Integer productID : uniqueProductList) {
                while (streamPID < productID) {
                    int numLoopPoints = dis.readInt();
                    dis.skipBytes(numLoopPoints * 3 * 8);
                    streamPID++;
                }
                final int numLoopPoints = dis.readInt();
                final double[] pointData = new double[numLoopPoints * 3];
                for (int i = 0; i < pointData.length; i++) {
                    pointData[i] = dis.readDouble();
                }
                streamPID++;

                S2Polygon s2Polygon = S2IEoProduct.createS2Polygon(pointData);
                List<S2Point> s2Points = candidatesMap.get(productID);
                for (S2Point s2Point : s2Points) {
                    if (s2Polygon.contains(s2Point)) {
                        matches.add(productID);
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    return matches;
}

From source file:org.esa.beam.occci.MultiPassMatcher.java

@Override
public Set<EoProduct> matchInsitu(List<SimpleRecord> insituRecords, long maxTimeDifference) {
    Map<S2IEoProduct, List<S2Point>> candidatesMap = new HashMap<>();

    long globalStartTime;
    long globalEndTime;
    try {//  w w  w.jav a 2s .  c  om
        globalStartTime = AbstractEoProduct.DATE_FORMAT
                .parse(DateUtils.getNoFractionString("2014-01-01T00:00:00")).getTime();
        globalEndTime = AbstractEoProduct.DATE_FORMAT
                .parse(DateUtils.getNoFractionString("2015-01-01T00:00:00")).getTime();
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }

    try (StopWatch sw = new StopWatch("  >>test for time and cell")) {
        for (SimpleRecord insituRecord : insituRecords) {
            S2CellId s2CellId = null;
            S2Point s2Point = null;
            int level1Mask = 0;
            final long referenceTime = insituRecord.getTime();
            final long windowStartTime;
            final long windowEndTime;
            if (referenceTime == -1) {
                windowStartTime = globalStartTime;
                windowEndTime = globalEndTime;
            } else {
                windowStartTime = referenceTime - maxTimeDifference;
                windowEndTime = referenceTime + maxTimeDifference;
            }

            int productIndex = productDB.getIndexForTime(windowStartTime);
            if (productIndex == -1) {
                continue;
            }

            boolean finishedWithInsitu = false;
            while (!finishedWithInsitu) {
                S2IEoProduct eoProduct = (S2IEoProduct) productDB.getRecord(productIndex);
                productIndex++;

                if (eoProduct == null) {
                    finishedWithInsitu = true;
                } else if (eoProduct.getStartTime() > windowEndTime) {
                    finishedWithInsitu = true;
                } else if (eoProduct.getEndTime() < windowStartTime) {
                    //test next product;
                } else {
                    // time match
                    if (s2CellId == null) {
                        Point2D.Float location = insituRecord.getLocation();
                        double lon = location.getX();
                        double lat = location.getY();
                        S2LatLng s2LatLng = S2LatLng.fromDegrees(lat, lon);
                        s2Point = s2LatLng.toPoint();
                        s2CellId = S2CellId.fromPoint(s2Point);
                        level1Mask = (1 << (int) (s2CellId.id() >>> S2IndexCreatorMain.MASK_SHIFT));
                    }
                    if ((eoProduct.level1Mask & level1Mask) != 0) {
                        if (S2CellIdInteger.containsPoint(eoProduct.cellIds, s2CellId)) {
                            List<S2Point> candidateProducts = candidatesMap.get(eoProduct);
                            if (candidateProducts == null) {
                                candidateProducts = new ArrayList<>();
                                candidatesMap.put(eoProduct, candidateProducts);
                            }
                            candidateProducts.add(s2Point);
                        }
                    }
                }
            }
        }
        System.out.println("candidatesMap = " + candidatesMap.size());
    }

    List<S2IEoProduct> uniqueProductList = new ArrayList<>(candidatesMap.size());
    uniqueProductList.addAll(candidatesMap.keySet());
    Set<EoProduct> matches = new HashSet<>();
    try (StopWatch sw = new StopWatch("  >>load and test polygons")) {
        Collections.sort(uniqueProductList, (o1, o2) -> Integer.compare(o1.productID, o2.productID));
        try (DataInputStream dis = new DataInputStream(
                new BufferedInputStream(new FileInputStream(polygonFile)))) {
            int streamPID = 0;
            for (S2IEoProduct eoProduct : uniqueProductList) {
                final int productID = eoProduct.productID;
                while (streamPID < productID) {
                    int numLoopPoints = dis.readInt();
                    dis.skipBytes(numLoopPoints * 3 * 8);
                    streamPID++;
                }
                final int numLoopPoints = dis.readInt();
                final double[] pointData = new double[numLoopPoints * 3];
                for (int i = 0; i < pointData.length; i++) {
                    pointData[i] = dis.readDouble();
                }
                streamPID++;

                S2Polygon s2Polygon = S2IEoProduct.createS2Polygon(pointData);
                List<S2Point> s2Points = candidatesMap.get(eoProduct);
                for (S2Point s2Point : s2Points) {
                    if (s2Polygon.contains(s2Point)) {
                        matches.add(eoProduct);
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    return matches;
}