Geotools学习一(Eclipse+Geotools+maven协同搭建Quickstart)

本文完成于2019-5-28(新手)。

使用的软件和插件版本分别是Eclipse Oxygen,maven3.6.1,java jdk 1.8.0_152。

QuickStart搭建有Eclipse直接创建Java项目,另外一种是创建Maven项目,本文是创建maven项目,根据官方文档摸索了几天终于成功显示出图像,还望在评论区指正。

1.首先配置好maven,环境变量的添加等网上都有,就不再赘述,配置成功后win+R,输入cmd,在控制台输入mvn - v,出现下图即为配置成功。

2.打开Eclipse,菜单栏中找到Windows→preference找到下列选项,点击Add添加maven路径,如下图所示:

下图中第二行是maven中xml的位置,第三行是自己设置的maven本地仓库,用于下载jar,默认是在C盘需要在settings.xml文件中进行修改,然后这里就会自动更改。

3.打开Eclipse,菜单栏中选择File→New→Other,选择向导Maven→Maven Project并按next,如下图所示,最后点击finish。

4.这时候已经可以看到新建的项目了,打开src/main/test并运行App.test作为测试,如下图所示:

5.点击pom.xml,在控制台上面有一行很小的选项卡,点击pom.xml,为了使用geotools,我们将在pom.xml中添加三个内容:

(1)设置geotools的版本信息:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>22-SNAPSHOT</geotools.version>
    </properties>

(2)添加对Geotools中gt-main和gt-swing的依赖

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>${geotools.version}</version>
        </dependency>
    </dependencies>

(3)我们需要列出外部存储库,maven可以从中下载GeoTools和其他必须的jar。

 <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>boundless</id>
          <name>Boundless Maven Repository</name>
          <url>http://repo.boundlessgeo.com/main</url>
        </repository>
    </repositories>

这里是完整的pom.xml文件

6.现在maven已经设置好了,可以写一个QuickStart类,如下图所示:

在这个类中可以添加如下代码:

package org.geotools.tutorial.quickstart;

import java.io.File;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

/**
 * Prompts the user for a shapefile and displays the contents on the screen in a map frame.
 *
 * <p>This is the GeoTools Quickstart application used in documentationa and tutorials. *
 */
public class Quickstart {

    /**
     * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
     * contents on the screen in a map frame
     */
    public static void main(String[] args) throws Exception {
        // display a data store file chooser dialog for shapefiles
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        SimpleFeatureSource featureSource = store.getFeatureSource();

        // Create a map content and add our shapefile to it
        MapContent map = new MapContent();
        map.setTitle("Quickstart");

        Style style = SLD.createSimpleStyle(featureSource.getSchema());
        Layer layer = new FeatureLayer(featureSource, style);
        map.addLayer(layer);

        // Now display the map
        JMapFrame.showMap(map);
    }
}

7.最后点击调试就可以了,成功如下图所示:

图中shp文件下载

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值