Search in sources :

Example 1 with SpatialIndexException

use of org.apache.jena.query.spatial.SpatialIndexException in project jena by apache.

the class DirectionWithPointPFBase method objectToStruct.

/** Deconstruct the node or list object argument and make a SpatialMatch */
@Override
protected SpatialMatch objectToStruct(PropFuncArg argObject) {
    if (argObject.isNode()) {
        log.warn("Object not a List: " + argObject);
        return null;
    }
    List<Node> list = argObject.getArgList();
    if (list.size() < 2 || list.size() > 3)
        throw new SpatialIndexException("Change in object list size");
    int idx = 0;
    Node x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Latitude is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Latitude is not a decimal " + list);
        return null;
    }
    Double latitude = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Longitude is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Longitude is not a decimal " + list);
        return null;
    }
    Double longitude = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    int limit = -1;
    if (idx < list.size()) {
        x = list.get(idx);
        if (!x.isLiteral()) {
            log.warn("Limit is not a literal " + list);
            return null;
        }
        LiteralLabel lit = x.getLiteral();
        if (!XSDDatatype.XSDinteger.isValidLiteral(lit)) {
            log.warn("Limit is not an integer " + list);
            return null;
        }
        int v = NodeFactoryExtra.nodeToInt(x);
        limit = (v < 0) ? -1 : v;
        idx++;
        if (idx < list.size()) {
            log.warn("Limit is not the last parameter " + list);
            return null;
        }
    }
    SpatialMatch match = this.getSpatialMatch(latitude, longitude, limit);
    if (log.isDebugEnabled())
        log.debug("Trying SpatialMatch: " + match.toString());
    return match;
}
Also used : Node(org.apache.jena.graph.Node) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Example 2 with SpatialIndexException

use of org.apache.jena.query.spatial.SpatialIndexException in project jena by apache.

the class spatialindexdump method dump.

private static void dump(SpatialIndexLucene spatialIndex) {
    try {
        Directory directory = spatialIndex.getDirectory();
        Analyzer analyzer = spatialIndex.getAnalyzer();
        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        QueryParser queryParser = new QueryParser(spatialIndex.getDocDef().getEntityField(), analyzer);
        Query query = queryParser.parse("*:*");
        ScoreDoc[] sDocs = indexSearcher.search(query, 1000).scoreDocs;
        for (ScoreDoc sd : sDocs) {
            System.out.println("Doc: " + sd.doc);
            Document doc = indexSearcher.doc(sd.doc);
            //System.out.println(doc) ;
            for (IndexableField f : doc) {
                //System.out.println("  "+f) ;
                System.out.println("  " + f.name() + " = " + f.stringValue());
            }
        }
    } catch (Exception ex) {
        throw new SpatialIndexException(ex);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexableField(org.apache.lucene.index.IndexableField) QueryParser(org.apache.lucene.queryparser.classic.QueryParser) Query(org.apache.lucene.search.Query) IndexReader(org.apache.lucene.index.IndexReader) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException) Analyzer(org.apache.lucene.analysis.Analyzer) Document(org.apache.lucene.document.Document) CmdException(jena.cmd.CmdException) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException) Directory(org.apache.lucene.store.Directory) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 3 with SpatialIndexException

use of org.apache.jena.query.spatial.SpatialIndexException in project jena by apache.

the class EntityDefinitionAssembler method open.

// V1
/*
<#definition> a spatial:EntityDefinition ;
    spatial:entityField      "uri" ;
    spatial:geoField         "geo" ;
    spatial:hasSpatialPredicatePairs (
         [ spatial:latitude <#latitude_1> ; spatial:longitude <#longitude_1> ]
         [ spatial:latitude <#latitude_2> ; spatial:longitude <#longitude_2> ]
    ) ;
    spatial:hasWKTPredicates (<#wkt_1> <#wkt_2>) ;
    spatial:spatialContextFactory
         "org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory"  .
    */
