Changeset 276 for tools

Show
Ignore:
Timestamp:
03/11/09 16:18:51 (12 months ago)
Author:
anton
Message:

Big cleaning at IOHelper class

Location:
tools/routingservice/trunk/src/jp/co/orkney/restlet
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • tools/routingservice/trunk/src/jp/co/orkney/restlet/geo/ClosestEdge.java

    r172 r276  
    121121                StringTemplate template = this.configuration.getFormatOut().getTemplate(); 
    122122         
    123                 result = this.ioHelper.fillMultilinestringTemplate(rs, this.configuration.getFormatOut().getName(), 
     123                result = this.ioHelper.fillTemplate(rs, this.configuration.getFormatOut().getName(), 
    124124                        template, reqId);                
    125125 
  • tools/routingservice/trunk/src/jp/co/orkney/restlet/geo/ShortestPath.java

    r252 r276  
    198198                .getTemplate(); 
    199199 
    200         result = this.ioHelper.fillMultilinestringTemplate(rs, 
     200        result = this.ioHelper.fillTemplate(rs, 
    201201                this.configuration.getFormatOut().getName(), template, reqId); 
    202202 
  • tools/routingservice/trunk/src/jp/co/orkney/restlet/geo/TravelingSalesPerson.java

    r172 r276  
    145145                StringTemplate template = this.configuration.getFormatOut().getTemplate(); 
    146146         
    147                 result = this.ioHelper.fillMultilinestringTemplate(rs, this.configuration.getFormatOut().getName(), 
     147                result = this.ioHelper.fillTemplate(rs, this.configuration.getFormatOut().getName(), 
    148148                        template, reqId); 
    149149                 
  • tools/routingservice/trunk/src/jp/co/orkney/restlet/util/IOHelper.java

    r275 r276  
    195195 
    196196        /** 
    197          * Draws the result of the closest Edge algorithm in HTML format 
    198          *  
    199          * @param resultSet 
    200          *            result of the SQL query 
    201          * @param point 
    202          *            the point provided input data 
    203          * @return result this query's result in HTML format 
    204          * @throws SQLException 
    205          *             Error to extract data from ResultSet object 
    206          */ 
    207         public String closestEdgeHTMLResult(ResultSet resultSet, Point point, String reqId) throws SQLException 
    208         { 
    209                 String result = "<html><head>" + "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + configuration.getCss() + "\" />" 
    210                                 + "</head><body><table class=\"table\"><tr>"; 
    211                 result += "<td colspan=\"2\">From the point (" + point.getX() + ";" + point.getY() + "), the closest edge is: </td></tr>"; 
    212                 if (resultSet != null && resultSet.next()) 
    213                 { 
    214                         result += "<tr class=\"line\"><td>Gid:</td><td>" + resultSet.getInt("gid") + "</td></tr>"; 
    215                         result += "<tr class=\"line\"><td>Name:</td><td>name_edge</td></tr>"; 
    216                         result += "<tr class=\"line\"><td>Source:</td><td>" + resultSet.getInt("source") + "</td></tr>"; 
    217                         result += "<tr class=\"line\"><td>Target:</td><td>" + resultSet.getInt("target") + "</td></tr>"; 
    218                         result += "<tr class=\"line\"><td>Geom:</td><td>" + resultSet.getString("wkt") + "</td></tr>"; 
    219                 } 
    220                 else 
    221                 { 
    222                         result += "<tr class=\"line\"><td>Gid:</td><td>0</td></tr>"; 
    223                         result += "<tr class=\"line\"><td>Name:</td><td>unkown_edge</td></tr>"; 
    224                         result += "<tr class=\"line\"><td>Source:</td><td>0</td></tr>"; 
    225                         result += "<tr class=\"line\"><td>Target:</td><td>0</td></tr>"; 
    226                         result += "<tr class=\"line\"><td>Geom:</td><td></td></tr>"; 
    227                         result += "<tr class=\"line\"><td>Distance:</td><td>0</td></tr>"; 
    228                 } 
    229                 result += "<tr class=\"line\"><td>Request ID:</td><td>"+reqId+"</td></tr>"; 
    230                 result += "</body></html>"; 
    231                 return result; 
    232         } 
    233  
    234         /** 
    235          * Draws the result of the closest Edge algorithm in Geojson format 
    236          *  
    237          * @param resultSet 
    238          *            result of the SQL query 
    239          * @return result this query's result in Geojson format 
    240          * @throws SQLException 
    241          *             Error to extract data from ResultSet object 
    242          * @throws JSONException 
    243          *             Error to make Geojson object 
    244          */ 
    245         public String closestEdgeGEOJSONResult(ResultSet resultSet, String reqId) throws SQLException, JSONException 
    246         { 
    247                 String result = ""; 
    248                 JSONObject joRes = new JSONObject(); 
    249                 JSONObject joGeo = new JSONObject(); 
    250                 JSONObject joPro = new JSONObject(); 
    251                 JSONObject joCrs = new JSONObject(); 
    252                 JSONObject joPro2 = new JSONObject(); 
    253                 JSONArray joCor = new JSONArray(); 
    254  
    255                 if (resultSet != null && resultSet.next()) 
    256                 { 
    257                         joRes.put("type", "Feature"); 
    258  
    259                         joPro.put("gid", resultSet.getInt("gid")); 
    260                         joPro.put("name", "name_edge"); 
    261                         joRes.put("properties", joPro); 
    262  
    263                         joGeo.put("type", "MultiLineString"); 
    264                         String tmpSplit = resultSet.getString("wkt"); 
    265                         tmpSplit = tmpSplit.split("MULTILINESTRING\\(\\(")[1].split("\\)\\)")[0]; 
    266                         String wkt[] = tmpSplit.split("\\)\\("); 
    267                         for (int i = 0; i < wkt.length; i++) 
    268                         { 
    269                                 JSONArray tmp = new JSONArray(); 
    270                                 String wkt2[] = wkt[i].split(","); 
    271  
    272                                 for (int j = 0; j < wkt2.length; j++) 
    273                                 { 
    274                                         JSONArray tmp2 = new JSONArray(); 
    275                                         tmp2.put(new BigDecimal(wkt2[j].split(" ")[0])); 
    276                                         tmp2.put(new BigDecimal(wkt2[j].split(" ")[1])); 
    277                                         tmp.put(tmp2); 
    278                                 } 
    279                                 joCor.put(tmp); 
    280                         } 
    281  
    282                         joGeo.put("coordinates", joCor); 
    283                         joRes.put("geometry", joGeo); 
    284  
    285                         joCrs.put("type", "EPSG"); 
    286                         joPro2.put("code", "900913"); 
    287                         joCrs.put("properties", joPro2); 
    288                         joRes.put("crs", joCrs); 
    289                 } 
    290                 else 
    291                 { 
    292                         joRes.put("type", "Feature"); 
    293  
    294                         joPro.put("gid", 0); 
    295                         joPro.put("name", "unkown_edge"); 
    296                         joRes.put("properties", joPro); 
    297  
    298                         joGeo.put("type", "MultiLineString"); 
    299                         JSONArray tmp = new JSONArray(); 
    300                         joCor.put(tmp); 
    301                         joGeo.put("coordinates", joCor); 
    302                         joRes.put("geometry", joGeo); 
    303  
    304                         joCrs.put("type", "EPSG"); 
    305                         joPro2.put("code", "900913"); 
    306                         joCrs.put("properties", joPro2); 
    307                         joRes.put("crs", joCrs); 
    308                 } 
    309                 joRes.put("requestId", reqId); 
    310                 result = joRes.toString(); 
    311                 return result; 
    312         } 
    313  
    314         /** 
    315197         * Draws the result of the driving distance algorithm in Geojson format 
    316198         *  
     
    529411 
    530412                return result; 
    531         } 
    532  
    533          
    534         /** 
    535          * <p> 
    536          * Returns the "OpenLayers id" provided by OpenLayers interface. "OpenLayers 
    537          * id" is required to use WebRouting Service with OpenLayers interface. 
    538          * <br /> 
    539          * Requirement: Geojson data 
    540          * </p> 
    541          *  
    542          * @param data 
    543          *            data provided by the GET or POST request 
    544          * @return if not null, returns OpenLayers id, otherwise 0 
    545          */ 
    546         public String getOpenLayersIdFromGEOJSON(String data) 
    547         { 
    548                 String openLayersId = "0"; 
    549                 try 
    550                 { 
    551                         JSONObject lePoint = new JsonRepresentation(data).toJsonObject(); 
    552                         openLayersId = (String) lePoint.getString("id"); 
    553                 } 
    554                 catch (JSONException e) 
    555                 { 
    556                         log.write("*ERROR* getOpenLayersIdFromGEOJSON()", 0); 
    557                 } 
    558  
    559                 return openLayersId; 
    560         } 
    561  
    562         /** 
    563          * <p> 
    564          * Returns the "OpenLayers id" provided by OpenLayers interface. "OpenLayers 
    565          * id" is required to use WebRouting Service with OpenLayers interface. 
    566          * <br /> 
    567          * Requirement: GML data 
    568          * </p> 
    569          *  
    570          * @param data 
    571          *            data provided by the GET or POST request 
    572          * @return if not null, returns OpenLayers id, otherwise 0 
    573          */ 
    574         public String getOpenLayersIdFromGML(String data) 
    575         { 
    576                 String openLayersId = "0"; 
    577                 try 
    578                 { 
    579                         openLayersId = data.split("fid=\"")[1].split("\"><")[0]; 
    580                 } 
    581                 catch (Exception e) 
    582                 { 
    583                         log.write("*ERROR* getOpenLayersIdFromGML()", 0); 
    584                 } 
    585  
    586                 return openLayersId; 
    587         } 
    588  
    589         /** 
    590          * <p> 
    591          * Returns the "OpenLayers id" provided by OpenLayers interface. "OpenLayers 
    592          * id" is required to use WebRouting Service with OpenLayers interface. 
    593          * <br /> 
    594          * Requirement: KML data 
    595          * </p> 
    596          *  
    597          * @param data 
    598          *            data provided by the GET or POST request 
    599          * @return if not null, returns OpenLayers id, otherwise 0 
    600          */ 
    601         public String getOpenLayersIdFromKML(String data) 
    602         { 
    603                 String openLayersId = ""; 
    604                 try 
    605                 { 
    606                         openLayersId = data.split("<Placemark><name>")[1].split("</name>")[0]; 
    607                 } 
    608                 catch (Exception e) 
    609                 { 
    610                         log.write("*ERROR* getOpenLayersIdFromKML()", 0); 
    611                 } 
    612  
    613                 return openLayersId; 
    614413        } 
    615414 
     
    710509        } 
    711510        /** 
    712          * Load a file 
     511         * Loads a file 
    713512         * @param f a file object 
    714513         * @return the contain of the file into a String variable 
     
    735534        } 
    736535         
    737         public StringTemplate loadTemplate(String filename) 
    738         { 
    739             StringTemplateGroup group = new StringTemplateGroup("formatGroup", "template"); 
    740             StringTemplate template = group.getInstanceOf(filename); 
    741             return template; 
    742         } 
    743          
    744         public String fillMultilinestringTemplate(ResultSet resultSet, String format, StringTemplate template, String reqId) throws SQLException 
     536        /** 
     537         * Fills a string template 
     538         * @param resultSet 
     539         * @param format 
     540         * @param template 
     541         * @param reqId 
     542         * @return 
     543         * @throws SQLException 
     544         */ 
     545        public String fillTemplate(ResultSet resultSet, String format, StringTemplate template, String reqId) throws SQLException 
    745546        { 
    746547                ArrayList<Edge> edges = new ArrayList<Edge>(); 
     
    757558                    edge.setGid(resultSet.getInt("gid")); 
    758559 
    759                         String wkt = resultSet.getString("wkt"); 
    760                         wkt = wkt.split("MULTILINESTRING\\(\\(")[1].split("\\)\\)")[0]; 
    761                         String wkts[] = wkt.split("\\)\\("); 
     560                         
     561                    String[] wkts = parseWKT(resultSet); 
    762562                                                 
    763563                        for (int i = 0; i < wkts.length; i++) 
     
    780580 
    781581                return st.toString(); 
     582        } 
     583 
     584        /** 
     585         * Parses a WKT geometry string to an array 
     586         * @param resultSet 
     587         * @return 
     588         * @throws SQLException 
     589         */ 
     590        private String[] parseWKT(ResultSet resultSet) 
     591                        throws SQLException { 
     592                String wkt = resultSet.getString("wkt"); 
     593                String wkts[] = null; 
     594                 
     595                if(wkt.contains("MULTILINESTRING")) 
     596                { 
     597                        wkt = wkt.split("MULTILINESTRING\\(\\(")[1].split("\\)\\)")[0]; 
     598                        wkts = wkt.split("\\)\\("); 
     599                } 
     600                else if(wkt.contains("POINT")) 
     601                { 
     602                        wkts = wkt.split("POINT\\(\\(")[1].split("\\)\\)"); 
     603                }                
     604                else if(wkt.contains("LINESTRING")) 
     605                { 
     606                        wkts = wkt.split("LINESTRING\\(\\(")[1].split("\\)\\)"); 
     607                }                
     608                 
     609                return wkts; 
    782610        }        
    783611}