Search in sources :

Example 11 with FactoryException

use of org.opengis.referencing.FactoryException in project GeoGig by boundlessgeo.

the class ResponseWriter method writeConflicts.

/**
     * Writes the response for a set of conflicts while also supplying the geometry.
     * 
     * @param geogig - a CommandLocator to call commands from
     * @param conflicts - a Conflict iterator to build the response from
     * @throws XMLStreamException
     */
public void writeConflicts(final Context geogig, Iterator<Conflict> conflicts, final ObjectId ours, final ObjectId theirs) throws XMLStreamException {
    Iterator<GeometryConflict> conflictIterator = Iterators.transform(conflicts, new Function<Conflict, GeometryConflict>() {

        @Override
        public GeometryConflict apply(Conflict input) {
            ObjectId commitId = ours;
            if (input.getOurs().equals(ObjectId.NULL)) {
                commitId = theirs;
            }
            Optional<RevObject> object = geogig.command(RevObjectParse.class).setObjectId(commitId).call();
            RevCommit commit = null;
            if (object.isPresent() && object.get() instanceof RevCommit) {
                commit = (RevCommit) object.get();
            } else {
                throw new CommandSpecException("Couldn't resolve id: " + commitId.toString() + " to a commit");
            }
            object = geogig.command(RevObjectParse.class).setObjectId(commit.getTreeId()).call();
            Optional<NodeRef> node = Optional.absent();
            if (object.isPresent()) {
                RevTree tree = (RevTree) object.get();
                node = geogig.command(FindTreeChild.class).setParent(tree).setChildPath(input.getPath()).call();
            } else {
                throw new CommandSpecException("Couldn't resolve commit's treeId");
            }
            RevFeatureType type = null;
            RevFeature feature = null;
            if (node.isPresent()) {
                object = geogig.command(RevObjectParse.class).setObjectId(node.get().getMetadataId()).call();
                if (object.isPresent() && object.get() instanceof RevFeatureType) {
                    type = (RevFeatureType) object.get();
                } else {
                    throw new CommandSpecException("Couldn't resolve newCommit's featureType");
                }
                object = geogig.command(RevObjectParse.class).setObjectId(node.get().objectId()).call();
                if (object.isPresent() && object.get() instanceof RevFeature) {
                    feature = (RevFeature) object.get();
                } else {
                    throw new CommandSpecException("Couldn't resolve newCommit's feature");
                }
            }
            GeometryConflict conflict = null;
            if (feature != null && type != null) {
                String crsCode = null;
                Collection<PropertyDescriptor> attribs = type.type().getDescriptors();
                for (PropertyDescriptor attrib : attribs) {
                    PropertyType attrType = attrib.getType();
                    if (attrType instanceof GeometryType) {
                        GeometryType gt = (GeometryType) attrType;
                        CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
                        if (crs != null) {
                            try {
                                crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                            } catch (FactoryException e) {
                                crsCode = null;
                            }
                            if (crsCode != null) {
                                crsCode = "EPSG:" + crsCode;
                            }
                        }
                        break;
                    }
                }
                FeatureBuilder builder = new FeatureBuilder(type);
                GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(feature.getId().toString(), feature);
                Geometry geom = null;
                List<Object> attributes = simpleFeature.getAttributes();
                for (Object attribute : attributes) {
                    if (attribute instanceof Geometry) {
                        geom = (Geometry) attribute;
                        break;
                    }
                }
                conflict = new GeometryConflict(input, geom, crsCode);
            }
            return conflict;
        }
    });
    while (conflictIterator.hasNext()) {
        GeometryConflict next = conflictIterator.next();
        if (next != null) {
            out.writeStartElement("Feature");
            writeElement("change", "CONFLICT");
            writeElement("id", next.getConflict().getPath());
            writeElement("ourvalue", next.getConflict().getOurs().toString());
            writeElement("theirvalue", next.getConflict().getTheirs().toString());
            writeElement("geometry", next.getGeometry().toText());
            if (next.getCRS() != null) {
                writeElement("crs", next.getCRS());
            }
            out.writeEndElement();
        }
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) FactoryException(org.opengis.referencing.FactoryException) PropertyType(org.opengis.feature.type.PropertyType) GeometryType(org.opengis.feature.type.GeometryType) RevFeature(org.locationtech.geogig.api.RevFeature) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeogigSimpleFeature(org.locationtech.geogig.api.GeogigSimpleFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) RevCommit(org.locationtech.geogig.api.RevCommit) Optional(com.google.common.base.Optional) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) ObjectId(org.locationtech.geogig.api.ObjectId) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) Geometry(com.vividsolutions.jts.geom.Geometry) Conflict(org.locationtech.geogig.api.plumbing.merge.Conflict) Collection(java.util.Collection) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevObject(org.locationtech.geogig.api.RevObject) RevTree(org.locationtech.geogig.api.RevTree)

Example 12 with FactoryException

use of org.opengis.referencing.FactoryException in project GeoGig by boundlessgeo.

the class MappingRule method getFeatureType.

/**
     * Returns the feature type defined by this rule. This is the feature type that features
     * transformed by this rule will have
     * 
     * @return
     */
