/modules/plugin/mongodb/src/test/java/org/geotools/data/mongodb/complex/MongoComplexUtilitiesTest.java

https://github.com/geotools/geotools · Java · 114 lines · 80 code · 6 blank · 28 comment · 3 complexity · c18e9d8efa3bb9631fdd4b1e96b28db2 MD5 · raw file

  1. /*
  2. * GeoTools - The Open Source Java GIS Toolkit
  3. * http://geotools.org
  4. *
  5. * (C) 2019, 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.mongodb.complex;
  18. import com.mongodb.BasicDBList;
  19. import com.mongodb.DBObject;
  20. import java.io.IOException;
  21. import java.util.Collections;
  22. import org.geotools.data.mongodb.MongoFeature;
  23. import org.geotools.data.mongodb.MongoGeometryBuilder;
  24. import org.geotools.data.mongodb.MongoTestSetup;
  25. import org.geotools.data.mongodb.MongoTestSupport;
  26. import org.geotools.data.simple.SimpleFeatureSource;
  27. import org.geotools.feature.FeatureIterator;
  28. import org.geotools.feature.FeatureTypes;
  29. import org.geotools.feature.SchemaException;
  30. import org.geotools.feature.simple.SimpleFeatureBuilder;
  31. import org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer;
  32. import org.geotools.referencing.CRS;
  33. import org.locationtech.jts.geom.Geometry;
  34. import org.opengis.feature.simple.SimpleFeature;
  35. import org.opengis.feature.simple.SimpleFeatureType;
  36. import org.opengis.referencing.FactoryException;
  37. import org.opengis.referencing.crs.CoordinateReferenceSystem;
  38. import org.opengis.referencing.operation.TransformException;
  39. public abstract class MongoComplexUtilitiesTest extends MongoTestSupport {
  40. protected MongoComplexUtilitiesTest(MongoTestSetup testSetup) {
  41. super(testSetup);
  42. }
  43. /**
  44. * Checks no exception occurs when an empty list is evaluated by the complex utilities get value
  45. * method.
  46. */
  47. public void testGetValueEmptyList() throws IOException {
  48. final BasicDBList list = new BasicDBList();
  49. Object value =
  50. MongoComplexUtilities.getValue(list, Collections.emptyMap(), "path1.path2", null);
  51. // no exception thrown, the value is null
  52. assertNull(value);
  53. }
  54. public void testFeatureAttributeValueIsReturnedFromJsonPath()
  55. throws IOException, FactoryException, TransformException {
  56. // test that in case of a jsonpath can be resolved against the feature,
  57. // the feature value is picked up instead of the one from the DBObject
  58. SimpleFeatureSource source = dataStore.getFeatureSource("ft3");
  59. try (FeatureIterator<SimpleFeature> it = source.getFeatures().features()) {
  60. GeometryCoordinateSequenceTransformer transformer =
  61. new GeometryCoordinateSequenceTransformer();
  62. CoordinateReferenceSystem original = source.getSchema().getCoordinateReferenceSystem();
  63. CoordinateReferenceSystem target = CRS.decode("urn:x-ogc:def:crs:EPSG:6.11.2:4326");
  64. transformer.setMathTransform(CRS.findMathTransform(original, target, false));
  65. while (it.hasNext()) {
  66. SimpleFeature f = it.next();
  67. // transforming the geometry so that the SimpleFeature geometry will be different
  68. // from
  69. // the one in the DBObject
  70. Geometry geom = transformer.transform((Geometry) f.getDefaultGeometry());
  71. f.setDefaultGeometry(geom);
  72. Geometry geometry2 = (Geometry) MongoComplexUtilities.getValue(f, "geometry");
  73. assertEquals(geom, geometry2);
  74. MongoFeature mongoFeature = (MongoFeature) f;
  75. Geometry dbGeom =
  76. new MongoGeometryBuilder()
  77. .toGeometry(
  78. (DBObject) mongoFeature.getMongoObject().get("geometry"));
  79. assertFalse(geometry2.equals(dbGeom));
  80. }
  81. }
  82. }
  83. public void testReprojectedValuesNotIgnored()
  84. throws IOException, FactoryException, TransformException, SchemaException {
  85. SimpleFeatureSource source = dataStore.getFeatureSource("ft3");
  86. try (FeatureIterator<SimpleFeature> it = source.getFeatures().features()) {
  87. GeometryCoordinateSequenceTransformer transformer =
  88. new GeometryCoordinateSequenceTransformer();
  89. CoordinateReferenceSystem original = source.getSchema().getCoordinateReferenceSystem();
  90. CoordinateReferenceSystem target = CRS.decode("urn:x-ogc:def:crs:EPSG:6.11.2:4326");
  91. transformer.setMathTransform(CRS.findMathTransform(original, target, false));
  92. SimpleFeatureType ft = FeatureTypes.transform(source.getSchema(), target);
  93. while (it.hasNext()) {
  94. SimpleFeature f = it.next();
  95. Geometry geom = transformer.transform((Geometry) f.getDefaultGeometry());
  96. f.setDefaultGeometry(geom);
  97. // Mocking the case when a feature is reprojected
  98. // and the MongoFeature in userData is copied in the newly built feature
  99. SimpleFeature rep = SimpleFeatureBuilder.build(ft, f.getAttributes(), f.getID());
  100. if (f.hasUserData()) {
  101. rep.getUserData().putAll(f.getUserData());
  102. }
  103. Geometry geometry2 = (Geometry) MongoComplexUtilities.getValue(rep, "geometry");
  104. assertEquals(geom, geometry2);
  105. }
  106. }
  107. }
  108. }