2013-08-17 49 views
1

我一直在努力爲我的項目使用Neo4j Spatial,但我無法使它工作。Neo4j空間:不能運行空間

由於有限的文檔和示例,我想出瞭如何將OSM映射加載到數據庫。但是爲了檢查它是否被加載,我試圖執行空間查詢。

試圖運行我的代碼,我得到這個錯誤:

import.java:69: error: cannot access GremlinGroovyPipeline 
         .startIntersectSearch(layer, bbox) 
         ^
class file for com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline not found 

我明白了什麼是錯的(它無法找到所需的庫),但我不知道如何解決它。原因是當我運行Neo4j Spatial測試時,LayerTest.java和TestSpatial.java確實包含GeoPipeline庫,並且它工作得很好。然而,當我創建我的簡單java文件來測試Neo4j,並試圖執行依賴GeoPipeline庫的命令時,我得到了上面的錯誤。

我閱讀GitHub上的指示Neo4j的,看到這樣一個字條:

Note: neo4j-spatial has a mandatory dependency on GremlinGroovyPipeline from the com.tinkerpop.gremlin.groovy package. The dependency in neo4j is type 'provided', so when using neo4j-spatial in your own Java project, make sure to add the following dependency to your pom.xml, too.

不過,我沒有使用Maven構建我的應用程序。這是一個簡單的java文件,我想運行它來測試我是否知道一切正常。

這裏是從我的java文件中的代碼:

package org.neo4j.gis.spatial; 

import java.io.File; 
import java.io.IOException; 
import java.nio.charset.Charset; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import org.geotools.data.DataStore; 
import org.geotools.data.neo4j.Neo4jSpatialDataStore; 
import org.geotools.data.simple.SimpleFeatureCollection; 
import org.neo4j.gis.spatial.osm.OSMDataset; 
import org.neo4j.gis.spatial.osm.OSMDataset.Way; 
import org.neo4j.gis.spatial.osm.OSMGeometryEncoder; 
import org.neo4j.gis.spatial.osm.OSMImporter; 
import org.neo4j.gis.spatial.osm.OSMLayer; 
import org.neo4j.gis.spatial.osm.OSMRelation; 
import org.neo4j.gis.spatial.pipes.osm.OSMGeoPipeline; 
import org.neo4j.graphdb.Direction; 
import org.neo4j.graphdb.Node; 
import org.neo4j.graphdb.Relationship; 

import com.vividsolutions.jts.geom.Envelope; 
import com.vividsolutions.jts.geom.Geometry; 

import org.neo4j.kernel.impl.batchinsert.BatchInserter; 
import org.neo4j.kernel.impl.batchinsert.BatchInserterImpl; 
import org.neo4j.kernel.EmbeddedGraphDatabase; 
import org.neo4j.graphdb.GraphDatabaseService; 
import org.neo4j.gis.spatial.pipes.GeoPipeline; 

class SpatialOsmImport { 
    public static void main(String[] args) 
    { 
     OSMImporter importer = new OSMImporter("ott.osm"); 
     Map<String, String> config = new HashMap<String, String>(); 
     config.put("neostore.nodestore.db.mapped_memory", "90M"); 
     config.put("dump_configuration", "true"); 
     config.put("use_memory_mapped_buffers", "true"); 
     BatchInserter batchInserter = new BatchInserterImpl("target/dependency", config); 
     importer.setCharset(Charset.forName("UTF-8")); 
     try{ 
      importer.importFile(batchInserter, "ott.osm", false); 
      batchInserter.shutdown(); 
      GraphDatabaseService db = new EmbeddedGraphDatabase("target/dependency"); 
      importer.reIndex(db, 10000); 
      db.shutdown(); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 

     GraphDatabaseService database = new EmbeddedGraphDatabase("target/dependency"); 
     try{ 
      SpatialDatabaseService spatialService = new SpatialDatabaseService(database); 
      Layer layer = spatialService.getLayer("layer_roads"); 
      LayerIndexReader spatialIndex = layer.getIndex(); 
      System.out.println("Have " + spatialIndex.count() + " geometries in " + spatialIndex.getBoundingBox()); 

      Envelope bbox = new Envelope(-75.80, 45.19, -75.7, 45.23); 
      // Search searchQuery = new SearchIntersectWindow(bbox); 
      // spatialIndex.executeSearch(searchQuery); 
      // List<SpatialDatabaseRecord> results = searchQuery.getResults(); 
      List<SpatialDatabaseRecord> results = GeoPipeline 
         .startIntersectSearch(layer, bbox) 
         .toSpatialDatabaseRecordList(); 
      doGeometryTestsOnResults(bbox, results); 
     } finally { 
      database.shutdown(); 
     } 
    } 
    private static void doGeometryTestsOnResults(Envelope bbox, List<SpatialDatabaseRecord> results) { 
     System.out.println("Found " + results.size() + " geometries in " + bbox); 
     Geometry geometry = results.get(0).getGeometry(); 
     System.out.println("First geometry is " + geometry); 
     geometry.buffer(2); 
    } 

} 

這是非常簡單的權利,但我不能使它發揮作用。如何在我的應用程序中包含com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline,因此它有效?

我在Ubuntu 12.04和Java版本「1.7.0_25」,Java(TM)SE運行時環境(build 1.7.0_25-b15)上運行了所有東西。

任何幫助,非常感謝。

回答

0

讓所有的地方所需要的依賴,你可以將它們包含在類路徑的最佳方法是運行

mvn dependency:copy-dependencies 
在Neo4j的空間

,並找到庫的目標/ DEPS包括,請參閱http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

+0

儘管如此,我仍然認爲我做得不對。我曾嘗試使用maven創建我自己的項目幷包含依賴項,但我仍然失敗。 在上面的例子中,我剛剛創建了一個java文件,並試圖通過它導入所有內容。哪個不行。我嘗試了你的建議,但我仍然無法弄清楚如何使它工作。你有沒有關於如何在neo4j空間上構建應用的教程? – rahanar

+0

如果你不使用maven構建,你仍然可以從他們的倉庫獲得依賴關係。轉到[Maven Central](http://search.maven.org)並搜索諸如'g:「com.tinkerpop.gremlin」和a:「gremlin-groovy」'之類的東西。找到正確的版本並將其放在你的類路徑中。 – jjaderberg