Search in sources :

Example 1 with Line

use of org.apache.lucene.geo.Line in project Anserini by castorini.

the class GeoIndexerTestBase method buildTestIndex.

private void buildTestIndex() throws IOException {
    try {
        Directory dir = FSDirectory.open(tempDir1);
        IndexWriterConfig config = new IndexWriterConfig();
        config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        IndexWriter writer = new IndexWriter(dir, config);
        // Index Lake Ontario (a Polygon) using Polygon.fromGeoJSON
        Document doc1 = new Document();
        Path path1 = Paths.get("src/test/resources/sample_docs/geosearch/lake_ontario.geojson");
        byte[] encoded = Files.readAllBytes(path1);
        String s = new String(encoded);
        Polygon lakeOntario = Polygon.fromGeoJSON(s)[0];
        Field[] fields = LatLonShape.createIndexableFields("geometry", lakeOntario);
        for (Field f : fields) {
            doc1.add(f);
        }
        doc1.add(new StringField(IndexArgs.ID, "id", Field.Store.YES));
        writer.addDocument(doc1);
        // Index Polygon with a hole in it using SimpleWKTShapeParser
        Document doc2 = new Document();
        Path path2 = Paths.get("src/test/resources/sample_docs/geosearch/self_containing_polygon.wkt");
        List<String> listLines2 = Files.readAllLines(path2);
        String[] lines2 = listLines2.toArray(new String[0]);
        Polygon polygonWithHole = (Polygon) SimpleWKTShapeParser.parse(lines2[0]);
        Field[] fields2 = LatLonShape.createIndexableFields("geometry", polygonWithHole);
        for (Field f : fields2) {
            doc2.add(f);
        }
        doc2.add(new StringField(IndexArgs.ID, "id", Field.Store.YES));
        writer.addDocument(doc2);
        // Index MultiPolygon using SimpleWKTShapeParser
        Document doc3 = new Document();
        Path path3 = Paths.get("src/test/resources/sample_docs/geosearch/multipolygon.wkt");
        List<String> listLines3 = Files.readAllLines(path3);
        String[] lines3 = listLines3.toArray(new String[0]);
        Polygon[] multipolygon = (Polygon[]) SimpleWKTShapeParser.parse(lines3[0]);
        for (Polygon p : multipolygon) {
            Field[] fields3 = LatLonShape.createIndexableFields("geometry", p);
            for (Field f : fields3) {
                doc3.add(f);
            }
        }
        doc3.add(new StringField(IndexArgs.ID, "id", Field.Store.YES));
        writer.addDocument(doc3);
        // Index LineString using SimpleWKTShapeParser
        Document doc4 = new Document();
        Path path4 = Paths.get("src/test/resources/sample_docs/geosearch/line.wkt");
        List<String> listLines4 = Files.readAllLines(path4);
        String[] lines4 = listLines4.toArray(new String[0]);
        Line lineShape = (Line) SimpleWKTShapeParser.parse(lines4[0]);
        Field[] fields4 = LatLonShape.createIndexableFields("geometry", lineShape);
        for (Field f : fields4) {
            doc4.add(f);
        }
        doc4.add(new StringField(IndexArgs.ID, "id", Field.Store.YES));
        writer.addDocument(doc4);
        // Index MultiLineString using SimpleWKTShapeParser
        Document doc5 = new Document();
        Path path5 = Paths.get("src/test/resources/sample_docs/geosearch/multiline.wkt");
        List<String> listLines5 = Files.readAllLines(path5);
        String[] lines5 = listLines5.toArray(new String[0]);
        Line[] lineShapes = (Line[]) SimpleWKTShapeParser.parse(lines5[0]);
        for (Line l : lineShapes) {
            Field[] fields5 = LatLonShape.createIndexableFields("geometry", l);
            for (Field f : fields5) {
                doc5.add(f);
            }
        }
        doc5.add(new StringField(IndexArgs.ID, "id", Field.Store.YES));
        writer.addDocument(doc5);
        // Index Grand River (a LineString) using SimpleWKTShapeParser
        Document doc6 = new Document();
        Path path6 = Paths.get("src/test/resources/sample_docs/geosearch/grand_river.wkt");
        List<String> listLines6 = Files.readAllLines(path6);
        String[] lines6 = listLines6.toArray(new String[0]);
        Line grand_river = (Line) SimpleWKTShapeParser.parse(lines6[0]);
        Field[] fields6 = LatLonShape.createIndexableFields("geometry", grand_river);
        for (Field f : fields6) {
            doc6.add(f);
        }
        doc6.add(new StringField(IndexArgs.ID, "id", Field.Store.YES));
        writer.addDocument(doc6);
        writer.commit();
        writer.forceMerge(1);
        writer.close();
        dir.close();
    } catch (ParseException e) {
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) Document(org.apache.lucene.document.Document) Line(org.apache.lucene.geo.Line) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) ParseException(java.text.ParseException) Polygon(org.apache.lucene.geo.Polygon) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with Line

