Search in sources :

Example 1 with FactoryException

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

the class FormatCommonV2 method writePropertyType.

private static void writePropertyType(PropertyType type, DataOutput data) throws IOException {
    writeName(type.getName(), data);
    data.writeByte(FieldType.forBinding(type.getBinding()).getTag());
    if (type instanceof GeometryType) {
        GeometryType gType = (GeometryType) type;
        CoordinateReferenceSystem crs = gType.getCoordinateReferenceSystem();
        String srsName;
        if (crs == null) {
            srsName = "urn:ogc:def:crs:EPSG::0";
        } else {
            final boolean longitudeFirst = CRS.getAxisOrder(crs, false) == AxisOrder.EAST_NORTH;
            final boolean codeOnly = true;
            String crsCode = CRS.toSRS(crs, codeOnly);
            if (crsCode != null) {
                srsName = (longitudeFirst ? "EPSG:" : "urn:ogc:def:crs:EPSG::") + crsCode;
                // able to decode it later. If not, we will use WKT instead
                try {
                    CRS.decode(srsName, longitudeFirst);
                } catch (NoSuchAuthorityCodeException e) {
                    srsName = null;
                } catch (FactoryException e) {
                    srsName = null;
                }
            } else {
                srsName = null;
            }
        }
        if (srsName != null) {
            data.writeBoolean(true);
            data.writeUTF(srsName);
        } else {
            final String wkt;
            if (crs instanceof Formattable) {
                wkt = ((Formattable) crs).toWKT(Formattable.SINGLE_LINE);
            } else {
                wkt = crs.toWKT();
            }
            data.writeBoolean(false);
            data.writeUTF(wkt);
        }
    }
}
Also used : GeometryType(org.opengis.feature.type.GeometryType) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Integer.toBinaryString(java.lang.Integer.toBinaryString) Formattable(org.geotools.referencing.wkt.Formattable)

Example 2 with FactoryException

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

the class CrsTextSerializer method deserialize.

public static CoordinateReferenceSystem deserialize(String crsText) {
    CoordinateReferenceSystem crs;
    boolean crsCode = crsText.startsWith("EPSG") || crsText.startsWith("urn:ogc:def:crs:EPSG");
    try {
        if (crsCode) {
            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 IllegalArgumentException("Cannot parse CRS definition: " + crsText);
    }
    return crs;
}
Also used : FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 3 with FactoryException

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

the class ResponseWriter method writeMerged.

/**
     * Writes the response for a set of merged features while also supplying the geometry.
     * 
     * @param geogig - a CommandLocator to call commands from
     * @param features - a FeatureInfo iterator to build the response from
     * @throws XMLStreamException
     */
public void writeMerged(final Context geogig, Iterator<FeatureInfo> features) throws XMLStreamException {
    Iterator<GeometryChange> changeIterator = Iterators.transform(features, new Function<FeatureInfo, GeometryChange>() {

        @Override
        public GeometryChange apply(FeatureInfo input) {
            GeometryChange change = null;
            RevFeature revFeature = RevFeatureBuilder.build(input.getFeature());
            RevFeatureType featureType = input.getFeatureType();
            Collection<PropertyDescriptor> attribs = featureType.type().getDescriptors();
            String crsCode = null;
            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(featureType);
            GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(revFeature.getId().toString(), revFeature);
            change = new GeometryChange(simpleFeature, ChangeType.MODIFIED, input.getPath(), crsCode);
            return change;
        }
    });
    while (changeIterator.hasNext()) {
        GeometryChange next = changeIterator.next();
        if (next != null) {
            GeogigSimpleFeature feature = next.getFeature();
            out.writeStartElement("Feature");
            writeElement("change", "MERGED");
            writeElement("id", next.getPath());
            List<Object> attributes = feature.getAttributes();
            for (Object attribute : attributes) {
                if (attribute instanceof Geometry) {
                    writeElement("geometry", ((Geometry) attribute).toText());
                    break;
                }
            }
            if (next.getCRS() != null) {
                writeElement("crs", next.getCRS());
            }
            out.writeEndElement();
        }
    }
}
Also used : FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) RevFeatureBuilder(org.locationtech.geogig.api.RevFeatureBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) FactoryException(org.opengis.referencing.FactoryException) FeatureInfo(org.locationtech.geogig.api.FeatureInfo) PropertyType(org.opengis.feature.type.PropertyType) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryType(org.opengis.feature.type.GeometryType) RevFeature(org.locationtech.geogig.api.RevFeature) Collection(java.util.Collection) RevObject(org.locationtech.geogig.api.RevObject) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeogigSimpleFeature(org.locationtech.geogig.api.GeogigSimpleFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType)

