Feature.java

  1. package de.turnertech.ows.gml;

  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import java.util.Objects;

  7. import javax.xml.stream.XMLStreamWriter;

  8. import de.turnertech.ows.Logging;
  9. import de.turnertech.ows.common.OwsContext;
  10. import de.turnertech.ows.srs.SpatialReferenceSystemRepresentation;

  11. public class Feature implements IFeature {

  12.     private final Map<String, Object> fields;

  13.     private final FeatureType featureType;

  14.     Feature(FeatureType featureType) {
  15.         fields = new HashMap<>();
  16.         this.featureType = featureType;
  17.     }

  18.     public Object getPropertyValue(String propertyName) {
  19.         return fields.get(propertyName);
  20.     }

  21.     public boolean hasPropertyValue(String propertyName) {
  22.         return fields.containsKey(propertyName);
  23.     }

  24.     public Object setPropertyValue(String propertyName, Object value) {
  25.         return fields.put(propertyName, value);
  26.     }

  27.     @Override
  28.     public Envelope getBoundingBox() {
  29.         List<FeatureProperty> bboxProperties = featureType.getBoundingBoxProperties();
  30.         if(bboxProperties.size() == 0) {
  31.             return null;
  32.         }
  33.         Envelope returnBox = new Envelope();
  34.         for (FeatureProperty property : bboxProperties) {
  35.             BoundingBoxProvider bboxProvider =  (BoundingBoxProvider)fields.get(property.getName());
  36.             if(bboxProvider != null) {
  37.                 returnBox.expandToFit(bboxProvider.getBoundingBox());
  38.             }
  39.         }
  40.         return returnBox;
  41.     }

  42.     @Override
  43.     public void writeGml(XMLStreamWriter out, String localName, String namespaceURI, SpatialReferenceSystemRepresentation srs) {
  44.         try {
  45.             writeGmlStartElement(out, localName, namespaceURI);

  46.             String id = getId();
  47.             if(id != null && !"".equals(id)) {
  48.                 out.writeAttribute(OwsContext.GML_URI, "id", id);
  49.             }

  50.             for (Entry<String, Object> field : fields.entrySet()) {
  51.                 FeatureProperty property = featureType.getProperty(field.getKey());
  52.                 FeaturePropertyType propertyType = property.getPropertyType();

  53.                 if(propertyType == FeaturePropertyType.TEXT) {
  54.                     out.writeStartElement(namespaceURI, field.getKey());
  55.                     out.writeCharacters(field.getValue().toString());
  56.                     out.writeEndElement();
  57.                 } else if(
  58.                         propertyType == FeaturePropertyType.POLYGON ||
  59.                         propertyType == FeaturePropertyType.POINT ||
  60.                         propertyType == FeaturePropertyType.LINE_STRING ||
  61.                         propertyType == FeaturePropertyType.GEOMETRY) {
  62.                     GmlElement geom = (GmlElement)field.getValue();
  63.                     out.writeStartElement(namespaceURI, field.getKey());
  64.                     geom.writeGml(out, geom.getGmlName(), GmlElement.NAMESPACE, srs);
  65.                     out.writeEndElement();
  66.                 } else if(propertyType == FeaturePropertyType.ID) {
  67.                     // Do Nothing, special field
  68.                 } else {
  69.                     out.writeEmptyElement(namespaceURI, field.getKey());
  70.                     Logging.LOG.severe("Feature: parameter not written as property type not implemented: " + propertyType);
  71.                 }
  72.             }
  73.             out.writeEndElement();
  74.         } catch (Exception e) {
  75.             Logging.LOG.severe("Could not get GML for Feature");
  76.         }
  77.     }

  78.     @Override
  79.     public String getGmlName() {
  80.         return featureType.getName();
  81.     }

  82.     @Override
  83.     public String getId() {
  84.         FeatureProperty idProperty = featureType.getIdProperty();
  85.         if(idProperty == null) return null;
  86.         return Objects.toString(getPropertyValue(idProperty.getName()), null);
  87.     }

  88.     @Override
  89.     public FeatureType getFeatureType() {
  90.         return featureType;
  91.     }
  92.    
  93. }