gpt4 book ai didi

java - 如何在 GeoTools 12-RC1 中创建开放折线?

转载 作者:行者123 更新时间:2023-12-01 11:21:07 24 4
gpt4 key购买 nike

我需要创建一个 GeoJSON 流,其中包含有关道路的数据。道路就像多边形,只不过它们是开放的(起点和终点不同)。

我尝试使用以下代码来实现该目标:

protected void addPolyLine(final double[] coords,
final GeometryBuilder builder,
final SimpleFeatureBuilder fbuilder,
final List<SimpleFeature> features,
final String id) {
final double[] modcoords = new double[coords.length+2];
for (int i=0; i < coords.length; i++) {
modcoords[i] = coords[i];
}
modcoords[modcoords.length-2] = coords[0];
modcoords[modcoords.length-1] = coords[1];
final double[] holeStart = new double[] {coords[0], coords[1],
coords[coords.length-2], coords[coords.length - 1]};
final LinearRing shell = builder.linearRing(modcoords);
final LinearRing hole = builder.linearRing(holeStart);

final Polygon polygon = builder.polygon(shell, hole);

fbuilder.add(polygon);
final SimpleFeature feature = fbuilder.buildFeature(id);
features.add(feature);
}

它不起作用 - 我收到错误

java.lang.IllegalArgumentException: Invalid number of points in LinearRing (found 3 - must be 0 or >= 4)
at com.vividsolutions.jts.geom.LinearRing.validateConstruction(LinearRing.java:114)
at com.vividsolutions.jts.geom.LinearRing.<init>(LinearRing.java:106)
at com.vividsolutions.jts.geom.GeometryFactory.createLinearRing(GeometryFactory.java:341)
at org.geotools.geometry.jts.GeometryBuilder.linearRing(GeometryBuilder.java:199)

当我使用 builder.lineString(coords) 时,生成的 GeoJSON 不包含应有的坐标。

如何使用 GeoTools 12-RC1 创建折线(一条连接多个点且不闭合的线)?

更新 1 (05.07.2015 21:22 MSK):这就是我定义点、多边形和线的要素类型的方式。点和多边形工作正常,但线不行。

private final static SimpleFeatureType POINT =
createType("Test", "Location:Point");
private final static SimpleFeatureType POLYGON =
createType("Test2", "Location:Polygon");
private final BuildingsReader buildingsReader =
new DefaultBuildingsReader();
private final static SimpleFeatureType LINE =
createType("Test3", "Line");
private static SimpleFeatureType createType(final String x1, final String x2) {
try
{
return DataUtilities.createType(x1, x2);
}
catch (final SchemaException exception) {
exception.printStackTrace();
}
return null;
}

最佳答案

您需要使用 createLineString 做一些事情像:

 line=builder.createLineString(Arrays.asList(coords));
fbuilder.add(line);
final SimpleFeature feature = fbuilder.buildFeature(id);
features.add(feature);

您将需要修改要素类型以期望线而不是多边形。

关于java - 如何在 GeoTools 12-RC1 中创建开放折线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com