use of org.opengis.feature.simple.SimpleFeatureType in project GeoGig by boundlessgeo.
the class GeoJsonExport method getFeatureType.
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().getTree(rootTreeId.get());
Optional<NodeRef> featureTypeTree = geogig.command(FindTreeChild.class).setChildPath(refspec.split(":")[1]).setParent(rootTree).setIndex(true).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();
if (revFeatureType.type() instanceof SimpleFeatureType) {
return (SimpleFeatureType) revFeatureType.type();
} else {
throw new InvalidParameterException("Cannot find feature type for the specified path");
}
} else {
throw new InvalidParameterException("Cannot find feature type for the specified path");
}
}
use of org.opengis.feature.simple.SimpleFeatureType in project GeoGig by boundlessgeo.
the class SLExport method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.isEmpty()) {
printUsage(cli);
throw new CommandFailedException();
}
String path = args.get(0);
String tableName = args.get(1);
checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
DataStore dataStore = getDataStore();
ObjectId featureTypeId = null;
if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
SimpleFeatureType outputFeatureType;
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 {
SimpleFeatureType sft = getFeatureType(path, cli);
outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
} catch (GeoToolsOpException e) {
throw new CommandFailedException("No features to export.", e);
}
}
try {
dataStore.createSchema(outputFeatureType);
} catch (IOException e) {
throw new CommandFailedException("Cannot create new table in database", e);
}
} else {
if (!overwrite) {
throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
}
}
SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Can't write to the selected table");
}
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
if (overwrite) {
try {
featureStore.removeFeatures(Filter.INCLUDE);
} catch (IOException e) {
throw new CommandFailedException("Error truncating table: " + e.getMessage(), e);
}
}
ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
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) {
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("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);
}
}
cli.getConsole().println(path + " exported successfully to " + tableName);
}
use of org.opengis.feature.simple.SimpleFeatureType in project GeoGig by boundlessgeo.
the class Insert method createFeature.
private Feature createFeature(List<String> featureChanges, Map<String, SimpleFeatureBuilder> featureTypes) {
String path = featureChanges.get(0);
String tree = NodeRef.parentPath(path);
String featureId = NodeRef.nodeFromPath(path);
if (!featureTypes.containsKey(tree)) {
Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class).setRefSpec("WORK_HEAD:" + tree).call();
checkParameter(opt.isPresent(), "The parent tree does not exist: " + tree);
SimpleFeatureBuilder builder = new SimpleFeatureBuilder((SimpleFeatureType) opt.get().type());
featureTypes.put(tree, builder);
}
SimpleFeatureBuilder ftb = featureTypes.get(tree);
SimpleFeatureType ft = ftb.getFeatureType();
for (int i = 1; i < featureChanges.size(); i++) {
String[] tokens = featureChanges.get(i).split("\t");
Preconditions.checkArgument(tokens.length == 2, "Wrong attribute definition: " + featureChanges.get(i));
String fieldName = tokens[0];
AttributeDescriptor desc = ft.getDescriptor(fieldName);
Preconditions.checkNotNull(desc, "Wrong attribute in feature description");
FieldType type = FieldType.forBinding(desc.getType().getBinding());
Object value = TextValueSerializer.fromString(type, tokens[1]);
ftb.set(tokens[0], value);
}
return ftb.buildFeature(featureId);
}
use of org.opengis.feature.simple.SimpleFeatureType in project GeoGig by boundlessgeo.
the class FormatCommonV2 method readFeatureType.
public static RevFeatureType readFeatureType(ObjectId id, DataInput in, FeatureTypeFactory typeFactory) throws IOException {
Name name = readName(in);
int propertyCount = readUnsignedVarInt(in);
List<AttributeDescriptor> attributes = new ArrayList<AttributeDescriptor>();
for (int i = 0; i < propertyCount; i++) {
attributes.add(readAttributeDescriptor(in, typeFactory));
}
SimpleFeatureType ftype = typeFactory.createSimpleFeatureType(name, attributes, null, false, Collections.<Filter>emptyList(), BasicFeatureTypes.FEATURE, null);
return new RevFeatureTypeImpl(id, ftype);
}
use of org.opengis.feature.simple.SimpleFeatureType in project GeoGig by boundlessgeo.
the class MergeOpTest method testMergeWithPolygonAutoMerge.
@Test
public void testMergeWithPolygonAutoMerge() throws Exception {
String polyId = "polyId";
String polygonTypeSpec = "poly:Polygon:srid=4326";
SimpleFeatureType polygonType = DataUtilities.createType("http://geogig.polygon", "polygons", polygonTypeSpec);
Feature polygonOriginal = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0,3 0,4 0,5 0,5 1,4 1,3 1,2 1,1 1,1 0,0 0))");
insertAndAdd(polygonOriginal);
geogig.command(CommitOp.class).call();
geogig.command(BranchCreateOp.class).setName("TestBranch").call();
Feature polygonMaster = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0.2,3 0.2,4 0,5 0,5 1,4 1,3 1,2 1,1 1,1 0,0 0))");
insertAndAdd(polygonMaster);
geogig.command(CommitOp.class).call();
geogig.command(CheckoutOp.class).setSource("TestBranch").call();
Feature polygonBranch = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0,3 0,4 0,5 0,5 1,4 1,3 0.8,2 0.8,1 1,1 0,0 0))");
insertAndAdd(polygonBranch);
geogig.command(CommitOp.class).call();
geogig.command(CheckoutOp.class).setSource("master").call();
Ref branch = geogig.command(RefParse.class).setName("TestBranch").call().get();
geogig.command(MergeOp.class).addCommit(Suppliers.ofInstance(branch.getObjectId())).call();
Optional<RevFeature> feature = repo.command(RevObjectParse.class).setRefSpec("WORK_HEAD:polygons/polyId").call(RevFeature.class);
assertTrue(feature.isPresent());
RevFeature merged = feature.get();
Feature expected = feature(polygonType, polyId, "POLYGON((0 0,1 0,2 0.2,3 0.2,4 0,5 0,5 1,4 1,3 0.8,2 0.8,1 1,1 0,0 0))");
assertEquals(expected.getProperty("poly").getValue(), merged.getValues().get(0).get());
}
Aggregations