Skip to content

Commit

Permalink
updated to 2.0.0-M06
Browse files Browse the repository at this point in the history
  • Loading branch information
jexp committed Oct 23, 2013
1 parent 58886bd commit 2b76f17
Show file tree
Hide file tree
Showing 23 changed files with 853 additions and 740 deletions.
12 changes: 8 additions & 4 deletions pom.xml
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<neo4j.version>2.0.0-M04</neo4j.version>
<neo4j.version>2.0.0-M06</neo4j.version>
<neo4j.java.version>1.7</neo4j.java.version>
<neo4j.graphcollections.version>0.7-neo4j-2.0.0-SNAPSHOT</neo4j.graphcollections.version>
<neo4j.graphcollections.version>0.7.1-neo4j-2.0.0-M06</neo4j.graphcollections.version>
<skinGroupId>org.neo4j.maven.skins</skinGroupId>
<skinArtifactId>default-skin</skinArtifactId>
<skinVersion>2</skinVersion>
Expand Down Expand Up @@ -35,8 +35,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -181,6 +181,10 @@
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-lucene-index</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
72 changes: 38 additions & 34 deletions src/main/java/org/geotools/data/neo4j/StyledImageExporter.java
Expand Up @@ -59,6 +59,7 @@
import org.geotools.styling.StyleFactory;
import org.neo4j.gis.spatial.SpatialTopologyUtils;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
Expand Down Expand Up @@ -199,40 +200,43 @@ public void saveImage(FeatureCollection<SimpleFeatureType,SimpleFeature>[] featu

public void saveLayerImage(String[] layerNames, String sldFile, File imagefile, ReferencedEnvelope bounds) throws IOException {
DataStore store = new Neo4jSpatialDataStore(db);
// debugStore(store, layerNames);
StringBuffer names = new StringBuffer();
for (String name : layerNames) {
if (names.length() > 0)
names.append(", ");
names.append(name);
}
System.out.println("Exporting layers '" + names + "' to styled image " + imagefile.getPath());

Style style = getStyleFromSLDFile(sldFile);

MapContent mapContent = new MapContent();
for (int i = 0; i < layerNames.length; i++) {
SimpleFeatureSource featureSource = store.getFeatureSource(layerNames[i]);
Style featureStyle = style;
if (featureStyle == null) {
featureStyle = getStyle(i);
}

if (featureStyle == null) {
featureStyle = createStyleFromGeometry((SimpleFeatureType) featureSource.getSchema(), Color.BLUE, Color.CYAN);
System.out.println("Created style from geometry '" + featureSource.getSchema().getGeometryDescriptor().getType() + "': " + featureStyle);
}

mapContent.addLayer(new org.geotools.map.FeatureLayer(featureSource, featureStyle));

if (bounds == null) {
bounds = featureSource.getBounds();
} else {
bounds.expandToInclude(featureSource.getBounds());
}
}

saveMapContentToImageFile(mapContent, imagefile, bounds);
try (Transaction tx=db.beginTx()) {
// debugStore(store, layerNames);
StringBuffer names = new StringBuffer();
for (String name : layerNames) {
if (names.length() > 0)
names.append(", ");
names.append(name);
}
System.out.println("Exporting layers '" + names + "' to styled image " + imagefile.getPath());

Style style = getStyleFromSLDFile(sldFile);

MapContent mapContent = new MapContent();
for (int i = 0; i < layerNames.length; i++) {
SimpleFeatureSource featureSource = store.getFeatureSource(layerNames[i]);
Style featureStyle = style;
if (featureStyle == null) {
featureStyle = getStyle(i);
}

if (featureStyle == null) {
featureStyle = createStyleFromGeometry((SimpleFeatureType) featureSource.getSchema(), Color.BLUE, Color.CYAN);
System.out.println("Created style from geometry '" + featureSource.getSchema().getGeometryDescriptor().getType() + "': " + featureStyle);
}

mapContent.addLayer(new org.geotools.map.FeatureLayer(featureSource, featureStyle));

if (bounds == null) {
bounds = featureSource.getBounds();
} else {
bounds.expandToInclude(featureSource.getBounds());
}
}

saveMapContentToImageFile(mapContent, imagefile, bounds);
tx.success();
}
}

private Style getStyleFromSLDFile(String sldFile) {
Expand Down
50 changes: 27 additions & 23 deletions src/main/java/org/neo4j/gis/spatial/ShapefileExporter.java
Expand Up @@ -30,6 +30,7 @@
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
Expand Down Expand Up @@ -70,29 +71,32 @@ private File checkFile(File file) {
}

public File exportLayer(String layerName, File file) throws Exception {
file = checkFile(file);
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<String, Serializable>();
URL url = file.toURI().toURL();
create.put("url", url);
create.put("create spatial index", Boolean.TRUE);
create.put("charset", "UTF-8");
ShapefileDataStore shpDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
SimpleFeatureType featureType = neo4jDataStore.getSchema(layerName);
GeometryDescriptor geometryType = featureType.getGeometryDescriptor();
CoordinateReferenceSystem crs = geometryType.getCoordinateReferenceSystem();
// crs = neo4jDataStore.getFeatureSource(layerName).getInfo().getCRS();
try (Transaction tx = neo4jDataStore.beginTx()) {
file = checkFile(file);
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<String, Serializable>();
URL url = file.toURI().toURL();
create.put("url", url);
create.put("create spatial index", Boolean.TRUE);
create.put("charset", "UTF-8");
ShapefileDataStore shpDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
SimpleFeatureType featureType = neo4jDataStore.getSchema(layerName);
GeometryDescriptor geometryType = featureType.getGeometryDescriptor();
CoordinateReferenceSystem crs = geometryType.getCoordinateReferenceSystem();
// crs = neo4jDataStore.getFeatureSource(layerName).getInfo().getCRS();

shpDataStore.createSchema(featureType);
FeatureStore store = (FeatureStore) shpDataStore.getFeatureSource();
store.addFeatures(neo4jDataStore.getFeatureSource(layerName).getFeatures());
if (crs != null)
shpDataStore.forceSchemaCRS(crs);
if (!file.exists()) {
throw new Exception("Shapefile was not created: " + file);
} else if (file.length() < 10) {
throw new Exception("Shapefile was unexpectedly small, only " + file.length() + " bytes: " + file);
}
return file;
shpDataStore.createSchema(featureType);
FeatureStore store = (FeatureStore) shpDataStore.getFeatureSource();
store.addFeatures(neo4jDataStore.getFeatureSource(layerName).getFeatures());
if (crs != null)
shpDataStore.forceSchemaCRS(crs);
if (!file.exists()) {
throw new Exception("Shapefile was not created: " + file);
} else if (file.length() < 10) {
throw new Exception("Shapefile was unexpectedly small, only " + file.length() + " bytes: " + file);
}
tx.success();
return file;
}
}
}
61 changes: 35 additions & 26 deletions src/main/java/org/neo4j/gis/spatial/SpatialDatabaseService.java
Expand Up @@ -60,20 +60,18 @@ public SpatialDatabaseService(GraphDatabaseService database) {
// Public methods

private Node getOrCreateRootFrom(Node ref, RelationshipType relType) {
Relationship rel = ref.getSingleRelationship(relType, Direction.OUTGOING);
if (rel == null) {
Transaction tx = database.beginTx();
try {
Node node = database.createNode();
try (Transaction tx = database.beginTx()) {
Relationship rel = ref.getSingleRelationship(relType, Direction.OUTGOING);
Node node;
if (rel == null) {
node = database.createNode();
node.setProperty("type", "spatial");
ref.createRelationshipTo(node, relType);
tx.success();
return node;
} finally {
tx.finish();
} else {
node = rel.getEndNode();
}
} else {
return rel.getEndNode();
tx.success();
return node;
}
}

Expand All @@ -100,13 +98,19 @@ public String[] getLayerNames() {
}

public Layer getLayer(String name) {
for (Relationship relationship : getSpatialRoot().getRelationships(SpatialRelationshipTypes.LAYER, Direction.OUTGOING)) {
Node node = relationship.getEndNode();
if (name.equals(node.getProperty(PROP_LAYER))) {
return DefaultLayer.makeLayerFromNode(this, node);
}
}
return getDynamicLayer(name);
try (Transaction tx = getDatabase().beginTx()) {
for (Relationship relationship : getSpatialRoot().getRelationships(SpatialRelationshipTypes.LAYER, Direction.OUTGOING)) {
Node node = relationship.getEndNode();
if (name.equals(node.getProperty(PROP_LAYER))) {
Layer layer = DefaultLayer.makeLayerFromNode(this, node);
tx.success();
return layer;
}
}
Layer layer = getDynamicLayer(name);
tx.success();
return layer;
}
}

public Layer getDynamicLayer(String name) {
Expand Down Expand Up @@ -188,13 +192,15 @@ public EditableLayer getOrCreatePointLayer(String name, String xProperty, String
}

public Layer getOrCreateLayer(String name, Class< ? extends GeometryEncoder> geometryEncoder, Class< ? extends Layer> layerClass, String config) {
Layer layer = getLayer(name);
if (layer == null) {
return createLayer(name, geometryEncoder, layerClass, config);
} else if(layerClass == null || layerClass.isInstance(layer)) {
return layer;
} else {
throw new SpatialDatabaseException("Existing layer '"+layer+"' is not of the expected type: "+layerClass);
try (Transaction tx = database.beginTx()) {
Layer layer = getLayer(name);
if (layer == null) {
layer = createLayer(name, geometryEncoder, layerClass, config);
} else if (!(layerClass == null || layerClass.isInstance(layer))) {
throw new SpatialDatabaseException("Existing layer '"+layer+"' is not of the expected type: "+layerClass);
}
tx.success();
return layer;
}
}

Expand Down Expand Up @@ -297,7 +303,10 @@ public void deleteLayer(String name, Listener monitor) {
if (layer == null)
throw new SpatialDatabaseException("Layer " + name + " does not exist");

layer.delete(monitor);
try (Transaction tx = database.beginTx()) {
layer.delete(monitor);
tx.success();
}
}

public GraphDatabaseService getDatabase() {
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.neo4j.graphdb.index.RelationshipIndex;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource;

public class SpatialIndexProvider extends IndexProvider
{
Expand Down Expand Up @@ -60,7 +61,7 @@ public SpatialIndexImplementation(GraphDatabaseService db) {

@Override
public String getDataSourceName() {
return Config.DEFAULT_DATA_SOURCE_NAME;
return NeoStoreXaDataSource.DEFAULT_DATA_SOURCE_NAME;
}

@Override
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/neo4j/gis/spatial/osm/OSMDataset.java
Expand Up @@ -57,23 +57,21 @@ public class OSMDataset implements SpatialDataset, Iterable<OSMDataset.Way>, Ite
* @param datasetId
*/
public OSMDataset(SpatialDatabaseService spatialDatabase, OSMLayer osmLayer, Node layerNode, long datasetId) {
this.layer = osmLayer;
this.datasetNode = spatialDatabase.getDatabase().getNodeById(datasetId);
Relationship rel = layerNode.getSingleRelationship(SpatialRelationshipTypes.LAYERS, Direction.INCOMING);
if (rel == null) {
Transaction tx = spatialDatabase.getDatabase().beginTx();
try {
datasetNode.createRelationshipTo(layerNode, SpatialRelationshipTypes.LAYERS);
tx.success();
} finally {
tx.finish();
}
} else {
Node node = rel.getStartNode();
if (!node.equals(datasetNode)) {
throw new SpatialDatabaseException("Layer '" + osmLayer + "' already belongs to another dataset: " + node);
try (Transaction tx = spatialDatabase.getDatabase().beginTx()) {
this.layer = osmLayer;
this.datasetNode = spatialDatabase.getDatabase().getNodeById(datasetId);
Relationship rel = layerNode.getSingleRelationship(SpatialRelationshipTypes.LAYERS, Direction.INCOMING);
if (rel == null) {
datasetNode.createRelationshipTo(layerNode, SpatialRelationshipTypes.LAYERS);
} else {
Node node = rel.getStartNode();
if (!node.equals(datasetNode)) {
throw new SpatialDatabaseException("Layer '" + osmLayer + "' already belongs to another dataset: " + node);
}
}
tx.success();
}

}

/**
Expand Down
Expand Up @@ -472,7 +472,7 @@ private class CombinedAttributes {
}
} catch (NullPointerException e) {
if (missingTags++ < 10) {
System.err.println("Geometry has no related tags node: " + geomNode);
System.err.println("Geometry has no related tags node: " + geomNode);
} else if (missingTags % 100 == 0) {
System.err.println("Geometries without tags found " + missingTags + " times");
}
Expand Down Expand Up @@ -506,8 +506,7 @@ private CombinedAttributes getProperties(Node geomNode) {
* models with different encodings.
*
* @param geomNode
* @param attribute
* to test
* @param name to test
* @return
*/
public boolean hasAttribute(Node geomNode, String name) {
Expand All @@ -522,8 +521,7 @@ public boolean hasAttribute(Node geomNode, String name) {
* the method returns null.
*
* @param geomNode
* @param attribute
* to test
* @param name to test
* @return attribute, or null
*/
public Object getAttribute(Node geomNode, String name) {
Expand Down

0 comments on commit 2b76f17

Please sign in to comment.