/
SpatialOps.java
224 lines (209 loc) · 8.18 KB
/
SpatialOps.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* Copyright (c) 2012-2016 Boundless and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/edl-v10.html
*
* Contributors:
* Gabriel Roldan (Boundless) - initial implementation
*/
package org.locationtech.geogig.repository.impl;
import java.util.List;
import org.eclipse.jdt.annotation.Nullable;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.locationtech.geogig.data.EPSGBoundsCalc;
import org.locationtech.geogig.model.Node;
import org.locationtech.geogig.model.RevFeature;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.porcelain.CRSException;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.geometry.BoundingBox;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import com.google.common.base.Splitter;
/**
* Utility methods to deal with various spatial operations
*
*/
public class SpatialOps {
private static final GeometryFactory gfac = new GeometryFactory();
/**
* @param oldObject
* @param newObject
* @return the aggregated bounding box
*/
public static org.locationtech.jts.geom.Envelope aggregatedBounds(Node oldObject,
Node newObject) {
Envelope env = new Envelope();
if (oldObject != null) {
oldObject.expand(env);
}
if (newObject != null) {
newObject.expand(env);
}
return env;
}
/**
* Creates and returns a geometry out of bounds (a point if bounds.getSpan(0) ==
* bounds.getSpan(1) == 0D, a polygon otherwise), setting the bounds
* {@link BoundingBox#getCoordinateReferenceSystem() CRS} as the geometry's
* {@link Geometry#getUserData() user data}.
*
* @param bounds the bounding box to build from
* @return the newly constructed geometry
*/
public static Geometry toGeometry(final BoundingBox bounds) {
if (bounds == null) {
return null;
}
Geometry geom;
if (bounds.getSpan(0) == 0D && bounds.getSpan(1) == 0D) {
geom = gfac.createPoint(new Coordinate(bounds.getMinX(), bounds.getMinY()));
} else {
geom = JTS.toGeometry(bounds, gfac);
}
geom.setUserData(bounds.getCoordinateReferenceSystem());
return geom;
}
public static Envelope boundsOf(RevTree tree) {
Envelope env = new Envelope();
tree.forEachBucket(bucket -> bucket.expand(env));
tree.forEachTree(n -> n.expand(env));
tree.forEachFeature(n -> n.expand(env));
return env;
}
@Nullable
public static Envelope boundsOf(RevFeature feat) {
Envelope env = new Envelope();
feat.forEach((o) -> {
if (o instanceof Geometry) {
env.expandToInclude(((Geometry) o).getEnvelopeInternal());
}
});
return env.isNull() ? null : env;
}
/**
* Get the bounds of the desired CRS, uses JTS ReferencedEnvelope transform to properly handle
* polar projections
*
* @param crs the target CoordinateReferenceSystem
* @return bounds an Envelope containing the CRS bounds, throws a NoSuchAuthorityCodeException
* if the crs cannot be found
*/
public @Nullable static Envelope boundsOf(CoordinateReferenceSystem crs) throws Exception {
crs = findKnownCrs(crs);
return EPSGBoundsCalc.getExtents(crs);
}
/**
* Parses a bounding box in the format {@code <minx,miny,maxx,maxy,SRS>} where SRS is an EPSG
* code like {@code EPSG:4325} etc.
* <p>
* The oridinates must be given in "longitude first" format, and the SRS will be decoded the
* same way.
*
* @throws IllegalArgumentException if the argument doesn't match the expected format, or the
* SRS can't be parsed.
*/
@Nullable
public static ReferencedEnvelope parseBBOX(final @Nullable String bboxArg) {
if (bboxArg == null) {
return null;
}
List<String> split = Splitter.on(',').omitEmptyStrings().splitToList(bboxArg);
if (split.size() != 5) {
throw new IllegalArgumentException(String.format(
"Invalid bbox parameter: '%s'. Expected format: <minx,miny,maxx,maxy,CRS>",
bboxArg));
}
double minx;
double miny;
double maxx;
double maxy;
try {
minx = Double.parseDouble(split.get(0));
miny = Double.parseDouble(split.get(1));
maxx = Double.parseDouble(split.get(2));
maxy = Double.parseDouble(split.get(3));
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException(String.format(
"Invalid bbox parameter: '%s'. Expected format: <minx,miny,maxx,maxy,CRS>",
bboxArg));
}
final String srs = split.get(4);
final CoordinateReferenceSystem crs;
try {
crs = CRS.decode(srs, true);
} catch (FactoryException e) {
throw new IllegalArgumentException(String
.format("Invalid bbox parameter: '%s'. Can't parse CRS '%s'", bboxArg, srs));
}
ReferencedEnvelope env = new ReferencedEnvelope(minx, maxx, miny, maxy, crs);
return env;
}
/**
* Parses a bounding box in the format {@code <minx,miny,maxx,maxy>}
* <p>
* The oridinates must be given in "longitude first" format
*
* @throws IllegalArgumentException if the argument doesn't match the expected format
*/
@Nullable
public static Envelope parseNonReferencedBBOX(final @Nullable String bboxArg) {
if (bboxArg == null) {
return null;
}
List<String> split = Splitter.on(',').omitEmptyStrings().splitToList(bboxArg);
if (split.size() != 4) {
throw new IllegalArgumentException(String.format(
"Invalid bbox parameter: '%s'. Expected format: <minx,miny,maxx,maxy>",
bboxArg));
}
double minx;
double miny;
double maxx;
double maxy;
try {
minx = Double.parseDouble(split.get(0));
miny = Double.parseDouble(split.get(1));
maxx = Double.parseDouble(split.get(2));
maxy = Double.parseDouble(split.get(3));
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException(String.format(
"Invalid bbox parameter: '%s'. Expected format: <minx,miny,maxx,maxy>",
bboxArg));
}
Envelope env = new Envelope(minx, maxx, miny, maxy);
return env;
}
public static CoordinateReferenceSystem findIdentifier(GeometryDescriptor geometryDescriptor)
throws FactoryException, CRSException {
if (geometryDescriptor != null) {
CoordinateReferenceSystem crs = geometryDescriptor.getCoordinateReferenceSystem();
return findKnownCrs(crs);
}
return null;
}
public static CoordinateReferenceSystem findKnownCrs(CoordinateReferenceSystem crs)
throws FactoryException, CRSException {
String srs = CRS.toSRS(crs);
if (srs != null && !srs.startsWith("EPSG:")) {
boolean fullScan = true;
String knownIdentifier;
knownIdentifier = CRS.lookupIdentifier(crs, fullScan);
if (knownIdentifier != null) {
boolean longitudeFirst = CRS.getAxisOrder(crs).equals(CRS.AxisOrder.EAST_NORTH);
crs = CRS.decode(knownIdentifier, longitudeFirst);
} else {
throw new CRSException(
"Could not find identifier associated with the defined CRS: \n" + crs);
}
}
return crs;
}
}