@Override
public EntityDefinition open(Assembler a, Resource root, Mode mode) {
    String prologue = "PREFIX : <" + NS + ">   PREFIX list: <http://jena.apache.org/ARQ/list#> ";
    Model model = root.getModel();
    String qs1 = StrUtils.strjoinNL(prologue, "SELECT * {", "  ?definition  :entityField  ?entityField ;", "               :geoField ?geoField", "}");
    ParameterizedSparqlString pss = new ParameterizedSparqlString(qs1);
    pss.setIri("definition", root.getURI());
    Query query1 = QueryFactory.create(pss.toString());
    QueryExecution qexec1 = QueryExecutionFactory.create(query1, model);
    ResultSet rs1 = qexec1.execSelect();
    List<QuerySolution> results = ResultSetFormatter.toList(rs1);
    if (results.size() == 0) {
        //Log.warn(this, "Failed to find a valid EntityDefinition for : "+root) ;
        throw new SpatialIndexException("Failed to find a valid EntityDefinition for : " + root);
    }
    if (results.size() != 1) {
        Log.warn(this, "Multiple matches for EntityMap for : " + root);
        throw new SpatialIndexException("Multiple matches for EntityDefinition for : " + root);
    }
    QuerySolution qsol1 = results.get(0);
    String entityField = qsol1.getLiteral("entityField").getLexicalForm();
    String geoField = qsol1.getLiteral("geoField").getLexicalForm();
    EntityDefinition docDef = new EntityDefinition(entityField, geoField);
    String qs2 = StrUtils.strjoinNL("SELECT * { ?definition :hasSpatialPredicatePairs [ list:member [ :latitude ?latitude ; :longitude ?longitude ] ]}");
    Query query2 = QueryFactory.create(prologue + " " + qs2);
    QueryExecution qexec2 = QueryExecutionFactory.create(query2, model, qsol1);
    ResultSet rs2 = qexec2.execSelect();
    List<QuerySolution> mapEntries = ResultSetFormatter.toList(rs2);
    for (QuerySolution qsol : mapEntries) {
        Resource latitude = qsol.getResource("latitude");
        Resource longitude = qsol.getResource("longitude");
        docDef.addSpatialPredicatePair(latitude, longitude);
    }
    String qs3 = StrUtils.strjoinNL("SELECT * { ?definition :hasWKTPredicates [ list:member ?wkt ] }");
    Query query3 = QueryFactory.create(prologue + " " + qs3);
    QueryExecution qexec3 = QueryExecutionFactory.create(query3, model, qsol1);
    ResultSet rs3 = qexec3.execSelect();
    mapEntries = ResultSetFormatter.toList(rs3);
    for (QuerySolution qsol : mapEntries) {
        Resource wkt = qsol.getResource("wkt");
        docDef.addWKTPredicate(wkt);
    }
    String qs4 = StrUtils.strjoinNL("SELECT * { ?definition :spatialContextFactory ?factory }");
    Query query4 = QueryFactory.create(prologue + " " + qs4);
    QueryExecution qexec4 = QueryExecutionFactory.create(query4, model, qsol1);
    ResultSet rs4 = qexec4.execSelect();
    List<QuerySolution> results4 = ResultSetFormatter.toList(rs4);
    if (results4.size() == 0) {
        return docDef;
    } else if (results4.size() != 1) {
        Log.warn(this, "Multiple matches for SpatialContextFactory for : " + root);
        throw new SpatialIndexException("Multiple matches for SpatialContextFactory for : " + root);
    } else {
        QuerySolution qsol4 = results4.get(0);
        String spatialContextFactory = qsol4.getLiteral("factory").getLexicalForm();
        try {
            docDef.setSpatialContextFactory(spatialContextFactory);
        } catch (NoClassDefFoundError e) {
            Log.warn(this, "Custom SpatialContextFactory lib is not ready in classpath:" + e.getMessage());
        }
        return docDef;
    }
}
Also used : EntityDefinition(org.apache.jena.query.spatial.EntityDefinition) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException)

Example 4 with SpatialIndexException

use of org.apache.jena.query.spatial.SpatialIndexException in project jena by apache.

the class SpatialIndexLuceneAssembler method open.

/*
    <#index> a :SpatialIndexLucene ;
        #spatial:directory "mem" ;
        spatial:directory <file:DIR> ;
        spatial:definition <#definition> ;
        .
    */
