Search in sources :

Example 6 with SpatialIndex

use of org.apache.jena.geosparql.spatial.SpatialIndex in project jena by apache.

the class WestGeomPFTest method testCheckSearchEnvelope_no_wrap.

/**
 * Test of checkSearchEnvelope method, of class WestGeomPF.
 */
@Test
public void testCheckSearchEnvelope_no_wrap() {
    SpatialIndex spatialIndex = SpatialIndexTestData.createTestIndex();
    // Search Envelope
    GeometryWrapper geometryWrapper = SpatialIndexTestData.PERTH_GEOMETRY_WRAPPER;
    WestGeomPF instance = new WestGeomPF();
    // Needed to initialise the search.
    SearchEnvelope searchEnvelope = instance.buildSearchEnvelope(geometryWrapper, SpatialIndexTestData.WGS_84_SRS_INFO);
    HashSet<Resource> expResult = new HashSet<>(Arrays.asList(SpatialIndexTestData.LONDON_FEATURE, SpatialIndexTestData.PERTH_FEATURE));
    HashSet<Resource> result = searchEnvelope.check(spatialIndex);
    assertEquals(expResult, result);
}
Also used : SpatialIndex(org.apache.jena.geosparql.spatial.SpatialIndex) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) Resource(org.apache.jena.rdf.model.Resource) SearchEnvelope(org.apache.jena.geosparql.spatial.SearchEnvelope) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with SpatialIndex

use of org.apache.jena.geosparql.spatial.SpatialIndex in project jena by apache.

the class EastGeomPFTest method testCheckSearchEnvelope_wrap.

/**
 * Test of checkSearchEnvelope method, of class EastGeomPF.
 */
@Test
public void testCheckSearchEnvelope_wrap() {
    SpatialIndex spatialIndex = SpatialIndexTestData.createTestIndex();
    // Search Envelope
    GeometryWrapper geometryWrapper = SpatialIndexTestData.PERTH_GEOMETRY_WRAPPER;
    EastGeomPF instance = new EastGeomPF();
    // Needed to initialise the search.
    SearchEnvelope searchEnvelope = instance.buildSearchEnvelope(geometryWrapper, SpatialIndexTestData.WGS_84_SRS_INFO);
    HashSet<Resource> expResult = new HashSet<>(Arrays.asList(SpatialIndexTestData.AUCKLAND_FEATURE, SpatialIndexTestData.PERTH_FEATURE, SpatialIndexTestData.HONOLULU_FEATURE, SpatialIndexTestData.NEW_YORK_FEATURE));
    HashSet<Resource> result = searchEnvelope.check(spatialIndex);
    assertEquals(expResult, result);
}
Also used : SpatialIndex(org.apache.jena.geosparql.spatial.SpatialIndex) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) Resource(org.apache.jena.rdf.model.Resource) SearchEnvelope(org.apache.jena.geosparql.spatial.SearchEnvelope) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with SpatialIndex

use of org.apache.jena.geosparql.spatial.SpatialIndex in project jena by apache.

the class GenericPropertyFunction method findIndex.

