use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class StreamGmlWriter method writeElement.
/**
* Write a property element.
*
* @param value the element value
* @param propDef the property definition
* @param report the reporter
* @throws XMLStreamException if writing the element fails
*/
private void writeElement(Object value, PropertyDefinition propDef, IOReporter report) throws XMLStreamException {
Group group = null;
if (value instanceof Group) {
group = (Group) value;
if (value instanceof Instance) {
// extract value from instance
value = ((Instance) value).getValue();
}
}
if (group == null) {
if (value == null) {
// null value
if (propDef.getConstraint(Cardinality.class).getMinOccurs() > 0) {
// write empty element
GmlWriterUtil.writeEmptyElement(writer, propDef.getName());
// mark as nil
writeElementValue(null, propDef);
}
// otherwise just skip it
} else {
GmlWriterUtil.writeStartElement(writer, propDef.getName());
Pair<Geometry, CRSDefinition> pair = extractGeometry(value, true, report);
if (pair != null) {
String srsName = extractCode(pair.getSecond());
// write geometry
writeGeometry(pair.getFirst(), propDef, srsName, report);
} else {
// simple element with value
// write value as content
writeElementValue(value, propDef);
}
writer.writeEndElement();
}
} else {
// children and maybe a value
GmlWriterUtil.writeStartElement(writer, propDef.getName());
boolean hasValue = propDef.getPropertyType().getConstraint(HasValueFlag.class).isEnabled();
Pair<Geometry, CRSDefinition> pair = extractGeometry(value, true, report);
// handle about annotated geometries
if (!hasValue && pair != null) {
String srsName = extractCode(pair.getSecond());
// write geometry
writeGeometry(pair.getFirst(), propDef, srsName, report);
} else {
boolean hasOnlyNilReason = hasOnlyNilReason(group);
// write no elements if there is a value or only a nil reason
boolean writeElements = !hasValue && !hasOnlyNilReason;
boolean isNil = !writeElements && (!hasValue || value == null);
// write all children
writeProperties(group, group.getDefinition(), writeElements, isNil, report);
// write value
if (hasValue) {
writeElementValue(value, propDef);
} else if (hasOnlyNilReason) {
// complex element with a nil value -> write xsi:nil if
// possible
/*
* XXX open question: should xsi:nil be there also if there
* are other attributes than nilReason?
*/
writeElementValue(null, propDef);
}
}
writer.writeEndElement();
}
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class CRSDefinitionUtil method createDefinition.
/**
* Create a {@link CRSDefinition} from an existing coordinate reference
* system.
*
* @param crs the coordinate reference system
* @param cache the cache for CRS resolving
* @return the CRS definition
*/
public static CRSDefinition createDefinition(CoordinateReferenceSystem crs, @Nullable CRSResolveCache cache) {
ReferenceIdentifier name = crs.getName();
// try to find CRS in EPSG DB
CRSDefinition def;
if (cache != null) {
def = cache.resolveCRS(crs);
} else {
def = lookupCrs(crs);
}
if (def != null) {
return def;
}
// try by code
if (name != null) {
String code = name.getCode();
if (code != null && !code.isEmpty()) {
// try decoding
try {
boolean lonFirst = (CRS.getAxisOrder(crs) == AxisOrder.EAST_NORTH);
crs = CRS.decode(code, lonFirst);
return new CodeDefinition(code, crs);
} catch (Exception e) {
// ignore
}
}
}
// use WKT
return new WKTDefinition(crs.toWKT(), crs);
}
Aggregations