1 /***********************************************************************
2  * Copyright (c) 2013-2024 Commonwealth Computer Research, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Apache License, Version 2.0
5  * which accompanies this distribution and is available at
6  * http://www.opensource.org/licenses/apache2.0.php.
7  ***********************************************************************/
8 
9 package org.locationtech.geomesa.utils.index
10 
11 import org.locationtech.jts.geom.{Envelope, Geometry}
12 import org.locationtech.jts.index.{SpatialIndex => JSTSpatialIndex}
13 
14 import scala.collection.JavaConverters._
15 
16 class WrapperIndex[T, Index <: JSTSpatialIndex](val indexBuider : () => Index) extends SpatialIndex[T] with Serializable {
17 
18   protected var index = indexBuider()
19 
20   override def insert(geom: Geometry, key: String, value: T): Unit = index.insert(geom.getEnvelopeInternal, (key, value))
21 
22   override def remove(geom: Geometry, key: String): T = {
23     val envelope = geom.getEnvelopeInternal
24     index.query(envelope).asScala.asInstanceOf[Seq[(String, T)]].find(_._1 == key) match {
25       case None => null.asInstanceOf[T]
26       case Some(kv) => index.remove(envelope, kv); kv._2
27     }
28   }
29 
30   override def get(geom: Geometry, key: String): T = {
31     val intersect = index.query(geom.getEnvelopeInternal).asScala.asInstanceOf[Seq[(String, T)]]
32     intersect.find(_._1 == key).map(_._2).getOrElse(null.asInstanceOf[T])
33   }
34 
35   override def query(xmin: Double, ymin: Double, xmax: Double, ymax: Double): Iterator[T] =
36     index.query(new Envelope(xmin, xmax, ymin, ymax)).iterator.asScala.asInstanceOf[Iterator[(String, T)]].map(_._2)
37 
38   override def query(): Iterator[T] = {
39     query(-180,-90,180,90)
40   }
41 
42   override def size(): Int = query().size
43 
44   override def clear(): Unit = index = indexBuider()
45 }
Line Stmt Id Pos Tree Symbol Tests Code
18 10502 824 - 837 Apply scala.Function0.apply WrapperIndex.this.indexBuider.apply()
20 10503 921 - 945 Apply org.locationtech.jts.geom.Geometry.getEnvelopeInternal geom.getEnvelopeInternal()
20 10504 947 - 959 Apply scala.Tuple2.apply scala.Tuple2.apply[String, T](key, value)
20 10505 908 - 960 Apply org.locationtech.jts.index.SpatialIndex.insert WrapperIndex.this.index.insert(geom.getEnvelopeInternal(), scala.Tuple2.apply[String, T](key, value))
23 10506 1039 - 1063 Apply org.locationtech.jts.geom.Geometry.getEnvelopeInternal geom.getEnvelopeInternal()
24 10507 1068 - 1089 Apply org.locationtech.jts.index.SpatialIndex.query WrapperIndex.this.index.query(envelope)
24 10508 1134 - 1145 Apply java.lang.Object.== x$1._1.==(key)
24 10509 1068 - 1146 Apply scala.collection.IterableLike.find scala.collection.JavaConverters.asScalaBufferConverter[E](WrapperIndex.this.index.query(envelope)).asScala.asInstanceOf[Seq[(String, T)]].find(((x$1: (String, T)) => x$1._1.==(key)))
25 10510 1174 - 1178 Literal <nosymbol> null
25 10511 1174 - 1194 TypeApply scala.Any.asInstanceOf null.asInstanceOf[T]
25 10512 1174 - 1194 Block scala.Any.asInstanceOf null.asInstanceOf[T]
26 10513 1218 - 1244 Apply org.locationtech.jts.index.SpatialIndex.remove WrapperIndex.this.index.remove(envelope, kv)
26 10514 1246 - 1251 Select scala.Tuple2._2 kv._2
26 10515 1215 - 1251 Block <nosymbol> { WrapperIndex.this.index.remove(envelope, kv); kv._2 }
31 10516 1350 - 1374 Apply org.locationtech.jts.geom.Geometry.getEnvelopeInternal geom.getEnvelopeInternal()
31 10517 1338 - 1375 Apply org.locationtech.jts.index.SpatialIndex.query WrapperIndex.this.index.query(geom.getEnvelopeInternal())
31 10518 1338 - 1414 TypeApply scala.Any.asInstanceOf scala.collection.JavaConverters.asScalaBufferConverter[E](WrapperIndex.this.index.query(geom.getEnvelopeInternal())).asScala.asInstanceOf[Seq[(String, T)]]
32 10519 1434 - 1445 Apply java.lang.Object.== x$2._1.==(key)
32 10520 1451 - 1455 Select scala.Tuple2._2 x$3._2
32 10521 1467 - 1471 Literal <nosymbol> null
32 10522 1467 - 1487 TypeApply scala.Any.asInstanceOf null.asInstanceOf[T]
32 10523 1419 - 1488 Apply scala.Option.getOrElse intersect.find(((x$2: (String, T)) => x$2._1.==(key))).map[T](((x$3: (String, T)) => x$3._2)).getOrElse[T](null.asInstanceOf[T])
36 10524 1590 - 1648 Apply java.util.List.iterator WrapperIndex.this.index.query(new org.locationtech.jts.geom.Envelope(xmin, xmax, ymin, ymax)).iterator()
36 10525 1697 - 1701 Select scala.Tuple2._2 x$4._2
36 10526 1590 - 1702 Apply scala.collection.Iterator.map scala.collection.JavaConverters.asScalaIteratorConverter[E](WrapperIndex.this.index.query(new org.locationtech.jts.geom.Envelope(xmin, xmax, ymin, ymax)).iterator()).asScala.asInstanceOf[Iterator[(String, T)]].map[T](((x$4: (String, T)) => x$4._2))
39 10527 1748 - 1770 Apply org.locationtech.geomesa.utils.index.WrapperIndex.query WrapperIndex.this.query(-180.0, -90.0, 180.0, 90.0)
42 10528 1805 - 1817 Select scala.collection.TraversableOnce.size WrapperIndex.this.query().size
44 10529 1858 - 1871 Apply scala.Function0.apply WrapperIndex.this.indexBuider.apply()
44 10530 1850 - 1871 Apply org.locationtech.geomesa.utils.index.WrapperIndex.index_= WrapperIndex.this.index_=(WrapperIndex.this.indexBuider.apply())