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

The following examples show how to use org.locationtech.jts.geom.Geometry#getUserData() . 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
/**
 * Gets the Coordinate Reference System (CRS) from the given geometry.
 * This method expects the CRS to be stored in one the following ways:
 *
 * <ul>
 *   <li>Geometry {@linkplain Geometry#getUserData() user data} is an instance of {@code CoordinateReferenceSystem}.</li>
 *   <li>{@linkplain Geometry#getUserData() user data} is a (@link Map} with a value for the {@value #CRS_KEY} key.</li>
 *   <li>Geometry SRID is strictly positive, in which case it is interpreted as an EPSG code.</li>
 * </ul>
 *
 * If none of the above is valid, {@code null} is returned.
 *
 * @param  geometry the geometry from which to get the CRS, or {@code null}.
 * @return the coordinate reference system, or {@code null} if none.
 * @throws FactoryException if the CRS can not be created from the SRID code.
 */
public static CoordinateReferenceSystem getCoordinateReferenceSystem(final Geometry geometry) throws FactoryException {
    if (geometry != null) {
        final Object userData = geometry.getUserData();
        if (userData instanceof CoordinateReferenceSystem) {
            return (CoordinateReferenceSystem) userData;
        } else if (userData instanceof Map<?,?>) {
            final Map<?,?> map = (Map<?,?>) userData;
            final Object value = map.get(CRS_KEY);
            if (value instanceof CoordinateReferenceSystem) {
                return (CoordinateReferenceSystem) value;
            }
        }
        /*
         * Fallback on SRID with the assumption that they are EPSG codes.
         */
        final int srid = geometry.getSRID();
        if (srid > 0) {
            return CRS.forCode(Constants.EPSG + ':' + srid);
        }
    }
    return null;
}
 
Example 2
Source File: GmlEncoderv321.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private String getGmlID(Geometry geom, String gmlId) {
    String id;
    if (!Strings.isNullOrEmpty(gmlId)) {
        id = gmlId;
    } else if (geom.getUserData() != null && geom.getUserData() instanceof Map
            && ((Map) geom.getUserData()).containsKey(XmlBeansEncodingFlags.GMLID.name())) {
        id = (String) ((Map) geom.getUserData()).get(XmlBeansEncodingFlags.GMLID.name());
    } else {
        id = IdGenerator.generate(geom.toText());
    }
    return geom.getGeometryType() + "_" + id;
}
 
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: ALasDataManager.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Retrieve all the envelope features that intersect the geometry.
 *
 * <p>an elev attribute is added with the max elev contained in the envelope.
 *
 * @param checkGeom the {@link org.locationtech.jts.geom.Geometry} to use to check.
 * @param doOnlyEnvelope check for the geom envelope instead of a intersection with it.
 * @param minMaxZI an array to be filled with the [minz,maxz, minintensity, maxintensity] to be used as style.
 * @param doPoints if <code>true</code>, create points instead of polygons.
 * @return the features of the envelopes contained in the supplied geometry.
 * @throws Exception
 */
public synchronized SimpleFeatureCollection getEnvelopeFeaturesInGeometry( Geometry checkGeom, boolean doOnlyEnvelope,
        double[] minMaxZI, boolean doPoints ) throws Exception {
    List<Geometry> envelopesInGeometry = getEnvelopesInGeometry(checkGeom, doOnlyEnvelope, null);

    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
    b.setName("overview");
    b.setCRS(crs);
    if (!doPoints) {
        b.add("the_geom", Polygon.class);
    } else {
        b.add("the_geom", Point.class);
    }
    b.add("elev", Double.class);
    b.add("intensity", Double.class);

    SimpleFeatureType type = b.buildFeatureType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(type);
    double minZ = Double.POSITIVE_INFINITY;
    double maxZ = Double.NEGATIVE_INFINITY;
    double minI = Double.POSITIVE_INFINITY;
    double maxI = Double.NEGATIVE_INFINITY;

    DefaultFeatureCollection newFeatures = new DefaultFeatureCollection();
    for( int i = 0; i < envelopesInGeometry.size(); i++ ) {
        Geometry geom = envelopesInGeometry.get(i);
        if (doPoints) {
            Envelope envelope = geom.getEnvelopeInternal();
            Coordinate centre = envelope.centre();
            geom = gf.createPoint(centre);
        }

        double elev = -9999.0;
        double intens = -9999.0;
        Object userData = geom.getUserData();
        if (userData instanceof double[]) {
            double[] data = (double[]) userData;
            elev = data[0];
            intens = data[1];
        }

        if (minMaxZI != null) {
            minZ = Math.min(minZ, elev);
            maxZ = Math.max(maxZ, elev);
            minI = Math.min(minI, intens);
            maxI = Math.max(maxI, intens);
        }

        Object[] objs = new Object[]{geom, elev, intens};
        builder.addAll(objs);
        SimpleFeature feature = builder.buildFeature(null);
        newFeatures.add(feature);
    }

    if (minMaxZI != null) {
        minMaxZI[0] = minZ;
        minMaxZI[1] = maxZ;
        minMaxZI[2] = minI;
        minMaxZI[3] = maxI;
    }
    return newFeatures;
}