我正在跟踪here发布的解决方案,该解决方案使用Java将平方度转换为平方米。我的Maven导入有问题。
尤其是在遵循GeoTools Maven快速入门here之后,对于Cannot resolve method
和org.geotools.measure.Measure.valueOf()
来说,我仍然都遇到org.opengis.geometry.coordinate.Polygon.getCentroid()
错误。奇怪的是,对于前者,我找到了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
答案 0 :(得分:0)
您正在查看的是适用于旧版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
将Search ConversionException替换为IncommensurableException
这是一个已检查的异常,在找到它的GeoTools库区域中,我们现在返回一个IllegalArgument异常。
搜索转换器== UnitConverter.IDENTITY替换converter.isIdentity()
搜索javax.measure.unit.NonSI替换导入si.uom.NonSI
搜索javax.measure.unit.SI替换导入si.uom.SI
搜索SI.METER替换SI.METRE
搜索javax.measure.converter.UnitConverter替换javax.measure.UnitConverter
搜索javax.measure.unit.UnitFormat替换导入javax.measure.format.UnitFormat
搜索Unit.ONE替换AbstractUnit.ONE
搜索Dimensionless.UNIT替换AbstractUnit.ONE
搜索Unit.valueOf(unitString)替换Units.parseUnit(unitString)
要使用Quanaties:
使用单位
如果您之前在代码中使用了单位,则可以 单位转换或只是保持单位笔直。你可能有 像这样的代码:
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");