public
Created

neo4j spatial football stadiums

  • Download Gist
SampleSpatialGraph.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
package org.neo4j.gis.spatial;
 
import org.neo4j.gis.spatial.indexprovider.SpatialIndexProvider;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;
import org.neo4j.kernel.EmbeddedGraphDatabase;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
public class SampleSpatialGraph {
public static void main(String[] args) throws IOException {
List<String> lines = readFile("/path/to/stadiums.csv");
 
EmbeddedGraphDatabase db = new EmbeddedGraphDatabase("/path/to/neo4j-community-1.9.M05/data/graph.db");
Index<Node> index = createSpatialIndex(db, "stadiumsLocation");
Transaction tx = db.beginTx();
 
for (String stadium : lines) {
String[] columns = stadium.split(",");
Node stadiumNode = db.createNode();
stadiumNode.setProperty("wkt", String.format("POINT(%s %s)", columns[4], columns[3]));
stadiumNode.setProperty("name", columns[0]);
index.add(stadiumNode, "dummy", "value");
}
 
tx.success();
tx.finish();
}
 
private static Index<Node> createSpatialIndex(EmbeddedGraphDatabase db, String indexName) {
return db.index().forNodes(indexName, SpatialIndexProvider.SIMPLE_WKT_CONFIG);
}
 
private static List<String> readFile(String stadiumsFile) throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(stadiumsFile));
br.readLine();
String line = br.readLine();
while (line != null) {
lines.add(line);
line = br.readLine();
}
return lines;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.