PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/beam-core/src/main/java/org/esa/beam/dataio/geometry/VectorDataNodeWriter.java

http://github.com/bcdev/beam
Java | 143 lines | 105 code | 17 blank | 21 comment | 14 complexity | f3741a4e3868bdbe52271800fc9426c3 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de)
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version.
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, see http://www.gnu.org/licenses/
  15. */
  16. package org.esa.beam.dataio.geometry;
  17. import com.bc.ceres.binding.Converter;
  18. import com.thoughtworks.xstream.core.util.OrderRetainingMap;
  19. import org.esa.beam.framework.datamodel.ProductNode;
  20. import org.esa.beam.framework.datamodel.VectorDataNode;
  21. import org.esa.beam.util.StringUtils;
  22. import org.esa.beam.util.converters.JavaTypeConverter;
  23. import org.geotools.feature.FeatureCollection;
  24. import org.geotools.feature.FeatureIterator;
  25. import org.opengis.feature.simple.SimpleFeature;
  26. import org.opengis.feature.simple.SimpleFeatureType;
  27. import org.opengis.feature.type.AttributeDescriptor;
  28. import java.io.File;
  29. import java.io.FileWriter;
  30. import java.io.IOException;
  31. import java.io.Writer;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Set;
  35. // todo - use new CsvWriter here (nf, 2012-04-12)
  36. /**
  37. * A writer for VectorDataNodes.
  38. *
  39. * @author Norman
  40. */
  41. public class VectorDataNodeWriter {
  42. private static long id = System.nanoTime();
  43. public void write(VectorDataNode vectorDataNode, File file) throws IOException {
  44. FileWriter writer = new FileWriter(file);
  45. try {
  46. writeNodeProperties(vectorDataNode, writer);
  47. writeFeatures(vectorDataNode.getFeatureCollection(), writer);
  48. } finally {
  49. writer.close();
  50. }
  51. }
  52. public void writeFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection, Writer writer) throws IOException {
  53. SimpleFeatureType simpleFeatureType = featureCollection.getSchema();
  54. writeFeatureType(simpleFeatureType, writer);
  55. writeFeatures0(featureCollection, writer);
  56. }
  57. public void writeProperties(Map<String, String> properties, Writer writer) throws IOException {
  58. Set<Map.Entry<String, String>> entries = properties.entrySet();
  59. for (Map.Entry<String, String> entry : entries) {
  60. writer.write("#" + entry.getKey() + "=" + entry.getValue() + "\n");
  61. }
  62. }
  63. void writeNodeProperties(VectorDataNode vectorDataNode, Writer writer) throws IOException {
  64. OrderRetainingMap properties = new OrderRetainingMap();
  65. final Map<Object, Object> userData = vectorDataNode.getFeatureType().getUserData();
  66. final Set<Map.Entry<Object, Object>> entries = userData.entrySet();
  67. for (Map.Entry<Object, Object> entry : entries) {
  68. if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
  69. properties.put(entry.getKey().toString(), entry.getValue().toString());
  70. }
  71. }
  72. String description = vectorDataNode.getDescription();
  73. if (StringUtils.isNotNullAndNotEmpty(description)) {
  74. properties.put(ProductNode.PROPERTY_NAME_DESCRIPTION, description);
  75. }
  76. String defaultCSS = vectorDataNode.getDefaultStyleCss();
  77. if (StringUtils.isNotNullAndNotEmpty(defaultCSS)) {
  78. properties.put(VectorDataNodeIO.PROPERTY_NAME_DEFAULT_CSS, defaultCSS);
  79. }
  80. writeProperties(properties, writer);
  81. }
  82. private void writeFeatures0(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection, Writer writer) throws IOException {
  83. Converter[] converters = VectorDataNodeIO.getConverters(featureCollection.getSchema());
  84. final FeatureIterator<SimpleFeature> features = featureCollection.features();
  85. try {
  86. while (features.hasNext()) {
  87. SimpleFeature simpleFeature = features.next();
  88. String fid = simpleFeature.getID();
  89. if (fid == null || fid.isEmpty()) {
  90. fid = String.format("FID%s", Long.toHexString(id++));
  91. }
  92. writer.write(fid);
  93. List<Object> attributes = simpleFeature.getAttributes();
  94. for (int i = 0; i < attributes.size(); i++) {
  95. Object value = attributes.get(i);
  96. String text = VectorDataNodeIO.NULL_TEXT;
  97. if (value != null) {
  98. Converter converter = converters[i];
  99. text = converter.format(value);
  100. text = VectorDataNodeIO.encodeTabString(text);
  101. }
  102. writer.write(VectorDataNodeIO.DEFAULT_DELIMITER_CHAR);
  103. writer.write(text);
  104. }
  105. writer.write('\n');
  106. }
  107. } finally {
  108. features.close();
  109. }
  110. }
  111. private void writeFeatureType(SimpleFeatureType simpleFeatureType, Writer writer) throws IOException {
  112. writer.write(simpleFeatureType.getTypeName());
  113. List<AttributeDescriptor> attributeDescriptors = simpleFeatureType.getAttributeDescriptors();
  114. JavaTypeConverter typeConverter = new JavaTypeConverter();
  115. for (AttributeDescriptor attributeDescriptor : attributeDescriptors) {
  116. Class<?> binding = attributeDescriptor.getType().getBinding();
  117. String name = attributeDescriptor.getLocalName();
  118. String type = typeConverter.format(binding);
  119. writer.write(VectorDataNodeIO.DEFAULT_DELIMITER_CHAR);
  120. writer.write(name);
  121. writer.write(':');
  122. writer.write(type);
  123. }
  124. writer.write('\n');
  125. }
  126. }