/tests/Format/Filter/v1.html

https://github.com/aabt/openlayers · HTML · 261 lines · 195 code · 41 blank · 25 comment · 0 complexity · b0671c9385ada9d5c662ff07923dcf94 MD5 · raw file

  1. <html>
  2. <head>
  3. <script src="../../OLLoader.js"></script>
  4. <script type="text/javascript">
  5. function test_PropertyIsBetween(t) {
  6. t.plan(6);
  7. var test_xml, parser, xml;
  8. parser = new OpenLayers.Format.Filter.v1();
  9. xml = new OpenLayers.Format.XML();
  10. test_xml =
  11. '<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
  12. '<ogc:PropertyIsBetween>' +
  13. '<ogc:PropertyName>number</ogc:PropertyName>' +
  14. '<ogc:LowerBoundary>' +
  15. '<ogc:Literal>0</ogc:Literal>' +
  16. '</ogc:LowerBoundary>' +
  17. '<ogc:UpperBoundary>' +
  18. '<ogc:Literal>100</ogc:Literal>' +
  19. '</ogc:UpperBoundary>' +
  20. '</ogc:PropertyIsBetween>' +
  21. '</ogc:Filter>';
  22. var filter = parser.read(xml.read(test_xml).documentElement);
  23. t.eq(filter.type, OpenLayers.Filter.Comparison.BETWEEN,
  24. "[0] read correct type");
  25. t.eq(filter.lowerBoundary, 0,
  26. "[0] record correct lower boundary value");
  27. t.eq(filter.upperBoundary, 100,
  28. "[0] record correct upper boundary value");
  29. test_xml =
  30. '<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">' +
  31. '<ogc:PropertyIsBetween>' +
  32. '<ogc:PropertyName>number</ogc:PropertyName>' +
  33. '<ogc:LowerBoundary>0</ogc:LowerBoundary>' +
  34. '<ogc:UpperBoundary>100</ogc:UpperBoundary>' +
  35. '</ogc:PropertyIsBetween>' +
  36. '</ogc:Filter>';
  37. var filter = parser.read(xml.read(test_xml).documentElement);
  38. t.eq(filter.type, OpenLayers.Filter.Comparison.BETWEEN,
  39. "[1] read correct type");
  40. t.eq(filter.lowerBoundary, 0,
  41. "[1] record correct lower boundary value");
  42. t.eq(filter.upperBoundary, 100,
  43. "[1] record correct upper boundary value");
  44. }
  45. function test_Intersects(t) {
  46. t.plan(4);
  47. var str =
  48. '<Filter xmlns="http://www.opengis.net/ogc">' +
  49. '<Intersects>' +
  50. '<PropertyName>Geometry</PropertyName>' +
  51. '<gml:Polygon xmlns:gml="http://www.opengis.net/gml">' +
  52. '<gml:outerBoundaryIs>' +
  53. '<gml:LinearRing>' +
  54. '<gml:coordinates decimal="." cs="," ts=" ">2488789,289552 2588789,289552 2588789,389552 2488789,389552 2488789,289552</gml:coordinates>' +
  55. '</gml:LinearRing>' +
  56. '</gml:outerBoundaryIs>' +
  57. '</gml:Polygon>' +
  58. '</Intersects>' +
  59. '</Filter>';
  60. var format = new OpenLayers.Format.Filter.v1_0_0();
  61. var filter = new OpenLayers.Filter.Spatial({
  62. type: OpenLayers.Filter.Spatial.INTERSECTS,
  63. property: "Geometry",
  64. value: OpenLayers.Geometry.fromWKT("POLYGON((2488789 289552, 2588789 289552, 2588789 389552, 2488789 389552, 2488789 289552))")
  65. });
  66. // test writing
  67. var node = format.write(filter);
  68. t.xml_eq(node, str, "filter correctly written");
  69. // test reading
  70. var doc = (new OpenLayers.Format.XML).read(str);
  71. var got = format.read(doc.firstChild);
  72. t.eq(got.type, filter.type, "read correct type");
  73. t.eq(got.property, filter.property, "read correct property");
  74. t.geom_eq(got.value, filter.value, "read correct value");
  75. }
  76. function test_Within(t) {
  77. t.plan(4);
  78. var str =
  79. '<Filter xmlns="http://www.opengis.net/ogc">' +
  80. '<Within>' +
  81. '<PropertyName>Geometry</PropertyName>' +
  82. '<gml:Polygon xmlns:gml="http://www.opengis.net/gml">' +
  83. '<gml:outerBoundaryIs>' +
  84. '<gml:LinearRing>' +
  85. '<gml:coordinates decimal="." cs="," ts=" ">2488789,289552 2588789,289552 2588789,389552 2488789,389552 2488789,289552</gml:coordinates>' +
  86. '</gml:LinearRing>' +
  87. '</gml:outerBoundaryIs>' +
  88. '</gml:Polygon>' +
  89. '</Within>' +
  90. '</Filter>';
  91. var format = new OpenLayers.Format.Filter.v1_0_0();
  92. var filter = new OpenLayers.Filter.Spatial({
  93. type: OpenLayers.Filter.Spatial.WITHIN,
  94. property: "Geometry",
  95. value: OpenLayers.Geometry.fromWKT("POLYGON((2488789 289552, 2588789 289552, 2588789 389552, 2488789 389552, 2488789 289552))")
  96. });
  97. // test writing
  98. var node = format.write(filter);
  99. t.xml_eq(node, str, "filter correctly written");
  100. // test reading
  101. var doc = (new OpenLayers.Format.XML).read(str);
  102. var got = format.read(doc.firstChild);
  103. t.eq(got.type, filter.type, "read correct type");
  104. t.eq(got.property, filter.property, "read correct property");
  105. t.geom_eq(got.value, filter.value, "read correct value");
  106. }
  107. function test_Contains(t) {
  108. t.plan(4);
  109. var str =
  110. '<Filter xmlns="http://www.opengis.net/ogc">' +
  111. '<Contains>' +
  112. '<PropertyName>Geometry</PropertyName>' +
  113. '<gml:Polygon xmlns:gml="http://www.opengis.net/gml">' +
  114. '<gml:outerBoundaryIs>' +
  115. '<gml:LinearRing>' +
  116. '<gml:coordinates decimal="." cs="," ts=" ">2488789,289552 2588789,289552 2588789,389552 2488789,389552 2488789,289552</gml:coordinates>' +
  117. '</gml:LinearRing>' +
  118. '</gml:outerBoundaryIs>' +
  119. '</gml:Polygon>' +
  120. '</Contains>' +
  121. '</Filter>';
  122. var format = new OpenLayers.Format.Filter.v1_0_0();
  123. var filter = new OpenLayers.Filter.Spatial({
  124. type: OpenLayers.Filter.Spatial.CONTAINS,
  125. property: "Geometry",
  126. value: OpenLayers.Geometry.fromWKT("POLYGON((2488789 289552, 2588789 289552, 2588789 389552, 2488789 389552, 2488789 289552))")
  127. });
  128. // test writing
  129. var node = format.write(filter);
  130. t.xml_eq(node, str, "filter correctly written");
  131. // test reading
  132. var doc = (new OpenLayers.Format.XML).read(str);
  133. var got = format.read(doc.firstChild);
  134. t.eq(got.type, filter.type, "read correct type");
  135. t.eq(got.property, filter.property, "read correct property");
  136. t.geom_eq(got.value, filter.value, "read correct value");
  137. }
  138. function test_logical_fid(t) {
  139. // the Filter Encoding spec doesn't allow for FID filters inside logical filters
  140. // however, to be liberal, we will write them without complaining
  141. t.plan(3);
  142. var filter = new OpenLayers.Filter.Logical({
  143. type: OpenLayers.Filter.Logical.OR,
  144. filters: [
  145. new OpenLayers.Filter.Comparison({
  146. type: OpenLayers.Filter.Comparison.LIKE,
  147. property: "person",
  148. value: "me"
  149. }),
  150. new OpenLayers.Filter.FeatureId({fids: ["foo.1", "foo.2"]})
  151. ]
  152. });
  153. var format = new OpenLayers.Format.Filter.v1_0_0();
  154. var got = format.write(filter);
  155. var exp = readXML("LogicalFeatureId");
  156. t.xml_eq(got, exp, "wrote FID filter in logical OR without complaint");
  157. filter = new OpenLayers.Filter.Logical({
  158. type: OpenLayers.Filter.Logical.AND,
  159. filters: [
  160. new OpenLayers.Filter.Comparison({
  161. type: OpenLayers.Filter.Comparison.LIKE,
  162. property: "person",
  163. value: "me"
  164. }),
  165. new OpenLayers.Filter.FeatureId({fids: ["foo.1", "foo.2"]})
  166. ]
  167. });
  168. got = format.write(filter);
  169. exp = readXML("LogicalFeatureIdAnd");
  170. t.xml_eq(got, exp, "wrote FID filter in logical AND without complaint");
  171. filter = new OpenLayers.Filter.Logical({
  172. type: OpenLayers.Filter.Logical.NOT,
  173. filters: [
  174. new OpenLayers.Filter.FeatureId({fids: ["foo.2"]})
  175. ]
  176. });
  177. got = format.write(filter);
  178. exp = readXML("LogicalFeatureIdNot");
  179. t.xml_eq(got, exp, "wrote FID filter in logical NOT without complaint");
  180. }
  181. function readXML(id) {
  182. var xml = document.getElementById(id).firstChild.nodeValue;
  183. return new OpenLayers.Format.XML().read(xml).documentElement;
  184. }
  185. </script>
  186. </head>
  187. <body>
  188. <div id="LogicalFeatureId"><!--
  189. <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
  190. <ogc:Or>
  191. <ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">
  192. <ogc:PropertyName>person</ogc:PropertyName>
  193. <ogc:Literal>me</ogc:Literal>
  194. </ogc:PropertyIsLike>
  195. <ogc:FeatureId fid="foo.1"/>
  196. <ogc:FeatureId fid="foo.2"/>
  197. </ogc:Or>
  198. </ogc:Filter>
  199. --></div>
  200. <div id="LogicalFeatureIdAnd"><!--
  201. <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
  202. <ogc:And>
  203. <ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">
  204. <ogc:PropertyName>person</ogc:PropertyName>
  205. <ogc:Literal>me</ogc:Literal>
  206. </ogc:PropertyIsLike>
  207. <ogc:FeatureId fid="foo.1"/>
  208. <ogc:FeatureId fid="foo.2"/>
  209. </ogc:And>
  210. </ogc:Filter>
  211. --></div>
  212. <div id="LogicalFeatureIdNot"><!--
  213. <ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
  214. <ogc:Not>
  215. <ogc:FeatureId fid="foo.2"/>
  216. </ogc:Not>
  217. </ogc:Filter>
  218. --></div>
  219. </body>
  220. </html>