View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2015 Voyager Search and MITRE
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Apache License, Version 2.0 which
5    * accompanies this distribution and is available at
6    *    http://www.apache.org/licenses/LICENSE-2.0.txt
7    ******************************************************************************/
8   
9   package org.locationtech.spatial4j.shape.jts;
10  
11  
12  import org.locationtech.spatial4j.context.SpatialContext;
13  import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
14  import org.locationtech.spatial4j.shape.BaseShape;
15  import org.locationtech.spatial4j.shape.Circle;
16  import org.locationtech.spatial4j.shape.Point;
17  import org.locationtech.spatial4j.shape.Rectangle;
18  import org.locationtech.spatial4j.shape.Shape;
19  import org.locationtech.spatial4j.shape.SpatialRelation;
20  import org.locationtech.spatial4j.shape.impl.PointImpl;
21  import org.locationtech.jts.geom.CoordinateSequence;
22  
23  /** Wraps a {@link org.locationtech.jts.geom.Point}. */
24  public class JtsPoint extends BaseShape<JtsSpatialContext> implements Point {
25  
26    private org.locationtech.jts.geom.Point pointGeom;
27    private final boolean empty;//cached
28  
29    /** A simple constructor without normalization / validation. */
30    public JtsPoint(org.locationtech.jts.geom.Point pointGeom, JtsSpatialContext ctx) {
31      super(ctx);
32      this.pointGeom = pointGeom;
33      this.empty = pointGeom.isEmpty();
34    }
35  
36    public org.locationtech.jts.geom.Point getGeom() {
37      return pointGeom;
38    }
39  
40    @Override
41    public boolean isEmpty() {
42      return empty;
43    }
44  
45    @Override
46    public org.locationtech.spatial4j.shape.Point getCenter() {
47      return this;
48    }
49  
50    @Override
51    public boolean hasArea() {
52      return false;
53    }
54  
55    @Override
56    public double getArea(SpatialContext ctx) {
57      return 0;
58    }
59  
60    @Override
61    public Rectangle getBoundingBox() {
62      return ctx.makeRectangle(this, this);
63    }
64  
65    @Override
66    public Circle getBuffered(double distance, SpatialContext ctx) {
67      return ctx.makeCircle(this, distance);
68    }
69  
70    @Override
71    public SpatialRelation relate(Shape other) {
72      // ** NOTE ** the overall order of logic is kept consistent here with simple.PointImpl.
73      if (isEmpty() || other.isEmpty())
74        return SpatialRelation.DISJOINT;
75      if (other instanceof org.locationtech.spatial4j.shape.Point)
76        return this.equals(other) ? SpatialRelation.INTERSECTS : SpatialRelation.DISJOINT;
77      return other.relate(this).transpose();
78    }
79  
80    @Override
81    public double getX() {
82      return isEmpty() ? Double.NaN : pointGeom.getX();
83    }
84  
85    @Override
86    public double getY() {
87      return isEmpty() ? Double.NaN : pointGeom.getY();
88    }
89  
90    @Override
91    public double getLat() {
92      return getY();
93    }
94  
95    @Override
96    public double getLon() {
97      return getX();
98    }
99  
100   @Override
101   public void reset(double x, double y) {
102     assert ! isEmpty();
103     CoordinateSequence cSeq = pointGeom.getCoordinateSequence();
104     cSeq.setOrdinate(0, CoordinateSequence.X, x);
105     cSeq.setOrdinate(0, CoordinateSequence.Y, y);
106   }
107 
108   @Override
109   public String toString() {
110     return "Pt(x="+getX()+",y="+getY()+")";
111   }
112 
113   @Override
114   public boolean equals(Object o) {
115     return PointImpl.equals(this,o);
116   }
117 
118   @Override
119   public int hashCode() {
120     return PointImpl.hashCode(this);
121   }
122 }