1

I am trying to get the Neo4j spatial plugin working. However, when I try to query withinDistance via REST, either at the plugin URI or with cypher, I do not get any results, even if I set the distance to several million km. I've looked at Neo4j Spatial 'WithinDistance' Cypher query returns empty while REST call returns data which has a similar issue and am indexing the nodes but still no joy. Could someone have a look at the code below and tell me what the issue is? Thanks.

private void checkSpatialDB() {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(SERVER_ROOT_URI + "ext/SpatialPlugin");
    Response response = target.request(MediaType.APPLICATION_JSON)
            .get(Response.class);

    System.out.println(String.format("Spatial Plugin data:\n" + System.getProperty("line.separator") + "%s",
            response.readEntity(String.class)));
    response.close();
}

private void addSpatialDataTest() {
    String layerProperties = "{ \"layer\" : \"geom\", \"lat\" : \"lat\", \"lon\" : \"lon\" }";

    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(SERVER_ROOT_URI + "ext/SpatialPlugin/graphdb/addSimplePointLayer");
    Response response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(layerProperties, MediaType.APPLICATION_JSON));

    System.out.println(String.format("Add geometry layer:\n" + System.getProperty("line.separator") + "%s",
            response.readEntity(String.class)));

    response.close();

    // Now add a spatial index
    String indexProperties = "{ \"name\" : \"geom\", \"config\" : {"
            + "\"provider\" : \"spatial\", \"geometry_type\" : \"point\", \"lat\" : \"lat\","
            + "\"lon\" : \"lon\" } }";
    client = ClientBuilder.newClient();
    target = client.target(SERVER_ROOT_URI + "index/node/");
    response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(indexProperties, MediaType.APPLICATION_JSON));

    System.out.println(String.format("Add geometry index:\n" + System.getProperty("line.separator") + "%s",
            response.readEntity(String.class)));
    response.close();

    // Now add a spatial node
    String yeovil = "{ \"lat\" : 50.9, \"lon\" : -2.6 }";
    client = ClientBuilder.newClient();
    target = client.target(SERVER_ROOT_URI + "node");
    response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(yeovil, MediaType.APPLICATION_JSON));
    URI newNode = response.getLocation();

    System.out.println(String.format("Add node for Yeovil:\n" + System.getProperty("line.separator") + "%s"
            + "URI for new node [%s]",
            response.readEntity(String.class), newNode));
    response.close();

    // Add the created node to the spatial layer
    String addToLayer = "{ \"layer\" : \"geom\", \"node\" : \"" + newNode + "\" }";
    client = ClientBuilder.newClient();
    target = client.target(SERVER_ROOT_URI + "ext/SpatialPlugin/graphdb/addNodeToLayer");
    response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(addToLayer, MediaType.APPLICATION_JSON));

    System.out.println(String.format("Add node to spatial layer:\n" + System.getProperty("line.separator") + "%s",
            response.readEntity(String.class)));
    response.close();

    // Add the created node to the geom index
    String addToIndex = "{ \"key\" : \"noKey\", \"value\" : \"noValue\", \"uri\" : \"" + newNode + "\" }";
    client = ClientBuilder.newClient();
    target = client.target(SERVER_ROOT_URI + "index/node/geom");
    response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(addToIndex, MediaType.APPLICATION_JSON));
    response.close();

    // Now add another spatial node
    String rayleigh = "{ \"lat\" : 51.6, \"lon\" : 0.6 }";
    client = ClientBuilder.newClient();
    target = client.target(SERVER_ROOT_URI + "node");
    response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(rayleigh, MediaType.APPLICATION_JSON));
    URI anotherNode = response.getLocation();

    System.out.println(String.format("Add node for Yeovil:\n" + System.getProperty("line.separator") + "%s"
            + "URI for new node [%s]",
            response.readEntity(String.class), anotherNode));

    // Add the created node to the spatial layer
    String addAnotherToLayer = "{ \"layer\" : \"geom\", \"node\" : \"" + anotherNode + "\" }";
    client = ClientBuilder.newClient();
    target = client.target(SERVER_ROOT_URI + "ext/SpatialPlugin/graphdb/addNodeToLayer");
    response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(addAnotherToLayer, MediaType.APPLICATION_JSON));

    System.out.println(String.format("Add node to spatial layer:\n" + System.getProperty("line.separator") + "%s",
            response.readEntity(String.class)));
    response.close();

    // Add the created node to the geom index
    String addAnotherToIndex = "{ \"key\" : \"noKey\", \"value\" : \"noValue\", \"uri\" : \"" + anotherNode + "\" }";
    client = ClientBuilder.newClient();
    target = client.target(SERVER_ROOT_URI + "index/node/geom");
    response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(addAnotherToIndex, MediaType.APPLICATION_JSON));
    System.out.println(response.readEntity(String.class));
    response.close();
}

private void findSpatialNode() {
    // Find nodes within 135km of Portsmouth - should be Yeovil
    // String queryString = "{ \"layer\" : \"geom\", \"pointX\" : 50.8, \"pointY\" : -1.1, \"distanceInKm\" : 1350 }";
    String queryString = "{ \"query\" : \"start n=node:geom('withinDistance:[50.8, -1.1, 500.0]') return n\" }";
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target(SERVER_ROOT_URI + "cypher");
    Response response = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(queryString, MediaType.APPLICATION_JSON));

    System.out.println(String.format("Sent query [%s], status code [%d]\nShould just get Yeovil in search result:"
            + System.getProperty("line.separator") + "%s", queryString, response.getStatus(),
            response.readEntity(String.class)));
    response.close();
}
Community
  • 1
  • 1
Ashley Bye
  • 1,752
  • 2
  • 23
  • 40
  • Which version are you using? And it should be enough to add the node to the index not to the layer itself (might cause an issue if the layer node is already there). – Michael Hunger Mar 02 '14 at 12:49

0 Answers0