Showing posts with label CQL. Show all posts
Showing posts with label CQL. Show all posts

Tuesday, May 12, 2015

Translate CQL filter to OGC XML using GEOTOOLS and JAVA servlet

In many cases you may want to translate a cql filter into its xml representation.

For example cql filter -->
ID > 5

would be tranlated to-->
<?xml version="1.0" encoding="UTF-8"?>
<ogc:PropertyIsGreaterThan 
xmlns="http://www.opengis.net/ogc" 
xmlns:ogc="http://www.opengis.net/ogc" 
xmlns:gml="http://www.opengis.net/gml">
<ogc:PropertyName>ID</ogc:PropertyName>
<ogc:Literal>5</ogc:Literal>
</ogc:PropertyIsGreaterThan>


We are going to create a Java servlet to get the cql filter as parameter and output its xml represantation.


CODE for our servlet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/** 
* Servlet to convert a cql syntax like query 
* to xml ogc compatitable format 
* and send it back to the client for further manipulation 
* 
* @parameter String accepts one parameter which is the cql filter 
* @returns XML String which is the ogc xml representation of the supplied cql 
* If the cql syntax is false returns the error code and info 
*/
package freeopengis.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.xml.transform.TransformerException;
import org.geotools.filter.FilterTransformer;
import org.opengis.filter.Filter;
import org.geotools.filter.text.cql2.CQL;
import org.geotools.filter.text.cql2.CQLException;
import org.geotools.filter.text.ecql.ECQL;

/** 
* 
* @author pt 
*/@WebServlet(name = "CqlToXmlFilter", urlPatterns = {"/cqltoxmlfilter"})
public class CqlToXmlFilter extends HttpServlet {        
private static final String CONTENT_TYPE = "text/html; charset=UTF-8";    
private static final Logger logger = LoggerFactory.getLogger(CqlToXmlFilter.class);    
private static final long serialVersionUID = 1L;           
@Override    
public void init(ServletConfig config) throws ServletException {        
super.init(config);    
}   
    
@Override    
public void doPost(HttpServletRequest request,                       
HttpServletResponse response) throws ServletException,                                                            IOException {        
response.setContentType(CONTENT_TYPE);        
PrintWriter out = response.getWriter();        
try  {          //get the cql string as paramater.          
String cqlQuery = request.getParameter("cqlQuery");          
//e.g --> String cqlQuery = "id is not null";          
logger.debug(cqlQuery);         
//In some cases CQL class does not support the cql syntax. Such case is when using IN operator 
// In such cases we need to use the ECQL class
// to support the IN operator.         
Boolean b = cqlQuery.matches("(?i).* in .*");         
Filter filter;         
if (b==true)         
{         
filter = ECQL.toFilter(cqlQuery);         
}         
else         
{         
filter = CQL.toFilter(cqlQuery);         
}         
FilterTransformer transform = new FilterTransformer();         
transform.setIndentation(2);         
String xml = transform.transform( filter );         
out.println(xml);         
out.flush();         
out.close();        
} catch (CQLException | TransformerException ex) {                
logger.error("error = ", ex);        
out.println(ex);        
out.flush();        
out.close();        
}    
}

}

Urban Growth Lab - UGLab

Proud to release a first version of the UGLab project .  UGLab is a python open source project capable to execute Urban Growth predictions f...