@SuppressWarnings("resource")
@Override
public SpatialIndex open(Assembler a, Resource root, Mode mode) {
    try {
        if (!GraphUtils.exactlyOneProperty(root, pDirectory))
            throw new SpatialIndexException("No 'spatial:directory' property on " + root);
        Directory directory;
        RDFNode n = root.getProperty(pDirectory).getObject();
        if (n.isLiteral()) {
            if (!"mem".equals(n.asLiteral().getLexicalForm()))
                throw new SpatialIndexException("No 'spatial:directory' property on " + root + " is a literal and not \"mem\"");
            directory = new RAMDirectory();
        } else {
            Resource x = n.asResource();
            String path = IRILib.IRIToFilename(x.getURI());
            File dir = new File(path);
            directory = FSDirectory.open(dir.toPath());
        }
        Resource r = GraphUtils.getResourceValue(root, pDefinition);
        EntityDefinition docDef = (EntityDefinition) a.open(r);
        return SpatialDatasetFactory.createLuceneIndex(directory, docDef);
    } catch (IOException e) {
        IO.exception(e);
        return null;
    }
}
Also used : EntityDefinition(org.apache.jena.query.spatial.EntityDefinition) Resource(org.apache.jena.rdf.model.Resource) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException) IOException(java.io.IOException) File(java.io.File) RDFNode(org.apache.jena.rdf.model.RDFNode) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) SpatialVocab.pDirectory(org.apache.jena.query.spatial.assembler.SpatialVocab.pDirectory)

Example 5 with SpatialIndexException

use of org.apache.jena.query.spatial.SpatialIndexException in project jena by apache.

the class SpatialOperationWithBoxPFBase method objectToStruct.

/** Deconstruct the node or list object argument and make a SpatialMatch */
@Override
protected SpatialMatch objectToStruct(PropFuncArg argObject) {
    if (argObject.isNode()) {
        log.warn("Object not a List: " + argObject);
        return null;
    }
    List<Node> list = argObject.getArgList();
    if (list.size() < 4 || list.size() > 5)
        throw new SpatialIndexException("Change in object list size");
    int idx = 0;
    Node x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Latitude 1 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Latitude 1 is not a decimal " + list);
        return null;
    }
    Double latitude1 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Longitude 1 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Longitude 1 is not a decimal " + list);
        return null;
    }
    Double longtitude1 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Latitude 2 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Latitude 2 is not a decimal " + list);
        return null;
    }
    Double latitude2 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    x = list.get(idx);
    if (!x.isLiteral()) {
        log.warn("Longitude 2 is not a literal " + list);
        return null;
    }
    if (!SpatialValueUtil.isDecimal(x)) {
        log.warn("Longitude 2 is not a decimal " + list);
        return null;
    }
    Double longtitude2 = Double.parseDouble(x.getLiteralLexicalForm());
    idx++;
    int limit = -1;
    if (idx < list.size()) {
        x = list.get(idx);
        if (!x.isLiteral()) {
            log.warn("Limit is not a literal " + list);
            return null;
        }
        LiteralLabel lit = x.getLiteral();
        if (!XSDDatatype.XSDinteger.isValidLiteral(lit)) {
            log.warn("Limit is not an integer " + list);
            return null;
        }
        int v = NodeFactoryExtra.nodeToInt(x);
        limit = (v < 0) ? -1 : v;
        idx++;
        if (idx < list.size()) {
            log.warn("Limit is not the last parameter " + list);
            return null;
        }
    }
    SpatialMatch match = new SpatialMatch(latitude1, longtitude1, latitude2, longtitude2, limit, getSpatialOperation());
    if (log.isDebugEnabled())
        log.debug("Trying SpatialMatch: " + match.toString());
    return match;
}
Also used : Node(org.apache.jena.graph.Node) SpatialIndexException(org.apache.jena.query.spatial.SpatialIndexException) LiteralLabel(org.apache.jena.graph.impl.LiteralLabel)

Aggregations

SpatialIndexException (org.apache.jena.query.spatial.SpatialIndexException)6 Node (org.apache.jena.graph.Node)3 LiteralLabel (org.apache.jena.graph.impl.LiteralLabel)3 EntityDefinition (org.apache.jena.query.spatial.EntityDefinition)2 Resource (org.apache.jena.rdf.model.Resource)2 Directory (org.apache.lucene.store.Directory)2 File (java.io.File)1 IOException (java.io.IOException)1 CmdException (jena.cmd.CmdException)1 SpatialVocab.pDirectory (org.apache.jena.query.spatial.assembler.SpatialVocab.pDirectory)1 Model (org.apache.jena.rdf.model.Model)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 Document (org.apache.lucene.document.Document)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexableField (org.apache.lucene.index.IndexableField)1 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1