use of org.opengis.feature.type.AttributeDescriptor in project GeoGig by boundlessgeo.
the class GeogigSimpleFeature method validate.
@Override
public void validate() throws IllegalAttributeException {
for (int i = 0; i < getAttributeCount(); i++) {
AttributeDescriptor descriptor = getType().getDescriptor(i);
Types.validate(descriptor, getAttribute(i));
}
}
use of org.opengis.feature.type.AttributeDescriptor in project GeoGig by boundlessgeo.
the class GeoJsonImport method runInternal.
@Override
protected void runInternal(GeogigCLI cli) throws InvalidParameterException, CommandFailedException, IOException {
checkParameter(geoJSONList != null && !geoJSONList.isEmpty(), "No GeoJSON specified");
checkParameter(geomName == null || !geomNameAuto, "Cannot use --geom-name and --geom-name-auto at the same time");
for (String geoJSON : geoJSONList) {
DataStore dataStore = null;
try {
dataStore = getDataStore(geoJSON);
} catch (InvalidParameterException e) {
cli.getConsole().println("The GeoJSON file '" + geoJSON + "' could not be found, skipping...");
continue;
}
if (fidAttribute != null) {
AttributeDescriptor attrib = dataStore.getSchema(dataStore.getNames().get(0)).getDescriptor(fidAttribute);
if (attrib == null) {
throw new InvalidParameterException("The specified attribute does not exist in the selected GeoJSON file");
}
}
if (geomNameAuto) {
String destPath = destTable;
if (destPath == null) {
destPath = dataStore.getSchema(dataStore.getNames().get(0)).getTypeName();
}
Optional<RevFeatureType> ft = cli.getGeogig().command(RevObjectParse.class).setRefSpec("WORK_HEAD:" + destPath).call(RevFeatureType.class);
// attribute
if (ft.isPresent()) {
GeometryDescriptor geomDescriptor = ft.get().type().getGeometryDescriptor();
if (geomDescriptor != null) {
geomName = geomDescriptor.getLocalName();
}
}
}
try {
cli.getConsole().println("Importing from GeoJSON " + geoJSON);
ProgressListener progressListener = cli.getProgressListener();
cli.getGeogig().command(ImportOp.class).setAll(true).setTable(null).setAlter(alter).setOverwrite(!add).setDestinationPath(destTable).setDataStore(dataStore).setFidAttribute(fidAttribute).setGeometryNameOverride(geomName).setAdaptToDefaultFeatureType(!forceFeatureType).setProgressListener(progressListener).call();
cli.getConsole().println(geoJSON + " imported successfully.");
} catch (GeoToolsOpException e) {
switch(e.statusCode) {
case NO_FEATURES_FOUND:
throw new CommandFailedException("No features were found in the GeoJSON file.", e);
case UNABLE_TO_GET_NAMES:
throw new CommandFailedException("Unable to get feature types from the GeoJSON file.", e);
case UNABLE_TO_GET_FEATURES:
throw new CommandFailedException("Unable to get features from the GeoJSON file.", e);
case UNABLE_TO_INSERT:
throw new CommandFailedException("Unable to insert features into the working tree.", e);
case INCOMPATIBLE_FEATURE_TYPE:
throw new CommandFailedException("The feature type of the data to import does not match the feature type of the destination tree and cannot be imported\n" + "USe the --force-featuretype switch to import using the original featuretype and crete a mixed type tree", e);
default:
throw new CommandFailedException("Import failed with exception: " + e.statusCode.name(), e);
}
}
}
}
use of org.opengis.feature.type.AttributeDescriptor 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.type.AttributeDescriptor 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.type.AttributeDescriptor in project GeoGig by boundlessgeo.
the class ExportDiffOpTest method testExportDiffUsingOldVersion.
@Test
public void testExportDiffUsingOldVersion() throws Exception {
insertAndAdd(points1);
final RevCommit insertCommit = geogig.command(CommitOp.class).setAll(true).call();
final String featureId = points1.getIdentifier().getID();
final Feature modifiedFeature = feature((SimpleFeatureType) points1.getType(), featureId, "changedProp", new Integer(1500));
insertAndAdd(modifiedFeature, points2);
final RevCommit changeCommit = geogig.command(CommitOp.class).setAll(true).call();
Feature[] points = new Feature[] { points1 };
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.add("geogig_fid", String.class);
for (AttributeDescriptor descriptor : pointsType.getAttributeDescriptors()) {
builder.add(descriptor);
}
builder.setName(pointsType.getName());
builder.setCRS(pointsType.getCoordinateReferenceSystem());
SimpleFeatureType outputFeatureType = builder.buildFeatureType();
MemoryDataStore dataStore = new MemoryDataStore(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(pointsName).setNewRef(changeCommit.getId().toString()).setOldRef(insertCommit.getId().toString()).setUseOld(true).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), points.length);
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, points));
}
Aggregations