| 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 | |
|---|
| 18 | package handler; |
|---|
| 19 | |
|---|
| 20 | import java.util.ArrayList; |
|---|
| 21 | import java.util.HashMap; |
|---|
| 22 | |
|---|
| 23 | import org.geotools.geometry.jts.JTS; |
|---|
| 24 | import org.geotools.referencing.CRS; |
|---|
| 25 | |
|---|
| 26 | import org.opengis.geometry.MismatchedDimensionException; |
|---|
| 27 | import org.opengis.referencing.FactoryException; |
|---|
| 28 | import org.opengis.referencing.NoSuchAuthorityCodeException; |
|---|
| 29 | import org.opengis.referencing.crs.CoordinateReferenceSystem; |
|---|
| 30 | import org.opengis.referencing.operation.MathTransform; |
|---|
| 31 | import org.opengis.referencing.operation.TransformException; |
|---|
| 32 | |
|---|
| 33 | import com.vividsolutions.jts.geom.Geometry; |
|---|
| 34 | import com.vividsolutions.jts.io.ParseException; |
|---|
| 35 | import com.vividsolutions.jts.io.WKTReader; |
|---|
| 36 | |
|---|
| 37 | import exception.InvalidServiceException; |
|---|
| 38 | import geometry.Point; |
|---|
| 39 | |
|---|
| 40 | import model.Resource; |
|---|
| 41 | import model.Service; |
|---|
| 42 | import util.IOHelper; |
|---|
| 43 | import util.Log; |
|---|
| 44 | import util.Parameter; |
|---|
| 45 | import util.ServiceRequest; |
|---|
| 46 | |
|---|
| 47 | public 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 | } |
|---|