Skip to content

Instantly share code, notes, and snippets.

@geosmart
Last active April 10, 2016 13:29
Show Gist options
  • Save geosmart/9456d26a926c3bfddca50944eed0282f to your computer and use it in GitHub Desktop.
Save geosmart/9456d26a926c3bfddca50944eed0282f to your computer and use it in GitHub Desktop.
参考Neo4j Spatial源码写的测试用例,不能直接用LayerNodeIndex 新建索引
package com.lt.uadb.etl.service.test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.gis.spatial.indexprovider.LayerNodeIndex;
import org.neo4j.gis.spatial.indexprovider.SpatialIndexProvider;
import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;
import org.neo4j.graphdb.index.IndexManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lt.uadb.etl.util.TrextConstantUtil.EnumAddressRuleNode;
import com.lt.util.geo.GeoUtil;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
/**
* Neo4j Spatial Query Test
*
* @author geosmart
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:neo4j.db.embed.cfg.xml"})
public class Neo4jSpatialTest {
@Autowired
GraphDatabaseService graphDatabaseService;
final Map<String, String> config = new HashMap<String, String>();
@Before
public void setup() {
System.out.println("---setup----");
config.putAll(SpatialIndexProvider.SIMPLE_POINT_CONFIG);
config.put(LayerNodeIndex.LAT_PROPERTY_KEY, "lati");
config.put(LayerNodeIndex.LON_PROPERTY_KEY, "long");
}
@Test
public void test_createAddressNodeSpatialIndex() {
try (Transaction tx = graphDatabaseService.beginTx()) {
IndexManager indexMan = graphDatabaseService.index();
Index<Node> index = indexMan.forNodes("geoindex", config);
assertNotNull(index);
int count = 1;
ResourceIterator<Node> nodes = graphDatabaseService.findNodes(DynamicLabel.label("AddressNode"));
while (nodes.hasNext()) {
Node node = nodes.next();
Object lon = node.getProperty("long", null);
Object lat = node.getProperty("lati", null);
if (lon != null && lat != null) {
System.out.println(count + " " + lon + " " + lat);
index.add(node, "", "");
count++;
}
}
tx.success();
}
}
@Test
public void test_deleteAddressNodeSpatialIndex() {
try (Transaction tx = graphDatabaseService.beginTx()) {
IndexManager indexMan = graphDatabaseService.index();
Index<Node> index = indexMan.forNodes("geoindex", config);
index.delete();
tx.success();
}
}
@Test
public void test_SpatialQuery_withinWKTGeometry() {
try (Transaction tx = graphDatabaseService.beginTx()) {
String queryStr =
" start n = node:geoindex('withinWKTGeometry:POLYGON ((120.678966 31.300864, 120.678966 31.330864, 120.978966 31.330864, 120.978966 31.300864, 120.678966 31.300864))') match (n) where (n.ruleabbr in ['POI','STR']) and n.text='拙政别墅' return n";
Result result = graphDatabaseService.execute(queryStr);
assertTrue(result.hasNext());
while (result.hasNext()) {
Map<String, Object> row = result.next();
System.out.println(row);
}
}
}
@Test
public void test_SpatialQuery_withinDistance_API() {
try (Transaction tx = graphDatabaseService.beginTx()) {
Index<Node> layerIndex = graphDatabaseService.index().forNodes("geoindex", config);
assertNotNull(layerIndex);
Map<String, Object> params = new HashMap<String, Object>();
params.put(LayerNodeIndex.POINT_PARAMETER, new Double[] {31.331937, 120.638154});
params.put(LayerNodeIndex.DISTANCE_IN_KM_PARAMETER, 1.0);
System.out.println(params);
IndexHits<Node> hits = layerIndex.query(LayerNodeIndex.WITHIN_DISTANCE_QUERY, params);
System.out.println(hits.size());
}
}
@Test
public void test_SpatialQuery_withinDistance_CQL() {
try (Transaction tx = graphDatabaseService.beginTx()) {
// CypherQuery
String queryStr =
" start n = node:geoindex('withinDistance:[31.331937,120.638154,1.0]') match (n) where (n.ruleabbr in ['POI','STR']) and n.text='拙政别墅' return n";
Result result = graphDatabaseService.execute(queryStr);
assertTrue(result.hasNext());
while (result.hasNext()) {
Map<String, Object> row = result.next();
System.out.println(row);
}
}
}
@Test
public void test_SpatialQuery_BBox() {
try (Transaction tx = graphDatabaseService.beginTx()) {
Index<Node> layerIndex = graphDatabaseService.index().forNodes("geoindex", config);
Coordinate p1 = new Coordinate(120.684968d, 31.330263d);
Coordinate p2 = new Coordinate(120.784968d, 31.930263d);
Geometry rec = GeoUtil.createRectangle(p1, p2);
IndexHits<Node> hits = layerIndex.query(LayerNodeIndex.WITHIN_WKT_GEOMETRY_QUERY, rec.toText());
for (Node spatialNode : hits) {
Node dbNode = graphDatabaseService.getNodeById(spatialNode.getId());
System.out.println(spatialNode.getId());
for (String key : dbNode.getPropertyKeys()) {
System.out.println(key + " " + dbNode.getProperty(key));
}
System.out.println("******\n");
}
}
}
@Test
public void test_addNodeToLayer() {
try (Transaction tx = graphDatabaseService.beginTx()) {
LayerNodeIndex layerIndex = new LayerNodeIndex("geoIndex", graphDatabaseService, config);
Node node = graphDatabaseService.createNode(DynamicLabel.label(EnumAddressRuleNode.PRO.toString()));
node.setProperty("lati", 32.214);
node.setProperty("long", 118.123);
node.setProperty("text", "测试");
layerIndex.add(node, "", "");
tx.success();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment