use of org.locationtech.geogig.api.RevFeatureType in project GeoGig by boundlessgeo.
the class ImportOp method _call.
/**
* Executes the import operation using the parameters that have been specified. Features will be
* added to the working tree, and a new working tree will be constructed. Either {@code all} or
* {@code table}, but not both, must be set prior to the import process.
*
* @return RevTree the new working tree
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected RevTree _call() {
// check preconditions and get the actual list of type names to import
final String[] typeNames = checkPreconditions();
for (int i = 0; i < typeNames.length; i++) {
try {
typeNames[i] = URLDecoder.decode(typeNames[i], Charsets.UTF_8.displayName());
} catch (UnsupportedEncodingException e) {
// shouldn't reach here.
}
}
ProgressListener progressListener = getProgressListener();
progressListener.started();
// use a local variable not to alter the command's state
boolean overwrite = this.overwrite;
if (alter) {
overwrite = false;
}
final WorkingTree workTree = workingTree();
RevFeatureType destPathFeatureType = null;
final boolean destPathProvided = destPath != null;
if (destPathProvided) {
destPathFeatureType = this.command(ResolveFeatureType.class).setRefSpec(destPath).call().orNull();
// only the last one will be imported.
if (overwrite) {
try {
workTree.delete(destPath);
} catch (Exception e) {
throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
}
overwrite = false;
}
}
int tableCount = 0;
for (String typeName : typeNames) {
{
tableCount++;
String tableName = String.format("%-16s", typeName);
if (typeName.length() > 16) {
tableName = tableName.substring(0, 13) + "...";
}
progressListener.setDescription("Importing " + tableName + " (" + tableCount + "/" + typeNames.length + ")... ");
}
FeatureSource featureSource = getFeatureSource(typeName);
SimpleFeatureType featureType = (SimpleFeatureType) featureSource.getSchema();
final String fidPrefix = featureType.getTypeName() + ".";
String path;
if (destPath == null) {
path = featureType.getTypeName();
} else {
NodeRef.checkValidPath(destPath);
path = destPath;
featureType = forceFeatureTypeName(featureType, path);
}
featureType = overrideGeometryName(featureType);
featureSource = new ForceTypeAndFidFeatureSource<FeatureType, Feature>(featureSource, featureType, fidPrefix);
boolean hasPrimaryKey = hasPrimaryKey(typeName);
boolean forbidSorting = !usePaging || !hasPrimaryKey;
((ForceTypeAndFidFeatureSource) featureSource).setForbidSorting(forbidSorting);
if (destPathFeatureType != null && adaptToDefaultFeatureType && !alter) {
featureSource = new FeatureTypeAdapterFeatureSource<FeatureType, Feature>(featureSource, destPathFeatureType.type());
}
ProgressListener taskProgress = subProgress(100.f / typeNames.length);
if (overwrite) {
try {
workTree.delete(path);
workTree.createTypeTree(path, featureType);
} catch (Exception e) {
throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
}
}
if (alter) {
// first we modify the feature type and the existing features, if needed
workTree.updateTypeTree(path, featureType);
Iterator<Feature> transformedIterator = transformFeatures(featureType, path);
try {
final Integer collectionSize = collectionSize(featureSource);
workTree.insert(path, transformedIterator, taskProgress, null, collectionSize);
} catch (Exception e) {
throw new GeoToolsOpException(StatusCode.UNABLE_TO_INSERT);
}
}
try {
insert(workTree, path, featureSource, taskProgress);
} catch (GeoToolsOpException e) {
throw e;
} catch (Exception e) {
throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
}
}
progressListener.setProgress(100.f);
progressListener.complete();
return workTree.getTree();
}
use of org.locationtech.geogig.api.RevFeatureType in project GeoGig by boundlessgeo.
the class OSMMapTest method testMappingWithNoChanges.
@Test
public void testMappingWithNoChanges() throws Exception {
String filename = OSMImportOp.class.getResource("ways.xml").getFile();
File file = new File(filename);
String mappingFilename = OSMMap.class.getResource("mapping.json").getFile();
File mappingFile = new File(mappingFilename);
cli.execute("osm", "import", file.getAbsolutePath(), "--mapping", mappingFile.getAbsolutePath());
Optional<RevFeatureType> revFeatureType = cli.getGeogig().command(ResolveFeatureType.class).setRefSpec("onewaystreets").call();
assertTrue(revFeatureType.isPresent());
cli.execute("osm", "map", mappingFile.getAbsolutePath());
}
use of org.locationtech.geogig.api.RevFeatureType in project GeoGig by boundlessgeo.
the class WorkingTree method updateTypeTree.
/**
* Updates the definition of a Feature type associated as default feature type to a given path.
* It also modifies the metadataId associated to features under the passed path, which used the
* previous default feature type.
*
* @param path the path
* @param featureType the new feature type definition to set as default for the passed path
*/
public NodeRef updateTypeTree(final String treePath, final FeatureType featureType) {
// TODO: This is not the optimal way of doing this. A better solution should be found.
final RevTree workHead = getTree();
Optional<NodeRef> typeTreeRef = context.command(FindTreeChild.class).setIndex(true).setParent(workHead).setChildPath(treePath).call();
Preconditions.checkArgument(typeTreeRef.isPresent(), "Tree does not exist: %s", treePath);
Iterator<NodeRef> iter = context.command(LsTreeOp.class).setReference(treePath).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).call();
final RevFeatureType revType = RevFeatureTypeImpl.build(featureType);
indexDatabase.put(revType);
final ObjectId metadataId = revType.getId();
RevTreeBuilder treeBuilder = new RevTreeBuilder(indexDatabase);
final RevTree newTree = treeBuilder.build();
ObjectId newWorkHeadId = context.command(WriteBack.class).setToIndex(true).setAncestor(workHead.builder(indexDatabase)).setChildPath(treePath).setTree(newTree).setMetadataId(metadataId).call();
updateWorkHead(newWorkHeadId);
Map<ObjectId, FeatureBuilder> featureBuilders = Maps.newHashMap();
while (iter.hasNext()) {
NodeRef noderef = iter.next();
RevFeature feature = context.command(RevObjectParse.class).setObjectId(noderef.objectId()).call(RevFeature.class).get();
if (!featureBuilders.containsKey(noderef.getMetadataId())) {
RevFeatureType ft = context.command(RevObjectParse.class).setObjectId(noderef.getMetadataId()).call(RevFeatureType.class).get();
featureBuilders.put(noderef.getMetadataId(), new FeatureBuilder(ft));
}
FeatureBuilder fb = featureBuilders.get(noderef.getMetadataId());
String parentPath = NodeRef.parentPath(NodeRef.appendChild(treePath, noderef.path()));
insert(parentPath, fb.build(noderef.getNode().getName(), feature));
}
return context.command(FindTreeChild.class).setIndex(true).setParent(getTree()).setChildPath(treePath).call().get();
}
use of org.locationtech.geogig.api.RevFeatureType in project GeoGig by boundlessgeo.
the class WorkingTreeTest method testCreateTypeTreeAutomaticallyWhenInsertingWitNoExistingTypeTree.
@Test
public void testCreateTypeTreeAutomaticallyWhenInsertingWitNoExistingTypeTree() throws Exception {
insert(points1, points2);
Optional<NodeRef> treeRef = repo.command(FindTreeChild.class).setChildPath(pointsName).setIndex(true).setParent(workTree.getTree()).call();
assertTrue(treeRef.isPresent());
assertTrue(treeRef.get().getNode().getMetadataId().isPresent());
assertFalse(treeRef.get().getNode().getMetadataId().get().isNull());
RevFeatureType featureType = repo.stagingDatabase().getFeatureType(treeRef.get().getMetadataId());
assertEquals(pointsType, featureType.type());
}
use of org.locationtech.geogig.api.RevFeatureType in project GeoGig by boundlessgeo.
the class WorkingTreeTest method testCreateTypeNestedNonExistingParent.
@Test
public void testCreateTypeNestedNonExistingParent() throws Exception {
NodeRef treeRef = workTree.createTypeTree("path/to/nested/type", pointsType);
assertNotNull(treeRef);
assertEquals("path/to/nested/type", treeRef.path());
assertEquals("path/to/nested", treeRef.getParentPath());
assertTrue(treeRef.getNode().getMetadataId().isPresent());
assertSame(treeRef.getMetadataId(), treeRef.getNode().getMetadataId().get());
RevFeatureType featureType = repo.stagingDatabase().getFeatureType(treeRef.getMetadataId());
assertEquals(pointsType, featureType.type());
}
Aggregations