手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

图数据库实践系列 (二)--Neo4J空间数据存储

[日期:2013-08-14] 来源:Linux社区  作者:ventlam [字体:大 中 小]

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 ===");

         GraphDatabaseService graphdb = new EmbeddedGraphDatabase(databasepath);
         try
         {
             ShapefileImporter shpImporter = new ShapefileImporter(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) 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
    {

        OSMImporter osmIm = new OSMImporter("FUJIAN");
        Map<String, String> 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 = new EmbeddedGraphDatabase(databasepath);
        osmIm.importFile(db, dataset);
        osmIm.reIndex(db, 100);
        db.shutdown();
    }
    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/

Neo4j 的详细介绍:请点这里
Neo4j 的下载地址:请点这里

linux
相关资讯       Neo4j 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款