java绘制地形、等值线

记录一下项目使用的各种东西。

为了便于版本管理,这里使用maven进行安装,因此不需要从官网上下载jar包。

Geotools

主要参考Geotools官网教程

Maven配置(IDEA)

  1. 先创建一个maven的quickstart样本
  2. 然后需要为pom.xml添加geotools包
  • properties中添加(版本自定)
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>26-SNAPSHOT</geotools.version>
</properties>
  • dependencies中添加
    <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</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>
  • 添加外部仓库repositories
    <repositories>
  <repository>
    <id>osgeo</id>
    <name>OSGeo Release Repository</name>
    <url>https://repo.osgeo.org/repository/release/</url>
    <snapshots><enabled>false</enabled></snapshots>
    <releases><enabled>true</enabled></releases>
  </repository>
  <repository>
    <id>osgeo-snapshot</id>
    <name>OSGeo Snapshot Repository</name>
    <url>https://repo.osgeo.org/repository/snapshot/</url>
    <snapshots><enabled>true</enabled></snapshots>
    <releases><enabled>false</enabled></releases>
  </repository>
</repositories>

创建shapefile

geotools可视化需要shapefile(.shp)

pom.xml 需要这些东西

<dependencies>
 <dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-shapefile</artifactId>
  <version>${geotools.version}</version>
 </dependency>
 <dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-epsg-hsql</artifactId>
  <version>${geotools.version}</version>
 </dependency>
</dependencies>
 <repositories>
   <repository>
     <id>osgeo</id>
     <name>OSGeo Release Repository</name>
     <url>https://repo.osgeo.org/repository/release/</url>
     <snapshots><enabled>false</enabled></snapshots>
     <releases><enabled>true</enabled></releases>
   </repository>
   <repository>
     <id>osgeo-snapshot</id>
     <name>OSGeo Snapshot Repository</name>
     <url>https://repo.osgeo.org/repository/snapshot/</url>
     <snapshots><enabled>true</enabled></snapshots>
     <releases><enabled>false</enabled></releases>
   </repository>
 </repositories>

...... 由于教程有很多,就不再搬运到这里了。 geotools适用于绘制复杂的地形图,是一个较为成熟复杂的平台。

wContour

基本用法:

  1. 准备好数据:需要提供地图的行列数以及数据,数据是由三部分组成:
  • _X代表所有数据的X坐标
  • _Y代表所有数据的Y坐标
  • _gridData 代表所有每个点上的值 对于grid data(每个点都是网格上的坐标)直接赋值即可,对于discrete data需要进行插值:
// 首先我们需要读入
// 第一维是X坐标,第二维是Y坐标,第三维是值
double _discreteData[3][dataNum] = ...;

double[] _X = new double[cols];
double[] _Y = new double[rows];

// 根据行列数生成每个格点的坐标,前四个参数代表了横纵坐标的起始和终止
Interpolate.CreateGridXY_Num(Xlb, Ylb, Xrt, Yrt, _X, _Y);

// 接下来进行插值,第四个参数指周围节点数,第五个指半径,第六个是对没有值的格点进行赋值(一般-9999)
// 还有其他的插值方法。
_gridData = Interpolate.Interpolation_IDW_Radius(_discreteData, _X, _Y, 4, 100, _undefData);
  1. 之后需要设置刻度,即等高线的每条线的值
double _CValues = new double[]{20, 30, 40, 50, 60};
  1. 然后开始生成等高线
int nc = _CValues.length;
int[][] S1 = new int[_gridData.length][_gridData[0].length];
// 生成当前数据的边界线
_borders = Contour.tracingBorders(_gridData, _X, _Y, S1, _undefData);
// 生成当前数据的等值线
_contourLines = Contour.tracingContourLines(_gridData, _X, _Y, nc, _CValues, _undefData, _borders, S1);

_contourLines = Contour.smoothLines(_contourLines);
  1. 现在获得了等值线,还可以继续生成登高面:
_contourPolygons = Contour.tracingPolygons(_gridData, _contourLines, _borders, _CValues);
  1. 如果想要截取生成某一范围的等值线,首先需要生成 List<List> 形式的截线,然后
  • 截取等高线:
  _clipContourLines = new ArrayList<PolyLine>();
for (List< PointD> cLine : _clipLines) {
    _clipContourLines.addAll(Contour.clipPolylines(_contourLines, cLine));
}
  • 截取等高面 ···java

    _clipContourPolygons = new ArrayList<wContour.Global.Polygon>();
    
    for (List<PointD> cLine : _clipLines) {
        _clipContourPolygons.addAll(Contour.clipPolygons(_contourPolygons, cLine));
    }

``` 6. 之后就可以调用swing.canvas等等来绘制了,_ContourLines包含了所需要的点的坐标


待补充……