Neo4J空间数据存储
1.Neo4j Spatial 简介
1.1Neo4j Spatial概念
Neo4j Spatial项目是图数据库Neo4j的一个插件,它通过将空间数据映射到图模型(graph model),它将对象和关系当作顶点和边存储在图模型中。因而使得Neo4j具有空间数据的导入,存储,查询等功能[8]。Neo4j Spatial支持的地理要素遵循OpenGIS的规范[9],包括点(point),线段(line-string),面(polygon),多点(multipoint),多线段(multi-linestring)等简单要素。Neo4j Spatial使用R树作为空间索引,主要是集成了Lucene 的索引库,支持的空间查询包括覆盖(cover),被覆盖(cover by),包含(contian),相交(intersect)等。一般而言,R树会将叶子结点(COUNT,LEVEL,<OIi,MBRi>)上几何要素分组并用它们的数据矩形来表示,相比PostGIS所使用的优化过GisT[13]索引要低效,会使Neo4j在范围查询上效率有所不及PostGIS,但它对适合图模型的数据(如网络数据)操作效率非常高[14]。综上所述,Neo4j在符合图数据模型的分析中如邻近搜索,路径分析等这些特定类型的应用有非常大的优势。
Neo4J图数据库实践系列
图数据库实践系列 (一)--Neo4J简介与安装 http://www.linuxidc.com/Linux/2013-08/88766.htm
图数据库实践系列 (二)--Neo4J空间数据存储 http://www.linuxidc.com/Linux/2013-08/88767.htm
图数据库实践系列 (三)--Neo4j Spatial的REST集成 http://www.linuxidc.com/Linux/2013-08/88768.htm
1.2 Neo4j Spatial特性
Neo4j Spatial的部分核心特性包括
- 支持Esri Shapefile格式,OSM(OpenStreetMap)格式(只支持.osm格式)(补充:Open Street Map目前逐步适用PBF替代OSM)
- 支持所有通用的几何要素
- 在空间查询的时候支持拓扑操作
- 允许任何图数据都实现空间操作功能
- 能够将单一图层或者数据集拆分成多个子图层
2.Neo4j Spatial安装
2.1 EGit安装
EGit是Eclipse团队提供的Git插件,它基于JGit(Git的java实现版本)实现的,允许在Eclipse中实现Git的各种命令[15]。它的安装和其他插件一样,在eclipse中选择Help->Install New Software,加入Update site http://download.eclipse.org/egit/updates 会如图1所示:
图1 安装EGit
安装成功的话,需要重启Eclipse。File ->New -> Other,会看到Git的字样。
2.2 M2eclipse安装
M2eclipse是Maven的Eclipse插件,项目的目标是提供为Eclipse 提供一流的Maven支持,使它能够更方便地编辑POM文件,构建项目等[16]。同样可以通过Update Site 安装:http://download.eclipse.org/technology/m2e/releases。如图2所示:
图2 安装M2Eclipse
安装成功的话,需要重启Eclipse。File ->New -> Other,会看到Maven的字样。
2.2Neo4j Spatial安装
Neo4j Spatial源码可以在Github下载,我们通过以上安装的两个插件,来构建和安装我们的Neo4j Spatial项目。首先在eclipse中File中导入Git项目,
File -> Import -> Git -> Project from Git.如果本地已经存在Git库,可以选择Local.我们远程下载Spatial,选择URI,如图3所示。
图3 选择远程库
输入Neo4j Spatial URI,然后Next,如图-4所示:
图4 输入URI
EGit会自动Fetch回所有的源码,我们可以按照自己需要选择特定版本,然后Next。如图5所示:
图5 选择源码版本
然后可以一直Next,直至选择导入的项目类型,选择Import as general projcet.如图6 所示:
图6 导入项目
再Next然后点finish 完成neo4j spatial 的导出。此时,还需要将该项目转化成Maven Project ,选中项目,右键:Configure ->Convert to Maven Project.最后我们得到了一个git管理下的maven项目。使用如下命令来安装Neo4j Spatial。
mvn clean install
在eclipse操作是 Run as ->5. Maven Build ->输入构建目标。 如图7所示:
图7 构建Neo4j Spatial 项目
M2eclipse会自动构建工程。如果安装失败的话,可以使用如下命令跳过单元测试
mvn clean install -DskipTests
最后eclipse的console会报出build success。
3.Neo4j Spatial 空间数据读取
3.1地理图层与编码
Spatial 库中首先需要定义图层中几何要素,可供查询的索引,同时图层是否可编辑 ,其次是确定几何编码接口。Spatial类库提供的默认图层是标准图层,使用WKBGeometryEncoder,将所有的要素以字节数组的形式来存 储。地理数据中实体的属性会当作图数据模型中顶点的属性来存储。OSMLayer 是支持Open Street Map格式的特殊图层,neo4j使用单个完全图[17]来存储该图层。同时 OMSLayer继承动态图层,允许包含任意个子图层。
3.2导入shapefile
导入shapefile大概过程是1.生存Neo4j数据库(调用已有) 2.实例化,并调用ShapefileImporter 3.关闭数据库 实例代码如下所示:
package edu.sirc.gis.data;import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.Charset;
import org.geotools.data.shapefile.shp.ShapefileException; import org.neo4j.gis.spatial.Layer; import org.neo4j.gis.spatial.ShapefileImporter; import org.neo4j.gis.spatial.SpatialDatabaseService; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.kernel.EmbeddedGraphDatabase;
public class importShapefiletest {
/** * @param args * 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() throws ShapefileException, FileNotFoundException, IOException
{
System.out.println("\n=== Test Import Shapefile ===");</span><span class="typ">GraphDatabaseService</span><span class="pln"> graphdb </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">EmbeddedGraphDatabase</span><span class="pun">(</span><span class="pln">databasepath</span><span class="pun">);</span><span class="pln"> </span><span class="kwd">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="typ">ShapefileImporter</span><span class="pln"> shpImporter </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ShapefileImporter</span><span class="pun">(</span><span class="pln">graphdb</span><span class="pun">);</span><span class="pln"> shpImporter</span><span class="pun">.</span><span class="pln">importFile</span><span class="pun">(</span><span class="str">"/home/vent/environment/neo4j/data/shp/river.shp"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"river"</span><span class="pun">,</span><span class="pln"> </span><span class="typ">Charset</span><span class="pun">.</span><span class="pln">forName</span><span class="pun">(</span><span class="str">"UTF-8"</span><span class="pun">));</span><span class="pln"> </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">finally</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> graphdb</span><span class="pun">.</span><span class="pln">shutdown</span><span class="pun">();</span><span class="pln"> </span><span class="pun">}</span><span class="pln">
}
public static void main(String[] args) throws ShapefileException, FileNotFoundException, IOException {
// TODO Auto-generated method stub
importShapefiletest isf = new importShapefiletest();
isf.importShapfile();
}}
3.3导入OSM
导入OSM的过程和导入shapefile差异不大,旧版Neo4j Spatial 提供了批量读入的接口BatchInserter,可以加快大文件的导入。具体JavaDoc请参阅打包后的文档。
package edu.sirc.gis.data;import java.io.File; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map;
import org.neo4j.gis.spatial.osm.OSMImporter; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.kernel.EmbeddedGraphDatabase; import org.neo4j.kernel.impl.batchinsert.BatchInserter; import org.neo4j.kernel.impl.batchinsert.BatchInserterImpl;
public class importOSMTest {
/** * @param args */ //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<String, String> NORMAL_CONFIG = new HashMap<String, String>(); 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() throws Exception {
</span><span class="typ">OSMImporter</span><span class="pln"> osmIm </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">OSMImporter</span><span class="pun">(</span><span class="str">"FUJIAN"</span><span class="pun">);</span><span class="pln"> </span><span class="typ">Map</span><span class="pun">&</span><span class="pln">lt</span><span class="pun">;</span><span class="typ">String</span><span class="pun">,</span><span class="pln"> </span><span class="typ">String</span><span class="pun">&</span><span class="pln">gt</span><span class="pun">;</span><span class="pln"> config </span><span class="pun">=</span><span class="pln"> NORMAL_CONFIG</span><span class="pun">;</span><span class="pln"> </span><span class="lit">@SuppressWarnings</span><span class="pun">(</span><span class="str">"deprecation"</span><span class="pun">)</span><span class="pln"> </span><span class="com">//BatchInserter baIns = new BatchInserterImpl(osmPath,config);</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> dataset </span><span class="pun">=</span><span class="str">"/home/vent/Downloads/Fujian.osm"</span><span class="pun">;</span><span class="pln"> </span><span class="com">//osmIm.importFile(baIns,dataset,false);</span><span class="pln"> </span><span class="typ">GraphDatabaseService</span><span class="pln"> db </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">EmbeddedGraphDatabase</span><span class="pun">(</span><span class="pln">databasepath</span><span class="pun">);</span><span class="pln"> osmIm</span><span class="pun">.</span><span class="pln">importFile</span><span class="pun">(</span><span class="pln">db</span><span class="pun">,</span><span class="pln"> dataset</span><span class="pun">);</span><span class="pln"> osmIm</span><span class="pun">.</span><span class="pln">reIndex</span><span class="pun">(</span><span class="pln">db</span><span class="pun">,</span><span class="pln"> </span><span class="lit">100</span><span class="pun">);</span><span class="pln"> db</span><span class="pun">.</span><span class="pln">shutdown</span><span class="pun">();</span><span class="pln">
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
importOSMTest iOSM = new importOSMTest();
iOSM.importOSM();
}}
下讲会讲述Neo4j Spatial与GeoServer 的集成,以及与Neo4j Server的集成。
[8]Neo4j Spatial http://nowiki.neo4j.org/content/Neo4j_Spatial
[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
[11]Apache Lucene http://lucene.apache.org/core/
[12]R-tree http://en.wikipedia.org/wiki/R-tree
[13]GiST http://en.wikipedia.org/wiki/GiST
[14]空间数据库索引技术 郭薇 郭菁 上海交通大学出版社 P104-105 2006
[15]EGit http://www.eclipse.org/egit/
[16]Maven Integration (m2e) http://eclipse.org/m2e/