/projects/geotools-9.2/modules/library/main/src/main/java/org/geotools/data/store/ReprojectingIterator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 139 lines · 84 code · 21 blank · 34 comment · 2 complexity · afbd4fae2f64540e26f2bffa5bbe862a MD5 · raw file

  1. /*
  2. * GeoTools - The Open Source Java GIS Toolkit
  3. * http://geotools.org
  4. *
  5. * (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation;
  10. * version 2.1 of the License.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. package org.geotools.data.store;
  18. import java.io.IOException;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import org.geotools.factory.FactoryRegistryException;
  22. import org.geotools.feature.simple.SimpleFeatureBuilder;
  23. import org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer;
  24. import org.geotools.referencing.ReferencingFactoryFinder;
  25. import org.opengis.feature.IllegalAttributeException;
  26. import org.opengis.feature.simple.SimpleFeature;
  27. import org.opengis.feature.simple.SimpleFeatureType;
  28. import org.opengis.referencing.FactoryException;
  29. import org.opengis.referencing.crs.CoordinateReferenceSystem;
  30. import org.opengis.referencing.operation.MathTransform;
  31. import org.opengis.referencing.operation.MathTransform2D;
  32. import org.opengis.referencing.operation.OperationNotFoundException;
  33. import org.opengis.referencing.operation.TransformException;
  34. import com.vividsolutions.jts.geom.Geometry;
  35. /**
  36. *
  37. *
  38. * @source $URL$
  39. */
  40. public class ReprojectingIterator implements Iterator<SimpleFeature> {
  41. /**
  42. * decorated iterator
  43. */
  44. Iterator<SimpleFeature> delegate;
  45. /**
  46. * The target coordinate reference system
  47. */
  48. CoordinateReferenceSystem target;
  49. /**
  50. * schema of reprojected features
  51. */
  52. SimpleFeatureType schema;
  53. /**
  54. * Transformer
  55. */
  56. GeometryCoordinateSequenceTransformer tx;
  57. public ReprojectingIterator(
  58. Iterator<SimpleFeature> delegate, MathTransform transform, SimpleFeatureType schema,
  59. GeometryCoordinateSequenceTransformer transformer
  60. ) throws OperationNotFoundException, FactoryRegistryException, FactoryException {
  61. this.delegate = delegate;
  62. this.schema = schema;
  63. tx = transformer;
  64. tx.setMathTransform((MathTransform2D) transform);
  65. }
  66. public ReprojectingIterator(
  67. Iterator<SimpleFeature> delegate, CoordinateReferenceSystem source, CoordinateReferenceSystem target,
  68. SimpleFeatureType schema, GeometryCoordinateSequenceTransformer transformer
  69. ) throws OperationNotFoundException, FactoryRegistryException, FactoryException {
  70. this.delegate = delegate;
  71. this.target = target;
  72. this.schema = schema;
  73. tx = transformer;
  74. MathTransform transform = ReferencingFactoryFinder.getCoordinateOperationFactory(
  75. null).createOperation(source, target).getMathTransform();
  76. tx.setMathTransform(transform);
  77. }
  78. public Iterator<SimpleFeature> getDelegate() {
  79. return delegate;
  80. }
  81. public void remove() {
  82. delegate.remove();
  83. }
  84. public boolean hasNext() {
  85. return delegate.hasNext();
  86. }
  87. public SimpleFeature next() {
  88. SimpleFeature feature = (SimpleFeature) delegate.next();
  89. try {
  90. return reproject(feature);
  91. } catch (IOException e) {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. SimpleFeature reproject(SimpleFeature feature) throws IOException {
  96. List<Object> attributes = feature.getAttributes();
  97. for (int i = 0; i < attributes.size(); i++) {
  98. Object object = attributes.get(i);
  99. if (object instanceof Geometry) {
  100. // do the transformation
  101. Geometry geometry = (Geometry) object;
  102. try {
  103. attributes.set(i, tx.transform(geometry));
  104. } catch (TransformException e) {
  105. String msg = "Error occured transforming "
  106. + geometry.toString();
  107. throw (IOException) new IOException(msg).initCause(e);
  108. }
  109. }
  110. }
  111. try {
  112. return SimpleFeatureBuilder.build(schema, attributes, feature.getID());
  113. } catch (IllegalAttributeException e) {
  114. String msg = "Error creating reprojeced feature";
  115. throw (IOException) new IOException(msg).initCause(e);
  116. }
  117. }
  118. }