Java Code Examples for org.locationtech.jts.geom.Geometry#setUserData()

The following examples show how to use org.locationtech.jts.geom.Geometry#setUserData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JTS.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Transforms the given geometry using the given coordinate operation.
 * If the geometry or the operation is null, then the geometry is returned unchanged.
 * If the source CRS is not equals to the geometry CRS, a new operation is inferred.
 *
 * @todo Handle antimeridian case.
 *
 * @param  geometry   the geometry to transform, or {@code null}.
 * @param  operation  the coordinate operation to apply, or {@code null}.
 * @return the transformed geometry, or the same geometry if it is already in target CRS.
 * @throws FactoryException if transformation to the target CRS can not be constructed.
 * @throws TransformException if the given geometry can not be transformed.
 */
public static Geometry transform(Geometry geometry, CoordinateOperation operation)
        throws FactoryException, TransformException
{
    if (geometry != null && operation != null) {
        final CoordinateReferenceSystem sourceCRS = operation.getSourceCRS();
        if (sourceCRS != null) {
            final CoordinateReferenceSystem crs = getCoordinateReferenceSystem(geometry);
            if (crs != null && !Utilities.equalsIgnoreMetadata(sourceCRS, crs)) {
                operation = findOperation(crs, operation.getTargetCRS(), geometry);
            }
        }
        geometry = transform(geometry, operation.getMathTransform());
        geometry.setUserData(operation.getTargetCRS());
    }
    return geometry;
}
 
Example 2
Source File: JTSTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link JTS#getCoordinateReferenceSystem(Geometry)}.
 *
 * @throws FactoryException if an EPSG code can not be resolved.
 */
@Test
public void testGetCoordinateReferenceSystem() throws FactoryException {
    final GeometryFactory factory = new GeometryFactory();
    final Geometry geometry = factory.createPoint(new Coordinate(5, 6));

    CoordinateReferenceSystem crs = JTS.getCoordinateReferenceSystem(geometry);
    assertNull(crs);
    /*
     * Test CRS as user data.
     */
    geometry.setUserData(CommonCRS.ED50.geographic());
    assertEquals(CommonCRS.ED50.geographic(), JTS.getCoordinateReferenceSystem(geometry));
    /*
     * Test CRS as map value.
     */
    geometry.setUserData(Collections.singletonMap(JTS.CRS_KEY, CommonCRS.NAD83.geographic()));
    assertEquals(CommonCRS.NAD83.geographic(), JTS.getCoordinateReferenceSystem(geometry));
    /*
     * Test CRS as srid.
     */
    geometry.setUserData(null);
    geometry.setSRID(4326);
    assertEquals(CommonCRS.WGS84.geographic(), JTS.getCoordinateReferenceSystem(geometry));
}
 
Example 3
Source File: JTS.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Copies coordinate reference system information from the given source geometry to the target geometry.
 * Current implementation copies only CRS information, but future implementations could copy some other
 * values if they may apply to the target geometry as well.
 */
private static void copyMetadata(final Geometry source, final Geometry target) {
    target.setSRID(source.getSRID());
    Object crs = source.getUserData();
    if (!(crs instanceof CoordinateReferenceSystem)) {
        if (!(crs instanceof Map<?,?>)) {
            return;
        }
        crs = ((Map<?,?>) crs).get(org.apache.sis.internal.feature.jts.JTS.CRS_KEY);
        if (!(crs instanceof CoordinateReferenceSystem)) {
            return;
        }
    }
    target.setUserData(crs);
}
 
