首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Geotools添加styleUrl

Geotools添加styleUrl
EN

Stack Overflow用户
提问于 2022-08-03 08:23:11
回答 1查看 27关注 0票数 1

嗨,我正在尝试构建一个DefaultFeatureCollection并添加一个styleUrl。

最后,我想将所有这些编码成一个kml。

在方法createStyle的末尾,我只打印出kml,以查看它的外观。我的代码如下所示:

代码语言:javascript
运行
复制
public void createKML(List<VisualisierungsObjekt> visualList) throws ParseException, IOException,
            SAXException, TransformerException, KMLWriteException, URISyntaxException, SchemaException {

        final GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
        final WKTReader wkt = new WKTReader(gf);
        final List<SimpleFeature> features = new ArrayList<>();

        for (VisualisierungsObjekt objekt : visualList) {

            KmlStyling styling = STYLES.get(objekt.getColor_c() + objekt.getColor_f());

            if (styling == null) {
                String uuid = UUID.randomUUID().toString();
                final String url = String.format("%s/rest/icon/%s/%s/25x25/circle.png", iconUrl, objekt.getColor_c(),
                        objekt.getColor_f());
                styling = new KmlStyling(objekt.getColor_c(), url, uuid);
                STYLES.put(objekt.getColor_c() + objekt.getColor_f(), styling);
            }

            String transformed = transformWKT("SRID=" + sridDB + ";" + objekt.getWkt());

            final Geometry point = wkt.read(transformed.split(";")[1]);

            SimpleFeatureType featureType = DataUtilities.createType("http://www.opengis.net/kml/2.2", "KML_TYPE",
                    "styleUrl:String,description:String,Point:Point");

            final SimpleFeatureTypeBuilder type = new SimpleFeatureTypeBuilder();
            type.setName("KML_TYPE");
            type.setNamespaceURI("http://www.geotools.org/");

            final SimpleFeature feat = builder.buildFeature(String.valueOf(objekt.getId()));
            features.add(feat);

        }

        createStyle(features);

}

private void createStyle(List<SimpleFeature> features) throws TransformerException, IOException, SAXException {
        DefaultFeatureCollection collection = new DefaultFeatureCollection();
        collection.addAll(features);

        KMLConfiguration configuration = new KMLConfiguration();

        Encoder encoder = new Encoder(configuration);
        List<String> namespaces = 
        Document document = encoder.encodeAsDOM(collection, KML.kml);
        document.normalize();

        Node docNode = document.getFirstChild().getChildNodes().item(0);

        for (String key : STYLES.keySet()) {
            KmlStyling styling = STYLES.get(key);
            Element style = document.createElement("Style");
            style.setAttribute("id", styling.uuid());

            Element iconStyle = document.createElement("IconStyle");

            Element colorAttribut = document.createElement("color");
            colorAttribut.appendChild(document.createTextNode(styling.color()));

            Element scaleAttribut = document.createElement("scale");
            scaleAttribut.appendChild(document.createTextNode("1.0"));

            Element icon = document.createElement("Icon");

            Element hrefAttribut = document.createElement("href");
            hrefAttribut.appendChild(document.createTextNode(styling.url()));
            icon.appendChild(hrefAttribut);

            iconStyle.appendChild(colorAttribut);
            iconStyle.appendChild(scaleAttribut);
            iconStyle.appendChild(icon);

            style.appendChild(iconStyle);

            docNode.insertBefore(style, docNode.getFirstChild());

        }

        DOMSource domSource = new DOMSource(document);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transfomer = tf.newTransformer();
        transfomer.transform(domSource, result);

        System.out.println( writer.toString());

    }

目前,输出如下:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<kml:kml xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <kml:Document id="featureCollection">
    <Style id="92dc97aa-c167-4939-a430-d18c44e0ba7b">
      <IconStyle>
        <color>00ff0000</color>
        <scale>1.0</scale>
        <Icon>
          <href>http://link/to/icon/circle.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <kml:Placemark id="22">
      <kml:description>Hello new try</kml:description>
      <kml:ExtendedData>
        <kml:Data name="styleUrl">
          <kml:value>#92dc97aa-c167-4939-a430-d18c44e0ba7b</kml:value>
        </kml:Data>
        <kml:Data name="description">
          <kml:value>Hello new try</kml:value>
        </kml:Data>
      </kml:ExtendedData>
      <kml:Point>
        <kml:coordinates>11.512634059067835,48.532223718159365</kml:coordinates>
      </kml:Point>
    </kml:Placemark>
    <kml:Placemark id="23">
      <kml:description>Hello new11 try</kml:description>
      <kml:ExtendedData>
        <kml:Data name="styleUrl">
          <kml:value>#92dc97aa-c167-4939-a430-d18c44e0ba7b</kml:value>
        </kml:Data>
        <kml:Data name="description">
          <kml:value>Hello new11 try</kml:value>
        </kml:Data>
      </kml:ExtendedData>
      <kml:Point>
        <kml:coordinates>11.5127332365268,48.53231152627981</kml:coordinates>
      </kml:Point>
    </kml:Placemark>
  </kml:Document>
</kml:kml>

styleUrl在ExtendedData中,但我想要的是这样的东西

<kml:styleUrl>#92dc97aa-c167-4939-a430-d18c44e0ba7b</kml:styleUrl>

EN

回答 1

Stack Overflow用户

发布于 2022-08-03 09:41:00

KML模块的快速grep没有显示对StyleUrl的任何引用,因此它看起来还没有编写处理它的代码。我们欢迎为项目添加功能的拉请求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73218501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档