Neo4j Spatial数据导入

首先,安装neo4j数据库。我的开发环境是Ubuntu,安装过程参考官网:Neo4j Debian Packages,安装后配置:Post-installation tasks。

然后安装Neo4j Spatial的插件,参加Github介绍:neo4j-contrib/spatial。即把下载的文件解压到安装目录的plugins目录下$NEO4J_HOME/plugins。

接下来,我们使用Java代码在客户端插入数据到neo4j数据库。
我使用Maven构建工程(需要使用Java8进行maven编译)。pom文件如下:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.tzygroupId>
    <artifactId>neo4jartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>neo4jname>
    <url>http://maven.apache.orgurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <repositories>
        <repository>
            <id>repo2.maven.orgid>
            <name>maven2 repositoryname>
            <url>http://repo2.maven.org/maven2/url>
        repository>
        <repository>
            <id>osgeoid>
            <name>Open Source Geospatial Foundation Repositoryname>
            <url>http://download.osgeo.org/webdav/geotools/url>
        repository>
        <repository>
            <id>neo4j-contrib-releasesid>
            <url>https://raw.github.com/neo4j-contrib/m2/master/releasesurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
        <repository>
            <id>neo4j-contrib-snapshotsid>
            <url>https://raw.github.com/neo4j-contrib/m2/master/snapshotsurl>
            <releases>
                <enabled>falseenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
    repositories>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.neo4jgroupId>
            <artifactId>neo4jartifactId>
            <version>3.0.4version>
        dependency>
        <dependency>
            <groupId>org.neo4jgroupId>
            <artifactId>neo4j-spatialartifactId>
            <version>0.23-neo4j-3.0.4version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.5.1version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>
        plugins>
    build>
project>

导入shapefile文件的代码入下:

package cn.tzy.neo4j;

import java.io.File;
import java.io.IOException;

import org.neo4j.gis.spatial.ShapefileImporter;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class Neo4jEx {

    public static void main(String[] args) throws IOException {
        File storeDir = new File("C:/Users/theone/Desktop/spatial.db");
        GraphDatabaseService database = new GraphDatabaseFactory().newEmbeddedDatabase(storeDir);

        try (Transaction tx = database.beginTx()) {
            ShapefileImporter importer = new ShapefileImporter(database);
            importer.importFile("F:/2016/Data/World/continent.shp", "continent");

            tx.success();
        } finally {
            database.shutdown();
        }
    }
}

你可能感兴趣的:(#,数据库,GIS)