Created
March 10, 2013 21:36
-
-
Save mneedham/5130545 to your computer and use it in GitHub Desktop.
neo4j spatial football stadiums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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