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

时间:2015-07-04 12:58:47

标签: java geojson geotools

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

1 个答案:

答案 0 :(得分:1)

您需要对createLineString执行某些操作,例如:

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

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