Created
September 24, 2012 08:23
-
-
Save lassewesth/3774902 to your computer and use it in GitHub Desktop.
Spatial example
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 spatial; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.test.ImpermanentGraphDatabase; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.neo4j.annotation.GraphId; | |
import org.springframework.data.neo4j.annotation.Indexed; | |
import org.springframework.data.neo4j.annotation.NodeEntity; | |
import org.springframework.data.neo4j.config.EnableNeo4jRepositories; | |
import org.springframework.data.neo4j.config.Neo4jConfiguration; | |
import org.springframework.data.neo4j.conversion.EndResult; | |
import org.springframework.data.neo4j.repository.GraphRepository; | |
import org.springframework.data.neo4j.repository.NamedIndexRepository; | |
import org.springframework.data.neo4j.repository.SpatialRepository; | |
import org.springframework.data.neo4j.support.Neo4jTemplate; | |
import org.springframework.data.neo4j.support.index.IndexType; | |
import org.springframework.data.neo4j.support.node.Neo4jHelper; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.transaction.BeforeTransaction; | |
import org.springframework.transaction.annotation.Transactional; | |
import java.util.Set; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
@NodeEntity | |
class Venue { | |
@GraphId | |
private Long graphId; | |
@Indexed(indexType = IndexType.POINT, indexName = "VenueLocation") | |
private String wkt; | |
String name; | |
public void setLocation(float lon, float lat) { | |
this.wkt = String.format("POINT( %s %s )", lon, lat); | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getLocation() { | |
return wkt; | |
} | |
} | |
interface VenueRepository extends GraphRepository<Venue>, | |
SpatialRepository<Venue>, NamedIndexRepository<Venue> { | |
} | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration | |
@Transactional | |
public class SpatialTests { | |
@Configuration | |
@EnableNeo4jRepositories | |
static class TestConfig extends Neo4jConfiguration { | |
@Bean | |
GraphDatabaseService graphDatabaseService() { | |
return new ImpermanentGraphDatabase(); | |
} | |
} | |
private static final String[] vArray = | |
{"Apollo Theatre", | |
"Carnegie Hall", | |
"Roseland Ballroom", | |
"Radio City Music Hall", | |
"Bowery Ballroom"}; | |
private static final float[][] locArray = | |
{{(float) 40.810764, (float) -73.950231}, | |
{(float) 40.765331, (float) -73.979451}, | |
{(float) 40.763186, (float) -73.984215}, | |
{(float) 40.760553, (float) -73.980052}, | |
{(float) 40.720656, (float) -73.99327}}; | |
@Autowired | |
private VenueRepository venueRep; | |
@Autowired | |
Neo4jTemplate template; | |
@Autowired | |
GraphDatabaseService graphDatabaseService; | |
@Test | |
public void test1OneNode() { | |
Venue venue1 = new Venue(); | |
venue1.setLocation(locArray[0][1], locArray[0][0]); | |
venue1 = venueRep.save(venue1); | |
assertNotNull(venue1); | |
} | |
@Test | |
public void test2FiveNodes() { | |
for (int i = 0; i < vArray.length; i++) { | |
Venue venue = new Venue(); | |
venue.setLocation(locArray[i][1], locArray[i][0]); | |
System.out.println(i + ": " + venue.getLocation()); | |
venue = venueRep.save(venue); | |
} | |
assertEquals(venueRep.findAll().as(Set.class).size(), 5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This uses: