Пример #1
0
 /**
  * Tests all map projection creation.
  *
  * @throws FactoryException If a CRS can not be created.
  */
 @Test
 public void testMapProjections() throws FactoryException {
   out.println();
   out.println("Testing classification names");
   out.println("----------------------------");
   final CRSFactory crsFactory = ReferencingFactoryFinder.getCRSFactory(null);
   final MathTransformFactory mtFactory = ReferencingFactoryFinder.getMathTransformFactory(null);
   final Collection<OperationMethod> methods = mtFactory.getAvailableMethods(Projection.class);
   final Map<String, ?> dummyName = Collections.singletonMap("name", "Test");
   for (final OperationMethod method : methods) {
     final String classification = method.getName().getCode();
     final ParameterValueGroup param = mtFactory.getDefaultParameters(classification);
     try {
       param.parameter("semi_major").setValue(6377563.396);
       param.parameter("semi_minor").setValue(6356256.909237285);
     } catch (IllegalArgumentException e) {
       // Above parameters do not exists. Ignore.
     }
     final MathTransform mt;
     try {
       mt = mtFactory.createParameterizedTransform(param);
     } catch (FactoryException e) {
       // Probably not a map projection. This test is mostly about projection, so ignore.
       continue;
     } catch (UnsupportedOperationException e) {
       continue;
     }
     if (mt instanceof MapProjection) {
       /*
        * Tests map projection properties. Some tests are ommitted for south-oriented
        * map projections, since they are implemented as a concatenation of their North-
        * oriented variants with an affine transform.
        */
       out.println(classification);
       final boolean skip =
           classification.equalsIgnoreCase("Transverse Mercator (South Orientated)")
               || classification.equalsIgnoreCase("Equidistant_Cylindrical");
       if (!skip) {
         assertEquals(
             classification, ((MapProjection) mt).getParameterDescriptors().getName().getCode());
       }
       final ProjectedCRS projCRS =
           crsFactory.createProjectedCRS(
               dummyName,
               DefaultGeographicCRS.WGS84,
               new DefiningConversion(dummyName, method, mt),
               DefaultCartesianCS.PROJECTED);
       final Conversion conversion = projCRS.getConversionFromBase();
       assertSame(mt, conversion.getMathTransform());
       final OperationMethod projMethod = conversion.getMethod();
       assertEquals(classification, projMethod.getName().getCode());
     }
   }
 }
  public void testRead() throws Exception {
    URL url = TestData.url("shapes/pointtest.shp");
    ShapefileDataStore ds =
        (ShapefileDataStore) new ShapefileDataStoreFactory().createDataStore(url);

    Envelope env = ds.getFeatureSource().getBounds();
    CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
    MathTransform mt = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84);

    Rectangle rectangle = new Rectangle(300, 0, 300, 300);
    AffineTransform transform = RendererUtilities.worldToScreenTransform(env, rectangle);
    GeneralMatrix matrix = new GeneralMatrix(transform);
    MathTransform at =
        ReferencingFactoryFinder.getMathTransformFactory(null).createAffineTransform(matrix);
    mt = ReferencingFactoryFinder.getMathTransformFactory(null).createConcatenatedTransform(mt, at);

    ShapefileReader reader =
        new ShapefileReader(ShapefileRendererUtil.getShpFiles(ds), false, false);
    reader.setHandler(
        new org.geotools.renderer.shape.shapehandler.jts.PointHandler(
            reader.getHeader().getShapeType(), env, rectangle, mt, false));

    Object shape = reader.nextRecord().shape();
    assertNotNull(shape);
    assertTrue(shape instanceof Geometry);
    Coordinate[] coords = ((Geometry) shape).getCoordinates();
    for (int i = 0; i < coords.length; i++) {
      Coordinate coordinate = coords[i];
      assertNotNull(coordinate);
    }

    int i = 0;
    while (reader.hasNext()) {
      i++;
      shape = reader.nextRecord().shape();
      assertNotNull(shape);
      assertTrue(shape instanceof Geometry);
    }
    assertEquals(ds.getFeatureSource().getCount(Query.ALL) - 1, i);
  }
  private static MathTransform createTransverseMercatorMathTransform(
      TransverseMercatorDescriptor.TMT t) throws FactoryException {

    final MathTransformFactory transformFactory =
        ReferencingFactoryFinder.getMathTransformFactory(null);
    final ParameterValueGroup parameters = transformFactory.getDefaultParameters("EPSG:9807");

    parameters.parameter("semi_major").setValue(t.getSemiMajor());
    parameters.parameter("semi_minor").setValue(t.getSemiMinor());
    parameters.parameter("central_meridian").setValue(t.getCentralMeridian());
    parameters.parameter("latitude_of_origin").setValue(t.getLatitudeOfOrigin());
    parameters.parameter("scale_factor").setValue(t.getScaleFactor());
    parameters.parameter("false_easting").setValue(t.getFalseEasting());
    parameters.parameter("false_northing").setValue(t.getFalseNorthing());

    return transformFactory.createParameterizedTransform(parameters);
  }
  private static MathTransform createAlbersConicEqualAreaMathTransform(
      AlbersEqualAreaConicDescriptor.AEAC t) throws FactoryException {
    final MathTransformFactory transformFactory =
        ReferencingFactoryFinder.getMathTransformFactory(null);
    final ParameterValueGroup parameters = transformFactory.getDefaultParameters("EPSG:9822");

    parameters.parameter("semi_major").setValue(t.getSemiMajor());
    parameters.parameter("semi_minor").setValue(t.getSemiMinor());
    parameters.parameter("central_meridian").setValue(t.getCentralMeridian());
    parameters.parameter("latitude_of_origin").setValue(t.getLatitudeOfOrigin());
    parameters.parameter("standard_parallel_1").setValue(t.getStandardParallel1());
    parameters.parameter("standard_parallel_2").setValue(t.getStandardParallel2());
    parameters.parameter("false_easting").setValue(t.getFalseEasting());
    parameters.parameter("false_northing").setValue(t.getFalseNorthing());

    return transformFactory.createParameterizedTransform(parameters);
  }
  public static void attachGeoCoding(
      Product p,
      double upperLeftLon,
      double upperLeftLat,
      double lowerRightLon,
      double lowerRightLat,
      String projection,
      double[] projectionParameter) {
    double pixelSizeX = (lowerRightLon - upperLeftLon) / p.getSceneRasterWidth();
    double pixelSizeY = (upperLeftLat - lowerRightLat) / p.getSceneRasterHeight();

    AffineTransform transform = new AffineTransform();
    transform.translate(upperLeftLon, upperLeftLat);
    transform.scale(pixelSizeX, -pixelSizeY);
    transform.translate(PIXEL_CENTER, PIXEL_CENTER);
    Rectangle imageBounds = new Rectangle(p.getSceneRasterWidth(), p.getSceneRasterHeight());

    if (projection.equals("GCTP_GEO")) {
      if ((upperLeftLon >= -180 && upperLeftLon <= 180)
          && (upperLeftLat >= -90 && upperLeftLat <= 90)
          && (lowerRightLon >= -180 && lowerRightLon <= 180)
          && (lowerRightLat >= -90 && lowerRightLat <= 90)) {
        try {
          p.setGeoCoding(new CrsGeoCoding(DefaultGeographicCRS.WGS84, imageBounds, transform));
        } catch (FactoryException | TransformException ignore) {
        }
      }
    } else {
      if (projection.equals("GCTP_SNSOID")) {
        final MathTransformFactory transformFactory =
            ReferencingFactoryFinder.getMathTransformFactory(null);
        ParameterValueGroup parameters;
        try {
          parameters = transformFactory.getDefaultParameters("OGC:Sinusoidal");
        } catch (NoSuchIdentifierException ignore) {
          return;
        }
        double semi_major;
        double semi_minor;
        if (projectionParameter != null) {
          semi_major = projectionParameter[0];
          semi_minor = projectionParameter[1];
          if (semi_minor == 0) {
            semi_minor = semi_major;
          }
        } else {
          Ellipsoid ellipsoid = DefaultGeographicCRS.WGS84.getDatum().getEllipsoid();
          semi_major = ellipsoid.getSemiMajorAxis();
          semi_minor = ellipsoid.getSemiMinorAxis();
        }
        parameters.parameter("semi_major").setValue(semi_major);
        parameters.parameter("semi_minor").setValue(semi_minor);

        MathTransform mathTransform;
        try {
          mathTransform = transformFactory.createParameterizedTransform(parameters);
        } catch (Exception ignore) {
          return;
        }

        DefaultGeographicCRS base = DefaultGeographicCRS.WGS84;
        CoordinateReferenceSystem modelCrs =
            new DefaultProjectedCRS(
                "Sinusoidal", base, mathTransform, DefaultCartesianCS.PROJECTED);
        try {
          CrsGeoCoding geoCoding = new CrsGeoCoding(modelCrs, imageBounds, transform);

          p.setGeoCoding(geoCoding);
        } catch (Exception ignore) {
        }
      }
    }
  }
