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;
}
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);
}
}
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;
}
}
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);
}
}
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);
}
}
Aggregations