DirectPosition.java

  1. package de.turnertech.ows.gml;

  2. import java.util.Optional;

  3. import javax.xml.namespace.QName;
  4. import javax.xml.stream.XMLStreamWriter;

  5. import de.turnertech.ows.Logging;
  6. import de.turnertech.ows.common.OwsContext;
  7. import de.turnertech.ows.srs.SpatialReferenceSystem;
  8. import de.turnertech.ows.srs.SpatialReferenceSystemConverter;
  9. import de.turnertech.ows.srs.SpatialReferenceSystemRepresentation;

  10. /**
  11.  * gml:pos
  12.  *
  13.  * A location with a spatial reference. This is not "Geometry", but rather an actual position.
  14.  *
  15.  * This implementation maintains a list of the size of the {@link SpatialReferenceSystem#getDimension()}.
  16.  *
  17.  * For convenience, and to help prevent errors in axis order, X and Y getters and setters exist. This should
  18.  * make dealing with actual position vs coordinate representation easier.
  19.  */
  20. public class DirectPosition implements GmlElement {
  21.    
  22.     public static final String GML_NAME = "pos";

  23.     public static final QName QNAME = new QName(OwsContext.GML_URI, "DirectPosition");

  24.     public static final QName LOWER_CORNER_QNAME = new QName(OwsContext.GML_URI, "lowerCorner");

  25.     public static final QName UPPER_CORNER_QNAME = new QName(OwsContext.GML_URI, "upperCorner");

  26.     public static final QName POS_QNAME = new QName(OwsContext.GML_URI, "pos");

  27.     private final double[] pos;

  28.     /**
  29.      * Positions do not change often, but will be accessed very often as strings. It is significantly cheaper to just keep both
  30.      * a string and a numeric representation up to date.
  31.      */
  32.     private final String[] posString;

  33.     private final SpatialReferenceSystem srs;

  34.     public DirectPosition() {
  35.         this(0.0, 0.0);
  36.     }

  37.     public DirectPosition(double x, double y) {
  38.         this(SpatialReferenceSystem.EPSG4326, x, y);
  39.     }

  40.     public DirectPosition(SpatialReferenceSystem srs, double x, double y) {
  41.         this.srs = srs;
  42.         this.pos = new double[srs.getDimension()];
  43.         this.posString = new String[srs.getDimension()];
  44.         this.setX(x);
  45.         this.setY(y);
  46.     }

  47.     public double getX() {
  48.         return pos[srs.getXIndex()];
  49.     }

  50.     public void setX(double x) {
  51.         this.pos[srs.getXIndex()] = x;
  52.         this.posString[srs.getXIndex()] = Double.toString(x);
  53.     }

  54.     public double getY() {
  55.         return pos[srs.getYIndex()];
  56.     }

  57.     public void setY(double y) {
  58.         this.pos[srs.getYIndex()] = y;
  59.         this.posString[srs.getYIndex()] = Double.toString(y);
  60.     }

  61.     public SpatialReferenceSystem getSrs() {
  62.         return srs;
  63.     }

  64.     @Override
  65.     public String toString() {
  66.         return String.join(" ", posString);
  67.     }

  68.     @Override
  69.     public void writeGml(XMLStreamWriter out, String localName, String namespaceURI, SpatialReferenceSystemRepresentation srsRepresentation) {
  70.         try {
  71.             writeGmlStartElement(out, localName, namespaceURI);

  72.             Optional<DirectPosition> transformedPos = SpatialReferenceSystemConverter.convertDirectPosition(this, srsRepresentation.getSrs());
  73.             DirectPosition outPos = this;
  74.            
  75.             if (transformedPos.isPresent()) {
  76.                 outPos = transformedPos.get();
  77.             }

  78.             out.writeAttribute("srsDimension", Byte.toString(outPos.getSrs().getDimension()));
  79.             out.writeCharacters(outPos.toString());

  80.             out.writeEndElement();
  81.         } catch (Exception e) {
  82.             Logging.LOG.severe("Could not get GML for DirectPosition");
  83.         }
  84.     }

  85.     @Override
  86.     public String getGmlName() {
  87.         return GML_NAME;
  88.     }

  89. }