Search in sources :

Example 16 with DirectPosition2D

use of org.apache.sis.geometry.DirectPosition2D in project sis by apache.

the class LocationServlet method doGet.

/**
 * Provide GET requests for Bounding-box and Point-radius search queries.
 * Return search results to client in xml format.
 *
 * @param request
 *          Http Servlet Request
 * @param response
 *          Http Servlet Response
 * @exception ServletException
 *              General exception for servlet
 * @exception IOException
 *              General exception for I/O
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long beforeTime = 0;
    long afterTime = 0;
    response.setContentType("text/xml");
    PrintWriter out = response.getWriter();
    String type = request.getParameter("type");
    List<QuadTreeData> results = new ArrayList<QuadTreeData>();
    List<String> regions = new ArrayList<String>();
    if (type != null && type.equals("bbox")) {
        String llLat = request.getParameter("llLat");
        String llLon = request.getParameter("llLon");
        String urLat = request.getParameter("urLat");
        String urLon = request.getParameter("urLon");
        if (llLat != null && llLon != null && urLat != null && urLon != null) {
            try {
                Envelope2D bbox = new Envelope2D(new DirectPosition2D(Double.parseDouble(llLon), Double.parseDouble(llLat)), new DirectPosition2D(Double.parseDouble(urLon), Double.parseDouble(urLat)));
                beforeTime = System.currentTimeMillis();
                results = tree.queryByBoundingBox(bbox);
                afterTime = System.currentTimeMillis();
                // get the polygon that approximates the region
                Rectangle2D[] rects = bbox.toRectangles();
                for (int i = 0; i < rects.length; i++) {
                    final Rectangle2D r = rects[i];
                    String regionStr = (r.getMinY()) + "," + (r.getMinX()) + ",";
                    regionStr += (r.getMaxY()) + "," + (r.getMinX()) + ",";
                    regionStr += (r.getMaxY()) + "," + (r.getMaxX()) + ",";
                    regionStr += (r.getMinY()) + "," + (r.getMaxX()) + ",";
                    regionStr += (r.getMinY()) + "," + (r.getMinX());
                    regions.add(regionStr);
                }
            } catch (NumberFormatException ex) {
                System.out.println("[ERROR] Input parameters were not valid latitudes and longitudes");
            }
        }
    } else if (type != null && type.equals("pointradius")) {
        String radius = request.getParameter("radius");
        String lat = request.getParameter("lat");
        String lon = request.getParameter("lon");
        if (radius != null && lat != null && lon != null) {
            DirectPosition2D point = null;
            try {
                point = new DirectPosition2D(Double.parseDouble(lon), Double.parseDouble(lat));
            } catch (NumberFormatException ex) {
                System.out.println("{ERROR] Input parameters were not valid latitudes and longitudes");
            }
            double radiusKM = Double.parseDouble(radius);
            String regionStr = "";
            for (int i = 0; i < 360; i += 10) {
                DirectPosition2D pt = DistanceUtils.getPointOnGreatCircle(point.y, point.x, radiusKM, i);
                regionStr += pt.y + "," + pt.x + ",";
            }
            DirectPosition2D pt = DistanceUtils.getPointOnGreatCircle(point.y, point.x, radiusKM, 0);
            regionStr += pt.y + "," + pt.x + ",";
            regions.add(regionStr.substring(0, regionStr.length() - 1));
            beforeTime = System.currentTimeMillis();
            results = tree.queryByPointRadius(point, radiusKM);
            afterTime = System.currentTimeMillis();
        }
    }
    long timeSeconds = afterTime - beforeTime;
    // return matches from tree in xml format to client
    out.write(buildXML(results, regions, timeSeconds));
    out.close();
}
Also used : ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) Envelope2D(org.apache.sis.geometry.Envelope2D) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) QuadTreeData(org.apache.sis.index.tree.QuadTreeData) PrintWriter(java.io.PrintWriter)

Example 17 with DirectPosition2D

use of org.apache.sis.geometry.DirectPosition2D in project jena by apache.

the class GeometryWrapper method getUTMZoneURI.

/**
 * @return URI of the GeometryWrapper's UTM zone
 * @throws FactoryException
 * @throws MismatchedDimensionException
 * @throws TransformException
 */
