]> git.argeo.org Git - lgpl/argeo-commons.git/blob - gis/runtime/org.argeo.gis.geotools/src/main/java/org/argeo/geotools/jcr/GeoJcrUtils.java
Improve GIS
[lgpl/argeo-commons.git] / gis / runtime / org.argeo.gis.geotools / src / main / java / org / argeo / geotools / jcr / GeoJcrUtils.java
1 package org.argeo.geotools.jcr;
2
3 import javax.jcr.Node;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.argeo.ArgeoException;
8 import org.argeo.jcr.gis.GisNames;
9 import org.argeo.jcr.gis.GisTypes;
10 import org.geotools.geometry.DirectPosition2D;
11 import org.geotools.geometry.jts.JTS;
12 import org.geotools.referencing.CRS;
13 import org.opengis.geometry.DirectPosition;
14 import org.opengis.referencing.crs.CoordinateReferenceSystem;
15 import org.opengis.referencing.operation.MathTransform;
16
17 import com.vividsolutions.jts.geom.Coordinate;
18 import com.vividsolutions.jts.geom.Geometry;
19 import com.vividsolutions.jts.geom.GeometryFactory;
20 import com.vividsolutions.jts.geom.Point;
21
22 /** Utilities to map JCR from/to JTS and GeoTools */
23 public class GeoJcrUtils {
24 private final static Log log = LogFactory.getLog(GeoJcrUtils.class);
25
26 /** Transforms a geometry node into position within its CRS */
27 public static DirectPosition nodeToPosition(Node node) {
28 try {
29 if (node.isNodeType(GisTypes.GIS_POINT)) {
30 CoordinateReferenceSystem crs = getCoordinateReferenceSystem(node);
31 Point point = (Point) nodeToGeometry(node);
32 return new DirectPosition2D(crs, point.getX(), point.getY());
33 } else {
34 throw new ArgeoException(node + " is not of a supported type");
35 }
36 } catch (Exception e) {
37 throw new ArgeoException("Cannot extract position from " + node, e);
38 }
39 }
40
41 /** Transforms a geometry node into a JTS geometry. */
42 public static Geometry nodeToGeometry(Node node) {
43 try {
44 if (node.isNodeType(GisTypes.GIS_POINT)
45 || node.isNodeType(GisTypes.GIS_COORDINATE)) {
46 Coordinate coo;
47 if (node.hasProperty(GisNames.GIS_Z))
48 coo = new Coordinate(node.getProperty(GisNames.GIS_X)
49 .getDouble(), node.getProperty(GisNames.GIS_Y)
50 .getDouble(), node.getProperty(GisNames.GIS_Z)
51 .getDouble());
52 else
53 coo = new Coordinate(node.getProperty(GisNames.GIS_X)
54 .getDouble(), node.getProperty(GisNames.GIS_Y)
55 .getDouble());
56
57 // TODO: use factory finder
58 // GeometryFactory geometryFactory =
59 // JTSFactoryFinder.getGeometryFactory(null);
60 GeometryFactory geometryFactory = new GeometryFactory();
61 return geometryFactory.createPoint(coo);
62 } else {
63 throw new ArgeoException(node + " is not of a supported type");
64 }
65 } catch (Exception e) {
66 throw new ArgeoException("Cannot map " + node + " to a geometry", e);
67 }
68 }
69
70 /** Reads and interpret the coordinate reference system from a node. */
71 public static CoordinateReferenceSystem getCoordinateReferenceSystem(
72 Node node) {
73 try {
74 if (!node.isNodeType(GisTypes.GIS_LOCATED))
75 throw new ArgeoException(node + " is not of type "
76 + GisTypes.GIS_LOCATED);
77 // Coordinate reference system
78 String srs = node.getProperty(GisNames.GIS_SRS).getString();
79 CoordinateReferenceSystem crs;
80 try {
81 // first, try to decode an EPSG code
82 crs = CRS.decode(srs);
83 } catch (Exception e) {
84 // if it fails, try a WKT
85 try {
86 crs = CRS.parseWKT(srs);
87 } catch (Exception e1) {
88 // if it fails as well, log the error
89 log.error("Cannot parse WKT " + srs, e1);
90 // and then the previous error (probably more relevant)
91 throw e;
92 }
93 }
94 return crs;
95 } catch (Exception e) {
96 throw new ArgeoException(
97 "Cannot get coordinate reference system for " + node, e);
98 }
99 }
100
101 public static Geometry reproject(CoordinateReferenceSystem crs,
102 Geometry geometry, CoordinateReferenceSystem targetCrs) {
103 try {
104 MathTransform transform = CRS.findMathTransform(crs, targetCrs);
105 return JTS.transform(geometry, transform);
106 } catch (Exception e) {
107 throw new ArgeoException("Cannot reproject " + geometry + " from "
108 + crs + " to " + targetCrs);
109 }
110 }
111 }