PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/geotools-9.2/modules/plugin/shapefile/src/test/java/org/geotools/data/shapefile/indexed/FidQueryTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 265 lines | 169 code | 33 blank | 63 comment | 15 complexity | 196a7d039ce501a141155767796be752 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.shapefile.indexed;
  18. import java.io.IOException;
  19. import java.net.URL;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.geotools.data.DefaultQuery;
  26. import org.geotools.data.Query;
  27. import org.geotools.data.simple.SimpleFeatureCollection;
  28. import org.geotools.data.simple.SimpleFeatureIterator;
  29. import org.geotools.data.simple.SimpleFeatureStore;
  30. import org.geotools.factory.CommonFactoryFinder;
  31. import org.geotools.feature.DefaultFeatureCollection;
  32. import org.geotools.feature.FeatureCollections;
  33. import org.geotools.feature.simple.SimpleFeatureBuilder;
  34. import org.opengis.feature.simple.SimpleFeature;
  35. import org.opengis.feature.simple.SimpleFeatureType;
  36. import org.opengis.filter.Filter;
  37. import org.opengis.filter.FilterFactory2;
  38. import org.opengis.filter.Id;
  39. import org.opengis.filter.identity.FeatureId;
  40. import org.opengis.filter.spatial.BBOX;
  41. import com.vividsolutions.jts.geom.Coordinate;
  42. import com.vividsolutions.jts.geom.Geometry;
  43. import com.vividsolutions.jts.geom.GeometryFactory;
  44. /**
  45. *
  46. *
  47. * @source $URL$
  48. */
  49. public class FidQueryTest extends FIDTestCase {
  50. public FidQueryTest( ) throws IOException {
  51. super("FidQueryTest");
  52. }
  53. private IndexedShapefileDataStore ds;
  54. private static final FilterFactory2 fac = CommonFactoryFinder
  55. .getFilterFactory2(null);
  56. Map<String, SimpleFeature> fids = new HashMap<String, SimpleFeature>();
  57. SimpleFeatureStore featureStore;
  58. private int numFeatures;
  59. protected void setUp() throws Exception {
  60. super.setUp();
  61. URL url = backshp.toURI().toURL();
  62. ds = new IndexedShapefileDataStore(url, null, false, true,
  63. IndexType.QIX);
  64. numFeatures = 0;
  65. featureStore = (SimpleFeatureStore) ds.getFeatureSource();
  66. {
  67. SimpleFeatureIterator features = featureStore.getFeatures().features();
  68. try {
  69. while (features.hasNext()) {
  70. numFeatures++;
  71. SimpleFeature feature = features.next();
  72. fids.put(feature.getID(), feature);
  73. }
  74. } finally {
  75. if (features != null)
  76. features.close();
  77. }
  78. assertEquals(numFeatures, fids.size());
  79. }
  80. }
  81. @Override
  82. protected void tearDown() throws Exception {
  83. ds.dispose();
  84. super.tearDown();
  85. }
  86. public void testGetByFID() throws Exception {
  87. assertFidsMatch();
  88. }
  89. public void testAddFeature() throws Exception {
  90. SimpleFeature feature = fids.values().iterator().next();
  91. SimpleFeatureType schema = ds.getSchema();
  92. SimpleFeatureBuilder build = new SimpleFeatureBuilder(schema);
  93. GeometryFactory gf = new GeometryFactory();
  94. build.add(gf.createPoint((new Coordinate(0, 0))));
  95. build.add(new Long(0));
  96. build.add(new Long(0));
  97. build.add("Hey");
  98. SimpleFeature newFeature = build.buildFeature(null);
  99. DefaultFeatureCollection collection = new DefaultFeatureCollection();
  100. collection.add(newFeature);
  101. List<FeatureId> newFids = featureStore.addFeatures(collection);
  102. assertEquals(1, newFids.size());
  103. // this.assertFidsMatch();
  104. DefaultQuery query = new DefaultQuery(schema.getTypeName());
  105. FeatureId id = newFids.iterator().next();
  106. String fid = id.getID();
  107. Filter filter = fac.id(Collections.singleton(id));
  108. query.setFilter(filter);
  109. SimpleFeatureIterator features = featureStore.getFeatures(query).features();
  110. try {
  111. feature = features.next();
  112. for (int i = 0; i < schema.getAttributeCount(); i++) {
  113. Object value = feature.getAttribute(i);
  114. Object newValue = newFeature.getAttribute(i);
  115. assertEquals(newValue, value);
  116. }
  117. assertFalse(features.hasNext());
  118. } finally {
  119. if (features != null)
  120. features.close();
  121. }
  122. }
  123. public void testModifyFeature() throws Exception {
  124. SimpleFeature feature = this.fids.values().iterator().next();
  125. int newId = 237594123;
  126. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
  127. Id createFidFilter = ff.id(Collections.singleton(ff.featureId(feature
  128. .getID())));
  129. SimpleFeatureType schema = feature.getFeatureType();
  130. featureStore.modifyFeatures(schema.getDescriptor("ID"), new Integer(
  131. newId), createFidFilter);
  132. SimpleFeatureIterator features = featureStore.getFeatures(createFidFilter)
  133. .features();
  134. try {
  135. assertFalse(feature.equals(features.next()));
  136. } finally {
  137. if (features != null) {
  138. features.close();
  139. }
  140. }
  141. feature.setAttribute("ID", new Integer(newId));
  142. this.assertFidsMatch();
  143. }
  144. public void testDeleteFeature() throws Exception {
  145. SimpleFeatureIterator features = featureStore.getFeatures().features();
  146. SimpleFeature feature;
  147. try {
  148. feature = features.next();
  149. } finally {
  150. if (features != null)
  151. features.close();
  152. }
  153. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
  154. Id createFidFilter = ff.id(Collections.singleton(ff.featureId(feature
  155. .getID())));
  156. featureStore.removeFeatures(createFidFilter);
  157. fids.remove(feature.getID());
  158. assertEquals(fids.size(), featureStore.getCount(Query.ALL));
  159. features = featureStore.getFeatures(createFidFilter).features();
  160. try {
  161. assertFalse(features.hasNext());
  162. } finally {
  163. if (features != null)
  164. features.close();
  165. }
  166. this.assertFidsMatch();
  167. }
  168. /*
  169. public void testFIDBBoxQuery() throws Exception {
  170. SimpleFeatureIterator features = featureStore.getFeatures().features();
  171. SimpleFeature feature;
  172. try {
  173. feature = features.next();
  174. feature = features.next();
  175. feature = features.next();
  176. } finally {
  177. if (features != null)
  178. features.close();
  179. }
  180. // FilterFactory factory = FilterFactoryFinder.createFilterFactory();
  181. // BBoxExpression bb =
  182. // factory.createBBoxExpression(feature.getBounds());
  183. //
  184. // GeometryFilter bboxFilter =
  185. // factory.createGeometryFilter(FilterType.GEOMETRY_INTERSECTS);
  186. // bboxFilter.addRightGeometry(bb);
  187. //
  188. // String geom = ds.getSchema().getDefaultGeometry().getLocalName();
  189. //
  190. // bboxFilter.addLeftGeometry(factory.createAttributeExpression(geom));
  191. FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);
  192. BBOX bbox = ff.bbox(ff.property(""), feature.getBounds());
  193. features = featureStore.getFeatures(bbox).features();
  194. try {
  195. while (features.hasNext()) {
  196. SimpleFeature newFeature = features.next();
  197. assertEquals(newFeature, fids.get(newFeature.getID()));
  198. }
  199. } finally {
  200. if (features != null)
  201. features.close();
  202. }
  203. }
  204. */
  205. private void assertFidsMatch() throws IOException {
  206. // long start = System.currentTimeMillis();
  207. DefaultQuery query = new DefaultQuery(featureStore.getSchema()
  208. .getTypeName());
  209. int i = 0;
  210. for (Iterator iter = fids.entrySet().iterator(); iter.hasNext();) {
  211. i++;
  212. Map.Entry entry = (Map.Entry) iter.next();
  213. String fid = (String) entry.getKey();
  214. FeatureId id = fac.featureId(fid);
  215. Filter filter = fac.id(Collections.singleton(id));
  216. query.setFilter(filter);
  217. SimpleFeatureIterator features = featureStore.getFeatures(query)
  218. .features();
  219. try {
  220. SimpleFeature feature = features.next();
  221. assertFalse(features.hasNext());
  222. assertEquals(i + "th feature", entry.getValue(), feature);
  223. } finally {
  224. if (features != null)
  225. features.close();
  226. }
  227. }
  228. }
  229. }