public SimpleFeatureType getFeatureType() {
    if (featureType == null) {
        SimpleFeatureTypeBuilder fb = new SimpleFeatureTypeBuilder();
        fb.setName(name);
        fb.add("id", Long.class);
        if (defaultFields != null) {
            for (DefaultField df : defaultFields) {
                fb.add(df.name().toLowerCase(), df.getFieldClass());
            }
        }
        Set<String> keys = this.fields.keySet();
        for (String key : keys) {
            AttributeDefinition field = fields.get(key);
            Class<?> clazz = field.getType().getBinding();
            if (Geometry.class.isAssignableFrom(clazz)) {
                Preconditions.checkArgument(geometryType == null, "The mapping has more than one geometry attribute");
                CoordinateReferenceSystem epsg4326;
                try {
                    epsg4326 = CRS.decode("EPSG:4326", true);
                    fb.add(field.getName(), clazz, epsg4326);
                } catch (NoSuchAuthorityCodeException e) {
                } catch (FactoryException e) {
                }
                geometryType = clazz;
            } else {
                fb.add(field.getName(), clazz);
            }
        }
        Preconditions.checkNotNull(geometryType, "The mapping rule does not define a geometry field");
        if (!geometryType.equals(Point.class)) {
            fb.add("nodes", String.class);
        }
        featureType = fb.buildFeatureType();
        featureBuilder = new SimpleFeatureBuilder(featureType);
    }
    return featureType;
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Point(com.vividsolutions.jts.geom.Point) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 13 with FactoryException

use of org.opengis.referencing.FactoryException in project GeoGig by boundlessgeo.

the class FormatCommonV1 method readAttributeType.

private static AttributeType readAttributeType(DataInput in, FeatureTypeFactory typeFactory) throws IOException {
    final Name name = readName(in);
    final byte typeTag = in.readByte();
    final FieldType type = FieldType.valueOf(typeTag);
    if (Geometry.class.isAssignableFrom(type.getBinding())) {
        // as opposed to a raw
        final boolean isCRSCode = in.readBoolean();
        // WKT string
        final String crsText = in.readUTF();
        final CoordinateReferenceSystem crs;
        try {
            if (isCRSCode) {
                if ("urn:ogc:def:crs:EPSG::0".equals(crsText)) {
                    crs = null;
                } else {
                    boolean forceLongitudeFirst = crsText.startsWith("EPSG:");
                    crs = CRS.decode(crsText, forceLongitudeFirst);
                }
            } else {
                crs = CRS.parseWKT(crsText);
            }
        } catch (FactoryException e) {
            throw new RuntimeException(e);
        }
        return typeFactory.createGeometryType(name, type.getBinding(), crs, false, false, Collections.<Filter>emptyList(), null, null);
    } else {
        return typeFactory.createAttributeType(name, type.getBinding(), false, false, Collections.<Filter>emptyList(), null, null);
    }
}
Also used : FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Name(org.opengis.feature.type.Name) FieldType(org.locationtech.geogig.storage.FieldType)

Example 14 with FactoryException

use of org.opengis.referencing.FactoryException in project GeoGig by boundlessgeo.

the class FormatCommonV2 method readAttributeType.

private static AttributeType readAttributeType(DataInput in, FeatureTypeFactory typeFactory) throws IOException {
    final Name name = readName(in);
    final byte typeTag = in.readByte();
    final FieldType type = FieldType.valueOf(typeTag);
    if (Geometry.class.isAssignableFrom(type.getBinding())) {
        // as opposed to a raw WKT string
        final boolean isCRSCode = in.readBoolean();
        final String crsText = in.readUTF();
        final CoordinateReferenceSystem crs;
        try {
            if (isCRSCode) {
                if ("urn:ogc:def:crs:EPSG::0".equals(crsText)) {
                    crs = null;
                } else {
                    boolean forceLongitudeFirst = crsText.startsWith("EPSG:");
                    crs = CRS.decode(crsText, forceLongitudeFirst);
                }
            } else {
                crs = CRS.parseWKT(crsText);
            }
        } catch (FactoryException e) {
            throw new RuntimeException(e);
        }
        return typeFactory.createGeometryType(name, type.getBinding(), crs, false, false, Collections.<Filter>emptyList(), null, null);
    } else {
        return typeFactory.createAttributeType(name, type.getBinding(), false, false, Collections.<Filter>emptyList(), null, null);
    }
}
Also used : FactoryException(org.opengis.referencing.FactoryException) Integer.toBinaryString(java.lang.Integer.toBinaryString) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Name(org.opengis.feature.type.Name) FieldType(org.locationtech.geogig.storage.FieldType)

Aggregations

FactoryException (org.opengis.referencing.FactoryException)14 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)12 GeometryType (org.opengis.feature.type.GeometryType)5 Geometry (com.vividsolutions.jts.geom.Geometry)4 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)4 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)4 PropertyType (org.opengis.feature.type.PropertyType)4 TransformException (org.opengis.referencing.operation.TransformException)4 Point (com.vividsolutions.jts.geom.Point)3 Collection (java.util.Collection)3 FeatureBuilder (org.locationtech.geogig.api.FeatureBuilder)3 GeogigSimpleFeature (org.locationtech.geogig.api.GeogigSimpleFeature)3 RevFeature (org.locationtech.geogig.api.RevFeature)3 RevFeatureBuilder (org.locationtech.geogig.api.RevFeatureBuilder)3 RevObject (org.locationtech.geogig.api.RevObject)3 Optional (com.google.common.base.Optional)2 Integer.toBinaryString (java.lang.Integer.toBinaryString)2 GeoFormatException (org.codice.ddf.libs.geo.GeoFormatException)2 Hints (org.geotools.factory.Hints)2 RevObjectParse (org.locationtech.geogig.api.plumbing.RevObjectParse)2