Search in sources :

Example 16 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class SparseCloneTest method testFeatureMovingIntoAOI.

@Test
public void testFeatureMovingIntoAOI() throws Exception {
    Map<String, String> filter = new HashMap<String, String>();
    filter.put("Cities", "BBOX(pp,30, -125, 40, -110,'EPSG:4326')");
    createFilterFile(filter);
    // Commit several features to the remote
    List<Feature> features = Arrays.asList(city2, city1, city3, city1_modified);
    LinkedList<RevCommit> expected = new LinkedList<RevCommit>();
    Map<Feature, ObjectId> oids = new HashMap<Feature, ObjectId>();
    for (Feature f : features) {
        ObjectId oId = insertAndAdd(remoteGeogig.geogig, f);
        oids.put(f, oId);
        final RevCommit commit = remoteGeogig.geogig.command(CommitOp.class).setMessage(f.getIdentifier().toString()).call();
        expected.addFirst(commit);
        Optional<RevObject> childObject = remoteGeogig.geogig.command(RevObjectParse.class).setObjectId(oId).call();
        assertTrue(childObject.isPresent());
    }
    // Make sure the remote has all of the commits
    Iterator<RevCommit> logs = remoteGeogig.geogig.command(LogOp.class).call();
    List<RevCommit> logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(expected, logged);
    // Make sure the local repository has no commits prior to clone
    logs = localGeogig.geogig.command(LogOp.class).call();
    assertNotNull(logs);
    assertFalse(logs.hasNext());
    // clone from the remote
    CloneOp clone = clone();
    clone.setDepth(0);
    clone.setRepositoryURL(remoteGeogig.envHome.getCanonicalPath()).setBranch("master").call();
    // Cities.1 initially lies outside the filter, so the commit that adds it will not be part
    // of the sparse clone. Later the feature is moved into the AOI so it will be added at that
    // time.
    // Make sure the local repository got the correct commits
    logs = localGeogig.geogig.command(LogOp.class).call();
    logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(3, logged.size());
    assertEquals("Cities.1", logged.get(0).getMessage());
    assertFalse(expected.get(0).getId().equals(logged.get(0).getId()));
    assertEquals("Cities.3", logged.get(1).getMessage());
    assertFalse(expected.get(1).getId().equals(logged.get(1).getId()));
    assertEquals("Cities.2", logged.get(2).getMessage());
    assertTrue(expected.get(3).getId().equals(logged.get(2).getId()));
    assertExists(localGeogig, oids.get(city2), oids.get(city3), oids.get(city1_modified));
    assertNotExists(localGeogig, oids.get(city1));
}
Also used : CloneOp(org.locationtech.geogig.api.porcelain.CloneOp) HashMap(java.util.HashMap) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) LogOp(org.locationtech.geogig.api.porcelain.LogOp) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) LinkedList(java.util.LinkedList) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 17 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class SparseCloneTest method testSparseClone.

@Test
public void testSparseClone() throws Exception {
    Map<String, String> filter = new HashMap<String, String>();
    filter.put("default", "BBOX(pp,30, -125, 40, -110,'EPSG:4326')");
    filter.put("Cities", "BBOX(pp,33, -125, 40, -110,'EPSG:4326')");
    createFilterFile(filter);
    // Commit several features to the remote
    List<Feature> features = Arrays.asList(city1, city2, city3, road1, road2, road3);
    LinkedList<RevCommit> expected = new LinkedList<RevCommit>();
    Map<Feature, ObjectId> oids = new HashMap<Feature, ObjectId>();
    for (Feature f : features) {
        ObjectId oId = insertAndAdd(remoteGeogig.geogig, f);
        oids.put(f, oId);
        final RevCommit commit = remoteGeogig.geogig.command(CommitOp.class).setMessage(f.getIdentifier().toString()).call();
        expected.addFirst(commit);
        Optional<RevObject> childObject = remoteGeogig.geogig.command(RevObjectParse.class).setObjectId(oId).call();
        assertTrue(childObject.isPresent());
    }
    // Make sure the remote has all of the commits
    Iterator<RevCommit> logs = remoteGeogig.geogig.command(LogOp.class).call();
    List<RevCommit> logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(expected, logged);
    // Make sure the local repository has no commits prior to clone
    logs = localGeogig.geogig.command(LogOp.class).call();
    assertNotNull(logs);
    assertFalse(logs.hasNext());
    // clone from the remote
    CloneOp clone = clone();
    clone.setDepth(0);
    clone.setRepositoryURL(remoteGeogig.envHome.getCanonicalPath()).setBranch("master").call();
    // The features that match the filter are "Cities.3", "Roads.1", "Roads.2", and "Roads.3",
    // the "Cities.1" commit should be present since it added the "Cities" tree, but "Cities.1"
    // should not be present in the tree.
    // Make sure the local repository got the correct commits
    logs = localGeogig.geogig.command(LogOp.class).call();
    logged = new ArrayList<RevCommit>();
    for (; logs.hasNext(); ) {
        logged.add(logs.next());
    }
    assertEquals(5, logged.size());
    assertEquals("Roads.3", logged.get(0).getMessage());
    assertFalse(expected.get(0).getId().equals(logged.get(0).getId()));
    assertEquals("Roads.2", logged.get(1).getMessage());
    assertFalse(expected.get(1).getId().equals(logged.get(1).getId()));
    assertEquals("Roads.1", logged.get(2).getMessage());
    assertFalse(expected.get(2).getId().equals(logged.get(2).getId()));
    assertEquals("Cities.3", logged.get(3).getMessage());
    assertFalse(expected.get(3).getId().equals(logged.get(3).getId()));
    assertEquals("Cities.1", logged.get(4).getMessage());
    assertFalse(expected.get(5).getId().equals(logged.get(4).getId()));
    assertExists(localGeogig, oids.get(city3), oids.get(road1), oids.get(road2), oids.get(road3));
    assertNotExists(localGeogig, oids.get(city1), oids.get(city2));
}
Also used : CloneOp(org.locationtech.geogig.api.porcelain.CloneOp) HashMap(java.util.HashMap) ObjectId(org.locationtech.geogig.api.ObjectId) RevObject(org.locationtech.geogig.api.RevObject) LogOp(org.locationtech.geogig.api.porcelain.LogOp) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) LinkedList(java.util.LinkedList) RevCommit(org.locationtech.geogig.api.RevCommit) Test(org.junit.Test)

Example 18 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class RemoteRepositoryTestCase method populate.

protected List<RevCommit> populate(GeoGIG geogig, boolean oneCommitPerFeature, List<Feature> features) throws Exception {
    List<RevCommit> commits = new ArrayList<RevCommit>();
    for (Feature f : features) {
        insertAndAdd(geogig, f);
        if (oneCommitPerFeature) {
            RevCommit commit = geogig.command(CommitOp.class).call();
            commits.add(commit);
        }
    }
    if (!oneCommitPerFeature) {
        RevCommit commit = geogig.command(CommitOp.class).call();
        commits.add(commit);
    }
    return commits;
}
Also used : ArrayList(java.util.ArrayList) CommitOp(org.locationtech.geogig.api.porcelain.CommitOp) Feature(org.opengis.feature.Feature) RevCommit(org.locationtech.geogig.api.RevCommit)

Example 19 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class RemoteRepositoryTestCase method boundsOf.

/**
     * Computes the aggregated bounds of {@code features} in the {@code targetCrs}
     */
protected ReferencedEnvelope boundsOf(CoordinateReferenceSystem targetCrs, Feature... features) throws Exception {
    ReferencedEnvelope bounds = new ReferencedEnvelope(targetCrs);
    for (int i = 0; i < features.length; i++) {
        Feature f = features[i];
        BoundingBox fbounds = f.getBounds();
        if (!CRS.equalsIgnoreMetadata(targetCrs, fbounds)) {
            fbounds = fbounds.toBounds(targetCrs);
        }
        bounds.include(fbounds);
    }
    return bounds;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) BoundingBox(org.opengis.geometry.BoundingBox) Feature(org.opengis.feature.Feature)

Example 20 with Feature

use of org.opengis.feature.Feature in project GeoGig by boundlessgeo.

the class ShpExport 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 shapefile = args.get(1);
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    File targetShapefile = new File(shapefile);
    if (!targetShapefile.isAbsolute()) {
        File pwd = cli.getGeogig().getPlatform().pwd();
        String relativePath = targetShapefile.getPath();
        targetShapefile = new File(pwd, relativePath);
    }
    if (targetShapefile.exists() && !overwrite) {
        throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
    }
    Map<String, Serializable> params = new HashMap<String, Serializable>();
    URL targetShapefileAsUrl = targetShapefile.toURI().toURL();
    params.put(ShapefileDataStoreFactory.URLP.key, targetShapefileAsUrl);
    params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
    params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
    ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
    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.createSchema(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.");
    }
    Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
    final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter).setFeatureTypeConversionFunction(function);
    // shapefile transactions are memory bound, so avoid them
    op.setTransactional(false);
    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) {
        targetShapefile.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);
        }
    }
    cli.getConsole().println(path + " exported successfully to " + shapefile);
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) URL(java.net.URL) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Aggregations

Feature (org.opengis.feature.Feature)128 Test (org.junit.Test)90 RevCommit (org.locationtech.geogig.api.RevCommit)52 CommitOp (org.locationtech.geogig.api.porcelain.CommitOp)43 SimpleFeature (org.opengis.feature.simple.SimpleFeature)38 ObjectId (org.locationtech.geogig.api.ObjectId)35 RevFeature (org.locationtech.geogig.api.RevFeature)32 NodeRef (org.locationtech.geogig.api.NodeRef)29 LinkedList (java.util.LinkedList)27 LogOp (org.locationtech.geogig.api.porcelain.LogOp)23 Ref (org.locationtech.geogig.api.Ref)21 RefParse (org.locationtech.geogig.api.plumbing.RefParse)19 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)18 ArrayList (java.util.ArrayList)16 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)16 RevObject (org.locationtech.geogig.api.RevObject)16 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)14 Function (com.google.common.base.Function)13 Optional (com.google.common.base.Optional)12 HashMap (java.util.HashMap)12