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