目录

  1. 简介
  2. 准备工作
  • 安装GeoTools
  1. 创建地理点(Point)
  2. 创建地理线(LineString)
  3. 创建地理面(Polygon)
  4. 将几何对象存储到Shapefile
  5. 小结

一、简介

GeoTools是一个开源的Java库,广泛用于处理地理信息系统(GIS)中的数据。本文将详细介绍如何使用GeoTools在Java中构建地理点、线和面的几何对象,并将这些对象存储到Shapefile中。

二、准备工作

安装GeoTools

在开始之前,你需要确保已将GeoTools库添加到项目中。可以通过Maven来进行安装:

Maven依赖:
<dependencies>
    <!-- GeoTools Core dependency -->
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-main</artifactId>
        <version>24.4</version>
    </dependency>
    <!-- GeoTools Shapefile dependency -->
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-shapefile</artifactId>
        <version>24.4</version>
    </dependency>
</dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

三、创建地理点(Point)

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;

public class CreatePoint {
    public static void main(String[] args) {
        // 创建GeometryFactory实例
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

        // 创建地理点
        Coordinate coord = new Coordinate(116.397128, 39.916527); // 北京的经度和纬度
        Point point = geometryFactory.createPoint(coord);

        // 输出
        System.out.println("Created Point: " + point);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

四、创建地理线(LineString)

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;

public class CreateLineString {
    public static void main(String[] args) {
        // 创建GeometryFactory实例
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

        // 创建地理线(线串)
        Coordinate[] coords = new Coordinate[] {
            new Coordinate(116.397128, 39.916527), // 北京
            new Coordinate(121.473701, 31.230416)  // 上海
        };
        LineString lineString = geometryFactory.createLineString(coords);

        // 输出
        System.out.println("Created LineString: " + lineString);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

五、创建地理面(Polygon)

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.Polygon;

public class CreatePolygon {
    public static void main(String[] args) {
        // 创建GeometryFactory实例
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

        // 创建地理面(多边形)
        Coordinate[] coords = new Coordinate[] {
            new Coordinate(116.0, 39.0),
            new Coordinate(117.0, 39.0),
            new Coordinate(117.0, 40.0),
            new Coordinate(116.0, 40.0),
            new Coordinate(116.0, 39.0)
        };
        LinearRing ring = geometryFactory.createLinearRing(coords);
        Polygon polygon = geometryFactory.createPolygon(ring, null);

        // 输出
        System.out.println("Created Polygon: " + polygon);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

六、将几何对象存储到Shapefile

import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geoemtry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SaveToShapefile {
    public static void main(String[] args) throws IOException {
        // 创建GeometryFactory实例
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

        // 创建地理点
        Coordinate coord = new Coordinate(116.397128, 39.916527); // 北京
        Point point = geometryFactory.createPoint(coord);

        // 定义Shapefile的特征类型
        String typeSpec = "the_geom:Point:srid=4326," +
                          "name:String";
        SimpleFeatureType featureType = new SimpleFeatureTypeBuilder()
                                        .setName("Location")
                                        .add("the_geom", Point.class, DefaultGeographicCRS.WGS84)
                                        .add("name", String.class)
                                        .buildFeatureType();

        // 创建特征
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
        featureBuilder.add(point);
        featureBuilder.add("Beijing");
        SimpleFeature feature = featureBuilder.buildFeature(null);

        // 创建Shapefile并写入特征
        File file = new File("locations.shp");
        Map<String, Serializable> params = new HashMap<>();
        params.put("url", file.toURI().toURL());

        DataStoreFactorySpi factory = new ShapefileDataStoreFactory();
        FileDataStore dataStore = (FileDataStore) factory.createNewDataStore(params);
        dataStore.createSchema(featureType);
        Transaction transaction = new DefaultTransaction("create");

        try (FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(transaction)) {
            SimpleFeature toWrite = writer.next();
            toWrite.setAttributes(feature.getAttributes());
            writer.write();
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        } finally {
            transaction.close();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.

七、小结

本文介绍了如何在Java中使用GeoTools库来创建地理点、线和面,并将这些几何对象存储为Shapefile格式文件。通过这些基本步骤,你可以轻松构建和操作地理空间数据,为进一步的GIS分析打下基础。GeoTools作为一个强大的工具包,不仅提供了丰富的几何操作,还支持各种GIS数据格式和强大的坐标变换功能。希望本文对你有所帮助,祝你在GIS开发之路上取得更大的进步。