3.Neo4j Spatial 空间数据读取
3.1地理图层与编码
Spatial 库中首先需要定义图层中几何要素,可供查询的索引,同时图层是否可编辑 ,其次是确定几何编码接口。Spatial类库提供的默认图层是标准图层,使用WKBGeometryEncoder,将所有的要素以字节数组的形式来存储。地理数据中实体的属性会当作图数据模型中顶点的属性来存储。OSMLayer 是支持Open Street Map格式的特殊图层,neo4j使用单个完全图[17]来存储该图层。同时 OMSLayer继承动态图层,允许包含任意个子图层。
3.2导入shapefile
导入shapefile大概过程是1.生存Neo4j数据库(调用已有) 2.实例化,并调用ShapefileImporter 3.关闭数据库 实例代码如下所示:
packageedu.sirc.gis.data;importJava.io.FileNotFoundException;importjava.io.IOException;importjava.nio.charset.Charset;importorg.geotools.data.shapefile.shp.ShapefileException;importorg.neo4j.gis.spatial.Layer;importorg.neo4j.gis.spatial.ShapefileImporter;importorg.neo4j.gis.spatial.SpatialDatabaseService;importorg.neo4j.graphdb.GraphDatabaseService;importorg.neo4j.kernel.EmbeddedGraphDatabase;public classimportShapefiletest {/***@paramargs
* import shapefile to neo4j database
* just Extract from the org.neo4j.gis.spatial.testfordoc*/
private String databasepath="/home/vent/environment/neo4j/gis";public void importShapfile() throwsShapefileException, FileNotFoundException, IOException
{
System.out.println("\n=== Test Import Shapefile ===");
GraphDatabaseService graphdb= newEmbeddedGraphDatabase(databasepath);try{
ShapefileImporter shpImporter= newShapefileImporter(graphdb);
shpImporter.importFile("/home/vent/environment/neo4j/data/shp/river.shp", "river", Charset.forName("UTF-8"));
}finally{
graphdb.shutdown();
}
}public static void main(String[] args) throwsShapefileException, FileNotFoundException, IOException {//TODO Auto-generated method stub
importShapefiletest isf = newimportShapefiletest();
isf.importShapfile();
}
}
3.3导入OSM
导入OSM的过程和导入shapefile差异不大,旧版Neo4j Spatial 提供了批量读入的接口BatchInserter,可以加快大文件的导入。具体JavaDoc请参阅打包后的文档。
packageedu.sirc.gis.data;importjava.io.File;importjava.nio.charset.Charset;importjava.util.HashMap;importjava.util.Map;importorg.neo4j.gis.spatial.osm.OSMImporter;importorg.neo4j.graphdb.GraphDatabaseService;importorg.neo4j.kernel.EmbeddedGraphDatabase;importorg.neo4j.kernel.impl.batchinsert.BatchInserter;importorg.neo4j.kernel.impl.batchinsert.BatchInserterImpl;public classimportOSMTest {/***@paramargs*/
//import osm file into the default db for server .
private String databasepath="/home/vent/environment/neo4j/data/graph.db";private String osmPath="/home/vent/Downloads/";private static final Map NORMAL_CONFIG = new HashMap();static{
NORMAL_CONFIG.put("neostore.nodestore.db.mapped_memory", "50M");
NORMAL_CONFIG.put("neostore.relationshipstore.db.mapped_memory", "120M");
NORMAL_CONFIG.put("neostore.propertystore.db.mapped_memory", "150M");
NORMAL_CONFIG.put("neostore.propertystore.db.strings.mapped_memory", "200M");
NORMAL_CONFIG.put("neostore.propertystore.db.arrays.mapped_memory", "0M");
NORMAL_CONFIG.put("dump_configuration", "false");
}private void importOSM() throwsException
{
OSMImporter osmIm= new OSMImporter("FUJIAN");
Map config =NORMAL_CONFIG;
@SuppressWarnings("deprecation")//BatchInserter baIns = new BatchInserterImpl(osmPath,config);
String dataset ="/home/vent/Downloads/Fujian.osm";//osmIm.importFile(baIns,dataset,false);
GraphDatabaseService db= newEmbeddedGraphDatabase(databasepath);
osmIm.importFile(db, dataset);
osmIm.reIndex(db,100);
db.shutdown();
}public static void main(String[] args) throwsException {//TODO Auto-generated method stub
importOSMTest iOSM = newimportOSMTest();
iOSM.importOSM();
}
}
下讲会讲述Neo4j Spatial与GeoServer 的集成,以及与Neo4j Server的集成。
[9] OpenGIS® Simple Features Specification For SQL
[10]Vicknair, C. et al. 2010. A Comparison of a Graph Database and a Relational Database. In Proceedings of the 48th annual Southeast regional conference
[14]空间数据库索引技术 郭薇 郭菁 上海交通大学出版社 P104-105 2006
[16]Maven Integration (m2e) http://eclipse.org/m2e/
Neo4j 的详细介绍:请点这里
Neo4j 的下载地址:请点这里