Search in sources :

Example 1 with Envelope2D

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

the class NTv2Test method writeSubGrid.

// ////////////////////////////////////////////////
// //////                                  ////////
// //////        TEST FILE CREATION        ////////
// //////                                  ////////
// ////////////////////////////////////////////////
/**
 * Writes a sub-grid of the given grid in pseudo-NTv2 format. This method is used only for creating the test file.
 * The file created by this method is not fully NTv2 compliant (in particular, we do not write complete header),
 * but we take this opportunity for testing {@code NTv2.Loader} capability to be lenient.
 *
 * <p>This method has been executed once for creating the {@code "NTF_R93-extract.gsb"} test file and should not
 * be needed anymore, but we keep it around in case we have new test files to generate. The parameter used for
 * creating the test file are:</p>
 *
 * <ul>
 *   <li>{@code gridX} = 72</li>
 *   <li>{@code gridY} = 74</li>
 *   <li>{@code nx}    =  6</li>
 *   <li>{@code ny}    =  7</li>
 * </ul>
 *
 * This ensure that the grid indices (75.7432814, 78.4451225) is included in the test file.
 * Those grid indices is the location of the (2°25′32.4187″N 48°50′40.2441″W) test point to interpolate.
 *
 * <div class="section">Limitations</div>
 * This method assumes that bounding box and increments have integer values, and that any fractional part
 * is rounding errors. This is usually the case when using the {@code "SECONDS"} unit of measurement.
 * This assumption does not apply to the shift values.
 *
 * @param  grid   the full grid from which to extract a few values.
 * @param  out    where to write the test file.
 * @param  gridX  index along the longitude axis of the first cell to write.
 * @param  gridY  index along the latitude axis of the first cell to write.
 * @param  nx     number of cells to write along the longitude axis.
 * @param  ny     number of cells to write along the latitude axis.
 * @throws TransformException if an error occurred while computing the envelope.
 * @throws IOException if an error occurred while writing the test file.
 */
