Search in sources :

Example 1 with QuadTreeData

use of org.apache.sis.index.tree.QuadTreeData in project sis by apache.

the class LocationServlet method doGet.

/**
 * Provide GET requests for Bounding-box and Point-radius search queries.
 * Return search results to client in xml format.
 *
 * @param request
 *          Http Servlet Request
 * @param response
 *          Http Servlet Response
 * @exception ServletException
 *              General exception for servlet
 * @exception IOException
 *              General exception for I/O
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long beforeTime = 0;
    long afterTime = 0;
    response.setContentType("text/xml");
    PrintWriter out = response.getWriter();
    String type = request.getParameter("type");
    List<QuadTreeData> results = new ArrayList<QuadTreeData>();
    List<String> regions = new ArrayList<String>();
    if (type != null && type.equals("bbox")) {
        String llLat = request.getParameter("llLat");
        String llLon = request.getParameter("llLon");
        String urLat = request.getParameter("urLat");
        String urLon = request.getParameter("urLon");
        if (llLat != null && llLon != null && urLat != null && urLon != null) {
            try {
                Envelope2D bbox = new Envelope2D(new DirectPosition2D(Double.parseDouble(llLon), Double.parseDouble(llLat)), new DirectPosition2D(Double.parseDouble(urLon), Double.parseDouble(urLat)));
                beforeTime = System.currentTimeMillis();
                results = tree.queryByBoundingBox(bbox);
                afterTime = System.currentTimeMillis();
                // get the polygon that approximates the region
                Rectangle2D[] rects = bbox.toRectangles();
                for (int i = 0; i < rects.length; i++) {
                    final Rectangle2D r = rects[i];
                    String regionStr = (r.getMinY()) + "," + (r.getMinX()) + ",";
                    regionStr += (r.getMaxY()) + "," + (r.getMinX()) + ",";
                    regionStr += (r.getMaxY()) + "," + (r.getMaxX()) + ",";
                    regionStr += (r.getMinY()) + "," + (r.getMaxX()) + ",";
                    regionStr += (r.getMinY()) + "," + (r.getMinX());
                    regions.add(regionStr);
                }
            } catch (NumberFormatException ex) {
                System.out.println("[ERROR] Input parameters were not valid latitudes and longitudes");
            }
        }
    } else if (type != null && type.equals("pointradius")) {
        String radius = request.getParameter("radius");
        String lat = request.getParameter("lat");
        String lon = request.getParameter("lon");
        if (radius != null && lat != null && lon != null) {
            DirectPosition2D point = null;
            try {
                point = new DirectPosition2D(Double.parseDouble(lon), Double.parseDouble(lat));
            } catch (NumberFormatException ex) {
                System.out.println("{ERROR] Input parameters were not valid latitudes and longitudes");
            }
            double radiusKM = Double.parseDouble(radius);
            String regionStr = "";
            for (int i = 0; i < 360; i += 10) {
                DirectPosition2D pt = DistanceUtils.getPointOnGreatCircle(point.y, point.x, radiusKM, i);
                regionStr += pt.y + "," + pt.x + ",";
            }
            DirectPosition2D pt = DistanceUtils.getPointOnGreatCircle(point.y, point.x, radiusKM, 0);
            regionStr += pt.y + "," + pt.x + ",";
            regions.add(regionStr.substring(0, regionStr.length() - 1));
            beforeTime = System.currentTimeMillis();
            results = tree.queryByPointRadius(point, radiusKM);
            afterTime = System.currentTimeMillis();
        }
    }
    long timeSeconds = afterTime - beforeTime;
    // return matches from tree in xml format to client
    out.write(buildXML(results, regions, timeSeconds));
    out.close();
}
Also used : ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) Envelope2D(org.apache.sis.geometry.Envelope2D) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) QuadTreeData(org.apache.sis.index.tree.QuadTreeData) PrintWriter(java.io.PrintWriter)

Example 2 with QuadTreeData

use of org.apache.sis.index.tree.QuadTreeData in project sis by apache.

the class LocationServlet method buildXML.

/**
 * Builds the XML file to return to client.
 *
 * @param filterList
 *          list of QuadTreeData that are within the search region
 * @param regions
 *          the String coordinate representation of the search region
 * @param time
 *          the time it took to execute the query
 * @return XML string
 */
private String buildXML(final List<QuadTreeData> filterList, final List<String> regions, final long time) {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element root = doc.createElement("root");
        doc.appendChild(root);
        for (QuadTreeData geo : filterList) {
            Element item = doc.createElement("item");
            Element id = doc.createElement("id");
            Text idText = doc.createTextNode(geo.getFileName());
            id.appendChild(idText);
            item.appendChild(id);
            Element lat = doc.createElement("lat");
            Text latText = doc.createTextNode(Double.toString(geo.getLatLon().y));
            lat.appendChild(latText);
            item.appendChild(lat);
            Element lon = doc.createElement("lon");
            Text lonText = doc.createTextNode(Double.toString(geo.getLatLon().x));
            lon.appendChild(lonText);
            item.appendChild(lon);
            root.appendChild(item);
        }
        Element timeElem = doc.createElement("time");
        Text timeText = doc.createTextNode(Long.toString(time));
        timeElem.appendChild(timeText);
        root.appendChild(timeElem);
        if (timeToLoad != null) {
            Element indexLoadTimeElem = doc.createElement("indexLoadTime");
            Text indexLoadTimeText = doc.createTextNode(timeToLoad);
            indexLoadTimeElem.appendChild(indexLoadTimeText);
            root.appendChild(indexLoadTimeElem);
            // Only need to send this over to the client
            timeToLoad = null;
        // on initial load
        }
        Element query = doc.createElement("query");
        root.appendChild(query);
        for (String rStr : regions) {
            Element region = doc.createElement("region");
            Text regionText = doc.createTextNode(rStr);
            region.appendChild(regionText);
            query.appendChild(region);
        }
        try {
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            return sw.toString();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) QuadTreeData(org.apache.sis.index.tree.QuadTreeData) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

QuadTreeData (org.apache.sis.index.tree.QuadTreeData)2 Rectangle2D (java.awt.geom.Rectangle2D)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 DirectPosition2D (org.apache.sis.geometry.DirectPosition2D)1 Envelope2D (org.apache.sis.geometry.Envelope2D)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Text (org.w3c.dom.Text)1