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();
}
}
use of org.opengis.referencing.FactoryException in project ddf by codice.
the class GeospatialUtil method transformToEPSG4326LonLatFormat.
/**
* Transform a geometry to EPSG:4326 format with lon/lat coordinate ordering.
* NOTE: This method will NOT perform the transform swapping coordinates even if the sourceCrsName is
* EPSG:4326.
*
* @param geometry - Geometry to transform
* @param sourceCrs - Source geometry's coordinate reference system
* @return Geometry - Transformed geometry into EPSG:4326 lon/lat coordinate system
*/
public static Geometry transformToEPSG4326LonLatFormat(Geometry geometry, CoordinateReferenceSystem sourceCrs) throws GeoFormatException {
if (geometry == null) {
throw new GeoFormatException("Unable to convert null geometry");
}
//If we don't have source CRS just return geometry as we can't transform without that information
if (sourceCrs == null || CollectionUtils.isEmpty(sourceCrs.getIdentifiers())) {
return geometry;
}
Geometry transformedGeometry = geometry;
try {
boolean sourceCrsMatchesTarget = false;
for (ReferenceIdentifier referenceIdentifier : sourceCrs.getIdentifiers()) {
if (referenceIdentifier.toString().equalsIgnoreCase(EPSG_4326)) {
sourceCrsMatchesTarget = true;
break;
}
}
if (!sourceCrsMatchesTarget) {
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(EPSG_4326);
MathTransform transform = CRS.findMathTransform(sourceCrs, targetCRS);
transformedGeometry = JTS.transform(geometry, transform);
LOGGER.debug("Converted CRS {} into {} : {}", sourceCrs, EPSG_4326, geometry);
}
} catch (FactoryException | TransformException e) {
throw new GeoFormatException("Unable to convert coordinate to " + EPSG_4326, e);
}
return transformedGeometry;
}
use of org.opengis.referencing.FactoryException in project ddf by codice.
the class GeospatialUtil method transformToEPSG4326LonLatFormat.
/**
* Transform a geometry to EPSG:4326 format with lon/lat coordinate ordering.
* NOTE: This method will perform the transform swapping coordinates even if the sourceCrsName is
* EPSG:4326
*
* @param geometry - Geometry to transform
* @param sourceCrsName - Source geometry's coordinate reference system
* @return Geometry - Transformed geometry into EPSG:4326 lon/lat coordinate system
*/
public static Geometry transformToEPSG4326LonLatFormat(Geometry geometry, String sourceCrsName) throws GeoFormatException {
if (geometry == null) {
throw new GeoFormatException("Unable to convert null geometry");
}
//If we don't have source CRS just return geometry as we can't transform without that information
if (sourceCrsName == null) {
return geometry;
}
try {
CoordinateReferenceSystem sourceCrs = CRS.decode(sourceCrsName);
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory factory = ReferencingFactoryFinder.getCRSAuthorityFactory("EPSG", hints);
CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem(EPSG_4326);
MathTransform transform = CRS.findMathTransform(sourceCrs, targetCRS);
return JTS.transform(geometry, transform);
} catch (FactoryException | TransformException e) {
throw new GeoFormatException("Unable to convert coordinate to " + EPSG_4326, e);
}
}
use of org.opengis.referencing.FactoryException in project series-rest-api by 52North.
the class IoParameters method transformToInnerCrs.
/**
* @param point
* a GeoJSON point to be transformed to internally used CRS:84.
* @param crsUtils
* a reference helper.
* @return a transformed GeoJSON instance.
* @throws IoParseException
* if point could not be transformed, or if requested CRS object could not be created.
*/
private GeojsonPoint transformToInnerCrs(GeojsonPoint point, CRSUtils crsUtils) {
try {
Point toTransformed = crsUtils.convertToPointFrom(point, getCrs());
Point crs84Point = (Point) crsUtils.transformOuterToInner(toTransformed, getCrs());
return crsUtils.convertToGeojsonFrom(crs84Point);
} catch (TransformException e) {
throw new IoParseException("Could not transform to internally used CRS:84.", e);
} catch (FactoryException e) {
throw new IoParseException("Check if 'crs' parameter is a valid EPSG CRS. Was: '" + getCrs() + "'.", e);
}
}
use of org.opengis.referencing.FactoryException in project GeoGig by boundlessgeo.
the class ResponseWriter method writeGeometryChanges.
/**
* Writes the response for a set of diffs while also supplying the geometry.
*
* @param geogig - a CommandLocator to call commands from
* @param diff - a DiffEntry iterator to build the response from
* @throws XMLStreamException
*/
public void writeGeometryChanges(final Context geogig, Iterator<DiffEntry> diff, int page, int elementsPerPage) throws XMLStreamException {
Iterators.advance(diff, page * elementsPerPage);
int counter = 0;
Iterator<GeometryChange> changeIterator = Iterators.transform(diff, new Function<DiffEntry, GeometryChange>() {
@Override
public GeometryChange apply(DiffEntry input) {
Optional<RevObject> feature = Optional.absent();
Optional<RevObject> type = Optional.absent();
String path = null;
String crsCode = null;
GeometryChange change = null;
if (input.changeType() == ChangeType.ADDED || input.changeType() == ChangeType.MODIFIED) {
feature = geogig.command(RevObjectParse.class).setObjectId(input.newObjectId()).call();
type = geogig.command(RevObjectParse.class).setObjectId(input.getNewObject().getMetadataId()).call();
path = input.getNewObject().path();
} else if (input.changeType() == ChangeType.REMOVED) {
feature = geogig.command(RevObjectParse.class).setObjectId(input.oldObjectId()).call();
type = geogig.command(RevObjectParse.class).setObjectId(input.getOldObject().getMetadataId()).call();
path = input.getOldObject().path();
}
if (feature.isPresent() && feature.get() instanceof RevFeature && type.isPresent() && type.get() instanceof RevFeatureType) {
RevFeatureType featureType = (RevFeatureType) type.get();
Collection<PropertyDescriptor> attribs = featureType.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;
}
}
RevFeature revFeature = (RevFeature) feature.get();
FeatureBuilder builder = new FeatureBuilder(featureType);
GeogigSimpleFeature simpleFeature = (GeogigSimpleFeature) builder.build(revFeature.getId().toString(), revFeature);
change = new GeometryChange(simpleFeature, input.changeType(), path, crsCode);
}
return change;
}
});
while (changeIterator.hasNext() && (elementsPerPage == 0 || counter < elementsPerPage)) {
GeometryChange next = changeIterator.next();
if (next != null) {
GeogigSimpleFeature feature = next.getFeature();
ChangeType change = next.getChangeType();
out.writeStartElement("Feature");
writeElement("change", change.toString());
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();
counter++;
}
}
if (changeIterator.hasNext()) {
writeElement("nextPage", "true");
}
}
Aggregations