use of org.geotools.geojson.feature.FeatureJSON in project GeoGig by boundlessgeo.
the class AbstractGeoJsonCommand method getDataStore.
protected DataStore getDataStore(String geoJSON) throws FileNotFoundException, IOException {
try {
File geoJsonfile = new File(geoJSON);
checkParameter(geoJsonfile.exists(), "File does not exist '%s'", geoJsonfile);
InputStream in = new FileInputStream(geoJsonfile);
FeatureJSON fjson = new FeatureJSON();
@SuppressWarnings("rawtypes") FeatureCollection fc = fjson.readFeatureCollection(in);
@SuppressWarnings("unchecked") DataStore dataStore = new MemoryDataStore(fc);
return dataStore;
} catch (IOException ioe) {
throw new CommandFailedException("Error opening GeoJSON: " + ioe.getMessage(), ioe);
}
}
use of org.geotools.geojson.feature.FeatureJSON in project GeoGig by boundlessgeo.
the class GeoJsonExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String geojson = args.get(1);
File file = new File(geojson);
if (file.exists() && !overwrite) {
throw new CommandFailedException("The selected GeoJSON file already exists. Use -o to overwrite");
}
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 = (SimpleFeatureType) 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 dataStore = new MemoryDataStore(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.");
}
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
op.setTransactional(false);
if (defaultType) {
op.exportDefaultFeatureType();
}
FileWriter writer = null;
try {
op.setProgressListener(cli.getProgressListener()).call();
FeatureJSON fjson = new FeatureJSON();
@SuppressWarnings("rawtypes") FeatureCollection fc = featureSource.getFeatures();
writer = new FileWriter(file);
fjson.writeFeatureCollection(fc, writer);
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
file.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.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
} finally {
writer.flush();
writer.close();
}
cli.getConsole().println(path + " exported successfully to " + geojson);
}
Aggregations