Search in sources :

Example 76 with FactoryException

use of org.opengis.util.FactoryException in project jena by apache.

the class SpatialIndex method getGeoPredicateIndexItems.

/**
 * @param model
 * @param srsURI
 * @return Geo predicate objects prepared for adding to SpatialIndex.
 */
private static Collection<SpatialIndexItem> getGeoPredicateIndexItems(Model model, String srsURI) throws SpatialIndexException {
    List<SpatialIndexItem> items = new ArrayList<>();
    ResIterator resIt = model.listResourcesWithProperty(SpatialExtension.GEO_LAT_PROP);
    while (resIt.hasNext()) {
        Resource feature = resIt.nextResource();
        Literal lat = feature.getRequiredProperty(SpatialExtension.GEO_LAT_PROP).getLiteral();
        Literal lon = feature.getProperty(SpatialExtension.GEO_LON_PROP).getLiteral();
        if (lon == null) {
            LOGGER.warn("Geo predicates: latitude found but not longitude. " + feature);
            continue;
        }
        Literal latLonPoint = ConvertLatLon.toLiteral(lat.getFloat(), lon.getFloat());
        GeometryWrapper geometryWrapper = GeometryWrapper.extract(latLonPoint);
        try {
            // Ensure all entries in the target SRS URI.
            GeometryWrapper transformedGeometryWrapper = geometryWrapper.convertSRS(srsURI);
            Envelope envelope = transformedGeometryWrapper.getEnvelope();
            SpatialIndexItem item = new SpatialIndexItem(envelope, feature);
            items.add(item);
        } catch (FactoryException | MismatchedDimensionException | TransformException ex) {
            throw new SpatialIndexException("Transformation Exception: " + geometryWrapper.getLexicalForm() + ". " + ex.getMessage());
        }
    }
    return items;
}
Also used : ResIterator(org.apache.jena.rdf.model.ResIterator) FactoryException(org.opengis.util.FactoryException) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) TransformException(org.opengis.referencing.operation.TransformException) Envelope(org.locationtech.jts.geom.Envelope) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) Literal(org.apache.jena.rdf.model.Literal)

Example 77 with FactoryException

use of org.opengis.util.FactoryException in project jena by apache.

the class GreatCircleGeomFF method exec.

@Override
public NodeValue exec(NodeValue v1, NodeValue v2, NodeValue v3) {
    try {
        GeometryWrapper geometry1 = GeometryWrapper.extract(v1, GeometryLiteralIndex.GeometryIndex.PRIMARY);
        GeometryWrapper geometry2 = GeometryWrapper.extract(v2, GeometryLiteralIndex.GeometryIndex.SECONDARY);
        if (!(v3.isIRI() || v3.isString())) {
            throw new ExprEvalException("Not an IRI or String: " + FmtUtils.stringForNode(v3.asNode()));
        }
        String unitsURI;
        if (v3.isIRI()) {
            unitsURI = v3.asNode().getURI();
        } else {
            unitsURI = v3.asString();
        }
        double distance = geometry1.distanceGreatCircle(geometry2, unitsURI);
        return NodeValue.makeDouble(distance);
    } catch (DatatypeFormatException | FactoryException | MismatchedDimensionException | TransformException ex) {
        throw new ExprEvalException(ex.getMessage(), ex);
    }
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) FactoryException(org.opengis.util.FactoryException) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) TransformException(org.opengis.referencing.operation.TransformException) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException)

Example 78 with FactoryException

use of org.opengis.util.FactoryException in project jena by apache.

the class GeoSPARQLOperations method convertGeometryLiteral.

/**
 * Convert a string representation of a geometry literal to another
 * coordinate reference system.
 *
 * @param geometryLiteral
 * @param outputSrsURI Coordinate reference system URI
 * @param outputDatatype
 * @return Output of conversion.
 */
public static final String convertGeometryLiteral(String geometryLiteral, String outputSrsURI, GeometryDatatype outputDatatype) {
    Literal lit = ResourceFactory.createTypedLiteral(geometryLiteral, outputDatatype);
    GeometryWrapper geometryWrapper = GeometryWrapper.extract(lit);
    try {
        GeometryWrapper transformedGeometryWrapper = geometryWrapper.convertSRS(outputSrsURI);
        Literal transformedLit = transformedGeometryWrapper.asLiteral();
        return transformedLit.getLexicalForm();
    } catch (FactoryException | MismatchedDimensionException | TransformException ex) {
        LOGGER.error("{} : {} : {}", ex.getMessage(), geometryLiteral, outputSrsURI);
        return null;
    }
}
Also used : FactoryException(org.opengis.util.FactoryException) Literal(org.apache.jena.rdf.model.Literal) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) TransformException(org.opengis.referencing.operation.TransformException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException)