use of org.apache.lucene.geo.Line in project Anserini by castorini.

the class GeoGenerator method createDocument.

@Override
public Document createDocument(JsonCollection.Document geoDoc) {
    Document doc = new Document();
    // Add ID field
    String id = geoDoc.id();
    doc.add(new StringField(IndexArgs.ID, id, Field.Store.YES));
    // Store the raw JSON
    if (args.storeRaw) {
        doc.add(new StoredField(IndexArgs.RAW, geoDoc.raw()));
    }
    geoDoc.fields().forEach((k, v) -> {
        if ("geometry".equals(k)) {
            // parse the geometry fields using SimpleWKTParser and index them
            try {
                Object shape = SimpleWKTShapeParser.parse(v);
                Field[] fields = new Field[0];
                if (shape instanceof Line) {
                    fields = LatLonShape.createIndexableFields("geometry", (Line) shape);
                } else if (shape instanceof Polygon) {
                    fields = LatLonShape.createIndexableFields("geometry", (Polygon) shape);
                } else if (shape instanceof Line[]) {
                    for (Line line : (Line[]) shape) {
                        fields = LatLonShape.createIndexableFields("geometry", line);
                    }
                } else if (shape instanceof Polygon[]) {
                    for (Polygon polygon : (Polygon[]) shape) {
                        fields = LatLonShape.createIndexableFields("geometry", polygon);
                    }
                } else {
                    throw new IllegalArgumentException("unknown shape");
                }
                for (Field f : fields) {
                    doc.add(f);
                }
            } catch (ParseException | IOException e) {
                LOG.error("Error parsing unknown shape using SimpleWKTShapeParser: " + v);
            } catch (IllegalArgumentException e) {
                LOG.error("Error casting shape to any of the types Line, Line[], Polygon, Polygon[]: " + v);
            }
        } else {
            // go through all the non-geometry fields and try to index them as int or long if possible
            try {
                long vLong = Long.parseLong(v);
                doc.add(new LongPoint(k, vLong));
                doc.add(new StoredField(k, v));
            } catch (NumberFormatException e1) {
                try {
                    double vDouble = Double.parseDouble(v);
                    doc.add(new DoublePoint(k, vDouble));
                    doc.add(new StoredField(k, v));
                } catch (NumberFormatException e2) {
                    doc.add(new StringField(k, v, Field.Store.YES));
                }
            }
        }
    });
    return doc;
}
Also used : IOException(java.io.IOException) Line(org.apache.lucene.geo.Line) ParseException(java.text.ParseException) Polygon(org.apache.lucene.geo.Polygon)

Example 3 with Line

use of org.apache.lucene.geo.Line in project Anserini by castorini.

the class GeoSearchExplorationTest method testGetLine.

@Test
public void testGetLine() throws Exception {
    Directory directory = FSDirectory.open(tempDir1);
    DirectoryReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader);
    Line queryLine = new Line(new double[] { 30, 50 }, new double[] { 10, 10 });
    Query q = LatLonShape.newLineQuery("geometry", ShapeField.QueryRelation.INTERSECTS, queryLine);
    TopDocs hits = searcher.search(q, 5);
    assertEquals(1, hits.totalHits.value);
    assertEquals(3, hits.scoreDocs[0].doc);
    reader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Line(org.apache.lucene.geo.Line) TopDocs(org.apache.lucene.search.TopDocs) Query(org.apache.lucene.search.Query) DirectoryReader(org.apache.lucene.index.DirectoryReader) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) Test(org.junit.Test)

Example 4 with Line

use of org.apache.lucene.geo.Line in project Anserini by castorini.

the class SimpleGeoSearcherTest method testGetLine.

@Test
public void testGetLine() throws Exception {
    SimpleGeoSearcher searcher = new SimpleGeoSearcher(super.tempDir1.toString());
    Line queryLine = new Line(new double[] { 30, 50 }, new double[] { 10, 10 });
    Query q = LatLonShape.newLineQuery("geometry", ShapeField.QueryRelation.INTERSECTS, queryLine);
    SimpleSearcher.Result[] hits = searcher.searchGeo(q, 5);
    assertEquals(1, hits.length);
    assertEquals(3, hits[0].lucene_docid);
    searcher.close();
}
Also used : Line(org.apache.lucene.geo.Line) Query(org.apache.lucene.search.Query) Test(org.junit.Test)

Aggregations

Line (org.apache.lucene.geo.Line)4 ParseException (java.text.ParseException)2 Polygon (org.apache.lucene.geo.Polygon)2 Query (org.apache.lucene.search.Query)2 Directory (org.apache.lucene.store.Directory)2 FSDirectory (org.apache.lucene.store.FSDirectory)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 StringField (org.apache.lucene.document.StringField)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 TopDocs (org.apache.lucene.search.TopDocs)1