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

How to create an open polyline in GeoTools 12-RC1?

我需要创建一个 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);

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