@Override
    public Feature decorate(Feature feature, KmlEncodingContext context) {
      Placemark pm = (Placemark) feature;

      // try with the template
      SimpleFeature sf = context.getCurrentFeature();
      String title = null;
      try {
        title = context.getTemplate().title(sf);
      } catch (IOException e) {
        String msg = "Error occured processing 'title' template.";
        LOGGER.log(Level.WARNING, msg, e);
      }

      // if we got nothing, set the title to the ID, but also try the text symbolizers
      if (title == null || "".equals(title)) {
        title = sf.getID();
        StringBuffer label = new StringBuffer();

        for (Symbolizer sym : context.getCurrentSymbolizers()) {
          if (sym instanceof TextSymbolizer) {
            Expression e = SLD.textLabel((TextSymbolizer) sym);
            String value = e.evaluate(feature, String.class);

            if ((value != null) && !"".equals(value.trim())) {
              label.append(value);
            }
          }
        }

        if (label.length() > 0) {
          title = label.toString();
        }
      }

      pm.setName(title);
      return pm;
    }
예제 #2
0
  public void writeFeatures(List<Feature> features) throws KettleException {

    Kml kml = new Kml();
    Document document = kml.createAndSetDocument();

    if (this.documentName != null) {
      document.setName(documentName);
    }

    if (this.documentDescription != null) {
      document.setDescription(documentDescription);
    }

    // Si export des attributs
    if (exportWithAttributs) {

      Schema schema = document.createAndAddSchema();
      schema.setId("dataSchema");
      schema.setName("");

      Iterator<Field> fieldIt = this.fields.iterator();
      while (fieldIt.hasNext()) {

        Field field = fieldIt.next();

        // Pas pris en compte ici : une seule géométrie
        if (!field.getType().equals(FieldType.GEOMETRY)) {

          SimpleField simpleField = schema.createAndAddSimpleField();
          simpleField.setName(field.getName());

          // Texte
          if (field.getType().equals(FieldType.STRING)) {

            simpleField.setType("string");

            // Date
          } else if (field.getType().equals(FieldType.DATE)) {

            simpleField.setType("date");

            // Entier
          } else if (field.getType().equals(FieldType.LONG)) {

            simpleField.setType("int");

            // Double
          } else if (field.getType().equals(FieldType.DOUBLE)) {

            simpleField.setType("float");

            // Booléen
          } else if (field.getType().equals(FieldType.BOOLEAN)) {

            simpleField.setType("bool");

            // Autres types
          } else {
            simpleField.setType("string");
          }
        }
      }
    }

    // Récupération des champs utilisés
    Field geometryField = null;
    Field nameField = null;
    Field descriptionField = null;

    Iterator<Feature> featureIt = features.iterator();
    boolean first = true;
    while (featureIt.hasNext()) {

      Feature feature = featureIt.next();
      if (first) {

        geometryField = feature.getField(this.geometryFieldName);

        if (featureNameField != null) {
          nameField = feature.getField(this.featureNameField);
        }

        if (featureDescriptionField != null) {
          descriptionField = feature.getField(this.featureDescriptionField);
        }

        first = false;
      }

      Geometry geometry = (Geometry) feature.getValue(geometryField);
      Envelope envelope = geometry.getEnvelopeInternal();

      // Vérification de l'emprise : doit être en WGS 84
      if (envelope.getMaxX() > 180
          || envelope.getMinX() < -180
          || envelope.getMaxY() > 90
          || envelope.getMinY() < -90) {

        throw new KettleException("Bad coordinates for WGS84 system");
      }

      Placemark placemark = document.createAndAddPlacemark();

      // Nom de feature
      if (featureNameField != null) {
        String name = (String) feature.getValue(nameField);
        if (name != null) {
          placemark.setName(name);
        }
      }

      // Description de feature
      if (featureDescriptionField != null) {
        String description = (String) feature.getValue(descriptionField);
        if (description != null) {
          placemark.setDescription(description);
        }
      }

      // Attributs
      if (exportWithAttributs) {
        ExtendedData extendedData = placemark.createAndSetExtendedData();
        SchemaData schemaData = extendedData.createAndAddSchemaData();
        schemaData.setSchemaUrl("dataSchema");

        Iterator<Field> colIt = this.fields.iterator();
        while (colIt.hasNext()) {

          Field field = colIt.next();
          if (!field.getType().equals(FieldType.GEOMETRY)) {

            Object value = feature.getValue(field);
            SimpleData simpleData = schemaData.createAndAddSimpleData(field.getName());
            simpleData.setValue(String.valueOf(value));
          }
        }
      }

      // En fonction dy type de géométrie Jts, appel
      // aux fonctions de conversion en géométries Kml

      // POINT
      if (geometry instanceof Point) {

        placemark.setGeometry(getAsKmlPoint((Point) geometry));

        // LINESTRING
      } else if (geometry instanceof LineString) {

        placemark.setGeometry(getAsKmlLineString((LineString) geometry));

        // POLYGON
      } else if (geometry instanceof Polygon) {

        placemark.setGeometry(getAsKmlPolygon((Polygon) geometry));

        // MULTIPOINT
      } else if (geometry instanceof MultiPoint) {

        de.micromata.opengis.kml.v_2_2_0.MultiGeometry kmlMultiGeometry =
            placemark.createAndSetMultiGeometry();
        for (int i = 0; i < geometry.getNumGeometries(); i++) {

          kmlMultiGeometry.addToGeometry(getAsKmlPoint((Point) ((Point) geometry).getGeometryN(i)));
        }
        // MULTILINESTRING
      } else if (geometry instanceof MultiLineString) {

        de.micromata.opengis.kml.v_2_2_0.MultiGeometry kmlMultiGeometry =
            placemark.createAndSetMultiGeometry();
        for (int i = 0; i < geometry.getNumGeometries(); i++) {

          kmlMultiGeometry.addToGeometry(
              getAsKmlLineString((LineString) ((MultiLineString) geometry).getGeometryN(i)));
        }
        // MULTIPOLYGON
      } else if (geometry instanceof MultiPolygon) {

        de.micromata.opengis.kml.v_2_2_0.MultiGeometry kmlMultiGeometry =
            placemark.createAndSetMultiGeometry();
        for (int i = 0; i < geometry.getNumGeometries(); i++) {

          kmlMultiGeometry.addToGeometry(
              getAsKmlPolygon((Polygon) ((MultiPolygon) geometry).getGeometryN(i)));
        }
        // GEOMETRYCOLLECTION
      } else if (geometry instanceof GeometryCollection) {

        de.micromata.opengis.kml.v_2_2_0.MultiGeometry kmlMultiGeometry =
            placemark.createAndSetMultiGeometry();
        for (int i = 0; i < geometry.getNumGeometries(); i++) {

          Geometry currentGeometry = geometry.getGeometryN(i);

          if (currentGeometry instanceof Point) {
            kmlMultiGeometry.addToGeometry(getAsKmlPoint((Point) currentGeometry));
          } else if (currentGeometry instanceof LineString) {
            kmlMultiGeometry.addToGeometry(getAsKmlLineString((LineString) currentGeometry));
          } else if (currentGeometry instanceof Polygon) {
            kmlMultiGeometry.addToGeometry(getAsKmlPolygon((Polygon) currentGeometry));
          } else if (currentGeometry instanceof MultiPoint) {

            for (int j = 0; j < currentGeometry.getNumGeometries(); j++) {
              kmlMultiGeometry.addToGeometry(
                  getAsKmlPoint((Point) ((Point) currentGeometry).getGeometryN(j)));
            }

          } else if (currentGeometry instanceof MultiLineString) {

            for (int j = 0; j < currentGeometry.getNumGeometries(); j++) {
              kmlMultiGeometry.addToGeometry(
                  getAsKmlLineString((LineString) ((LineString) currentGeometry).getGeometryN(j)));
            }

          } else if (currentGeometry instanceof MultiPolygon) {

            for (int j = 0; j < currentGeometry.getNumGeometries(); j++) {
              kmlMultiGeometry.addToGeometry(
                  getAsKmlPolygon((Polygon) ((Polygon) currentGeometry).getGeometryN(j)));
            }
          }
        }
      }
    }

    if (isServletOutput) {

      if (features.size() > 0) {
        kml.marshal();
        kml.marshal(writer);
      }

    } else {

      try {

        FileOutputStream fileOutputStream = new FileOutputStream(this.kmlFileName);
        kml.marshal();
        kml.marshal(fileOutputStream);
        fileOutputStream.close();

      } catch (FileNotFoundException e) {
        throw new KettleException("Error writing features to " + this.kmlFileName, e);
      } catch (IOException e) {
        throw new KettleException("Error writing features to " + this.kmlFileName, e);
      }
    }
  }