Neo4j Spatial: can't run spatial

2019-07-27 10:24发布

I have been trying to work with Neo4j Spatial for my project, but I can't make it work.

With limited documentation and examples I figured out how to load OSM map to the database. But to check if it is loaded, I am trying to execute a spatial query.

While trying to run my code I get this error:

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

I understand what's wrong (it can't find the required library), but I don't know how to fix it. The reason is when I run Neo4j Spatial tests, LayerTest.java and TestSpatial.java do include GeoPipeline library and it works perfectly fine. However, when I created my simple java file to test Neo4j, and trying to execute commands that depend GeoPipeline library I get the error above.

I read the instructions on github for Neo4j and saw this note:

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.

However, I am not using Maven to build my app. It is a simple java file, that I want to run to test if I get how everything works.

here is the code from my java file:

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);
    }

}

It is very simple right now, but I can't make it work. How do I include com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline in my app, so it works?

I run everything on Ubuntu 12.04 and java version "1.7.0_25", Java(TM) SE Runtime Environment (build 1.7.0_25-b15).

Any help is greatly appreciated.

1条回答
Root(大扎)
2楼-- · 2019-07-27 10:52

the best way to get all the required dependencies in a place where you can include them in your classpath is to run

mvn dependency:copy-dependencies

in neo4j-spatial, and find the libs to include in target/deps, see http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

查看更多
登录 后发表回答