private Feature createFeature(List<String> featureChanges, Map<String, SimpleFeatureBuilder> featureTypes) { String path = featureChanges.get(0); String tree = NodeRef.parentPath(path); String featureId = NodeRef.nodeFromPath(path); if (!featureTypes.containsKey(tree)) { Optional<RevFeatureType> opt = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + tree).call(); checkParameter(opt.isPresent(), "The parent tree does not exist: " + tree); SimpleFeatureBuilder builder = new SimpleFeatureBuilder( (SimpleFeatureType) opt.get().type()); featureTypes.put(tree, builder); } SimpleFeatureBuilder ftb = featureTypes.get(tree); SimpleFeatureType ft = ftb.getFeatureType(); for (int i = 1; i < featureChanges.size(); i++) { String[] tokens = featureChanges.get(i).split("\t"); Preconditions.checkArgument(tokens.length == 2, "Wrong attribute definition: " + featureChanges.get(i)); String fieldName = tokens[0]; AttributeDescriptor desc = ft.getDescriptor(fieldName); Preconditions.checkNotNull(desc, "Wrong attribute in feature description"); FieldType type = FieldType.forBinding(desc.getType().getBinding()); Object value = TextValueSerializer.fromString(type, tokens[1]); ftb.set(tokens[0], value); } return ftb.buildFeature(featureId); } }
/** * Returns a feature from the Head 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 getFeatureFromHead(String path) { String name = NodeRef.nodeFromPath(path); String refSpec = "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; } }
/** * 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; } }
@Test public void testNoFeatureTypeNameSpecified() { exception.expect(IllegalStateException.class); geogig.command(ResolveFeatureType.class).call(); }
@Test public void testNoFeatureTypeNameSpecified() { exception.expect(IllegalStateException.class); geogig.command(ResolveFeatureType.class).call(); }
@Test public void testObjectNotInIndex() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + linesName).call(); assertFalse(featureType.isPresent()); }
@Test public void testObjectNotInIndex() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + linesName).call(); assertFalse(featureType.isPresent()); }
@Test public void testResolveFeatureType() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec(pointsName).call(); assertTrue(featureType.isPresent()); assertEquals(pointsName, featureType.get().getName().getLocalPart()); assertEquals(TYPE.FEATURETYPE, featureType.get().getType()); }
@Test public void testResolveFeatureType() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec(pointsName).call(); assertTrue(featureType.isPresent()); assertEquals(pointsName, featureType.get().getName().getLocalPart()); assertEquals(TYPE.FEATURETYPE, featureType.get().getType()); }
@Test public void testResolveFeatureTypeWithColonInFeatureTypeName() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + pointsName).call(); assertTrue(featureType.isPresent()); assertEquals(pointsName, featureType.get().getName().getLocalPart()); assertEquals(TYPE.FEATURETYPE, featureType.get().getType()); }
@Test public void testResolveFeatureTypeWithColonInFeatureTypeName() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + pointsName).call(); assertTrue(featureType.isPresent()); assertEquals(pointsName, featureType.get().getName().getLocalPart()); assertEquals(TYPE.FEATURETYPE, featureType.get().getType()); }
@Test public void testResolveFeatureTypeFromFeatureRefspec() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(pointsName, idP1)).call(); assertTrue(featureType.isPresent()); } }
@Test public void testResolveFeatureTypeFromFeatureRefspec() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(pointsName, idP1)).call(); assertTrue(featureType.isPresent()); } }
@Test public void featureTypeTest() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(pointsName, idP1)).call(); RevFeatureType ft = null; if (featureType.isPresent()) ft = featureType.get(); Envelope bounds = new EPSGBoundsCalc().getCRSBounds(ft); Envelope wgs84 = new Envelope(-180.0, 180.0, -90.0, 90.0); assertEquals("true", System.getProperty("org.geotools.referencing.forceXY")); assertEquals(wgs84, bounds); } }
@Test public void featureTypeTest() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(pointsName, idP1)).call(); RevFeatureType ft = null; if (featureType.isPresent()) ft = featureType.get(); Envelope bounds = new EPSGBoundsCalc().getCRSBounds(ft); Envelope wgs84 = new Envelope(-180.0, 180.0, -90.0, 90.0); assertEquals("true", System.getProperty("org.geotools.referencing.forceXY")); assertEquals(wgs84, bounds); } }
@Test public void featureTypeTest() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(pointsName, idP1)).call(); RevFeatureType ft = null; if (featureType.isPresent()) ft = featureType.get(); Envelope bounds = new EPSGBoundsCalc().getCRSBounds(ft); Envelope wgs84 = new Envelope(-90.0, 90.0, -180.0, 180.0); assertEquals(wgs84, bounds); } }
@Test public void featureTypeTest() throws Exception { insertAndAdd(points1); geogig.command(CommitOp.class).setMessage("Commit1").call(); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(pointsName, idP1)).call(); RevFeatureType ft = null; if (featureType.isPresent()) ft = featureType.get(); Envelope bounds = new EPSGBoundsCalc().getCRSBounds(ft); Envelope wgs84 = new Envelope(-90.0, 90.0, -180.0, 180.0); assertEquals(wgs84, bounds); } }
@Test(expected=CRSException.class) public void noCRSMatch() throws Exception { String noEPSGMatchWKT = "GEOGCS[\"GCS_WGS_1985\",DATUM[\"D_WGS_1985\",SPHEROID[\"WGS_1985\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]"; CoordinateReferenceSystem origCrs = CRS.parseWKT(noEPSGMatchWKT); String typeName = "noCRSMatchType"; String id = "Points.6"; //create the feature with the noEPSGMatchWKT CRS SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setCRS(origCrs); builder.add("geom", Point.class); builder.setName(typeName); //need to make this a feature, so it can be used with the insertAndAdd command SimpleFeatureType type = builder.buildFeatureType(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type); Feature f = featureBuilder.buildFeature(id); insertAndAdd(f); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(typeName, id)).call(); RevFeatureType ft = null; if (featureType.isPresent()) ft = featureType.get(); new EPSGBoundsCalc().getCRSBounds(ft); }
@Test(expected=CRSException.class) public void noCRSMatch() throws Exception { String noEPSGMatchWKT = "GEOGCS[\"GCS_WGS_1985\",DATUM[\"D_WGS_1985\",SPHEROID[\"WGS_1985\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]"; CoordinateReferenceSystem origCrs = CRS.parseWKT(noEPSGMatchWKT); String typeName = "noCRSMatchType"; String id = "Points.6"; //create the feature with the noEPSGMatchWKT CRS SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); builder.setCRS(origCrs); builder.add("geom", Point.class); builder.setName(typeName); //need to make this a feature, so it can be used with the insertAndAdd command SimpleFeatureType type = builder.buildFeatureType(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type); Feature f = featureBuilder.buildFeature(id); insertAndAdd(f); Optional<RevFeatureType> featureType = geogig.command(ResolveFeatureType.class) .setRefSpec("WORK_HEAD:" + NodeRef.appendChild(typeName, id)).call(); RevFeatureType ft = null; if (featureType.isPresent()) ft = featureType.get(); new EPSGBoundsCalc().getCRSBounds(ft); }
.setRefSpec("WORK_HEAD:" + NodeRef.appendChild(typeName, id)).call();