Created
April 3, 2016 15:16
-
-
Save geosmart/19e6e4cb0c953e1b63e9afe48425de8f to your computer and use it in GitHub Desktop.
Java实现Neo4j Spatial空间查询
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lt.uadb.etl.service.test; | |
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.ResourceIterable; | |
import org.neo4j.graphdb.Transaction; | |
import org.neo4j.graphdb.index.IndexHits; | |
import org.neo4j.tooling.GlobalGraphOperations; | |
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 TestCase | |
* | |
* @author wanggang | |
*/ | |
@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.put(LayerNodeIndex.LAT_PROPERTY_KEY, "lati"); | |
config.put(LayerNodeIndex.LON_PROPERTY_KEY, "long"); | |
config.put(SpatialIndexProvider.GEOMETRY_TYPE, LayerNodeIndex.POINT_PARAMETER); | |
} | |
@Test | |
public void test_deleteAddressNodeSpatialIndex() { | |
try (Transaction tx = graphDatabaseService.beginTx()) { | |
LayerNodeIndex layerIndex = new LayerNodeIndex("geom", graphDatabaseService, config); | |
layerIndex.delete(); | |
tx.success(); | |
} | |
} | |
@Test | |
public void test_createAddressNodeSpatialIndex() { | |
try (Transaction tx = graphDatabaseService.beginTx()) { | |
LayerNodeIndex layerIndex = new LayerNodeIndex("geom", graphDatabaseService, config); | |
ResourceIterable<Node> nodes = GlobalGraphOperations.at(graphDatabaseService).getAllNodes(); | |
for (Node node : nodes) { | |
Object lon = node.getProperty("long", null); | |
Object lat = node.getProperty("lati", null); | |
System.out.println(node.getId()); | |
System.out.println(node.getPropertyKeys()); | |
if (lon != null && lat != null) { | |
System.out.println(lon + " " + lat); | |
layerIndex.add(node, "", ""); | |
} | |
} | |
tx.success(); | |
} | |
} | |
@Test | |
public void test_SpatialQuery_Buffer() { | |
try (Transaction tx = graphDatabaseService.beginTx()) { | |
LayerNodeIndex layerIndex = new LayerNodeIndex("addressNodeLayer", graphDatabaseService, config); | |
// [y,x] rather than [x,y] | |
Double[] coords = new Double[] {31.330263d, 120.684968d}; | |
Map<String, Object> params = new HashMap<String, Object>(); | |
params.put(LayerNodeIndex.POINT_PARAMETER, coords); | |
params.put(LayerNodeIndex.DISTANCE_IN_KM_PARAMETER, 1.0); | |
IndexHits<Node> hits = layerIndex.query(LayerNodeIndex.WITHIN_DISTANCE_QUERY, params); | |
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_SpatialQuery_BBox() { | |
try (Transaction tx = graphDatabaseService.beginTx()) { | |
// XXX Cypher查询时,Index `addressNodelayer` does not exist | |
LayerNodeIndex layerIndex = new LayerNodeIndex("addressNodeLayer", graphDatabaseService, 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("addressNodeLayer", 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
参考Neo4j Spatial源码写的测试用例,直接用LayerNodeIndex 新建索引,上述test_createAddressNodeSpatialIndex会报错Index
geom
does not exist ,正确示例参考NewNeo4jSpatialTest.java