lucene空间索引
1、创建索引
-------------------------------------------------------------------------------------------
GeohashPrefixTree grid = new GeohashPrefixTree(ctx, 12);//< 1 meter == 11 maxLevels
-------------------------------------------------------------------------------------------
GeohashPrefixTree grid = new GeohashPrefixTree(ctx, 12);//< 1 meter == 11 maxLevels
this.strategy = new RecursivePrefixTreeStrategy(grid, "shape");
((RecursivePrefixTreeStrategy) this.strategy).setDistErrPct(LUCENE_4464_distErrPct);//1% radius (small!)
String wktstr = clobtoString(map.get("wkt"));//图形信息
String objectid = map.get("objectid").toString();//对象id
String name = map.get("name").toString();//对象名称
Shape shape = wktGeoRect(wktstr);
Document doc = new Document();
doc.add(new StringField("objectidtable", objectid + tableName, Field.Store.YES));
doc.add(new StringField("objectid", objectid, Field.Store.YES));
doc.add(new StringField("tableName", tableName, Field.Store.YES));
doc.add(new StringField("metadataid", mid.toString(), Field.Store.YES));
doc.add(new TextField ("title", name, Field.Store.YES));
if (shape != null) {
for (Field f : strategy.createIndexableFields(shape)) {
doc.add(f);
}
doc.add(new StoredField(strategy.getFieldName(), ctx.toString(shape)));
}
indexWriter.addDocument(doc);
---------------------------------------------------------------
public Shape wktGeoRect(String wktStr) {
JtsGeometry jtsGeom = null;
try {
if (StringUtils.trim(wktStr.substring(0, wktStr.indexOf("("))).equalsIgnoreCase("POINT")) {
wktStr = wktStr.substring(wktStr.indexOf("(") + 1, wktStr.lastIndexOf(")"));
String[] point = wktStr.split(" ");
return ctx.makePoint(Double.parseDouble(point[0]), Double.parseDouble(point[1]));
} else {
jtsGeom = (JtsGeometry) ctx.readShape(wktStr);
}
} catch (Exception e) {
}
return jtsGeom;
}
2、查询空间索引
@Test
public void testshape() throws Exception {
JtsSpatialContext ctx = JtsSpatialContext.GEO;
Directory directory = new SimpleFSDirectory(new File("D:/platform/spatiallucence"));
IndexReader[] indexReaders1 = new IndexReader[]{IndexReader.open(directory)};
MultiReader multiReader = new MultiReader(indexReaders1);
indexSearcher = new IndexSearcher(multiReader);
indexSearcher.setSimilarity(new NoScoreSimilarity());
GeohashPrefixTree grid = new GeohashPrefixTree(ctx, 12);//< 1 meter == 11 maxLevels
strategy = new RecursivePrefixTreeStrategy(grid, "shape");
((RecursivePrefixTreeStrategy) strategy).setDistErrPct(0.025);//1% radius (small!)
//POINT (121.591953019118 28.7566972164043)
//Shape shape = ctx.readShape("POINT (121.454715099823 28.860595871703)");
Shape shape = ctx.readShape("POLYGON(" +
"(121.10836126349 28.84450508816,\n" +
"121.12569906256 28.84450508816,\n" +
"121.12569906256 28.856950537989,\n" +
"121.10836126349 28.856950537989,\n" +
"121.10836126349 28.84450508816))");
shape = ctx.makeRectangle(121.10836126349d ,121.12569906256d, 28.84450508816d ,28.856950537989d);
SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, shape);
args.setDistErrPct(0.025);
Query query = strategy.makeQuery(args);
TopDocs results = indexSearcher.search(query, 1000);
int numTotalHits = results.totalHits;
System.out.println("共 " + numTotalHits + " 完全匹配的文档");
ScoreDoc[] hits = results.scoreDocs;
for (int i = 0; i < hits.length; i++) {
Document document = indexSearcher.doc(hits[i].doc);
System.out.println("Id: " + document);
}
}