public String getUTMZoneURI() throws FactoryException, MismatchedDimensionException, TransformException {
    if (utmURI == null) {
        // Find a point in the parsing geometry so can directly apply the SRS.
        Point coord = parsingGeometry.getCentroid();
        DirectPosition2D point = new DirectPosition2D(coord.getX(), coord.getY());
        // Convert to WGS84. Use WGS84 and not CRS84 as assuming WGS8 is more prevalent.
        CoordinateReferenceSystem wgs84CRS = SRSRegistry.getCRS(SRS_URI.WGS84_CRS);
        MathTransform transform = MathTransformRegistry.getMathTransform(srsInfo.getCrs(), wgs84CRS);
        DirectPosition wgs84Point = transform.transform(point, null);
        // Find the UTM zone.
        utmURI = SRSRegistry.findUTMZoneURIFromWGS84(wgs84Point.getOrdinate(0), wgs84Point.getOrdinate(1));
    }
    return utmURI;
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) MathTransform(org.opengis.referencing.operation.MathTransform) Point(org.locationtech.jts.geom.Point) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D)

Example 18 with DirectPosition2D

use of org.apache.sis.geometry.DirectPosition2D in project sis by apache.

the class LatLonPointRadius method getRectangularRegionApproximation.

/**
 * Calculates the rectangular region enclosing the circular search region.
 *
 * @param numberOfPoints
 *          the number of points used to estimate the circular search region
 * @return Java Rectangle2D object that bounds the circlar search region
 */
public Rectangle2D getRectangularRegionApproximation(int numberOfPoints) {
    if (radius >= DistanceUtils.HALF_EARTH_CIRCUMFERENCE) {
        return new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0);
    }
    int numberOfCrossOvers = 0;
    Path2D path = new Path2D.Double();
    DirectPosition2D initPT = DistanceUtils.getPointOnGreatCircle(center.getOrdinate(1), center.getOrdinate(0), radius, 0);
    path.moveTo(initPT.x + 180.0, initPT.y + 90.0);
    DirectPosition2D currPT = initPT;
    for (int i = 1; i < 360; i++) {
        DirectPosition2D pt = DistanceUtils.getPointOnGreatCircle(center.getOrdinate(1), center.getOrdinate(0), radius, i);
        path.lineTo(pt.x + 180.0, pt.y + 90.0);
        if (dateLineCrossOver(Longitude.normalize(currPT.x), Longitude.normalize(pt.x))) {
            numberOfCrossOvers++;
        }
        currPT = pt;
    }
    if (dateLineCrossOver(Longitude.normalize(initPT.x), Longitude.normalize(currPT.x))) {
        numberOfCrossOvers++;
    }
    /**
     * If the path crosses the dateline once, it's a special case, so take care
     * of it differently. It will need to include areas around the pole.
     */
    if (numberOfCrossOvers == 1) {
        Rectangle2D r = path.getBounds2D();
        Rectangle2D lowerHalf = new Rectangle2D.Double(0.0, 0.0, 360.0, r.getMaxY());
        if (lowerHalf.contains(center.getOrdinate(0) + 180.0, center.getOrdinate(1) + 90.0)) {
            return lowerHalf;
        } else {
            return new Rectangle2D.Double(0.0, r.getMinY(), 360.0, 180.0 - r.getMinY());
        }
    }
    if (path.contains(center.getOrdinate(0) + 180.0, center.getOrdinate(1) + 90.0)) {
        Rectangle2D r = path.getBounds2D();
        if ((r.getMaxX() - r.getMinX()) > 359.0) {
            return new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0);
        } else if (r.getMinX() < 0 || r.getMaxX() > 360.0) {
            /**
             * For circles that crosses the dateline instead of splitting in half
             * and having to go down the tree twice, for first version span
             * longitude 360.0 and use the exact height of the box
             */
            return new Rectangle2D.Double(0.0, r.getY(), 360.0, r.getHeight());
        } else {
            return path.getBounds2D();
        }
    } else {
        Area pathArea = new Area(path);
        Area wholeMap = new Area(new Rectangle2D.Double(0.0, 0.0, 360.0, 180.0));
        wholeMap.subtract(pathArea);
        return wholeMap.getBounds2D();
    }
}
Also used : Area(java.awt.geom.Area) Path2D(java.awt.geom.Path2D) Rectangle2D(java.awt.geom.Rectangle2D) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D)

Example 19 with DirectPosition2D

use of org.apache.sis.geometry.DirectPosition2D in project sis by apache.

the class MilitaryGridReferenceSystem method polarOffset.

/**
 * Returns the value to add to the row number in order to have the "A" letter on the southernmost or
 * northernmost value on Greenwich meridian of the Universal Polar Stereographic (UPS) projection.
 * If {@code south} is {@code true}, then this is computed from the northernmost value of UPS South;
 * otherwise this is computed from the southernmost value of UPS North. This value is derived from
 * the bottom of the 100 kilometres square labeled "A" in Grid Zone Designations A, B, Y and Z.
 */
