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
    评论
Maven是一个项目管理和构建工具,它可以帮助开发人员自动化构建、测试和部署Java项目。GeoTools是一个开源的Java库,用于处理地理空间数据。它提供了一组工具和API,用于读取、写入、分析和可视化地理空间数据。 要在Maven项目中使用GeoTools,你需要在项目的pom.xml文件中添加GeoTools的依赖项。你可以通过以下步骤来实现: 1. 打开你的项目的pom.xml文件。 2. 在<dependencies>标签中添加以下代码: ```xml <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>23.5</version> </dependency> ``` 这将添加GeoTools的主要模块作为项目的依赖项。你可以根据你的需求添加其他GeoTools模块的依赖项。 3. 保存并关闭pom.xml文件。 接下来,Maven将自动下载并安装GeoTools及其依赖项。一旦安装完成,你就可以在你的项目中使用GeoTools的类和方法了。 在你的代码中,你可以使用GeoTools来定义坐标系并建立坐标系转换关系。以下是一个示例代码: ```java import org.geotools.referencing.CRS; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; public class GeoToolsExample { public static void main(String[] args) throws Exception { CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326"); CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3785"); MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS); // 使用转换关系进行坐标转换 double[] sourceCoords = {longitude, latitude}; double[] targetCoords = new double[2]; transform.transform(sourceCoords, 0, targetCoords, 0, 1); // 输出转换后的坐标 System.out.println("转换后的坐标:"); System.out.println("经度:" + targetCoords[0]); System.out.println("纬度:" + targetCoords[1]); } } ``` 请注意,上述代码中的`longitude`和`latitude`是待转换的经度和纬度值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值