Changeset 306 for tools

Show
Ignore:
Timestamp:
04/23/09 14:47:18 (11 months ago)
Author:
anton
Message:

Geometry transformation added

Location:
tools/routingservice/branches/wrs-2.0
Files:
8 added
8 modified

Legend:

Unmodified
Added
Removed
  • tools/routingservice/branches/wrs-2.0/data/resources.xml

    r305 r306  
    66                <description>pgRouting 1.0.1, PostGIS 1.11, PostgreSQL 8.1</description> 
    77                <parameters> 
    8                         <parameter name="srid" type="integer" key="srid" value="4326" /> 
     8                        <parameter name="srid" type="integer" key="srid" value="EPSG:4326" /> 
    99                        <parameter name="units" type="string" key="units" value="dd" /> 
    1010                        <parameter name="rbox" type="double" key="box" value="0.1" /> 
  • tools/routingservice/branches/wrs-2.0/src/WRS.java

    r305 r306  
    3737 
    3838import org.antlr.stringtemplate.StringTemplate; 
     39import org.geotools.referencing.CRS; 
     40import org.opengis.referencing.FactoryException; 
     41import org.opengis.referencing.NoSuchAuthorityCodeException; 
     42import org.opengis.referencing.crs.CoordinateReferenceSystem; 
    3943import org.restlet.Component; 
    4044import org.restlet.Restlet; 
     
    186190                                                { 
    187191                                                        ResourceHandler handler = HandlerFactory.Handler 
    188                                                                         .valueOf(resource.getName().toUpperCase()) 
     192                                                                        .valueOf(resource.getType().toUpperCase()) 
    189193                                                                        .getInstance(); 
    190194                                                        handler.setResource(resource); 
     
    476480                        { 
    477481                                Include inc = li.next(); 
    478  
     482                                 
    479483                                switch (Includes.valueOf(inc.getName().toUpperCase())) 
    480484                                { 
     
    511515                return conf; 
    512516        } 
    513  
     517         
    514518        private void readResources(String url) 
    515519        { 
  • tools/routingservice/branches/wrs-2.0/src/geometry/Point.java

    r298 r306  
    2222public class Point 
    2323{ 
    24         private BigDecimal x; 
    25         private BigDecimal y; 
     24        private double x; 
     25        private double y; 
    2626 
    2727        public Point() 
    2828        { 
    29                 x = new BigDecimal(0); 
    30                 y = new BigDecimal(0); 
     29                x = 0; 
     30                y = 0; 
    3131        } 
    3232 
    33         public Point(BigDecimal x, BigDecimal y) 
     33        public Point(double x, double y) 
    3434        { 
    3535                this.x = x; 
     
    3737        } 
    3838 
    39         public BigDecimal getX() 
     39        public double getX() 
    4040        { 
    4141                return x; 
    4242        } 
    4343 
    44         public BigDecimal getX(float value) 
    45         { 
    46                 return x.add(new BigDecimal(value)); 
    47         } 
    4844 
    49         public BigDecimal getY() 
     45        public double getY() 
    5046        { 
    5147                return y; 
    5248        } 
    5349 
    54         public BigDecimal getY(float value) 
    55         { 
    56                 return y.add(new BigDecimal(value)); 
    57         } 
    58  
    59         public void setX(BigDecimal x) 
     50        public void setX(double x) 
    6051        { 
    6152                this.x = x; 
    6253        } 
    6354 
    64         public void setY(BigDecimal y) 
     55        public void setY(double y) 
    6556        { 
    6657                this.y = y; 
  • tools/routingservice/branches/wrs-2.0/src/handler/HandlerFactory.java

    r298 r306  
    2222        public static enum Handler 
    2323        { 
    24                 PGROUTING(PgRoutingHandler.class); 
     24                DATABASE(PgRoutingHandler.class); 
    2525 
    2626                private Class<? extends ResourceHandler> mappedClass; 
  • tools/routingservice/branches/wrs-2.0/src/handler/PgRoutingHandler.java

    r305 r306  
    4242 
    4343import org.antlr.stringtemplate.StringTemplate; 
     44import org.opengis.geometry.MismatchedDimensionException; 
     45import org.opengis.referencing.FactoryException; 
     46import org.opengis.referencing.NoSuchAuthorityCodeException; 
     47import org.opengis.referencing.operation.TransformException; 
     48 
     49import com.vividsolutions.jts.io.ParseException; 
    4450 
    4551public class PgRoutingHandler extends ResourceHandler 
     
    7177                        ArrayList getParameters() 
    7278                        { 
    73                                 String[] p = { "table", "x1", "y1", "distance", "rbox", "cost_value", 
    74                                                 "reverse_cost_value", "directed", "hasrc" }; 
     79                                String[] p = { "table", "x1", "y1", "distance", "rbox", 
     80                                                "cost_value", "reverse_cost_value", "directed", "hasrc" }; 
    7581                                return new ArrayList(Arrays.asList(p)); 
    7682                        } 
     
    100106                        String query = fillQueryTemplate(this.resource.getQuery(), request 
    101107                                        .getService(), request.getParameters()); 
    102                          
     108 
    103109                        System.out.println("Query:" + query); 
    104110 
     
    202208                                Parameter p = parameters.get(key); 
    203209                                String value = p.getValue(); 
     210 
     211                                // Check if we need to re-project coordinates 
     212                                if (p.getType().toLowerCase().equals(DOUBLE) 
     213                                                && key.toLowerCase().startsWith("x")) 
     214                                { 
     215                                        try 
     216                                        { 
     217                                                int srid_in = Integer.parseInt(parameters 
     218                                                                .get("srid_in").getValue()); 
     219                                                int srid_out = Integer.parseInt(resource 
     220                                                                .getParameters().get("srid").getValue()); 
     221 
     222                                                if (srid_in != srid_out) 
     223                                                { 
     224                                                        // try to find Y coordinate 
     225                                                        String yKey = key.toLowerCase().replaceFirst("x", 
     226                                                                        "y"); 
     227                                                        Parameter yP = parameters.get(yKey); 
     228                                                        String yValue = yP.getValue(); 
     229                                                        String[] transformed = transformPoint(value, yValue, 
     230                                                                        srid_in, srid_out); 
     231                                                        p.setValue(transformed[0]); 
     232                                                        yP.setValue(transformed[1]); 
     233                                                } 
     234                                        } 
     235                                        catch (NullPointerException e) 
     236                                        { 
     237                                                //Can't find srid_in or srid_out 
     238                                                //Do nothing 
     239                                        } 
     240                                        catch (MismatchedDimensionException e) 
     241                                        { 
     242                                                // TODO Auto-generated catch block 
     243                                                e.printStackTrace(); 
     244                                        } 
     245                                        catch (NoSuchAuthorityCodeException e) 
     246                                        { 
     247                                                // TODO Auto-generated catch block 
     248                                                e.printStackTrace(); 
     249                                        } 
     250                                        catch (FactoryException e) 
     251                                        { 
     252                                                // TODO Auto-generated catch block 
     253                                                e.printStackTrace(); 
     254                                        } 
     255                                        catch (ParseException e) 
     256                                        { 
     257                                                // TODO Auto-generated catch block 
     258                                                e.printStackTrace(); 
     259                                        } 
     260                                        catch (TransformException e) 
     261                                        { 
     262                                                // TODO Auto-generated catch block 
     263                                                e.printStackTrace(); 
     264                                        } 
     265                                } 
     266 
    204267                                if (p.getType().toLowerCase().equals(STRING)) 
    205268                                { 
     
    242305                        { 
    243306                                Point point = new Point(); 
    244                                 point.setX(new BigDecimal(wkt2[j].split(" ")[0])); 
    245                                 point.setY(new BigDecimal(wkt2[j].split(" ")[1])); 
     307                                point.setX(Double.parseDouble(wkt2[j].split(" ")[0])); 
     308                                point.setY(Double.parseDouble(wkt2[j].split(" ")[1])); 
    246309                                points.add(point); 
    247310                        } 
  • tools/routingservice/branches/wrs-2.0/src/handler/ResourceHandler.java

    r298 r306  
    2222import java.util.Hashtable; 
    2323import java.util.Vector; 
     24 
     25import org.geotools.geometry.jts.JTS; 
     26import org.geotools.referencing.CRS; 
     27 
     28import org.opengis.geometry.MismatchedDimensionException; 
     29import org.opengis.referencing.FactoryException; 
     30import org.opengis.referencing.NoSuchAuthorityCodeException; 
     31import org.opengis.referencing.crs.CoordinateReferenceSystem; 
     32import org.opengis.referencing.operation.MathTransform; 
     33import org.opengis.referencing.operation.TransformException; 
     34 
     35import com.vividsolutions.jts.geom.Geometry; 
     36import com.vividsolutions.jts.io.ParseException; 
     37import com.vividsolutions.jts.io.WKTReader; 
    2438 
    2539import exception.InvalidServiceException; 
     
    6579         
    6680        public abstract ArrayList<Point> parseGeometry(String geometry); 
     81         
     82        public String[] transformPoint(String x, String y, int srid_in, int srid_out) throws NoSuchAuthorityCodeException, FactoryException, ParseException, MismatchedDimensionException, TransformException 
     83        { 
     84                CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:"+srid_in); 
     85                CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:"+srid_out); 
     86                MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true); 
     87                 
     88                Geometry point = new WKTReader().read("POINT ("+x+" "+y+")"); 
     89                Geometry targetPoint = JTS.transform( point, transform); 
     90                 
     91                String[] result = {String.valueOf(targetPoint.getCoordinate().x), String.valueOf(targetPoint.getCoordinate().y)}; 
     92                return result; 
     93        } 
    6794 
    6895} 
  • tools/routingservice/branches/wrs-2.0/src/util/format/JSONParser.java

    r298 r306  
    5959                                                x.setName("x" + i); 
    6060                                                x.setSource("url"); 
    61                                                 x.setValue(sst.nextToken()); 
     61                                                x.setValue(sst.nextToken().trim()); 
    6262                                                x.setType(ResourceHandler.DOUBLE); 
    6363 
     
    6767                                                y.setName("y" + i); 
    6868                                                y.setSource("url"); 
    69                                                 y.setValue(sst.nextToken()); 
     69                                                y.setValue(sst.nextToken().trim()); 
    7070                                                y.setType(ResourceHandler.DOUBLE); 
    7171 
    72                                                 params.add(x); 
    7372                                                params.add(y); 
    7473                                                ++i; 
     
    7675                                        break; 
    7776                                case DISTANCE: 
    78                                         String distance = json.getString(key); 
     77                                        String distance = json.getString(key).trim(); 
    7978                                        Parameter dist = new Parameter(); 
    8079                                        dist.setName("distance"); 
     
    8382                                        dist.setType(ResourceHandler.DOUBLE); 
    8483                                        params.add(dist); 
     84                                        break; 
     85                                case CRS: 
     86                                        String crs = json.getString(key); 
     87                                        StringTokenizer crsst = new StringTokenizer(crs, ","); 
     88                                         
     89                                        Parameter in = new Parameter(); 
     90                                        in.setName("srid_in"); 
     91                                        in.setSource("url"); 
     92                                        in.setValue(crsst.nextToken().trim()); 
     93                                        in.setType(ResourceHandler.INTEGER); 
     94 
     95                                        params.add(in); 
     96                                         
     97                                        if (crsst.hasMoreTokens()) 
     98                                        { 
     99                                                Parameter out = new Parameter(); 
     100                                                out.setName("srid_out"); 
     101                                                out.setSource("url"); 
     102                                                out.setValue(crsst.nextToken().trim()); 
     103                                                out.setType(ResourceHandler.INTEGER); 
     104 
     105                                                params.add(out); 
     106                                        } 
    85107                                        break; 
    86108                                } 
  • tools/routingservice/branches/wrs-2.0/src/util/format/TemplateFiller.java

    r298 r306  
    2424import java.util.HashMap; 
    2525import java.util.Iterator; 
     26import java.util.ListIterator; 
    2627 
    2728import org.antlr.stringtemplate.StringTemplate; 
     29import org.opengis.geometry.MismatchedDimensionException; 
     30import org.opengis.referencing.FactoryException; 
     31import org.opengis.referencing.NoSuchAuthorityCodeException; 
     32import org.opengis.referencing.operation.TransformException; 
     33 
     34import com.vividsolutions.jts.io.ParseException; 
    2835 
    2936import util.Log; 
     
    3441{ 
    3542        protected abstract String getGeometryString(ArrayList<Point> points); 
    36          
     43 
    3744        public String fillOutputTemplate(ServiceRequest serviceRequest, 
    38                         ArrayList<HashMap<String, Parameter>> output, ResourceHandler handler, Log log) 
     45                        ArrayList<HashMap<String, Parameter>> output, 
     46                        ResourceHandler handler, Log log) 
    3947        { 
    4048                StringTemplate template = new StringTemplate(serviceRequest 
     
    4351                ArrayList<HashMap<String, String>> out = new ArrayList<HashMap<String, String>>(); 
    4452                Iterator<HashMap<String, Parameter>> it = output.iterator(); 
    45                 while(it.hasNext()) 
     53                while (it.hasNext()) 
    4654                { 
    4755                        HashMap<String, Parameter> paraMap = it.next(); 
    4856                        HashMap<String, String> stringMap = new HashMap<String, String>(); 
    4957                        Iterator<String> keys = paraMap.keySet().iterator(); 
    50                         while(keys.hasNext()) 
     58                        while (keys.hasNext()) 
    5159                        { 
    5260                                String key = keys.next(); 
    5361                                Parameter p = paraMap.get(key); 
    5462                                String value = p.getValue(); 
    55                                 //Geometry string needs to be handled in a special way 
    56                                 if(p.getType().toLowerCase().equals(ResourceHandler.GEOMETRY)) 
     63                                // Geometry string needs to be handled in a special way 
     64                                if (p.getType().toLowerCase().equals(ResourceHandler.GEOMETRY)) 
    5765                                { 
    58                                         value = getGeometryString(handler.parseGeometry(value)); 
     66                                        ArrayList<Point> points = handler.parseGeometry(value); 
     67                                        try 
     68                                        { 
     69                                                //Check if we need to re-project points 
     70                                                int srid_in = Integer.parseInt(serviceRequest.getResource() 
     71                                                                .getParameters().get("srid").getValue()); 
     72                                                int srid_out = Integer.parseInt(serviceRequest 
     73                                                                .getParameters().get("srid_out").getValue()); 
     74                                                if (srid_in != srid_out) 
     75                                                { 
     76                                                        ListIterator<Point> pi = points.listIterator(); 
     77                                                        while(pi.hasNext()) 
     78                                                        { 
     79                                                                Point point = pi.next(); 
     80                                                                String x = String.valueOf(point.getX()); 
     81                                                                String y = String.valueOf(point.getY()); 
     82                                                                String[] transformed = handler.transformPoint(x, y, srid_in, srid_out); 
     83                                                                point.setX(Double.parseDouble(transformed[0])); 
     84                                                                point.setY(Double.parseDouble(transformed[1])); 
     85                                                        } 
     86                                                } 
     87                                        } 
     88                                        catch (NullPointerException e) 
     89                                        { 
     90                                                //Can't find srid_in or srid_out 
     91                                                //Do nothing 
     92                                        } 
     93                                        catch (MismatchedDimensionException e) 
     94                                        { 
     95                                                // TODO Auto-generated catch block 
     96                                                e.printStackTrace(); 
     97                                        } 
     98                                        catch (NoSuchAuthorityCodeException e) 
     99                                        { 
     100                                                // TODO Auto-generated catch block 
     101                                                e.printStackTrace(); 
     102                                        } 
     103                                        catch (FactoryException e) 
     104                                        { 
     105                                                // TODO Auto-generated catch block 
     106                                                e.printStackTrace(); 
     107                                        } 
     108                                        catch (ParseException e) 
     109                                        { 
     110                                                // TODO Auto-generated catch block 
     111                                                e.printStackTrace(); 
     112                                        } 
     113                                        catch (TransformException e) 
     114                                        { 
     115                                                // TODO Auto-generated catch block 
     116                                                e.printStackTrace(); 
     117                                        } 
     118 
     119                                        value = getGeometryString(points); 
    59120                                } 
    60121                                stringMap.put(key, value);