Search in sources :

Example 71 with RevFeatureType

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();
}
Also used : ForwardingFeatureSource(org.locationtech.geogig.api.data.ForwardingFeatureSource) JDBCFeatureSource(org.geotools.jdbc.JDBCFeatureSource) FeatureSource(org.geotools.data.FeatureSource) ResolveFeatureType(org.locationtech.geogig.api.plumbing.ResolveFeatureType) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureType(org.opengis.feature.type.FeatureType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) DecoratingFeature(org.geotools.feature.DecoratingFeature) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) WorkingTree(org.locationtech.geogig.repository.WorkingTree) ProgressListener(org.locationtech.geogig.api.ProgressListener) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 72 with RevFeatureType

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());
}
Also used : OSMImportOp(org.locationtech.geogig.osm.internal.OSMImportOp) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Example 73 with RevFeatureType

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();
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) ObjectId(org.locationtech.geogig.api.ObjectId) RevTreeBuilder(org.locationtech.geogig.api.RevTreeBuilder) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) NodeRef(org.locationtech.geogig.api.NodeRef) LsTreeOp(org.locationtech.geogig.api.plumbing.LsTreeOp) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevTree(org.locationtech.geogig.api.RevTree)

Example 74 with RevFeatureType

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());
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Example 75 with RevFeatureType

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());
}
Also used : NodeRef(org.locationtech.geogig.api.NodeRef) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Aggregations

RevFeatureType (org.locationtech.geogig.api.RevFeatureType)88 RevFeature (org.locationtech.geogig.api.RevFeature)49 NodeRef (org.locationtech.geogig.api.NodeRef)40 ObjectId (org.locationtech.geogig.api.ObjectId)34 Test (org.junit.Test)31 Optional (com.google.common.base.Optional)28 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)26 RevObject (org.locationtech.geogig.api.RevObject)24 RevTree (org.locationtech.geogig.api.RevTree)24 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)22 SimpleFeature (org.opengis.feature.simple.SimpleFeature)19 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)17 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)14 ImmutableList (com.google.common.collect.ImmutableList)13 File (java.io.File)13 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)13 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)13 List (java.util.List)12 GeoGIG (org.locationtech.geogig.api.GeoGIG)12 AddOp (org.locationtech.geogig.api.porcelain.AddOp)12