/
GeometryToGeoMesaSimpleFeature.scala
66 lines (56 loc) · 2.57 KB
/
GeometryToGeoMesaSimpleFeature.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package geotrellis.geomesa.geotools
import geotrellis.proj4.{CRS => GCRS}
import geotrellis.vector.{Geometry, Line, MultiLine, MultiPoint, MultiPolygon, Point, Polygon}
import geotrellis.spark.util.cache.LRUCache
import com.vividsolutions.jts.{geom => jts}
import org.geotools.feature.simple.{SimpleFeatureBuilder, SimpleFeatureTypeBuilder}
import org.opengis.feature.simple.{SimpleFeature, SimpleFeatureType}
import org.locationtech.geomesa.accumulo.index.Constants
import com.typesafe.config.ConfigFactory
object GeometryToGeoMesaSimpleFeature {
val whenField = "when"
val whereField = "where"
lazy val featureTypeCache =
new LRUCache[String, SimpleFeatureType](
maxSize = ConfigFactory.load().getInt("geotrellis.geomesa.featureTypeCacheSize"),
sizeOf = {x => 1l}
)
def apply(featureName: String, geom: Geometry, featureId: Option[String], crs: Option[GCRS], data: Seq[(String, Any)]): SimpleFeature = {
val sft = featureTypeCache.getOrInsert(featureName, {
val sftb = (new SimpleFeatureTypeBuilder).minOccurs(1).maxOccurs(1).nillable(false)
sftb.setName(featureName)
crs match {
case Some(crs) => sftb.setSRS(s"EPSG:${crs.epsgCode.get}")
case None =>
}
geom match {
case pt: Point => sftb.add(whereField, classOf[jts.Point])
case ln: Line => sftb.add(whereField, classOf[jts.LineString])
case pg: Polygon => sftb.add(whereField, classOf[jts.Polygon])
case mp: MultiPoint => sftb.add(whereField, classOf[jts.MultiPoint])
case ml: MultiLine => sftb.add(whereField, classOf[jts.MultiLineString])
case mp: MultiPolygon => sftb.add(whereField, classOf[jts.MultiPolygon])
case g: Geometry => throw new Exception(s"Unhandled GeoTrellis Geometry $g")
}
sftb.setDefaultGeometry(whereField)
data.foreach({ case (key, value) => sftb
.minOccurs(1).maxOccurs(1).nillable(false)
.add(key, value.getClass)
})
sftb.buildFeatureType
})
if(data.map(_._1).contains(whenField)) sft.getUserData.put(Constants.SF_PROPERTY_START_TIME, whenField) // when field is date
val sfb = new SimpleFeatureBuilder(sft)
geom match {
case Point(pt) => sfb.add(pt)
case Line(ln) => sfb.add(ln)
case Polygon(pg) => sfb.add(pg)
case MultiPoint(mp) => sfb.add(mp)
case MultiLine(ml) => sfb.add(ml)
case MultiPolygon(mp) => sfb.add(mp)
case g: Geometry => throw new Exception(s"Unhandled Geotrellis Geometry $g")
}
data.foreach({ case (key, value) => sfb.add(value) })
sfb.buildFeature(featureId.orNull)
}
}