/projects/geotools-9.2/modules/unsupported/sfs/src/test/java/org/geotools/data/sfs/SFSDataStoreFeatureReaderTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 441 lines · 296 code · 86 blank · 59 comment · 8 complexity · 18fc4244afe1d66182baa9f638afd94e MD5 · raw file

  1. /*
  2. * GeoTools - The Open Source Java GIS Toolkit
  3. * http://geotools.org
  4. *
  5. * (C) 2002-2011, 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.sfs;
  18. import java.io.IOException;
  19. import java.io.StringReader;
  20. import java.util.ArrayList;
  21. import junit.framework.TestCase;
  22. import org.geotools.data.sfs.SFSDataStoreUtil;
  23. import org.geotools.geojson.feature.FeatureJSON;
  24. import org.opengis.feature.simple.SimpleFeature;
  25. import com.vividsolutions.jts.geom.Geometry;
  26. /**
  27. *
  28. *
  29. * @source $URL$
  30. */
  31. public class SFSDataStoreFeatureReaderTest extends TestCase {
  32. public SFSDataStoreFeatureReaderTest(String testName) {
  33. super(testName);
  34. }
  35. @Override
  36. protected void setUp() throws Exception {
  37. super.setUp();
  38. }
  39. @Override
  40. protected void tearDown() throws Exception {
  41. super.tearDown();
  42. }
  43. /* return geojson with point geometry*/
  44. public String getFeatureWithPointGeometry() {
  45. String json =
  46. "{"
  47. + " 'type': 'Feature',"
  48. + " 'geometry': {"
  49. + " 'type': 'Point',"
  50. + " 'coordinates': [1.5, 2.9]"
  51. + " },"
  52. + " 'properties': {"
  53. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  54. + " 'int': 1,"
  55. + " 'double': 1.1,"
  56. + " 'string': 'one'"
  57. + " },"
  58. + " 'id': 'feature.1'"
  59. + " }";
  60. return json;
  61. }
  62. /* return geojson with linsString geometry*/
  63. public String getFeatureWithLineStringGeometry() {
  64. String json =
  65. "{"
  66. + " 'type': 'Feature',"
  67. + " 'geometry': {"
  68. + " 'type': 'LineString',"
  69. + " 'coordinates': [[1.1, 1.2], [1.3, 1.4]]"
  70. + " },"
  71. + " 'properties': {"
  72. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  73. + " 'int': 1,"
  74. + " 'double': 1.1,"
  75. + " 'string': 'one'"
  76. + " },"
  77. + " 'id': 'feature.1'"
  78. + " }";
  79. return json;
  80. }
  81. public String getFeatureWithpolygonTyp1Geometry() {
  82. String json =
  83. "{"
  84. + " 'type': 'Feature',"
  85. + " 'geometry': {"
  86. + " 'type': 'Polygon',"
  87. + " 'coordinates': [[100.1, 0.1], [101.1, 0.1], [101.1, 1.1], [100.1, 1.1], [100.1, 0.1] ]"
  88. + " },"
  89. + " 'properties': {"
  90. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  91. + " 'int': 1,"
  92. + " 'double': 1.1,"
  93. + " 'string': 'one'"
  94. + " },"
  95. + " 'id': 'feature.1'"
  96. + " }";
  97. return json;
  98. }
  99. public String getFeatureWithpolygonTyp2Geometry() {
  100. String json =
  101. "{"
  102. + " 'type': 'Feature',"
  103. + " 'geometry': {"
  104. + " 'type': 'Polygon',"
  105. + " 'coordinates': ["
  106. + " [ [100.1, 0.1], [101.1, 0.1], [101.1, 1.1], [100.1, 1.1], [100.1, 0.1] ],"
  107. + " [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]"
  108. + " ]"
  109. + " },"
  110. + " 'properties': {"
  111. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  112. + " 'int': 1,"
  113. + " 'double': 1.1,"
  114. + " 'string': 'one'"
  115. + " },"
  116. + " 'id': 'feature.1'"
  117. + " }";
  118. return json;
  119. }
  120. public String getFeatureWithMultiPointGeometry() {
  121. String json =
  122. "{"
  123. + " 'type': 'Feature',"
  124. + " 'geometry': {"
  125. + " 'type': 'MultiPoint',"
  126. + " 'coordinates': [ [100.1, 0.1], [101.1, 1.1] ]"
  127. + " },"
  128. + " 'properties': {"
  129. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  130. + " 'int': 1,"
  131. + " 'double': 1.1,"
  132. + " 'string': 'one'"
  133. + " },"
  134. + " 'id': 'feature.1'"
  135. + " }";
  136. return json;
  137. }
  138. public String getFeatureWithMultiLineGeometry() {
  139. String json =
  140. "{"
  141. + " 'type': 'Feature',"
  142. + " 'geometry': {"
  143. + " 'type': 'MultiLineString',"
  144. + " 'coordinates':["
  145. + " [ [100.1, 0.1], [101.1, 1.1] ],"
  146. + " [ [102.1, 2.1], [103.1, 3.1] ]"
  147. + " ]"
  148. + " },"
  149. + " 'properties': {"
  150. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  151. + " 'int': 1,"
  152. + " 'double': 1.1,"
  153. + " 'string': 'one'"
  154. + " },"
  155. + " 'id': 'feature.1'"
  156. + " }";
  157. return json;
  158. }
  159. public String getFeatureWithMultiPolygonGeometry() {
  160. String json =
  161. "{"
  162. + " 'type': 'Feature',"
  163. + " 'geometry': {"
  164. + " 'type': 'MultiPolygon',"
  165. + " 'coordinates':["
  166. + " [[[102.1, 2.1], [103.1, 2.1], [103.1, 3.1], [102.1, 3.1], [102.1, 2.1]]],"
  167. + " [[[100.1, 0.1], [101.1, 0.1], [101.1, 1.1], [100.1, 1.1], [100.1, 0.1]],"
  168. + " [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]"
  169. + " ]"
  170. + " },"
  171. + " 'properties': {"
  172. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  173. + " 'int': 1,"
  174. + " 'double': 1.1,"
  175. + " 'string': 'one'"
  176. + " },"
  177. + " 'id': 'feature.1'"
  178. + " }";
  179. return json;
  180. }
  181. public String getFeatureWithGeometryCollectionGeometry() {
  182. String json =
  183. "{"
  184. + " 'type': 'Feature',"
  185. + " 'geometry' : { "
  186. + " 'type' : 'GeometryCollection',"
  187. + " 'geometries': ["
  188. + " { 'type': 'Point',"
  189. + " 'coordinates': [100.1, 0.1]"
  190. + " },"
  191. + " { 'type': 'LineString',"
  192. + " 'coordinates': [ [101.1, 0.1], [102.1, 1.1]]"
  193. + " }]"
  194. + " },"
  195. + " 'properties': {"
  196. + " 'boundedBy': [-1.1, -1.2, 1.3, 1.4],"
  197. + " 'int': 1,"
  198. + " 'double': 1.1,"
  199. + " 'string': 'one'"
  200. + " },"
  201. + " 'id': 'feature.1'"
  202. + " }";
  203. return json;
  204. }
  205. /* Test feature flipping with point geometry*/
  206. public void testFeatureFlippingWithPointGeom() throws IOException {
  207. FeatureJSON fjson = new FeatureJSON();
  208. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithPointGeometry())));
  209. Geometry fnG = (Geometry) sf.getDefaultGeometry();
  210. SFSDataStoreUtil.flipFeatureYX(fnG);
  211. assertEquals(fnG.getCoordinate().x, 2.9);
  212. assertEquals(fnG.getCoordinate().y, 1.5);
  213. }
  214. /* Test feature flipping with LineString geometry*/
  215. public void testFeatureFlippingWithLineStringGeom() throws IOException {
  216. FeatureJSON fjson = new FeatureJSON();
  217. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithLineStringGeometry())));
  218. Geometry fnG = (Geometry) sf.getDefaultGeometry();
  219. SFSDataStoreUtil.flipFeatureYX(fnG);
  220. /* Check for first point*/
  221. assertEquals(fnG.getCoordinates()[0].x, 1.2);
  222. assertEquals(fnG.getCoordinates()[0].y, 1.1);
  223. /* Check for second point*/
  224. assertEquals(fnG.getCoordinates()[1].x, 1.4);
  225. assertEquals(fnG.getCoordinates()[1].y, 1.3);
  226. }
  227. /* Test feature flipping with Polygon w/o holes geometry*/
  228. public void testFeatureFlippingWithPolygonNoHolesGeom() throws IOException {
  229. FeatureJSON fjson = new FeatureJSON();
  230. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithpolygonTyp1Geometry())));
  231. Geometry fnG = (Geometry) sf.getDefaultGeometry();
  232. SFSDataStoreUtil.flipFeatureYX(fnG);
  233. /* Check for first point*/
  234. assertEquals(fnG.getCoordinates()[0].x, 0.1);
  235. assertEquals(fnG.getCoordinates()[0].y, 100.1);
  236. /* Check for second point*/
  237. assertEquals(fnG.getCoordinates()[1].x, 0.1);
  238. assertEquals(fnG.getCoordinates()[1].y, 101.1);
  239. /* Check for last point*/
  240. assertEquals(fnG.getCoordinates()[4].x, 0.1);
  241. assertEquals(fnG.getCoordinates()[4].y, 100.1);
  242. }
  243. /* Test feature flipping with Polygon w/ holes geometry*/
  244. public void testFeatureFlippingWithPolygonHolesGeom() throws IOException {
  245. FeatureJSON fjson = new FeatureJSON();
  246. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithpolygonTyp2Geometry())));
  247. Geometry fnG = (Geometry) sf.getDefaultGeometry();
  248. SFSDataStoreUtil.flipFeatureYX(fnG);
  249. /* Check for first point*/
  250. assertEquals(fnG.getCoordinates()[0].x, 0.1);
  251. assertEquals(fnG.getCoordinates()[0].y, 100.1);
  252. /* Check for fourth point*/
  253. assertEquals(fnG.getCoordinates()[3].x, 1.1);
  254. assertEquals(fnG.getCoordinates()[3].y, 100.1);
  255. /* Check for sixth point*/
  256. assertEquals(fnG.getCoordinates()[5].x, 0.2);
  257. assertEquals(fnG.getCoordinates()[5].y, 100.2);
  258. /* Check for ninth point*/
  259. assertEquals(fnG.getCoordinates()[8].x, 0.8);
  260. assertEquals(fnG.getCoordinates()[8].y, 100.2);
  261. }
  262. /* Test feature flipping with Multi-Point geometry*/
  263. public void testFeatureFlippingWithMultiPointGeom() throws IOException {
  264. FeatureJSON fjson = new FeatureJSON();
  265. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithMultiPointGeometry())));
  266. Geometry fnG = (Geometry) sf.getDefaultGeometry();
  267. SFSDataStoreUtil.flipFeatureYX(fnG);
  268. /* Check for first point*/
  269. assertEquals(fnG.getCoordinates()[0].x, 0.1);
  270. assertEquals(fnG.getCoordinates()[0].y, 100.1);
  271. /* Check for second point*/
  272. assertEquals(fnG.getCoordinates()[1].x, 1.1);
  273. assertEquals(fnG.getCoordinates()[1].y, 101.1);
  274. }
  275. /* Test feature flipping with Multi-Line String geometry*/
  276. public void testFeatureFlippingWithMultiLineStringGeom() throws IOException {
  277. FeatureJSON fjson = new FeatureJSON();
  278. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithMultiLineGeometry())));
  279. Geometry fnG = (Geometry) sf.getDefaultGeometry();
  280. SFSDataStoreUtil.flipFeatureYX(fnG);
  281. /* Check for first point*/
  282. assertEquals(fnG.getCoordinates()[0].x, 0.1);
  283. assertEquals(fnG.getCoordinates()[0].y, 100.1);
  284. /* Check for second point*/
  285. assertEquals(fnG.getCoordinates()[1].x, 1.1);
  286. assertEquals(fnG.getCoordinates()[1].y, 101.1);
  287. /* Check for third point*/
  288. assertEquals(fnG.getCoordinates()[2].x, 2.1);
  289. assertEquals(fnG.getCoordinates()[2].y, 102.1);
  290. /* Check for last point*/
  291. assertEquals(fnG.getCoordinates()[3].x, 3.1);
  292. assertEquals(fnG.getCoordinates()[3].y, 103.1);
  293. }
  294. /* Test feature flipping with Multi-Polygongeometry*/
  295. public void testFeatureFlippingWithMultiPolygonGeom() throws IOException {
  296. FeatureJSON fjson = new FeatureJSON();
  297. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithMultiPolygonGeometry())));
  298. Geometry fnG = (Geometry) sf.getDefaultGeometry();
  299. SFSDataStoreUtil.flipFeatureYX(fnG);
  300. /* Check for first point*/
  301. assertEquals(fnG.getCoordinates()[0].x, 2.1);
  302. assertEquals(fnG.getCoordinates()[0].y, 102.1);
  303. /* Check for fourth point*/
  304. assertEquals(fnG.getCoordinates()[3].x, 3.1);
  305. assertEquals(fnG.getCoordinates()[3].y, 102.1);
  306. /* Check for sixth point*/
  307. assertEquals(fnG.getCoordinates()[5].x, 0.1);
  308. assertEquals(fnG.getCoordinates()[5].y, 100.1);
  309. /* Check for eight point*/
  310. assertEquals(fnG.getCoordinates()[7].x, 1.1);
  311. assertEquals(fnG.getCoordinates()[7].y, 101.1);
  312. /* Check for 12th point*/
  313. assertEquals(fnG.getCoordinates()[11].x, 0.2);
  314. assertEquals(fnG.getCoordinates()[11].y, 100.8);
  315. /* Check for last (15th) point*/
  316. assertEquals(fnG.getCoordinates()[14].x, 0.2);
  317. assertEquals(fnG.getCoordinates()[14].y, 100.2);
  318. }
  319. /**
  320. * Test Whether the boundingbox params get flipped properly or not
  321. * @throws IOException
  322. */
  323. public void testForBBoxFlipping() throws IOException {
  324. FeatureJSON fjson = new FeatureJSON();
  325. SimpleFeature sf = fjson.readFeature(reader(strip(getFeatureWithLineStringGeometry())));
  326. ArrayList al = (ArrayList) sf.getAttribute("boundedBy");
  327. SFSDataStoreUtil.flipYXInsideTheBoundingBox(al);
  328. /* Check for first point*/
  329. assertEquals(Double.parseDouble(al.get(0).toString()), -1.2);
  330. assertEquals(Double.parseDouble(al.get(1).toString()), -1.1);
  331. /* Check for second point*/
  332. assertEquals(Double.parseDouble(al.get(2).toString()), 1.4);
  333. assertEquals(Double.parseDouble(al.get(3).toString()), 1.3);
  334. }
  335. /* Methods used for testing*/
  336. protected StringReader reader(String json) throws IOException {
  337. return new StringReader(json);
  338. }
  339. /* Method used for testing*/
  340. protected String strip(String json) {
  341. StringBuilder sb = new StringBuilder();
  342. for (int i = 0; i < json.length(); i++) {
  343. char c = json.charAt(i);
  344. if (c == ' ' || c == '\n') {
  345. continue;
  346. }
  347. if (c == '\'') {
  348. sb.append("\"");
  349. } else {
  350. sb.append(c);
  351. }
  352. }
  353. return sb.toString();
  354. }
  355. }