final int polarOffset(final boolean south) throws TransformException {
    // No need to synchronized; not a big deal if computed twice.
    short origin = south ? southOffset : northOffset;
    if (origin == 0) {
        final DirectPosition2D position = new DirectPosition2D(south ? TransverseMercator.Zoner.SOUTH_BOUNDS : TransverseMercator.Zoner.NORTH_BOUNDS, 0);
        double northing = datum.universal(position.x * 1.01, position.y).getConversionFromBase().getMathTransform().transform(position, position).getOrdinate(1);
        if (south) {
            northing = 2 * PolarStereographicA.UPS_SHIFT - northing;
        }
        northing = Math.floor(northing / GRID_SQUARE_SIZE);
        origin = (short) northing;
        if (origin != northing) {
            // Paranoiac check (should never happen).
            throw new GazetteerException();
        }
        if (south) {
            southOffset = origin;
        } else {
            northOffset = origin;
        }
    }
    return origin;
}
Also used : DirectPosition2D(org.apache.sis.geometry.DirectPosition2D)

Example 20 with DirectPosition2D

use of org.apache.sis.geometry.DirectPosition2D in project sis by apache.

the class MilitaryGridReferenceSystemTest method testEncodeUTM.

/**
 * Tests encoding of various coordinates in Universal Transverse Mercator (UTM) projection.
 *
 * @throws TransformException if an error occurred while computing the MGRS label.
 */
@Test
@DependsOnMethod({ "verifyInvariants", "testLatitudeBand" })
public void testEncodeUTM() throws TransformException {
    final MilitaryGridReferenceSystem.Coder coder = coder();
    final DirectPosition2D position = new DirectPosition2D();
    /*
         * 41°N 10°E (UTM zone 32)
         */
    position.setCoordinateReferenceSystem(CommonCRS.WGS84.universal(41, 10));
    position.x = 584102;
    position.y = 4539239;
    assertEquals("32TNL8410239239", coder.encode(position));
    /*
         * 82°N 10°W (UTM zone 29) — should instantiate a new MilitaryGridReferenceSystem.Encoder.
         */
    position.setCoordinateReferenceSystem(CommonCRS.WGS84.universal(82, -10));
    position.x = 484463;
    position.y = 9104963;
    assertEquals("29XMM8446304963", coder.encode(position));
    /*
         * 41°S 10°E (UTM zone 32) — should reuse the Encoder created in first test.
         */
    position.setCoordinateReferenceSystem(CommonCRS.WGS84.universal(-41, 10));
    position.x = 584102;
    position.y = 5460761;
    assertEquals("32GNV8410260761", coder.encode(position));
    /*
         * 82°N 10°E (UTM zone 32) — in this special case, zone 32 is replaced by zone 33.
         * Call to WGS84.UTM(φ,λ) needs to specify a smaller latitude for getting zone 32.
         */
    position.setCoordinateReferenceSystem(CommonCRS.WGS84.universal(40, 10));
    position.x = 515537;
    position.y = 9104963;
    assertEquals("33XVM2240708183", coder.encode(position));
    /*
         * Same position as previously tested, but using geographic coordinates.
         */
    position.setCoordinateReferenceSystem(CommonCRS.WGS84.geographic());
    position.x = 82;
    position.y = 10;
    assertEquals("33XVM2240608183", coder.encode(position));
    position.x = -41;
    position.y = 10;
    assertEquals("32GNV8410260761", coder.encode(position));
}
Also used : DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Aggregations

DirectPosition2D (org.apache.sis.geometry.DirectPosition2D)25 Test (org.junit.Test)12 DependsOnMethod (org.apache.sis.test.DependsOnMethod)7 DirectPosition (org.opengis.geometry.DirectPosition)4 Rectangle2D (java.awt.geom.Rectangle2D)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Envelope2D (org.apache.sis.geometry.Envelope2D)2 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)2 MathTransform (org.opengis.referencing.operation.MathTransform)2 Matrix (org.opengis.referencing.operation.Matrix)2 WireFeed (com.sun.syndication.feed.WireFeed)1 GeoRSSModule (com.sun.syndication.feed.module.georss.GeoRSSModule)1 Channel (com.sun.syndication.feed.rss.Channel)1 Item (com.sun.syndication.feed.rss.Item)1 WireFeedInput (com.sun.syndication.io.WireFeedInput)1 XmlReader (com.sun.syndication.io.XmlReader)1 AffineTransform (java.awt.geom.AffineTransform)1