/
ShpExport.java
251 lines (216 loc) · 10.6 KB
/
ShpExport.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Copyright (c) 2013-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.geotools.cli.shp;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.jdt.annotation.Nullable;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.ValidatingFeatureFactoryImpl;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.locationtech.geogig.cli.CLICommand;
import org.locationtech.geogig.cli.CommandFailedException;
import org.locationtech.geogig.cli.GeogigCLI;
import org.locationtech.geogig.cli.InvalidParameterException;
import org.locationtech.geogig.cli.annotation.ReadOnly;
import org.locationtech.geogig.geotools.adapt.GT;
import org.locationtech.geogig.geotools.plumbing.ExportOp;
import org.locationtech.geogig.geotools.plumbing.GeoToolsOpException;
import org.locationtech.geogig.model.NodeRef;
import org.locationtech.geogig.model.ObjectId;
import org.locationtech.geogig.model.RevFeatureType;
import org.locationtech.geogig.model.RevObject;
import org.locationtech.geogig.model.RevObject.TYPE;
import org.locationtech.geogig.model.RevTree;
import org.locationtech.geogig.plumbing.FindTreeChild;
import org.locationtech.geogig.plumbing.ResolveObjectType;
import org.locationtech.geogig.plumbing.ResolveTreeish;
import org.locationtech.geogig.plumbing.RevObjectParse;
import org.locationtech.geogig.plumbing.RevParse;
import org.locationtech.geogig.repository.impl.GeoGIG;
import org.opengis.feature.Feature;
import org.opengis.feature.GeometryAttribute;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeatureType;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
/**
* Exports features from a feature type into a shapefile.
*
* @see ExportOp
*/
@ReadOnly
@Command(name = "export", description = "Export to Shapefile")
public class ShpExport extends AbstractShpCommand implements CLICommand {
@Parameters(description = "<path> <shapefile>", arity = "2")
public List<String> args = new ArrayList<>();
@Option(names = { "--overwrite", "-o" }, description = "Overwrite output file")
public boolean overwrite;
@Option(names = {
"--defaulttype" }, description = "Export only features with the tree default feature type if several types are found")
public boolean defaultType;
@Option(names = {
"--alter" }, description = "Export all features if several types are found, altering them to adapt to the output feature type")
public boolean alter;
@Option(names = {
"--featuretype" }, description = "Export only features with the specified feature type if several types are found")
@Nullable
public String sFeatureTypeId;
/**
* Charset to use for encoding attributes in DBF file
*/
@Option(names = {
"--charset" }, description = "Use the specified charset to encode attributes. Default is ISO-8859-1.")
public String charset = "ISO-8859-1";
/**
* Executes the export command using the provided options.
*/
protected @Override void runInternal(GeogigCLI cli) throws IOException {
String path = args.get(0);
String shapefile = args.get(1);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
File targetShapefile = new File(shapefile);
if (!targetShapefile.isAbsolute()) {
File pwd = cli.getGeogig().getPlatform().pwd();
String relativePath = targetShapefile.getPath();
targetShapefile = new File(pwd, relativePath);
}
if (targetShapefile.exists() && !overwrite) {
throw new CommandFailedException(
"The selected shapefile already exists. Use -o to overwrite");
}
Map<String, Serializable> params = new HashMap<String, Serializable>();
URL targetShapefileAsUrl = targetShapefile.toURI().toURL();
params.put(ShapefileDataStoreFactory.URLP.key, targetShapefileAsUrl);
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
params.put(ShapefileDataStoreFactory.DBFCHARSET.key, charset);
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory
.createNewDataStore(params);
SimpleFeatureType outputFeatureType;
ObjectId featureTypeId;
if (sFeatureTypeId != null) {
// Check the feature type id string is a correct id
Optional<ObjectId> id = cli.getGeogig().command(RevParse.class)
.setRefSpec(sFeatureTypeId).call();
checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get())
.call();
checkParameter(type.equals(TYPE.FEATURETYPE),
"Provided reference does not resolve to a feature type: ", sFeatureTypeId);
outputFeatureType = GT.adapt(cli.getGeogig().command(RevObjectParse.class)
.setObjectId(id.get()).call(RevFeatureType.class).get().type());
featureTypeId = id.get();
} else {
try {
outputFeatureType = getFeatureType(path, cli);
featureTypeId = null;
} catch (GeoToolsOpException e) {
cli.getConsole().println("No features to export.");
return;
}
}
dataStore.createSchema(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Could not create feature store.");
}
Function<Feature, Optional<Feature>> function = getTransformingFunction(
dataStore.getSchema());
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore)
.setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter)
.setFeatureTypeConversionFunction(function);
// shapefile transactions are memory bound, so avoid them
op.setTransactional(false);
if (defaultType) {
op.exportDefaultFeatureType();
}
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
targetShapefile.delete();
switch (e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException(
"Error: The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.",
true);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(),
e);
}
}
cli.getConsole().println(path + " exported successfully to " + shapefile);
}
private Function<Feature, Optional<Feature>> getTransformingFunction(
final SimpleFeatureType featureType) {
Function<Feature, Optional<Feature>> function = (feature) -> {
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType,
new ValidatingFeatureFactoryImpl());
for (Property property : feature.getProperties()) {
if (property instanceof GeometryAttribute) {
builder.set(featureType.getGeometryDescriptor().getName(), property.getValue());
} else {
String name = property.getName().getLocalPart();
if (name.length() > 10) {
name = name.substring(0, 10);
}
builder.set(name, property.getValue());
}
}
Feature modifiedFeature = builder.buildFeature(feature.getIdentifier().getID());
return Optional.ofNullable(modifiedFeature);
};
return function;
}
private SimpleFeatureType getFeatureType(String path, GeogigCLI cli) {
checkParameter(path != null, "No path specified.");
String refspec;
if (path.contains(":")) {
refspec = path;
} else {
refspec = "WORK_HEAD:" + path;
}
checkParameter(!refspec.endsWith(":"), "No path specified.");
final GeoGIG geogig = cli.getGeogig();
Optional<ObjectId> rootTreeId = geogig.command(ResolveTreeish.class)
.setTreeish(refspec.split(":")[0]).call();
checkParameter(rootTreeId.isPresent(),
"Couldn't resolve '" + refspec + "' to a treeish object");
RevTree rootTree = geogig.getRepository().context().objectDatabase()
.getTree(rootTreeId.get());
Optional<NodeRef> featureTypeTree = geogig.command(FindTreeChild.class)
.setChildPath(refspec.split(":")[1]).setParent(rootTree).call();
checkParameter(featureTypeTree.isPresent(),
"pathspec '" + refspec.split(":")[1] + "' did not match any valid path");
Optional<RevObject> revObject = cli.getGeogig().command(RevObjectParse.class)
.setObjectId(featureTypeTree.get().getMetadataId()).call();
if (revObject.isPresent() && revObject.get() instanceof RevFeatureType) {
RevFeatureType revFeatureType = (RevFeatureType) revObject.get();
return GT.adapt(revFeatureType.type());
} else {
throw new InvalidParameterException("Cannot find feature type for the specified path");
}
}
}