root/tools/routingservice/branches/wrs-2.0/src/handler/ResourceHandler.java

Revision 340, 3.1 KB (checked in by anton, 14 months ago)

WRS code cleanup

Line 
1/*  WRS 2.0
2 *  Copyright (C) 2009 Anton Patrushev
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18package handler;
19
20import java.util.ArrayList;
21import java.util.HashMap;
22
23import org.geotools.geometry.jts.JTS;
24import org.geotools.referencing.CRS;
25
26import org.opengis.geometry.MismatchedDimensionException;
27import org.opengis.referencing.FactoryException;
28import org.opengis.referencing.NoSuchAuthorityCodeException;
29import org.opengis.referencing.crs.CoordinateReferenceSystem;
30import org.opengis.referencing.operation.MathTransform;
31import org.opengis.referencing.operation.TransformException;
32
33import com.vividsolutions.jts.geom.Geometry;
34import com.vividsolutions.jts.io.ParseException;
35import com.vividsolutions.jts.io.WKTReader;
36
37import exception.InvalidServiceException;
38import geometry.Point;
39
40import model.Resource;
41import model.Service;
42import util.IOHelper;
43import util.Log;
44import util.Parameter;
45import util.ServiceRequest;
46
47public abstract class ResourceHandler
48{
49        protected enum DataTypes
50        {
51                STRING, INTEGER, DOUBLE, GEOMETRY, BOOLEAN;
52        }       
53       
54        protected enum AggregateTypes
55        {
56                MAX, MIN, AVG, SUM, COUNT;
57        }       
58       
59        public abstract void setResource(Resource resource);
60
61        public abstract boolean checkService(Service service);
62
63        public ArrayList<HashMap<String, Parameter>> handle(ServiceRequest request,
64                        Log log) throws InvalidServiceException
65        {
66                if (checkService(request.getService()))
67                {
68                        return handleRequest(request, log);
69                }
70                else
71                {
72                        throw new InvalidServiceException(request.getService().getName());
73                }
74        }
75
76        protected abstract ArrayList<HashMap<String, Parameter>> handleRequest(
77                        ServiceRequest request, Log log);
78
79        public abstract String fillQueryTemplate(String template, Service service,
80                        HashMap<String, Parameter> parameters);
81       
82        public abstract ArrayList<Point> parseGeometry(String geometry);
83       
84        public String[] transformPoint(String x, String y, String srid_in, String srid_out) throws NoSuchAuthorityCodeException, FactoryException, ParseException, MismatchedDimensionException, TransformException
85        {
86                CoordinateReferenceSystem sourceCRS = CRS.decode(srid_in);
87                CoordinateReferenceSystem targetCRS = CRS.decode(srid_out);
88                MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
89               
90                Geometry point = new WKTReader().read("POINT ("+x+" "+y+")");
91                Geometry targetPoint = JTS.transform(point, transform);
92               
93                String[] result = {IOHelper.FORMATTER.format(targetPoint.getCoordinate().x), IOHelper.FORMATTER.format(targetPoint.getCoordinate().y)};
94
95                return result;
96        }
97
98}
Note: See TracBrowser for help on using the browser.