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();
}
}
}
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;
}
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);
}
}
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);
}
}
Aggregations