[英]Lucene 5.5.3 spatial indexing with Recursive Prefix Tree strategy
I would like to spatially index shapes to support polygon and point based queries. 我想在空间上对形状进行索引以支持基于多边形和点的查询。 However, I am having a hard time creating the lucene document needed for this.
但是,我很难创建为此所需的lucene文档。 The idea is to use a recursive prefix tree to do a quick filtering of shapes based on the geohash, then do a granular filter by checking the serialization of the shape to see if the queried polygon does indeed match ( explained here ).
这个想法是使用递归前缀树基于geohash进行形状的快速过滤,然后通过检查形状的序列化以查看查询的多边形是否确实匹配来进行粒度过滤( 在此说明 )。
The first problem I have is the inability to create the indexable fields to be added to the spatial document: 我遇到的第一个问题是无法创建要添加到空间文档中的可索引字段:
Document doc = new Document();
Field[] fields = rptStrategy.createIndexableFields(spatial4jShape.getBoundingBox());
for (Field f : fields) {
doc.add(f);
}
The second problem I run into is not being able to use a Jts shape for the serialization part: 我遇到的第二个问题是无法为序列化部分使用Jts形状:
fields = dvStrategy.createIndexableFields((Shape)spatial4jShape);
That throws an IllegalArgumentException java.lang.IllegalArgumentException: Unsupported shape class com.spatial4j.core.shape.jts.JtsGeometry
引发IllegalArgumentException
java.lang.IllegalArgumentException: Unsupported shape class com.spatial4j.core.shape.jts.JtsGeometry
My question is now 我的问题是
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.context.jts.JtsSpatialContext;
import com.spatial4j.core.io.jts.JtsWKTReader;
import com.spatial4j.core.shape.Shape;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy;
import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree;
import org.apache.lucene.spatial.serialized.SerializedDVStrategy;
import java.text.ParseException;
public class TestLuceneSpatial {
public static void main(String[] args) throws ParseException {
String wkt = "POLYGON((-122.4604 37.7818,-122.4707 37.7645,-122.4659 37.7509,-122.4337 37.7476,-122.4192 37.7856,-122.4604 37.7818))";
SpatialContext ctx = SpatialContext.GEO;
GeohashPrefixTree grid = new GeohashPrefixTree(ctx, 6);
RecursivePrefixTreeStrategy rptStrategy = new RecursivePrefixTreeStrategy(grid, "rptBBox");
rptStrategy.setPrefixGridScanLevel(grid.getMaxLevels() - 1);
SerializedDVStrategy dvStrategy = new SerializedDVStrategy(ctx, "polygon");
JtsWKTReader wktReader = new JtsWKTReader(JtsSpatialContext.GEO, null);
Shape spatial4jShape = wktReader.parse(wkt);
Document doc = new Document();
Field[] fields = rptStrategy.createIndexableFields(spatial4jShape.getBoundingBox());
for (Field f : fields) {
doc.add(f);
}
fields = dvStrategy.createIndexableFields((Shape)spatial4jShape);
for (Field f : fields) {
doc.add(f);
}
for (IndexableField f : doc.getFields()) {
System.out.printf("%s => %s\n", f.name(), f.binaryValue());
}
System.out.println(doc);
}
}
Too late but may help 为时已晚,但可能会有所帮助
private val ctx = new JtsSpatialContextFactory().newSpatialContext
and use shape directly 并直接使用形状
Field[] fields = rptStrategy.createIndexableFields(spatial4jShape);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.