public static GeometryElement marshalFrom(Attribute attribute) throws CatalogTransformerException { GeometryElement element = new GeometryElement(); element.setName(attribute.getName()); if (attribute.getValue() != null) { for (Serializable value : attribute.getValues()) { if (!(value instanceof String)) { continue; } String wkt = (String) value; WKTReader wktReader = new WKTReader(geometryFactory); Geometry jtsGeometry = null; try { jtsGeometry = wktReader.read(wkt); } catch (ParseException e) { throw new CatalogTransformerException( "Could not transform Metacard to XML. Invalid WKT.", e); } JTSToGML311GeometryConverter converter = new JTSToGML311GeometryConverter(); @SuppressWarnings("unchecked") JAXBElement<AbstractGeometryType> gmlElement = (JAXBElement<AbstractGeometryType>) converter.createElement(jtsGeometry); GeometryElement.Value geoValue = new GeometryElement.Value(); geoValue.setGeometry(gmlElement); ((GeometryElement) element).getValue().add(geoValue); } } return element; }
public static Attribute unmarshalFrom(GeometryElement element) throws ConversionFailedException { AttributeImpl attribute = null; GML311ToJTSGeometryConverter converter = new GML311ToJTSGeometryConverter(); WKTWriter wktWriter = new WKTWriter(); for (Value xmlValue : element.getValue()) { JAXBElement<AbstractGeometryType> xmlGeometry = xmlValue.getGeometry(); Geometry geometry = converter.createGeometry(new DefaultRootObjectLocator(xmlValue), xmlGeometry.getValue()); String wkt = wktWriter.write(geometry); if (attribute == null) { attribute = new AttributeImpl(element.getName(), wkt); } else { attribute.addValue(wkt); } } return attribute; }