cql-functions.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. .. _app-schema.cql-functions:
  2. CQL functions
  3. =============
  4. CQL functions enable data conversion and conditional behaviour to be specified in mapping files. Some of these functions are provided by the app-schema plugin specifically for this purpose.
  5. * The uDig manual includes a list of CQL functions:
  6. * http://udig.refractions.net/confluence/display/EN/Constraint%20Query%20Language.html
  7. * CQL string literals are enclosed in single quotes, for example ``'urn:ogc:def:nil:OGC:missing'``.
  8. * Single quotes are represented in CQL string literals as two single quotes, just as in SQL. For example, ``'yyyy-MM-dd''T''HH:mm:ss''Z'''`` for the string ``yyyy-MM-dd'T'HH:mm:ss'Z'``.
  9. Vocabulary translation
  10. ----------------------
  11. This section describes how to serve vocabulary translations using some function expressions in application schema mapping file.
  12. If you're not familiar with application schema mapping file, read :ref:`app-schema.mapping-file`.
  13. Recode
  14. ``````
  15. This is similar to *if_then_else* function, except that there is no default clause.
  16. You have to specify a translation value for every vocabulary key.
  17. **Syntax**::
  18. Recode(COLUMN_NAME, key1, value1, key2, value2,...)
  19. * **COLUMN_NAME**: column name to get values from
  20. **Example**::
  21. <AttributeMapping>
  22. <targetAttribute>gml:name</targetAttribute>
  23. <sourceExpression>
  24. <OCQL>Recode(ABBREVIATION, '1GRAV', 'urn:cgi:classifier:CGI:SimpleLithology:2008:gravel',
  25. '1TILL', 'urn:cgi:classifier:CGI:SimpleLithology:2008:diamictite',
  26. '6ALLU', 'urn:cgi:classifier:CGI:SimpleLithology:2008:sediment')
  27. </OCQL>
  28. </sourceExpression>
  29. </AttributeMapping>
  30. The above example will map **gml:name** value to *urn:cgi:classifier:CGI:SimpleLithology:2008:gravel* if the ABBREVIATION column value is *1GRAV*.
  31. Categorize
  32. ``````````
  33. This is more suitable for numeric keys, where the translation value is determined by the key's position within the thresholds.
  34. **Syntax**::
  35. Categorize(COLUMN_NAME, default_value, threshold 1, value 1, threshold 2, value 2, ..., [preceding/succeeding])
  36. * **COLUMN_NAME**: data source column name
  37. * **default_value**: default value to be mapped if COLUMN_NAME value is not within the threshold
  38. * **threshold(n)**: threshold value
  39. * **value(n)**: value to be mapped if the threshold is met
  40. * **preceding/succeeding**:
  41. - optional, succeeding is used by default if not specified.
  42. - not case sensitive.
  43. - preceding: value is within threshold if COLUMN_NAME value > threshold
  44. - succeeding: value is within threshold if COLUMN_NAME value >= threshold
  45. **Example**::
  46. <AttributeMapping>
  47. <targetAttribute>gml:description</targetAttribute>
  48. <sourceExpression>
  49. <OCQL>Categorize(CGI_LOWER_RANGE, 'missing_value', 1000, 'minor', 5000, 'significant')</OCQL>
  50. </sourceExpression>
  51. </AttributeMapping>
  52. The above example means **gml:description** value would be *significant* if CGI_LOWER_RANGE column value is >= *5000*.
  53. Vocab
  54. `````
  55. This function is more useful for bigger vocabulary pairs.
  56. Instead of writing a long key-to-value pairs in the function, you can keep them in a separate properties file.
  57. The properties file serves as a lookup table to the function. It has no header, and only contains the pairs in ''<key>=<value>'' format.
  58. **Syntax**::
  59. Vocab(COLUMN_NAME, properties file)
  60. * **COLUMN_NAME**: column name to get values from
  61. * **properties file**: absolute path of the properties file
  62. **Example:**
  63. Properties file::
  64. 1GRAV=urn:cgi:classifier:CGI:SimpleLithology:2008:gravel
  65. 1TILL=urn:cgi:classifier:CGI:SimpleLithology:2008:diamictite
  66. 6ALLU=urn:cgi:classifier:CGI:SimpleLithology:2008:sediment
  67. Mapping file::
  68. <AttributeMapping>
  69. <targetAttribute>gml:name</targetAttribute>
  70. <sourceExpression>
  71. <OCQL>Vocab(ABBREVIATION, strconcat('${config.parent}', '/mapping.properties'))</OCQL>
  72. </sourceExpression>
  73. </AttributeMapping>
  74. The above example will map **gml:name** to *urn:cgi:classifier:CGI:SimpleLithology:2008:gravel* if ABBREVIATION value is *1GRAV*.
  75. This example uses the ``config.parent`` predefined interpolation property to specify a vocabulary properties file in the same directory as the mapping file. See :ref:`app-schema.property-interpolation` for details.
  76. Geometry creation
  77. -----------------
  78. toDirectPosition
  79. ````````````````
  80. This function converts double values to ``DirectPosition`` geometry type. This is needed when the data store doesn't have geometry type columns. This function expects:
  81. Literal
  82. ``'SRS_NAME'`` (optional)
  83. Expression
  84. expression of SRS name if ``'SRS_NAME'`` is present as the first argument
  85. Expression
  86. name of column pointing to first double value
  87. Expression
  88. name of column pointing to second double value (optional, only for 2D)
  89. ToEnvelope
  90. ``````````
  91. ``ToEnvelope`` function can take in the following set of parameters and return as either ``Envelope`` or ``ReferencedEnvelope`` type:
  92. **Option 1 (1D Envelope)**::
  93. ToEnvelope(minx,maxx)
  94. **Option 2 (1D Envelope with crsname)**::
  95. ToEnvelope(minx,maxx,crsname)
  96. **Option 3 (2D Envelope)**::
  97. ToEnvelope(minx,maxx,miny,maxy)
  98. **Option 4 (2D Envelope with crsname)**::
  99. ToEnvelope(minx,maxx,miny,maxy,crsname)
  100. toPoint
  101. ```````
  102. This function converts double values to a 2D Point geometry type. This is needed when the data store doesn't have geometry type columns. This function expects:
  103. Literal
  104. ``'SRS_NAME'`` (optional)
  105. Expression
  106. expression of SRS name if ``'SRS_NAME'`` is present as the first argument
  107. Expression
  108. name of column pointing to first double value
  109. Expression
  110. name of column pointing to second double value
  111. Expression
  112. expression of gml:id (optional)
  113. toLineString
  114. ````````````
  115. This function converts double values to 1D LineString geometry type. This is needed to express 1D borehole intervals with custom (non EPSG) CRS.
  116. Literal
  117. ``'SRS_NAME'`` (EPSG code or custom SRS)
  118. Expression
  119. name of column pointing to first double value
  120. Expression
  121. name of column pointing to second double value
  122. Reference
  123. ---------
  124. toXlinkHref
  125. ```````````
  126. This function redirects an attribute to be encoded as xlink:href, instead of being encoded as a full attribute. This is useful in polymorphism, where static client property cannot be used when the encoding is conditional. This function expects:
  127. Expression
  128. REFERENCE_VALUE (could be another function or literal)
  129. Date/time formatting
  130. --------------------
  131. FormatDateTimezone
  132. ``````````````````
  133. A function to format a date/time using a `SimpleDateFormat pattern <https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html>`_ in a `time zone supported by Java <http://joda-time.sourceforge.net/timezones.html>`_. This function improves on ``dateFormat``, which formats date/time in the server time zone and can produce unintended results. Note that the term "date" is derived from a Java class name; this class represents a date/time, not just a single day.
  134. **Syntax**::
  135. FormatDateTimezone(pattern, date, timezone)
  136. pattern
  137. formatting pattern supported by `SimpleDateFormat <http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html>`_, for example ``'yyyy-MM-dd'``. Use two single quotes to include a literal single quote in a CQL string literal, for example ``'yyyy-MM-dd''T''HH:mm:ss''Z'''``.
  138. date
  139. the date/time to be formatted or its string representation, for example ``'1948-01-01T00:00:00Z'``. An exception will be returned if the date is malformed (and not null). Database types with time zone information are recommended.
  140. timezone
  141. the name of a time zone supported by Java, for example ``'UTC'`` or ``'Canada/Mountain'``. Note that unrecognised timezones will silently be converted to UTC.
  142. This function returns null if any parameter is null.
  143. This example formats date/times from a column ``POSITION`` in UTC for inclusion in a ``csml:TimeSeries``::
  144. <AttributeMapping>
  145. <targetAttribute>csml:timePositionList</targetAttribute>
  146. <sourceExpression>
  147. <OCQL>FormatDateTimezone('yyyy-MM-dd''T''HH:mm:ss''Z''', POSITION, 'UTC')</OCQL>
  148. </sourceExpression>
  149. <isList>true</isList>
  150. </AttributeMapping>