Example 4
Source File: JTSTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * Tests various {@code transform} methods. This includes (sometime indirectly):
 *
 * <ul>
 *   <li>{@link JTS#transform(Geometry, CoordinateReferenceSystem)}</li>
 *   <li>{@link JTS#transform(Geometry, CoordinateOperation)}</li>
 *   <li>{@link JTS#transform(Geometry, MathTransform)}</li>
 * </ul>
 *
 * @throws FactoryException if an EPSG code can not be resolved.
 * @throws TransformException if a coordinate can not be transformed.
 */
@Test
public void testTransform() throws FactoryException, TransformException {
    final GeometryFactory factory = new GeometryFactory();
    final Geometry in = factory.createPoint(new Coordinate(5, 6));
    /*
     * Test exception when transforming geometry without CRS.
     */
    try {
        JTS.transform(in, CommonCRS.WGS84.geographic());
        fail("Geometry has no CRS, transform should have failed.");
    } catch (TransformException ex) {
        assertNotNull(ex.getMessage());
    }
    /*
     * Test axes inversion transform.
     */
    in.setUserData(CommonCRS.WGS84.normalizedGeographic());
    Geometry out = JTS.transform(in, CommonCRS.WGS84.geographic());
    assertTrue(out instanceof Point);
    assertEquals(6, ((Point) out).getX(), STRICT);
    assertEquals(5, ((Point) out).getY(), STRICT);
    assertEquals(CommonCRS.WGS84.geographic(), out.getUserData());
    /*
     * Test affine transform. User data must be preserved.
     */
    final AffineTransform2D trs = new AffineTransform2D(1, 0, 0, 1, 10, 20);
    out = JTS.transform(in, trs);
    assertTrue(out instanceof Point);
    assertEquals(15, ((Point) out).getX(), STRICT);
    assertEquals(26, ((Point) out).getY(), STRICT);
}
 
Example 5
Source File: OmsDelaunayTriangulation.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inMap);

    if (!EGeometryType.isPoint(inMap.getSchema().getGeometryDescriptor())) {
        throw new ModelsIllegalargumentException("The input geometry needs to be points.", this, pm);
    }

    if (fElev != null) {
        fElev = FeatureUtilities.findAttributeName(inMap.getSchema(), fElev);
        if (fElev == null) {
            throw new ModelsIllegalargumentException("Couldn't find field: " + fElev, this);
        }
    }

    CoordinateReferenceSystem crs = inMap.getBounds().getCoordinateReferenceSystem();
    List<SimpleFeature> fList = FeatureUtilities.featureCollectionToList(inMap);

    pm.beginTask("Processing...", fList.size());
    DelaunayTriangulationBuilder b = new DelaunayTriangulationBuilder();
    List<Coordinate> cList = new ArrayList<Coordinate>();
    for( SimpleFeature f : fList ) {
        Geometry geometry = (Geometry) f.getDefaultGeometry();
        double elev = 0.0;
        if (fElev != null)
            elev = (Double) f.getAttribute(fElev);

        Coordinate c = geometry.getCoordinate();
        c.z = elev;
        cList.add(c);
        pm.worked(1);
    }
    pm.done();

    b.setSites(cList);

    List<Geometry> geosList = new ArrayList<Geometry>();
    Geometry triangles = b.getTriangles(gf);
    for( int i = 0; i < triangles.getNumGeometries(); i++ ) {
        Geometry geometryN = triangles.getGeometryN(i);
        Coordinate[] coordinates = geometryN.getCoordinates();
        double min = Double.POSITIVE_INFINITY;
        double max = Double.NEGATIVE_INFINITY;
        for( Coordinate coordinate : coordinates ) {
            min = Math.min(min, coordinate.z);
            max = Math.max(max, coordinate.z);
        }
        geometryN.setUserData(new String[]{"" + min, "" + max});
        geosList.add(geometryN);
    }
    outMap = FeatureUtilities.featureCollectionFromGeometry(crs, geosList.toArray(new Geometry[0]));
}
 
Example 6
Source File: OmsVoronoiDiagram.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void process() throws Exception {
    checkNull(inMap);

    if (!EGeometryType.isPoint(inMap.getSchema().getGeometryDescriptor())) {
        throw new ModelsIllegalargumentException("The input geometry needs to be points.", this, pm);
    }

    if (fElev != null) {
        fElev = FeatureUtilities.findAttributeName(inMap.getSchema(), fElev);
        if (fElev == null) {
            throw new ModelsIllegalargumentException("Couldn't find field: " + fElev, this);
        }
    }

    CoordinateReferenceSystem crs = inMap.getBounds().getCoordinateReferenceSystem();
    List<SimpleFeature> fList = FeatureUtilities.featureCollectionToList(inMap);

    pm.beginTask("Processing...", fList.size());
    VoronoiDiagramBuilder b = new VoronoiDiagramBuilder();
    List<Coordinate> cList = new ArrayList<Coordinate>();
    for( SimpleFeature f : fList ) {
        Geometry geometry = (Geometry) f.getDefaultGeometry();
        double elev = 0.0;
        if (fElev != null)
            elev = (Double) f.getAttribute(fElev);

        Coordinate c = geometry.getCoordinate();
        c.z = elev;
        cList.add(c);
        pm.worked(1);
    }
    pm.done();

    b.setSites(cList);

    List<Geometry> geosList = new ArrayList<Geometry>();
    Geometry diagram = b.getDiagram(gf);
    for( int i = 0; i < diagram.getNumGeometries(); i++ ) {
        Geometry geometryN = diagram.getGeometryN(i);
        Coordinate[] coordinates = geometryN.getCoordinates();
        double min = Double.POSITIVE_INFINITY;
        double max = Double.NEGATIVE_INFINITY;
        for( Coordinate coordinate : coordinates ) {
            min = Math.min(min, coordinate.z);
            max = Math.max(max, coordinate.z);
        }
        geometryN.setUserData(new String[]{"" + min, "" + max});
        geosList.add(geometryN);
    }

    outMap = FeatureUtilities.featureCollectionFromGeometry(crs, geosList.toArray(new Geometry[0]));
}