public static void writeSubGrid(final DatumShiftGridFile<Angle, Angle> grid, final Path out, final int gridX, final int gridY, final int nx, final int ny) throws IOException, TransformException {
    Envelope envelope = new Envelope2D(null, gridX, gridY, nx - 1, ny - 1);
    envelope = Envelopes.transform(grid.getCoordinateToGrid().inverse(), envelope);
    final ByteBuffer buffer = ByteBuffer.allocate(4096);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    writeString(buffer, "NUM_OREC");
    buffer.putInt(5);
    nextRecord(buffer);
    writeString(buffer, "NUM_SREC");
    buffer.putInt(7);
    nextRecord(buffer);
    writeString(buffer, "NUM_FILE");
    buffer.putInt(1);
    nextRecord(buffer);
    writeString(buffer, "GS_TYPE");
    writeString(buffer, "SECONDS");
    // Last overview record.
    writeString(buffer, "VERSION");
    // Last overview record.
    writeString(buffer, "SIS_TEST");
    writeString(buffer, "S_LAT");
    buffer.putDouble(StrictMath.rint(envelope.getMinimum(1)));
    writeString(buffer, "N_LAT");
    buffer.putDouble(StrictMath.rint(envelope.getMaximum(1)));
    // Sign reversed.
    writeString(buffer, "E_LONG");
    // Sign reversed.
    buffer.putDouble(StrictMath.rint(-envelope.getMaximum(0)));
    writeString(buffer, "W_LONG");
    buffer.putDouble(StrictMath.rint(-envelope.getMinimum(0)));
    writeString(buffer, "LAT_INC");
    buffer.putDouble(StrictMath.rint(envelope.getSpan(1) / (ny - 1)));
    writeString(buffer, "LONG_INC");
    buffer.putDouble(StrictMath.rint(envelope.getSpan(0) / (nx - 1)));
    writeString(buffer, "GS_COUNT");
    buffer.putInt(nx * ny);
    nextRecord(buffer);
    for (int y = 0; y < ny; y++) {
        for (int x = 0; x < nx; x++) {
            buffer.putFloat((float) grid.getCellValue(1, gridX + x, gridY + y));
            buffer.putFloat((float) grid.getCellValue(0, gridX + x, gridY + y));
            buffer.putFloat(ACCURACY);
            buffer.putFloat(ACCURACY);
        }
    }
    writeString(buffer, "END");
    nextRecord(buffer);
    try (WritableByteChannel c = Files.newByteChannel(out, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
        buffer.flip();
        c.write(buffer);
    }
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) Envelope(org.opengis.geometry.Envelope) Envelope2D(org.apache.sis.geometry.Envelope2D) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Envelope2D

use of org.apache.sis.geometry.Envelope2D 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 3 with Envelope2D

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

the class MatricesTest method testCreateTransformFromEnvelopes.

/**
 * Tests {@link Matrices#createTransform(Envelope, Envelope)}.
 * This method tests the example given in {@code Matrices.createTransform(…)} javadoc.
 */
@Test
public void testCreateTransformFromEnvelopes() {
    final Envelope srcEnvelope = new Envelope2D(null, -20, -40, 100, 200);
    final Envelope dstEnvelope = new Envelope2D(null, -10, -25, 300, 500);
    MatrixSIS matrix = Matrices.createTransform(srcEnvelope, dstEnvelope);
    assertTrue("isAffine", matrix.isAffine());
    assertFalse("isIdentity", matrix.isIdentity());
    assertEquals("numRow", 3, matrix.getNumRow());
    assertEquals("numCol", 3, matrix.getNumCol());
    assertEquals(Matrices.create(3, 3, new double[] { 3.0, 0, 50, 0, 2.5, 75, 0, 0, 1 }), matrix);
    /*
         * Test dropping a dimension.
         */
    final GeneralEnvelope expanded = new GeneralEnvelope(3);
    expanded.subEnvelope(0, 2).setEnvelope(srcEnvelope);
    expanded.setRange(2, 1000, 2000);
    matrix = Matrices.createTransform(expanded, dstEnvelope);
    assertEquals("numRow", 3, matrix.getNumRow());
    assertEquals("numCol", 4, matrix.getNumCol());
    assertEquals(Matrices.create(3, 4, new double[] { 3.0, 0, 0, 50, 0, 2.5, 0, 75, 0, 0, 0, 1 }), matrix);
    /*
         * Test adding a dimension with ordinate values set to zero.
         */
    expanded.subEnvelope(0, 2).setEnvelope(dstEnvelope);
    matrix = Matrices.createTransform(srcEnvelope, expanded);
    assertEquals("numRow", 4, matrix.getNumRow());
    assertEquals("numCol", 3, matrix.getNumCol());
    assertEquals(Matrices.create(4, 3, new double[] { 3.0, 0, 50, 0, 2.5, 75, 0, 0, 0, 0, 0, 1 }), matrix);
}
Also used : Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Envelope2D(org.apache.sis.geometry.Envelope2D) Test(org.junit.Test)

Example 4 with Envelope2D

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

the class MatricesTest method testCreateTransformFromEnvelopesAndAxes.

/**
 * Tests {@link Matrices#createTransform(Envelope, AxisDirection[], Envelope, AxisDirection[])}.
 * This method tests the example given in {@code Matrices.createTransform(…)} javadoc.
 */
@Test
@DependsOnMethod({ "testCreateTransformFromEnvelopes", "testCreateTransformWithLessAxes" })
public void testCreateTransformFromEnvelopesAndAxes() {
    // swapped (y,-x)
    final Envelope srcEnvelope = new Envelope2D(null, -40, +20, 200, 100);
    final Envelope dstEnvelope = new Envelope2D(null, -10, -25, 300, 500);
    MatrixSIS matrix = Matrices.createTransform(srcEnvelope, new AxisDirection[] { NORTH, WEST }, dstEnvelope, new AxisDirection[] { EAST, NORTH });
    assertTrue("isAffine", matrix.isAffine());
    assertFalse("isIdentity", matrix.isIdentity());
    assertEquals("numRow", 3, matrix.getNumRow());
    assertEquals("numCol", 3, matrix.getNumCol());
    assertMatrixEquals("(N,E) → (E,N)", Matrices.create(3, 3, new double[] { 0, -3.0, 350, 2.5, 0, 75, 0, 0, 1 }), matrix, STRICT);
    /*
         * Test dropping a dimension.
         */
    final GeneralEnvelope expanded = new GeneralEnvelope(3);
    expanded.subEnvelope(0, 2).setEnvelope(srcEnvelope);
    expanded.setRange(2, 1000, 2000);
    matrix = Matrices.createTransform(expanded, new AxisDirection[] { NORTH, WEST, UP }, dstEnvelope, new AxisDirection[] { EAST, NORTH });
    assertEquals("numRow", 3, matrix.getNumRow());
    assertEquals("numCol", 4, matrix.getNumCol());
    assertMatrixEquals("(N,E,U) → (E,N)", Matrices.create(3, 4, new double[] { 0, -3.0, 0, 350, 2.5, 0, 0, 75, 0, 0, 0, 1 }), matrix, STRICT);
}
Also used : AxisDirection(org.opengis.referencing.cs.AxisDirection) Envelope(org.opengis.geometry.Envelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Envelope2D(org.apache.sis.geometry.Envelope2D) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 5 with Envelope2D

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

the class LocationViewer method main.

/**
 * Shows the locations tested by {@code MilitaryGridReferenceSystemTest.testIterator()} methods.
 *
 * @param  args  ignored.
 * @throws Exception if an error occurred while transforming an envelope to the display CRS.
 */
public static void main(final String[] args) throws Exception {
    final MilitaryGridReferenceSystem.Coder coder = new MilitaryGridReferenceSystem().createCoder();
    coder.setPrecision(100000);
    switch(1) {
        /*
             * UTM North: 3 zones (31, 32 and 33) and 3 latitude bands (T, U and V).
             * Include the Norway special case: in latitude band V, zone 32 is widened at the expense of zone 31.
             */
        case 1:
            {
                show("UTM zones 31, 32 and 33 North", coder, new Envelope2D(CommonCRS.defaultGeographic(), 5, 47, 8, 10), CommonCRS.WGS84.universal(1, 9));
                break;
            }
        /*
             * UTM South: 3 zones (31, 32 and 33) and 2 latitude bands (G and H).
             */
        case 2:
            {
                show("UTM zones 31, 32 and 33 South", coder, new Envelope2D(CommonCRS.defaultGeographic(), 5, -42, 8, 4), CommonCRS.WGS84.universal(1, 9));
                break;
            }
        /*
             * Crossing the anti-meridian. There is two columns of cells: on the west side and on the east side.
             */
        case 3:
            {
                final GeneralEnvelope ge = new GeneralEnvelope(CommonCRS.defaultGeographic());
                ge.setRange(0, 170, -175);
                ge.setRange(1, 40, 42);
                show("15° of longitude spanning the anti-meridian", coder, ge, null);
                break;
            }
        /*
             * Complete North pole case surrounded by part of V latitude band.
             * This include the Svalbard special case in zones 31 to 37.
             */
        case 4:
            {
                show("North pole surrounded by V latitude band", coder, new Envelope2D(CommonCRS.defaultGeographic(), -180, 80, 360, 10), CommonCRS.WGS84.universal(90, 0));
                break;
            }
        /*
             * Complete South pole case surrounded by one latitude band.
             */
        case 5:
            {
                show("South pole surrounded by C latitude band", coder, new Envelope2D(CommonCRS.defaultGeographic(), -180, -90, 360, 12), CommonCRS.WGS84.universal(-90, 0));
                break;
            }
        /*
             * Partial North pole case.
             */
        case 6:
            {
                show("10°W to 70°E close to North pole", coder, new Envelope2D(CommonCRS.defaultGeographic(), -10, 85, 80, 5), CommonCRS.WGS84.universal(90, 0));
                break;
            }
        /*
             * Partial South pole case with zone UTM zones.
             */
        case 7:
            {
                show("70°W to 120°W close to South pole", coder, new Envelope2D(CommonCRS.defaultGeographic(), -120, -83, 50, 5), CommonCRS.WGS84.universal(-90, 0));
                break;
            }
    }
}
Also used : GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) Envelope2D(org.apache.sis.geometry.Envelope2D)

Aggregations

Envelope2D (org.apache.sis.geometry.Envelope2D)8 Envelope (org.opengis.geometry.Envelope)5 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)3 DirectPosition2D (org.apache.sis.geometry.DirectPosition2D)2 Test (org.junit.Test)2 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedWriter (java.io.BufferedWriter)1 PrintWriter (java.io.PrintWriter)1 ByteBuffer (java.nio.ByteBuffer)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 QuadTreeData (org.apache.sis.index.tree.QuadTreeData)1 DependsOnMethod (org.apache.sis.test.DependsOnMethod)1 AxisDirection (org.opengis.referencing.cs.AxisDirection)1 MathTransform (org.opengis.referencing.operation.MathTransform)1