Example 4 with FactoryException

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

the class ResponseWriter method writeFeatureDiffResponse.

/**
     * Writes a set of feature diffs to the stream.
     * 
     * @param diffs a map of {@link PropertyDescriptor} to {@link AttributeDiffs} that specify the
     *        difference between two features
     * @throws XMLStreamException
     */
public void writeFeatureDiffResponse(Map<PropertyDescriptor, AttributeDiff> diffs) throws XMLStreamException {
    Set<Entry<PropertyDescriptor, AttributeDiff>> entries = diffs.entrySet();
    Iterator<Entry<PropertyDescriptor, AttributeDiff>> iter = entries.iterator();
    while (iter.hasNext()) {
        Entry<PropertyDescriptor, AttributeDiff> entry = iter.next();
        out.writeStartElement("diff");
        PropertyType attrType = entry.getKey().getType();
        if (attrType instanceof GeometryType) {
            writeElement("geometry", "true");
            GeometryType gt = (GeometryType) attrType;
            CoordinateReferenceSystem crs = gt.getCoordinateReferenceSystem();
            if (crs != null) {
                String crsCode = null;
                try {
                    crsCode = CRS.lookupIdentifier(Citations.EPSG, crs, false);
                } catch (FactoryException e) {
                    crsCode = null;
                }
                if (crsCode != null) {
                    writeElement("crs", "EPSG:" + crsCode);
                }
            }
        }
        writeElement("attributename", entry.getKey().getName().toString());
        writeElement("changetype", entry.getValue().getType().toString());
        if (entry.getValue().getOldValue() != null && entry.getValue().getOldValue().isPresent()) {
            writeElement("oldvalue", entry.getValue().getOldValue().get().toString());
        }
        if (entry.getValue().getNewValue() != null && entry.getValue().getNewValue().isPresent() && !entry.getValue().getType().equals(TYPE.NO_CHANGE)) {
            writeElement("newvalue", entry.getValue().getNewValue().get().toString());
        }
        out.writeEndElement();
    }
}
Also used : GeometryType(org.opengis.feature.type.GeometryType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) Entry(java.util.Map.Entry) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) FactoryException(org.opengis.referencing.FactoryException) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) PropertyType(org.opengis.feature.type.PropertyType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 5 with FactoryException

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

the class BoundsFilteringDiffConsumer method createProjectedFilter.

private ReferencedEnvelope createProjectedFilter(ObjectId metadataId) {
    final ReferencedEnvelope boundsFilter = this.boundsFilter;
    RevFeatureType featureType = ftypeSource.getFeatureType(metadataId);
    CoordinateReferenceSystem nativeCrs = featureType.type().getCoordinateReferenceSystem();
    if (null == nativeCrs || nativeCrs instanceof DefaultEngineeringCRS) {
        return boundsFilter;
    }
    ReferencedEnvelope transformedFilter;
    try {
        transformedFilter = boundsFilter.transform(nativeCrs, true);
    } catch (TransformException | FactoryException e) {
        throw Throwables.propagate(e);
    }
    return transformedFilter;
}
Also used : ReferencedEnvelope(org.geotools.geometry.jts.ReferencedEnvelope) FactoryException(org.opengis.referencing.FactoryException) TransformException(org.opengis.referencing.operation.TransformException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DefaultEngineeringCRS(org.geotools.referencing.crs.DefaultEngineeringCRS)

Aggregations

FactoryException (org.opengis.referencing.FactoryException)31 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)21 TransformException (org.opengis.referencing.operation.TransformException)11 NoSuchAuthorityCodeException (org.opengis.referencing.NoSuchAuthorityCodeException)8 MathTransform (org.opengis.referencing.operation.MathTransform)8 Collection (java.util.Collection)5 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 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ReferencedEnvelope (org.geotools.geometry.jts.ReferencedEnvelope)3 Geometry (org.locationtech.jts.geom.Geometry)3 InternationalString (org.opengis.util.InternationalString)3 Optional (com.google.common.base.Optional)2 Point (com.vividsolutions.jts.geom.Point)2 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)2 File (java.io.File)2