1
votes

Good day,

I want to work with building geometries on a larger scale. For that purpose, I am importing geometry data from various sources (also shp-files) to an embedded neo4j database working with spring-data-neo4j. So far so good.

Where I am stuck: In my domain, I define a Building entity that has a shape Polygon wkt; Is it possible to create a node having a Polygon property via the CRUDRepository?

With a Point it would work just fine: e.g.

@NodeEntity
public class Building implements Serializable{
    @GraphId
    private Long id;
    @Indexed(indexType = IndexType.POINT, indexName = "building_wkt_index", unique = false)
    private Point wkt;
}

public interface BuildingRepository extends GraphRepository<Building>, SpatialRepository<Building>{
/**
 * CRUD
 */
}

Building b = new Building();        
b.setWkt(new Point(12,12));
buildingService.save(b);

The problem is that there are no IndexTypes implemented for Lines and Polygons. This is my first project with spring-data and neo4j so I am unsure which direction I should take. Taking a look at the neo4j spatial documentation shows that using wkt's, it should be possible to store Polygons (see http://neo4j-contrib.github.io/spatial/#spatial-server-plugin)

https://stackoverflow.com/a/26567534 also proposes to create a spatial index via REST. Another suggestion is this one: https://stackoverflow.com/a/24741823.

I tried to manually create a spatial index like so:

Transaction tx = template.getGraphDatabaseService().beginTx();
template.getGraphDatabaseService().index().forNodes("building_wkt_index", MapUtil.stringMap(
IndexManager.PROVIDER, "spatial", "geometry_type", "polygon", "wkt", "wkt"));
tx.success();

and call the repository's save(). However, this doesn't seem to work. The wkt is not stored, it's null.

Is it possible to use this named index (is that even possible) in my Buildingsomehow - or is moving all the transactions over the neo4j spatial plugin's REST really the only way to do this.

Would it for example be possible to implement a new IndexType? all input is much appreciated!

1
hi,Can you please share your pom.xml versions of neo4j and neo4j-spatial plugin. I am unable to import GraphRepository and SpatialRepository into my project.Hema

1 Answers

0
votes

For anyone who is interested in a possible solution:

1st I manually create a spatial index:

template.getGraphDatabaseService().index().forNodes("your_spatial_index", MapUtil.stringMap(
            IndexManager.PROVIDER, "spatial", "geometry_type","polygon","wkt", "wkt"));

Within the domain entity, the wkt is stored as String:

String wkt;

Then I moved the repository access to a service. When a new node is saved, this node is manually added to the spatial index:

Transaction tx = template.getGraphDatabase().beginTx();
Node n = template.getNode(b.getId());
template.getGraphDatabase().getIndex("your_spatial_index").add(n,"id",b.getId());
tx.success();
tx.close();

At least, this is a quick fix to get a polygon wkt added to the R-Tree. However, updating and deleting the index node must be handled manually.