private QueryIterConcat findIndex(Graph graph, Node boundNode, Node unboundNode, Binding binding, boolean isSubjectBound, Node predicate, ExecutionContext execCxt) throws ExprEvalException {
    try {
        // Prepare for results.
        Var unboundVar = Var.alloc(unboundNode.getName());
        QueryIterConcat queryIterConcat = new QueryIterConcat(execCxt);
        // Find the asserted triples.
        List<Node> assertedNodes = findAsserted(graph, boundNode, isSubjectBound, predicate);
        for (Node node : assertedNodes) {
            Binding newBind = BindingFactory.binding(binding, unboundVar, node);
            QueryIterator queryIter = QueryIterSingleton.create(newBind, execCxt);
            queryIterConcat.add(queryIter);
        }
        // Find the GeometryLiteral of the Bound Node.
        SpatialObjectGeometryLiteral boundGeometryLiteral = SpatialObjectGeometryLiteral.retrieve(graph, boundNode);
        if (!boundGeometryLiteral.isValid()) {
            // Bound Node is not a Feature or a Geometry or there is no GeometryLiteral so exit.
            return queryIterConcat;
        }
        Node geometryLiteral = boundGeometryLiteral.getGeometryLiteral();
        // Perform the search of the Spatial Index of the Dataset.
        SpatialIndex spatialIndex = SpatialIndex.retrieve(execCxt);
        GeometryWrapper geom = GeometryWrapper.extract(geometryLiteral);
        GeometryWrapper transformedGeom = geom.transform(spatialIndex.getSrsInfo());
        Envelope searchEnvelope = transformedGeom.getEnvelope();
        HashSet<Resource> features = spatialIndex.query(searchEnvelope);
        // Check each of the Features that match the search.
        for (Resource feature : features) {
            Node featureNode = feature.asNode();
            // Ensure not already an asserted node.
            if (!assertedNodes.contains(featureNode)) {
                Binding newBind = BindingFactory.binding(binding, unboundVar, featureNode);
                QueryIterator queryIter;
                if (isSubjectBound) {
                    queryIter = bothBound(newBind, boundNode, predicate, featureNode, execCxt);
                } else {
                    queryIter = bothBound(newBind, featureNode, predicate, boundNode, execCxt);
                }
                queryIterConcat.add(queryIter);
            }
            // Also test all Geometry of the Features. All, some or one Geometry may have matched.
            ExtendedIterator<Triple> featureGeometryTriples = graph.find(feature.asNode(), Geo.HAS_GEOMETRY_NODE, null);
            while (featureGeometryTriples.hasNext()) {
                Triple unboundTriple = featureGeometryTriples.next();
                Node geomNode = unboundTriple.getObject();
                // Ensure not already an asserted node.
                if (!assertedNodes.contains(geomNode)) {
                    Binding newBind = BindingFactory.binding(binding, unboundVar, geomNode);
                    QueryIterator queryIter;
                    if (isSubjectBound) {
                        queryIter = bothBound(newBind, boundNode, predicate, geomNode, execCxt);
                    } else {
                        queryIter = bothBound(newBind, geomNode, predicate, boundNode, execCxt);
                    }
                    queryIterConcat.add(queryIter);
                }
            }
        }
        return queryIterConcat;
    } catch (MismatchedDimensionException | TransformException | FactoryException | SpatialIndexException ex) {
        throw new ExprEvalException(ex.getMessage() + ": " + FmtUtils.stringForNode(boundNode) + ", " + FmtUtils.stringForNode(unboundNode) + ", " + FmtUtils.stringForNode(predicate), ex);
    }
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) FactoryException(org.opengis.util.FactoryException) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) Resource(org.apache.jena.rdf.model.Resource) TransformException(org.opengis.referencing.operation.TransformException) SpatialIndexException(org.apache.jena.geosparql.spatial.SpatialIndexException) Envelope(org.locationtech.jts.geom.Envelope) MismatchedDimensionException(org.opengis.geometry.MismatchedDimensionException) QueryIterConcat(org.apache.jena.sparql.engine.iterator.QueryIterConcat) Triple(org.apache.jena.graph.Triple) QueryIterator(org.apache.jena.sparql.engine.QueryIterator) SpatialIndex(org.apache.jena.geosparql.spatial.SpatialIndex) ExprEvalException(org.apache.jena.sparql.expr.ExprEvalException)

Example 9 with SpatialIndex

use of org.apache.jena.geosparql.spatial.SpatialIndex in project jena by apache.

the class EastGeomPFTest method testCheckSearchEnvelope_no_wrap.

/**
 * Test of checkSearchEnvelope method, of class EastGeomPF.
 */
@Test
public void testCheckSearchEnvelope_no_wrap() {
    SpatialIndex spatialIndex = SpatialIndexTestData.createTestIndex();
    // Search Envelope
    GeometryWrapper geometryWrapper = SpatialIndexTestData.HONOLULU_GEOMETRY_WRAPPER;
    EastGeomPF instance = new EastGeomPF();
    // Needed to initialise the search.
    SearchEnvelope searchEnvelope = instance.buildSearchEnvelope(geometryWrapper, SpatialIndexTestData.WGS_84_SRS_INFO);
    HashSet<Resource> expResult = new HashSet<>(Arrays.asList(SpatialIndexTestData.LONDON_FEATURE, SpatialIndexTestData.HONOLULU_FEATURE, SpatialIndexTestData.NEW_YORK_FEATURE));
    HashSet<Resource> result = searchEnvelope.check(spatialIndex);
    assertEquals(expResult, result);
}
Also used : SpatialIndex(org.apache.jena.geosparql.spatial.SpatialIndex) GeometryWrapper(org.apache.jena.geosparql.implementation.GeometryWrapper) Resource(org.apache.jena.rdf.model.Resource) SearchEnvelope(org.apache.jena.geosparql.spatial.SearchEnvelope) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

GeometryWrapper (org.apache.jena.geosparql.implementation.GeometryWrapper)9 SpatialIndex (org.apache.jena.geosparql.spatial.SpatialIndex)9 Resource (org.apache.jena.rdf.model.Resource)9 HashSet (java.util.HashSet)8 SearchEnvelope (org.apache.jena.geosparql.spatial.SearchEnvelope)8 Test (org.junit.Test)8 SpatialIndexException (org.apache.jena.geosparql.spatial.SpatialIndexException)1 Node (org.apache.jena.graph.Node)1 Triple (org.apache.jena.graph.Triple)1 Var (org.apache.jena.sparql.core.Var)1 QueryIterator (org.apache.jena.sparql.engine.QueryIterator)1 Binding (org.apache.jena.sparql.engine.binding.Binding)1 QueryIterConcat (org.apache.jena.sparql.engine.iterator.QueryIterConcat)1 ExprEvalException (org.apache.jena.sparql.expr.ExprEvalException)1 Envelope (org.locationtech.jts.geom.Envelope)1 MismatchedDimensionException (org.opengis.geometry.MismatchedDimensionException)1 TransformException (org.opengis.referencing.operation.TransformException)1 FactoryException (org.opengis.util.FactoryException)1