Skip to content

Instantly share code, notes, and snippets.

@mneedham
Created March 10, 2013 21:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mneedham/5130545 to your computer and use it in GitHub Desktop.
Save mneedham/5130545 to your computer and use it in GitHub Desktop.
neo4j spatial football stadiums
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment