Search in sources :

Example 11 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class RevFeatureTextSerializationTest method testMalformedSerializedObject.

@Test
public void testMalformedSerializedObject() throws Exception {
    // a wrong value
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
    writer.write(TYPE.FEATURE.name() + "\n");
    writer.write(FieldType.FLOAT.name() + "\tNUMBER" + "\n");
    writer.flush();
    ObjectReader<RevFeature> reader = factory.createFeatureReader();
    try {
        reader.read(ObjectId.forString("ID_STRING"), new ByteArrayInputStream(out.toByteArray()));
        fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("wrong value"));
    }
    // an unrecognized class
    out = new ByteArrayOutputStream();
    writer = new OutputStreamWriter(out, "UTF-8");
    writer.write(TYPE.FEATURE.name() + "\n");
    writer.write(this.getClass().getName() + "\tvalue" + "\n");
    writer.flush();
    try {
        reader.read(ObjectId.forString("ID_STRING"), new ByteArrayInputStream(out.toByteArray()));
        fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Wrong type name"));
    }
    // a wrong category
    out = new ByteArrayOutputStream();
    writer = new OutputStreamWriter(out, "UTF-8");
    writer.write(TYPE.COMMIT.name() + "\n");
    writer.flush();
    reader = factory.createFeatureReader();
    try {
        reader.read(ObjectId.forString("ID_STRING"), new ByteArrayInputStream(out.toByteArray()));
        fail();
    } catch (Exception e) {
        assertTrue(e.getMessage().equals("Wrong type: COMMIT"));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RevFeature(org.locationtech.geogig.api.RevFeature) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevFeatureSerializationTest(org.locationtech.geogig.storage.RevFeatureSerializationTest) Test(org.junit.Test)

Example 12 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class GeoGigAPI method getFeatureFromWorkingTree.

/**
     * Returns a feature from the working tree of the repository, given its full path
     * 
     * Returns null if the given path doesn't resolve to a feature
     * 
     * @param path the path to the feature to return
     */
public Feature getFeatureFromWorkingTree(String path) {
    String name = NodeRef.nodeFromPath(path);
    String refSpec = "WORK_HEAD:" + path;
    Optional<RevFeature> revFeature = repository.command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class);
    if (revFeature.isPresent()) {
        RevFeatureType revFeatureType = repository.command(ResolveFeatureType.class).setRefSpec(refSpec).call().get();
        FeatureBuilder builder = new FeatureBuilder(revFeatureType);
        return builder.build(name, revFeature.get());
    } else {
        return null;
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) ResolveFeatureType(org.locationtech.geogig.api.plumbing.ResolveFeatureType) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 13 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class GeoGigAPI method getUnstagedFeatures.

public Feature[] getUnstagedFeatures(String path, boolean noDeletions) {
    Iterator<DiffEntry> diffs = repository.workingTree().getUnstaged(path);
    List<Feature> list = Lists.newArrayList();
    while (diffs.hasNext()) {
        DiffEntry diff = diffs.next();
        if (!diff.changeType().equals(ChangeType.REMOVED) || !noDeletions) {
            RevFeature revFeature = repository.command(RevObjectParse.class).setObjectId(diff.newObjectId()).call(RevFeature.class).get();
            RevFeatureType revFeatureType = repository.command(RevObjectParse.class).setObjectId(diff.getNewObject().getMetadataId()).call(RevFeatureType.class).get();
            FeatureBuilder builder = new FeatureBuilder(revFeatureType);
            list.add(builder.build(diff.getNewObject().name(), revFeature));
        }
    }
    return list.toArray(new Feature[0]);
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeature(org.locationtech.geogig.api.RevFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 14 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMHookTest method testOSMHook.

@Test
public void testOSMHook() throws Exception {
    // set the hook that will trigger an unmapping when something is imported to the busstops
    // tree
    CharSequence commitPreHookCode = "var diffs = geogig.getFeaturesToCommit(\"busstops\", false);\n" + "if (diffs.length > 0){\n" + "\tvar params = {\"path\" : \"busstops\"};\n" + "\tgeogig.run(\"org.locationtech.geogig.osm.internal.OSMUnmapOp\", params)\n}";
    File hooksFolder = new File(geogig.getPlatform().pwd(), ".geogig/hooks");
    File commitPreHookFile = new File(hooksFolder, "pre_commit.js");
    Files.write(commitPreHookCode, commitPreHookFile, Charsets.UTF_8);
    // Import
    String filename = OSMImportOp.class.getResource("nodes.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    // Map
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> mappings = Maps.newHashMap();
    mappings.put("highway", Lists.newArrayList("bus_stop"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.POINT));
    fields.put("name", new AttributeDefinition("name", FieldType.STRING));
    MappingRule mappingRule = new MappingRule("busstops", mappings, null, fields, null);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").call();
    geogig.command(OSMMapOp.class).setMapping(mapping).call();
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:busstops/507464799").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class).setRefSpec("HEAD:busstops/507464799").call();
    assertTrue(featureType.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    assertEquals(3, values.size());
    String wkt = "POINT (7.1959361 50.739397)";
    assertEquals(wkt, values.get(2).get().toString());
    assertEquals(507464799l, values.get(0).get());
    // Modify a node
    GeometryFactory gf = new GeometryFactory();
    SimpleFeatureBuilder fb = new SimpleFeatureBuilder((SimpleFeatureType) featureType.get().type());
    fb.set("geom", gf.createPoint(new Coordinate(0, 1)));
    fb.set("name", "newname");
    fb.set("id", 507464799l);
    SimpleFeature newFeature = fb.buildFeature("507464799");
    geogig.getRepository().workingTree().insert("busstops", newFeature);
    geogig.command(AddOp.class).call();
    // this should trigger the hook
    geogig.command(CommitOp.class).setMessage("msg").call();
    // check that the unmapping has been triggered and the unmapped node has the changes we
    // introduced
    Optional<RevFeature> unmapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/507464799").call(RevFeature.class);
    assertTrue(unmapped.isPresent());
    values = unmapped.get().getValues();
    assertEquals("POINT (0 1)", values.get(6).get().toString());
    assertEquals("bus:yes|public_transport:platform|highway:bus_stop|VRS:ortsteil:Hoholz|name:newname|VRS:ref:68566|VRS:gemeinde:BONN", values.get(3).get().toString());
    // check that unchanged nodes keep their attributes
    Optional<RevFeature> unchanged = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:node/1633594723").call(RevFeature.class);
    values = unchanged.get().getValues();
    assertEquals("14220478", values.get(4).get().toString());
    assertEquals("1355097351000", values.get(2).get().toString());
    assertEquals("2", values.get(1).get().toString());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevFeature(org.locationtech.geogig.api.RevFeature) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Example 15 with RevFeature

use of org.locationtech.geogig.api.RevFeature in project GeoGig by boundlessgeo.

the class OSMImportOpTest method testImportWithMapping.

@Test
public void testImportWithMapping() throws Exception {
    String filename = getClass().getResource("ways.xml").getFile();
    File file = new File(filename);
    // Define a mapping
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> mappings = Maps.newHashMap();
    mappings.put("oneway", Lists.newArrayList("yes"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.LINESTRING));
    fields.put("lit", new AttributeDefinition("lit", FieldType.STRING));
    Map<String, List<String>> filterExclude = Maps.newHashMap();
    MappingRule mappingRule = new MappingRule("onewaystreets", mappings, filterExclude, fields, null);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    // import with mapping and check import went ok
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).setMapping(mapping).call();
    Optional<RevTree> tree = geogig.command(RevObjectParse.class).setRefSpec("HEAD:node").call(RevTree.class);
    assertTrue(tree.isPresent());
    assertTrue(tree.get().size() > 0);
    tree = geogig.command(RevObjectParse.class).setRefSpec("HEAD:way").call(RevTree.class);
    assertTrue(tree.isPresent());
    assertTrue(tree.get().size() > 0);
    // check that the tree with the mapping exist and is not empty
    tree = geogig.command(RevObjectParse.class).setRefSpec("HEAD:onewaystreets").call(RevTree.class);
    assertTrue(tree.isPresent());
    assertTrue(tree.get().size() > 0);
    // check that the mapping was correctly performed
    Optional<Node> feature = geogig.getRepository().workingTree().findUnstaged("onewaystreets/31045880");
    assertTrue(feature.isPresent());
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setObjectId(feature.get().getObjectId()).call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    String wkt = "LINESTRING (7.1923367 50.7395887, 7.1923127 50.7396946, 7.1923444 50.7397419, 7.1924199 50.7397781)";
    assertEquals(wkt, values.get(2).get().toString());
    assertEquals("31045880", values.get(0).get().toString());
    assertEquals("yes", values.get(1).get());
}
Also used : Optional(com.google.common.base.Optional) Node(org.locationtech.geogig.api.Node) RevFeature(org.locationtech.geogig.api.RevFeature) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) File(java.io.File) RevTree(org.locationtech.geogig.api.RevTree) Test(org.junit.Test)

Aggregations

RevFeature (org.locationtech.geogig.api.RevFeature)89 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)46 Test (org.junit.Test)45 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)43 Optional (com.google.common.base.Optional)40 NodeRef (org.locationtech.geogig.api.NodeRef)32 File (java.io.File)24 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)24 ObjectId (org.locationtech.geogig.api.ObjectId)21 AddOp (org.locationtech.geogig.api.porcelain.AddOp)21 ImmutableList (com.google.common.collect.ImmutableList)20 Feature (org.opengis.feature.Feature)20 List (java.util.List)19 RevObject (org.locationtech.geogig.api.RevObject)18 RevTree (org.locationtech.geogig.api.RevTree)16 DiffEntry (org.locationtech.geogig.api.plumbing.diff.DiffEntry)16 SimpleFeature (org.opengis.feature.simple.SimpleFeature)16 ArrayList (java.util.ArrayList)15 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)15 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)15