【问题标题】:How to write only a jts geometry with gml3如何用 gml3 只写一个 jts 几何
【发布时间】:2014-12-23 10:44:23
【问题描述】:

我有一个几何 (JTS) 几何,我想用 GML3 编码这个几何

我以这种方式完成了 Gml2 的这项工作:

        GMLWriter writer = new GMLWriter();
        writer.setNamespace(true);
        writer.setSrsName(CRS.toSRS(pa.getFeature().getType().getCoordinateReferenceSystem()));
        final String s = writer.write(pa.getGeometry());

如下所示

       <gml:MultiPolygon srsName="EPSG:4326">
        <gml:polygonMember>
          <gml:Polygon>
            <gml:outerBoundaryIs>
              <gml:LinearRing>
                <gml:coordinates>388398.01680386113,4234936.561284632 388496.2268063746,4234925.211013514 388486.8865137336,4234818.841002756 388473.5261352561,4234681.43099238 388471.2816094029,4234659.843136447 388370.4591269435,4234691.257276198 388370.45921945036,4234691.2574215075 388386.1865097745,4234829.971280575 388398.01680386113,4234936.561284632</gml:coordinates>
              </gml:LinearRing>
            </gml:outerBoundaryIs>
            <gml:innerBoundaryIs>
              <gml:LinearRing>
                <gml:coordinates>388480.8967599605,4234910.071050081 388470.27675781824,4234910.611079167 388467.16673689516,4234903.311085147 388477.35673742165,4234902.231057049 388480.8967599605,4234910.071050081</gml:coordinates>
              </gml:LinearRing>
            </gml:innerBoundaryIs>
          </gml:Polygon>
        </gml:polygonMember>
        <gml:polygonMember>
          <gml:Polygon>
            <gml:outerBoundaryIs>
              <gml:LinearRing>
                <gml:coordinates>388471.2816094029,4234659.843136447 388471.2820757456,4234659.842991144 388471.28158315114,4234659.842883958 388471.2816094029,4234659.843136447</gml:coordinates>
              </gml:LinearRing>
            </gml:outerBoundaryIs>
          </gml:Polygon>
        </gml:polygonMember>
      </gml:MultiPolygon>

在 GML3 中有什么方法可以做到这一点吗?

我需要 gml:posList 标签而不是 gml:coordinates

【问题讨论】:

    标签: xml geometry geotools jts gmlib


    【解决方案1】:

    如果您使用的是 GeoTools,那么您可以关注XML Geometry Tutorial。所以像:

    SimpleFeatureType TYPE = DataUtilities.createType("location", "geom:Point,name:String");
    
    File locationFile = new File("location.xsd");
    locationFile = locationFile.getCanonicalFile();
    locationFile.createNewFile();
    
    URL locationURL = locationFile.toURI().toURL();
    URL baseURL = locationFile.getParentFile().toURI().toURL();
    
    FileOutputStream xsd = new FileOutputStream(locationFile);
    
    GML encode = new GML(Version.GML3);
    encode.setBaseURL(baseURL);
    encode.setNamespace("location", locationURL.toExternalForm());
    encode.encode(xsd, TYPE);
    
    xsd.close();
    
    WKTReader2 wkt = new WKTReader2();
    List<SimpleFeature> collection = new LinkedList<SimpleFeature>();
    collection.add(SimpleFeatureBuilder.build(TYPE, new Object[] { wkt.read("POINT (1 2)"),"name1" }, null));
    collection.add(SimpleFeatureBuilder.build(TYPE, new Object[] { wkt.read("POINT (4 4)"),"name2" }, null));
    
    ByteArrayOutputStream xml = new ByteArrayOutputStream();
    
    GML encode2 = new GML(Version.GML3);
    encode2.setLegacy(true);
    encode2.setBaseURL(baseURL);
    encode2.setNamespace("location", "location.xsd");
    encode2.encode(xml,  new ListFeatureCollection(TYPE, collection));
    
    xml.close();
    
    String gml = xml.toString();
    

    【讨论】:

    • 嗨,我尝试这种方式,但我只有一个几何图形和 crs,这是所有的特征集合,我只想获得几何标签
    • 创建一个只包含几何的特征?如果您不考虑其他所有内容,它就不是真正的 GML。
    • 感谢iant,我们发现问题是GML 类中没有GML3 的方法,只有版本2 有效。当你给 version.gml3 时,你所做的任何事情都不重要,因为 encode2(在我的代码中)没有 gml3 的选项,我们开源并检查了它
    • 您需要使用 GeoTools GML 类而不是 JTS 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多