/projects/geotools-9.2/modules/unsupported/shapefile-renderer/src/test/java/org/geotools/renderer/shape/QueryTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 176 lines · 126 code · 27 blank · 23 comment · 0 complexity · a0b0a3286caa620394e6a2d9ec5b582c 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.renderer.shape;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import junit.framework.TestCase;
  21. import org.geotools.data.DefaultQuery;
  22. import org.geotools.data.Query;
  23. import org.geotools.data.simple.SimpleFeatureSource;
  24. import org.geotools.factory.CommonFactoryFinder;
  25. import org.geotools.geometry.jts.ReferencedEnvelope;
  26. import org.geotools.map.DefaultMapContext;
  27. import org.geotools.renderer.RenderListener;
  28. import org.geotools.styling.Style;
  29. import org.opengis.feature.simple.SimpleFeature;
  30. import org.opengis.filter.And;
  31. import org.opengis.filter.FilterFactory2;
  32. import org.opengis.filter.PropertyIsEqualTo;
  33. import org.opengis.filter.expression.Literal;
  34. import org.opengis.filter.identity.FeatureId;
  35. import org.opengis.filter.spatial.BBOX;
  36. import org.opengis.filter.spatial.Beyond;
  37. import org.opengis.filter.spatial.DWithin;
  38. import org.opengis.filter.spatial.Intersects;
  39. import com.vividsolutions.jts.geom.Coordinate;
  40. import com.vividsolutions.jts.geom.Envelope;
  41. import com.vividsolutions.jts.geom.GeometryFactory;
  42. /**
  43. *
  44. *
  45. *
  46. *
  47. * @source $URL$
  48. */
  49. public class QueryTest extends TestCase {
  50. private static final boolean INTERACTIVE = false;
  51. private SimpleFeatureSource source;
  52. private Style style;
  53. private DefaultMapContext map;
  54. Envelope bounds = new Envelope(-5, 5, -5, 5);
  55. protected void setUp() throws Exception {
  56. source = TestUtilites.getDataStore("theme1.shp").getFeatureSource();
  57. style = TestUtilites.createTestStyle("theme1", null);
  58. map = new DefaultMapContext();
  59. map.addLayer(source, style);
  60. }
  61. public void testFidFilter() throws Exception {
  62. FeatureId id = TestUtilites.filterFactory.featureId("theme1.2");
  63. Query q = new DefaultQuery("theme1",
  64. TestUtilites.filterFactory.id(Collections.singleton(id)));
  65. map.getLayer(0).setQuery(q);
  66. ShapefileRenderer renderer = new ShapefileRenderer(map);
  67. renderer.addRenderListener(new RenderListener() {
  68. public void featureRenderer(SimpleFeature feature) {
  69. assertEquals("theme1.2", feature.getID());
  70. }
  71. public void errorOccurred(Exception e) {
  72. throw new RuntimeException(e);
  73. }
  74. });
  75. TestUtilites.INTERACTIVE = INTERACTIVE;
  76. TestUtilites.showRender("testFidFilter", renderer, 1000, bounds, 1);
  77. }
  78. public void testBBOXFilter() throws Exception {
  79. String geom = source.getSchema().getGeometryDescriptor().getLocalName();
  80. BBOX filter = TestUtilites.filterFactory.bbox(geom, -4, -3, -2, 0, null);
  81. Query q = new DefaultQuery("theme1", filter);
  82. map.getLayer(0).setQuery(q);
  83. ShapefileRenderer renderer = new ShapefileRenderer(map);
  84. renderer.addRenderListener(new RenderListener() {
  85. public void featureRenderer(SimpleFeature feature) {
  86. assertEquals("theme1.1", feature.getID());
  87. }
  88. public void errorOccurred(Exception e) {
  89. throw new RuntimeException(e);
  90. }
  91. });
  92. TestUtilites.showRender("testFidFilter", renderer, 1000, bounds, 1);
  93. }
  94. public void testMixedFilter() throws Exception {
  95. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
  96. Intersects bbox = ff.intersects(ff.property("the_geom"), ff.literal(new ReferencedEnvelope(-4, -2, 0, -3, null)));
  97. PropertyIsEqualTo idEqual = ff.equals(ff.property("ID"), ff.literal(1.0));
  98. PropertyIsEqualTo nameEqual = ff.equals(ff.property("NAME"), ff.literal("dave street"));
  99. And filter = ff.and(Arrays.asList(new org.opengis.filter.Filter[] {bbox, idEqual, nameEqual}));
  100. Query q = new DefaultQuery("theme1", filter);
  101. map.getLayer(0).setQuery(q);
  102. ShapefileRenderer renderer = new ShapefileRenderer(map);
  103. renderer.addRenderListener(new RenderListener() {
  104. public void featureRenderer(SimpleFeature feature) {
  105. assertEquals("theme1.1", feature.getID());
  106. }
  107. public void errorOccurred(Exception e) {
  108. throw new RuntimeException(e);
  109. }
  110. });
  111. TestUtilites.showRender("testFidFilter", renderer, 1000, bounds, 1);
  112. }
  113. public void testBeyondFilter() throws Exception {
  114. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
  115. GeometryFactory gf = new GeometryFactory();
  116. Literal point = ff.literal(gf.createPoint(new Coordinate(1, 0)));
  117. Beyond filter = ff.beyond(ff.property("the_geom"), point, 0.3, (String) null);
  118. Query q = new DefaultQuery("theme1", filter);
  119. map.getLayer(0).setQuery(q);
  120. ShapefileRenderer renderer = new ShapefileRenderer(map);
  121. renderer.addRenderListener(new RenderListener() {
  122. public void featureRenderer(SimpleFeature feature) {
  123. assertEquals("theme1.2", feature.getID());
  124. }
  125. public void errorOccurred(Exception e) {
  126. throw new RuntimeException(e);
  127. }
  128. });
  129. TestUtilites.showRender("testFidFilter", renderer, 1000, bounds, 1);
  130. }
  131. public void testDWithinFilter() throws Exception {
  132. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
  133. GeometryFactory gf = new GeometryFactory();
  134. Literal point = ff.literal(gf.createPoint(new Coordinate(1, 0)));
  135. DWithin filter = ff.dwithin(ff.property("the_geom"), point, 0.15, (String) null);
  136. Query q = new DefaultQuery("theme1", filter);
  137. map.getLayer(0).setQuery(q);
  138. ShapefileRenderer renderer = new ShapefileRenderer(map);
  139. renderer.addRenderListener(new RenderListener() {
  140. public void featureRenderer(SimpleFeature feature) {
  141. assertEquals("theme1.1", feature.getID());
  142. }
  143. public void errorOccurred(Exception e) {
  144. throw new RuntimeException(e);
  145. }
  146. });
  147. TestUtilites.showRender("testFidFilter", renderer, 1000, bounds, 1);
  148. }
  149. }