public boolean delete(GeogitTransaction transaction, Feature f) throws Exception { final WorkingTree workTree = (transaction != null ? transaction.getWorkingTree() : repo.getWorkingTree()); Name name = f.getType().getName(); String localPart = name.getLocalPart(); String id = f.getIdentifier().getID(); boolean existed = workTree.delete(localPart, id); return existed; }
/** Inserts the feature to the index but does not stages it to be committed */ public ObjectId insert(GeogitTransaction transaction, Feature f) throws Exception { final WorkingTree workTree = (transaction != null ? transaction.getWorkingTree() : repo.getWorkingTree()); Name name = f.getType().getName(); String parentPath = name.getLocalPart(); Node ref = workTree.insert(parentPath, f); ObjectId objectId = ref.getObjectId(); return objectId; }
@Test public void testRemoveEmptyFeatureTypePatch() throws Exception { WorkingTree workingTree = geogit.getRepository().getWorkingTree(); workingTree.createTypeTree(pointsName, pointsType); geogit.command(AddOp.class).setUpdateOnly(false).call(); Patch patch = new Patch(); RevFeatureType featureType = RevFeatureType.build(pointsType); patch.addFeatureType(featureType); patch.addAlteredTree(new FeatureTypeDiff(pointsName, featureType.getId(), null)); geogit.command(ApplyPatchOp.class).setPatch(patch).call(); RevTree root = repo.getWorkingTree().getTree(); assertNotNull(root); Optional<Node> typeTree = findTreeChild(root, pointsName); assertFalse(typeTree.isPresent()); }
private void insert( final WorkingTree workTree, final String path, final FeatureSource featureSource, final ProgressListener taskProgress) { final Query query = new Query(); CoordinateSequenceFactory coordSeq = new PackedCoordinateSequenceFactory(); query.getHints().add(new Hints(Hints.JTS_COORDINATE_SEQUENCE_FACTORY, coordSeq)); workTree.insert(path, featureSource, query, taskProgress); }
@Test public void testMappingAndUnmappingOfWays() throws Exception { // Import String filename = OSMImportOp.class.getResource("ways.xml").getFile(); File file = new File(filename); geogit.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call(); WorkingTree workTree = geogit.getRepository().getWorkingTree(); long unstaged = workTree.countUnstaged("node").getCount(); assertTrue(unstaged > 0); unstaged = workTree.countUnstaged("way").getCount(); assertTrue(unstaged > 0); geogit.command(AddOp.class).call(); geogit.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("residential")); fields.put("geom", new AttributeDefinition("geom", FieldType.LINESTRING)); fields.put("name", new AttributeDefinition("name", FieldType.STRING)); MappingRule mappingRule = new MappingRule("residential", mappings, fields); List<MappingRule> mappingRules = Lists.newArrayList(); mappingRules.add(mappingRule); Mapping mapping = new Mapping(mappingRules); geogit.command(OSMMapOp.class).setMapping(mapping).call(); // check that mapping was correctly performed Optional<RevFeature> revFeature = geogit .command(RevObjectParse.class) .setRefSpec("HEAD:residential/31347480") .call(RevFeature.class); assertTrue(revFeature.isPresent()); Optional<RevFeatureType> featureType = geogit.command(ResolveFeatureType.class).setRefSpec("HEAD:residential/31347480").call(); assertTrue(featureType.isPresent()); ImmutableList<Optional<Object>> values = revFeature.get().getValues(); assertEquals(4, values.size()); // modify a mapped feature. We add a new coordinate to the geometry (0,1) and change the // value of 'name' tag to "newvalue" ArrayList<Coordinate> coords = Lists.newArrayList(((Geometry) values.get(2).get()).getCoordinates()); coords.add(new Coordinate(0, 1)); assertEquals(31347480l, values.get(0).get()); GeometryFactory gf = new GeometryFactory(); SimpleFeatureBuilder fb = new SimpleFeatureBuilder((SimpleFeatureType) featureType.get().type()); fb.set("geom", gf.createLineString(coords.toArray(new Coordinate[0]))); fb.set("name", "newname"); fb.set("id", 31347480l); fb.set("nodes", values.get(3).get()); SimpleFeature newFeature = fb.buildFeature("31347480"); geogit.getRepository().getWorkingTree().insert("residential", newFeature); Optional<RevFeature> mapped = geogit .command(RevObjectParse.class) .setRefSpec("WORK_HEAD:residential/31347480") .call(RevFeature.class); assertTrue(mapped.isPresent()); values = mapped.get().getValues(); assertEquals( "LINESTRING (7.1960069 50.7399033, 7.195868 50.7399081, 7.1950788 50.739912, 7.1949262 50.7399053, " + "7.1942463 50.7398686, 7.1935778 50.7398262, 7.1931011 50.7398018, 7.1929987 50.7398009, 7.1925978 50.7397889, " + "7.1924199 50.7397781, 0 1)", values.get(2).get().toString()); assertEquals(31347480l, ((Long) values.get(0).get()).longValue()); assertEquals("newname", values.get(1).get().toString()); // unmap geogit.command(OSMUnmapOp.class).setPath("residential").call(); // Check that raw OSM data was updated // First, we check that the corresponding way has been modified Optional<RevFeature> unmapped = geogit .command(RevObjectParse.class) .setRefSpec("WORK_HEAD:way/31347480") .call(RevFeature.class); assertTrue(unmapped.isPresent()); values = unmapped.get().getValues(); assertEquals( "LINESTRING (7.1960069 50.7399033, 7.195868 50.7399081, 7.1950788 50.739912, 7.1949262 50.7399053, " + "7.1942463 50.7398686, 7.1935778 50.7398262, 7.1931011 50.7398018, 7.1929987 50.7398009, 7.1925978 50.7397889, " + "7.1924199 50.7397781, 0 1)", values.get(7).get().toString()); assertEquals("lit:yes|highway:residential|name:newname", values.get(3).get().toString()); // now we get the 'nodes' field in the unmapped feature and check take the id of its last // node, which refers to the node that we have added to the geometry int WAY_NODES_FIELD = 6; String nodes = values.get(WAY_NODES_FIELD).get().toString(); String[] nodeIds = nodes.split(";"); String newNodeId = nodeIds[nodeIds.length - 1]; // and we check that the node has been added to the 'node' tree and has the right // coordinates. Optional<RevFeature> newNode = geogit .command(RevObjectParse.class) .setRefSpec("WORK_HEAD:node/" + newNodeId) .call(RevFeature.class); assertTrue(newNode.isPresent()); values = newNode.get().getValues(); int NODE_GEOM_FIELD = 6; assertEquals("POINT (0 1)", values.get(NODE_GEOM_FIELD).get().toString()); }
@Test public void testMappingAndUnmappingOfNodesWithAlias() throws Exception { // Import String filename = OSMImportOp.class.getResource("nodes.xml").getFile(); File file = new File(filename); geogit.command(OSMImportOp.class).setDataSource(file.getAbsolutePath()).call(); geogit.command(AddOp.class).call(); geogit.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)); MappingRule mappingRule = new MappingRule("busstops", mappings, fields); List<MappingRule> mappingRules = Lists.newArrayList(); mappingRules.add(mappingRule); Mapping mapping = new Mapping(mappingRules); geogit.command(OSMMapOp.class).setMapping(mapping).call(); Optional<RevFeature> revFeature = geogit .command(RevObjectParse.class) .setRefSpec("HEAD:busstops/507464799") .call(RevFeature.class); assertTrue(revFeature.isPresent()); Optional<RevFeatureType> featureType = geogit.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()); // unmap without having made any changes and check that the canonical folders are not // modified geogit.command(OSMUnmapOp.class).setPath("busstops").call(); WorkingTree workTree = geogit.getRepository().getWorkingTree(); long unstaged = workTree.countUnstaged("way").getCount(); assertEquals(0, unstaged); unstaged = workTree.countUnstaged("node").getCount(); assertEquals(0, unstaged); // 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); SimpleFeature newFeature = fb.buildFeature("507464799"); geogit.getRepository().getWorkingTree().insert("busstops", newFeature); // check that it was correctly inserted in the working tree Optional<RevFeature> mapped = geogit .command(RevObjectParse.class) .setRefSpec("WORK_HEAD:busstops/507464799") .call(RevFeature.class); assertTrue(mapped.isPresent()); values = mapped.get().getValues(); assertEquals("POINT (0 1)", values.get(2).get().toString()); assertEquals(507464799l, ((Long) values.get(0).get()).longValue()); assertEquals("newname", values.get(1).get().toString()); // unmap geogit.command(OSMUnmapOp.class).setPath("busstops").call(); unstaged = workTree.countUnstaged("node").getCount(); assertEquals(1, unstaged); // check that the unmapped node has the changes we introduced Optional<RevFeature> unmapped = geogit .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 = geogit .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()); }
/** * 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("deprecation") @Override public RevTree call() { // check preconditions and get the actual list of type names to import final String[] typeNames = checkPreconditions(); 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 = getWorkTree(); final boolean destPathProvided = destPath != null; if (destPathProvided && overwrite) { // we delete the previous tree to honor the overwrite setting, but then turn it // to false. Otherwise, each table imported will overwrite the previous ones and // only the last one will be imported. 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 = createForceFeatureType(featureType, path); } featureSource = new ForceTypeAndFidFeatureSource<FeatureType, Feature>( featureSource, featureType, fidPrefix); 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 (Exception e) { throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT); } } progressListener.progress(100.f); progressListener.complete(); return workTree.getTree(); }