/
RevFeatureTypeBuilder.java
81 lines (70 loc) · 3.21 KB
/
RevFeatureTypeBuilder.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
/* Copyright (c) 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.model;
import static com.google.common.base.Preconditions.checkArgument;
import static org.geotools.referencing.crs.DefaultGeographicCRS.WGS84;
import javax.annotation.Nullable;
import org.geotools.data.DataUtilities;
import org.geotools.feature.SchemaException;
import org.geotools.referencing.CRS;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.FeatureType;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import lombok.NonNull;
import lombok.Setter;
import lombok.experimental.Accessors;
public @Accessors(fluent = true) class RevFeatureTypeBuilder {
private @Setter @Nullable ObjectId id;
private @Setter FeatureType type;
RevFeatureTypeBuilder() {
//
}
/**
* Builds a {@link RevFeatureType} for the given feature type, computing its id throuh
* {@link HashObject}
*/
public RevFeatureType build() {
return build(id, type);
}
/**
* Creates and returns a new {@link RevFeatureType} for the given FeatureType with the given
* {@link ObjectId id} without verifying the SHA-1 matches the contents of the
* {@link RevFeatureType}
*/
public RevFeatureType build(@Nullable ObjectId id, @NonNull FeatureType ftype) {
FeatureType featureType = adapt(ftype);
ObjectId oid = id == null ? HashObjectFunnels.hashFeatureType(featureType) : id;
return RevObjectFactory.defaultInstance().createFeatureType(oid, featureType);
}
// GeoTools treats DefaultGeographic.WGS84 as a special case when calling the
// CRS.toSRS() method, and that causes the parsed RevFeatureType to hash differently.
// To compensate that, we replace any instance of it with a CRS built using the
// EPSG:4326 code, which works consistently when storing it and later recovering it from
// the database.
private static FeatureType adapt(FeatureType featureType) {
CoordinateReferenceSystem defaultCrs = featureType.getCoordinateReferenceSystem();
final boolean compareMetadata = false;
if (null != defaultCrs && WGS84.equals(
(org.geotools.referencing.AbstractIdentifiedObject) defaultCrs, compareMetadata)) {
checkArgument(featureType instanceof SimpleFeatureType);
try {
final boolean longitudeFirst = true;
CoordinateReferenceSystem epsg4326 = CRS.decode("EPSG:4326", longitudeFirst);
String[] includeAllAttributes = null;
featureType = DataUtilities.createSubType((SimpleFeatureType) featureType,
includeAllAttributes, epsg4326);
} catch (FactoryException | SchemaException e) {
throw new RuntimeException(e);
}
}
return featureType;
}
}