Example 79 with FactoryException

use of org.opengis.util.FactoryException in project jena by apache.

the class GeoSPARQLOperations method handleLiteral.

private static void handleLiteral(Statement statement, Model outputModel, String outputSrsURI, GeometryDatatype outputDatatype) {
    Literal literal = statement.getLiteral();
    RDFDatatype datatype = literal.getDatatype();
    // Check whether a supported geometry literal.
    if (GeometryDatatype.check(datatype)) {
        GeometryWrapper originalGeom = GeometryWrapper.extract(literal);
        GeometryWrapper convertedGeom;
        try {
            if (outputSrsURI != null) {
                convertedGeom = originalGeom.convertSRS(outputSrsURI);
            } else {
                convertedGeom = originalGeom;
            }
        } catch (FactoryException | MismatchedDimensionException | TransformException ex) {
            LOGGER.error("SRS Conversion Exception: {} - Literal: {}, Output SRS URI: {}. Reusing original literal for output.", ex.getMessage(), literal, outputSrsURI);
            convertedGeom = originalGeom;
        }
        if (outputDatatype == null) {
            outputDatatype = GeometryDatatype.get(datatype);
        }
        Literal convertedGeometryLiteral = convertedGeom.asLiteral(outputDatatype);
        // Assign the existing property unless it needs to be switched for asGML and asWKT.
        Property outputProperty = statement.getPredicate();
        if (outputProperty.equals(Geo.AS_GML_PROP) && outputDatatype.equals(WKTDatatype.INSTANCE)) {
            outputProperty = Geo.AS_WKT_PROP;
        } else if (outputProperty.equals(Geo.AS_WKT_PROP) && outputDatatype.equals(GMLDatatype.INSTANCE)) {
            outputProperty = Geo.AS_GML_PROP;
        }
        Statement outputStatement = ResourceFactory.createStatement(statement.getSubject(), outputProperty, convertedGeometryLiteral);
        outputModel.add(outputStatement);
    } else {
        // Not a statement of interest so store for output.
        outputModel.add(statement);
    }
}
Also used : FactoryException(org.opengis.util.FactoryException) Statement(org.apache.jena.rdf.model.Statement) Literal(org.apache.jena.rdf.model.Literal) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) TransformException(org.opengis.referencing.operation.TransformException) RDFDatatype(org.apache.jena.datatypes.RDFDatatype) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) Property(org.apache.jena.rdf.model.Property)

Example 80 with FactoryException

use of org.opengis.util.FactoryException in project jena by apache.

the class DifferenceFF method exec.

@Override
public NodeValue exec(NodeValue v1, NodeValue v2) {
    try {
        GeometryWrapper geometry1 = GeometryWrapper.extract(v1, GeometryIndex.PRIMARY);
        GeometryWrapper geometry2 = GeometryWrapper.extract(v2, GeometryIndex.SECONDARY);
        GeometryWrapper difference = geometry1.difference(geometry2);
        return difference.asNodeValue();
    } catch (DatatypeFormatException ex) {
        throw new ExprEvalException(ex.getMessage(), ex);
    } catch (FactoryException | MismatchedDimensionException | TransformException ex) {
        throw new ExprEvalException(ex.getMessage() + ": " + FmtUtils.stringForNode(v1.asNode()) + ", " + FmtUtils.stringForNode(v2.asNode()), ex);
    }
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) FactoryException(org.opengis.util.FactoryException) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) TransformException(org.opengis.referencing.operation.TransformException) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException)

Aggregations

FactoryException (org.opengis.util.FactoryException)84 TransformException (org.opengis.referencing.operation.TransformException)27 GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)21 MismatchedDimensionException (org.opengis.geometry.MismatchedDimensionException)19 ExprEvalException (org.apache.jena.sparql.expr.ExprEvalException)17 MathTransform (org.opengis.referencing.operation.MathTransform)15 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)12 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)10 IdentifiedObject (org.opengis.referencing.IdentifiedObject)8 Envelope (org.locationtech.jts.geom.Envelope)7 ArrayList (java.util.ArrayList)6 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)6 NoninvertibleTransformException (org.opengis.referencing.operation.NoninvertibleTransformException)6 Literal (org.apache.jena.rdf.model.Literal)5 UnavailableFactoryException (org.apache.sis.referencing.factory.UnavailableFactoryException)5 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)5 ParseException (java.text.ParseException)4 AbstractIdentifiedObject (org.apache.sis.referencing.AbstractIdentifiedObject)4 BackingStoreException (org.apache.sis.util.collection.BackingStoreException)4 Test (org.junit.Test)4