Пример #6
0
  /**
   * Tests the creation of new coordinate reference systems.
   *
   * @throws FactoryException if a coordinate reference system can't be created.
   */
  @Test
  public void testCreation() throws FactoryException {
    out.println();
    out.println("Testing CRS creations");
    out.println("---------------------");
    out.println();
    out.println("create Coodinate Reference System....1: ");
    final DatumFactory datumFactory = ReferencingFactoryFinder.getDatumFactory(null);
    final CSFactory csFactory = ReferencingFactoryFinder.getCSFactory(null);
    final CRSFactory crsFactory = ReferencingFactoryFinder.getCRSFactory(null);
    final MathTransformFactory mtFactory = ReferencingFactoryFinder.getMathTransformFactory(null);

    final Ellipsoid airy1830;
    final Unit<Length> meters = SI.METER;
    airy1830 = datumFactory.createEllipsoid(name("Airy1830"), 6377563.396, 6356256.910, meters);
    out.println();
    out.println("create Coodinate Reference System....2: ");
    out.println(airy1830.toWKT());

    final PrimeMeridian greenwich;
    final Unit<Angle> degrees = NonSI.DEGREE_ANGLE;
    greenwich = datumFactory.createPrimeMeridian(name("Greenwich"), 0, degrees);
    out.println();
    out.println("create Coodinate Reference System....3: ");
    out.println(greenwich.toWKT());

    // NOTE: we could use the following pre-defined constant instead:
    //       DefaultPrimeMeridian.GREENWICH;
    final GeodeticDatum datum;
    datum = datumFactory.createGeodeticDatum(name("Airy1830"), airy1830, greenwich);
    out.println();
    out.println("create Coodinate Reference System....4: ");
    out.println(datum.toWKT());

    // NOTE: we could use the following pre-defined constant instead:
    //       DefaultEllipsoidalCS.GEODETIC_2D;
    final EllipsoidalCS ellCS;
    ellCS =
        csFactory.createEllipsoidalCS(
            name("Ellipsoidal"),
            csFactory.createCoordinateSystemAxis(
                name("Longitude"), "long", AxisDirection.EAST, degrees),
            csFactory.createCoordinateSystemAxis(
                name("Latitude"), "lat", AxisDirection.NORTH, degrees));
    out.println();
    out.println("create Coodinate Reference System....5: ");
    out.println(ellCS); // No WKT for coordinate systems

    final GeographicCRS geogCRS;
    geogCRS = crsFactory.createGeographicCRS(name("Airy1830"), datum, ellCS);
    out.println();
    out.println("create Coodinate Reference System....6: ");
    out.println(geogCRS.toWKT());

    final MathTransform p;
    final ParameterValueGroup param = mtFactory.getDefaultParameters("Transverse_Mercator");
    param.parameter("semi_major").setValue(airy1830.getSemiMajorAxis());
    param.parameter("semi_minor").setValue(airy1830.getSemiMinorAxis());
    param.parameter("central_meridian").setValue(49);
    param.parameter("latitude_of_origin").setValue(-2);
    param.parameter("false_easting").setValue(400000);
    param.parameter("false_northing").setValue(-100000);
    out.println();
    out.println("create Coodinate System....7: ");
    out.println(param);

    // NOTE: we could use the following pre-defined constant instead:
    //       DefaultCartesianCS.PROJECTED;
    final CartesianCS cartCS;
    cartCS =
        csFactory.createCartesianCS(
            name("Cartesian"),
            csFactory.createCoordinateSystemAxis(name("Easting"), "x", AxisDirection.EAST, meters),
            csFactory.createCoordinateSystemAxis(
                name("Northing"), "y", AxisDirection.NORTH, meters));
    out.println();
    out.println("create Coodinate Reference System....8: ");
    out.println(cartCS); // No WKT for coordinate systems

    final Hints hints = new Hints();
    hints.put(Hints.DATUM_FACTORY, datumFactory);
    hints.put(Hints.CS_FACTORY, csFactory);
    hints.put(Hints.CRS_FACTORY, crsFactory);
    hints.put(Hints.MATH_TRANSFORM_FACTORY, mtFactory);

    final ReferencingFactoryContainer container = new ReferencingFactoryContainer(hints);
    assertSame(datumFactory, container.getDatumFactory());
    assertSame(csFactory, container.getCSFactory());
    assertSame(crsFactory, container.getCRSFactory());
    assertSame(mtFactory, container.getMathTransformFactory());

    final Conversion conversion = new DefiningConversion("GBN grid", param);
    final ProjectedCRS projCRS =
        crsFactory.createProjectedCRS(
            name("Great_Britian_National_Grid"), geogCRS, conversion, cartCS);
    out.println();
    out.println("create Coodinate System....9: ");
    out.println(projCRS.toWKT());
  }