【问题标题】:GeoTools Maven Issue - Import ConfusionGeoTools Maven 问题 - 导入混淆
【发布时间】:2019-11-06 13:49:14
【问题描述】:

我正在关注here 发布的解决方案,该解决方案使用 Java 将平方度转换为平方米。我在使用 Maven 导入时遇到问题。

具体来说,遵循 GeoTools Maven 快速入门here,我仍然收到org.geotools.measure.Measure.valueOf()org.opengis.geometry.coordinate.Polygon.getCentroid()Cannot resolve method 错误。奇怪的是,对于前者,我找到了some source code,它说它使用javax.measure.Measure,但我对that import 也没有运气。

IDE 视图:

这是我的文件:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myapp.me</groupId>
    <artifactId>my-app</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>my app code</name>

    <parent>
        <groupId>com.myapp.me</groupId>
        <artifactId>my-app-root</artifactId>
        <version>1.0.0</version>
    </parent>

    <prerequisites>
        <maven>3</maven>
    </prerequisites>

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

    <organization>
        <name>my-org</name>
        <url>myurl</url>
    </organization>

    <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>

    <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>
</project>

Java

import si.uom.SI;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.geometry.Geometry;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.geometry.coordinate.Polygon;
import org.opengis.geometry.primitive.Point;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.geotools.measure.Measure; 

public class Mapper {

    private Measure<Double, Area> calcArea(SimpleFeature feature) {
        Polygon p = (Polygon) feature.getDefaultGeometry();
        Point centroid = p.getCentroid();
        try {
            String code = "AUTO:42001," + centroid.getX() + "," + centroid.getY();
            CoordinateReferenceSystem auto = CRS.decode(code);

            MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, auto);

            Polygon projed = (Polygon) JTS.transform(p, transform);
            return Measure.valueOf(projed.getArea(), SI.SQUARE_METRE);
        } catch (MismatchedDimensionException | TransformException | FactoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return Measure.valueOf(0.0, SI.SQUARE_METRE);
    }
}

我做错了什么?

编辑:

响应 RealSkeptic 的建议尝试最新的 Javax 导入。

注意右侧的导入,但左侧没有使用它的选项。就好像找不到一样。但是,它可以找到import javax.measure.quantity.Area

【问题讨论】:

  • 您提到的答案说它依赖于javax。您为javax.measure 链接的导入说有一个更新的版本。你检查过吗?
  • 请参阅我的答案底部以获取更新。由于某种原因,它找不到javax Measure 依赖项。
  • @RealSkeptic 我可能已经用Quantity 代替了它。我在文档中错过了这一点。会反馈的,谢谢!
  • 是的,任何感兴趣的人都可以使用 Quantity 类。
  • 查看 geotools 升级常见问题以了解 21.x 版本的类更改

标签: java maven geotools opengis


【解决方案1】:

您正在查看适用于旧版 GeoTools 的代码,您可以随时查看 GeoTools Upgrade notes 以了解使用新版本库需要进行哪些更改。

你需要这个依赖:

<dependency>
   <groupId>systems.uom</groupId>
   <artifactId>systems-common-java8</artifactId>
   <version>0.7.2</version>
</dependency>

包名已更改,导致升级时出现一些常见的搜索和替换:

搜索 javax.measure.unit.Unit 替换 javax.measure.Unit

搜索 ConversionException 替换 IncommensurableException

这是一个已检查异常,在 GeoTools 库中发现此异常的区域中,我们现在返回一个 IllegalArgument 异常。

搜索转换器 == UnitConverter.IDENTITY 替换 converter.isIdentity()

搜索 javax.measure.unit.NonSI 替换 import si.uom.NonSI

搜索 javax.measure.unit.SI 替换 import si.uom.SI

搜索 SI.METER 替换 SI.METRE

搜索 javax.measure.converter.UnitConverter 替换 javax.measure.UnitConverter

搜索 javax.measure.unit.UnitFormat 替换 import javax.measure.format.UnitFormat

搜索 Unit.ONE 替换 AbstractUnit.ONE

搜索 Dimensionless.UNIT 替换 AbstractUnit.ONE

搜索 Unit.valueOf(unitString) 替换 Units.parseUnit(unitString)

并使用量化:

使用单位

如果您之前在代码中使用了单位,则可以帮助您 单位转换或只是保持单位直。你可能有 代码如下:

Measure<Double, Length> dist = Measure.valueOf(distance, SI.METER);
System.out.println(dist.doubleValue(SI.KILOMETER) + " Km");
System.out.println(dist.doubleValue(NonSI.MILE) + " miles");

你会发现它不再编译。它应该被转换为使用 数量类。

import javax.measure.Quantity;
import javax.measure.quantity.Length;
import si.uom.SI;
import systems.uom.common.USCustomary;

import tec.uom.se.quantity.Quantities;
import tec.uom.se.unit.MetricPrefix;

Quantity<Length> dist = Quantities.getQuantity(distance, SI.METRE);
System.out.println(dist.to(MetricPrefix.KILO(SI.METRE)).getValue() + " Km");
System.out.println(dist.to(USCustomary.MILE) + " miles");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2018-05-08
    • 2021-03-22
    相关资源
    最近更新 更多