Class GeometryItemDistance

 1 /*
 2  * Copyright (c) 2016 Martin Davis.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12  
13 package org.locationtech.jts.index.strtree;
14  
15 import org.locationtech.jts.geom.Geometry;
16  
17 /**
18  * An {@link ItemDistance} function for 
19  * items which are {@link Geometry}s,
20  * using the {@link Geometry#distance(Geometry)} method.
21  * <p>
22  * To make this distance function suitable for
23  * using to query a single index tree,
24  * the distance metric is <i>anti-reflexive</i>.
25  * That is, if the two arguments are the same Geometry object,
26  * the distance returned is {@link Double.MAX_VALUE}.
27  * 
28  * @author Martin Davis
29  *
30  */
31 public class GeometryItemDistance
32 implements ItemDistance
33 {
34   /**
35    * Computes the distance between two {@link Geometry} items,
36    * using the {@link Geometry#distance(Geometry)} method.
37    * 
38    * @param item1 an item which is a Geometry
39    * @param item2 an item which is a Geometry
40    * @return the distance between the geometries
41    * @throws ClassCastException if either item is not a Geometry
42    */
43   public double distance(ItemBoundable item1, ItemBoundable item2) {
44     if (item1 == item2) return Double.MAX_VALUE;
45     Geometry g1 = (Geometry) item1.getItem();
46     Geometry g2 = (Geometry) item2.getItem();
47     return g1.distance(g2);    
48   }
49 }
50  
51