Search in sources :

Example 1 with DefaultField

use of org.locationtech.geogig.osm.internal.MappingRule.DefaultField in project GeoGig by boundlessgeo.

the class OSMUnmapOpTest method testMappingAndUnmappingOfNodesWithDefaultFieds.

@Test
public void testMappingAndUnmappingOfNodesWithDefaultFieds() throws Exception {
    // Import
    String filename = OSMImportOp.class.getResource("nodes.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").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_alias", FieldType.STRING));
    Map<String, List<String>> filterExclude = Maps.newHashMap();
    ArrayList<DefaultField> defaultFields = Lists.newArrayList(DefaultField.tags, DefaultField.timestamp);
    MappingRule mappingRule = new MappingRule("busstops", mappings, filterExclude, fields, defaultFields);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    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(5, values.size());
    String wkt = "POINT (7.1959361 50.739397)";
    assertEquals(wkt, values.get(4).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_alias", "newname");
    fb.set("id", 507464799l);
    fb.set("tags", "VRS:gemeinde:BONN|VRS:ortsteil:Hoholz|VRS:ref:68566|bus:yes|highway:bus_stop|name:Gielgen|public_transport:platform");
    fb.set("timestamp", 1355097351000l);
    SimpleFeature newFeature = fb.buildFeature("507464799");
    geogig.getRepository().workingTree().insert("busstops", newFeature);
    // check that it was correctly inserted in the working tree
    Optional<RevFeature> mapped = geogig.command(RevObjectParse.class).setRefSpec("WORK_HEAD:busstops/507464799").call(RevFeature.class);
    assertTrue(mapped.isPresent());
    values = mapped.get().getValues();
    assertEquals("POINT (0 1)", values.get(4).get().toString());
    assertEquals(507464799l, ((Long) values.get(0).get()).longValue());
    assertEquals("newname", values.get(3).get().toString());
    assertEquals("1355097351000", values.get(2).get().toString());
    assertEquals("VRS:gemeinde:BONN|VRS:ortsteil:Hoholz|VRS:ref:68566|bus:yes|highway:bus_stop|name:Gielgen|public_transport:platform", values.get(1).get().toString());
    // unmap
    geogig.command(OSMUnmapOp.class).setPath("busstops").call();
    WorkingTree workTree = geogig.getRepository().workingTree();
    long unstaged = workTree.countUnstaged("node").featureCount();
    assertEquals(1, unstaged);
    // check that 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) WorkingTree(org.locationtech.geogig.repository.WorkingTree) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Optional(com.google.common.base.Optional) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Coordinate(com.vividsolutions.jts.geom.Coordinate) DefaultField(org.locationtech.geogig.osm.internal.MappingRule.DefaultField) File(java.io.File) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) Test(org.junit.Test)

Example 2 with DefaultField

use of org.locationtech.geogig.osm.internal.MappingRule.DefaultField in project GeoGig by boundlessgeo.

the class OSMMapOpTest method testMappingDefaultFields.

@Test
public void testMappingDefaultFields() throws Exception {
    // import and check that we have both ways and nodes
    String filename = OSMImportOp.class.getResource("ways.xml").getFile();
    File file = new File(filename);
    geogig.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call();
    WorkingTree workTree = geogig.getRepository().workingTree();
    long unstaged = workTree.countUnstaged("way").count();
    assertTrue(unstaged > 0);
    unstaged = workTree.countUnstaged("node").count();
    assertTrue(unstaged > 0);
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setMessage("msg").call();
    // Define mapping
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> filter = Maps.newHashMap();
    filter.put("oneway", Lists.newArrayList("yes"));
    fields.put("geom", new AttributeDefinition("geom", FieldType.LINESTRING));
    fields.put("lit", new AttributeDefinition("lit", FieldType.STRING));
    ArrayList<DefaultField> defaultFields = Lists.newArrayList();
    defaultFields.add(DefaultField.timestamp);
    defaultFields.add(DefaultField.visible);
    MappingRule mappingRule = new MappingRule("onewaystreets", filter, null, fields, defaultFields);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    geogig.command(OSMMapOp.class).setMapping(mapping).call();
    // Check that mapping was correctly performed
    Optional<RevFeature> revFeature = geogig.command(RevObjectParse.class).setRefSpec("HEAD:onewaystreets/31045880").call(RevFeature.class);
    assertTrue(revFeature.isPresent());
    ImmutableList<Optional<Object>> values = revFeature.get().getValues();
    assertEquals(6, values.size());
    String wkt = "LINESTRING (7.1923367 50.7395887, 7.1923127 50.7396946, 7.1923444 50.7397419, 7.1924199 50.7397781)";
    assertEquals(wkt, values.get(4).get().toString());
    assertEquals("yes", values.get(3).get());
    assertEquals(true, values.get(2).get());
    assertEquals(1318750940000L, values.get(1).get());
    Optional<RevFeatureType> revFeatureType = geogig.command(ResolveFeatureType.class).setRefSpec("HEAD:onewaystreets/31045880").call();
    assertTrue(revFeatureType.isPresent());
    ImmutableList<PropertyDescriptor> descriptors = revFeatureType.get().sortedDescriptors();
    assertEquals("timestamp", descriptors.get(1).getName().toString());
    assertEquals("visible", descriptors.get(2).getName().toString());
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) WorkingTree(org.locationtech.geogig.repository.WorkingTree) RevFeature(org.locationtech.geogig.api.RevFeature) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DefaultField(org.locationtech.geogig.osm.internal.MappingRule.DefaultField) File(java.io.File) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Example 3 with DefaultField

use of org.locationtech.geogig.osm.internal.MappingRule.DefaultField in project GeoGig by boundlessgeo.

the class MappingTest method TestJsonSerialization.

@Test
public void TestJsonSerialization() {
    Map<String, AttributeDefinition> fields = Maps.newHashMap();
    Map<String, List<String>> filters = Maps.newHashMap();
    Map<String, List<String>> exclude = Maps.newHashMap();
    List<DefaultField> defaultFields = Lists.newArrayList();
    filters.put("highway", Lists.newArrayList("bus_stop"));
    exclude.put("public_transport", Lists.newArrayList("platform"));
    defaultFields.add(DefaultField.timestamp);
    defaultFields.add(DefaultField.changeset);
    fields.put("geom", new AttributeDefinition("geom", FieldType.POINT));
    fields.put("name", new AttributeDefinition("name_alias", FieldType.STRING));
    MappingRule mappingRule = new MappingRule("busstops", filters, exclude, fields, defaultFields);
    List<MappingRule> mappingRules = Lists.newArrayList();
    mappingRules.add(mappingRule);
    Mapping mapping = new Mapping(mappingRules);
    GsonBuilder gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
    Gson gson = gsonBuilder.create();
    String s = gson.toJson(mapping);
    System.out.println(s);
    Mapping unmarshalledMapping = gson.fromJson(s, Mapping.class);
    assertEquals(mapping, unmarshalledMapping);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ArrayList(java.util.ArrayList) List(java.util.List) DefaultField(org.locationtech.geogig.osm.internal.MappingRule.DefaultField) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.junit.Test)3 DefaultField (org.locationtech.geogig.osm.internal.MappingRule.DefaultField)3 Optional (com.google.common.base.Optional)2 ImmutableList (com.google.common.collect.ImmutableList)2 File (java.io.File)2 RevFeature (org.locationtech.geogig.api.RevFeature)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2 AddOp (org.locationtech.geogig.api.porcelain.AddOp)2 WorkingTree (org.locationtech.geogig.repository.WorkingTree)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Coordinate (com.vividsolutions.jts.geom.Coordinate)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)1