1 /***********************************************************************
2  * Copyright (c) 2013-2025 General Atomics Integrated Intelligence, 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  * https://www.apache.org/licenses/LICENSE-2.0
7  ***********************************************************************/
8 
9 package org.locationtech.geomesa.convert.all
10 
11 import com.typesafe.config.Config
12 import org.geotools.api.feature.simple.SimpleFeatureType
13 import org.locationtech.geomesa.convert.avro.AvroConverterFactory
14 import org.locationtech.geomesa.convert.json.JsonConverterFactory
15 import org.locationtech.geomesa.convert.parquet.ParquetConverterFactory
16 import org.locationtech.geomesa.convert.shp.ShapefileConverterFactory
17 import org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory
18 import org.locationtech.geomesa.convert.xml.XmlConverterFactory
19 import org.locationtech.geomesa.convert2.{SimpleFeatureConverter, SimpleFeatureConverterFactory}
20 
21 import java.io.InputStream
22 import java.util.Locale
23 import scala.util.Try
24 
25 object TypeAwareInference {
26 
27   private val mappings = Map[String, Class[_ <: SimpleFeatureConverterFactory]](
28     "avro"    -> classOf[AvroConverterFactory],
29     "json"    -> classOf[JsonConverterFactory],
30     "csv"     -> classOf[DelimitedTextConverterFactory],
31     "tsv"     -> classOf[DelimitedTextConverterFactory],
32     "parquet" -> classOf[ParquetConverterFactory],
33     "shp"     -> classOf[ShapefileConverterFactory],
34     "xml"     -> classOf[XmlConverterFactory]
35   )
36 
37   /**
38    * Infer a converter based on a data sample
39    *
40    * @param format data format (e.g. csv, avro, json, etc)
41    * @param is input stream to convert
42    * @param sft simple feature type, if known
43    * @param hints implementation specific hints about the input
44    * @return
45    */
46   def infer(
47       format: String,
48       is: () => InputStream,
49       sft: Option[SimpleFeatureType],
50       hints: Map[String, AnyRef]): Try[(SimpleFeatureType, Config)] = {
51     val priority = mappings.get(format.toLowerCase(Locale.US)).map { clas =>
52       Ordering.by[SimpleFeatureConverterFactory, Boolean](f => !clas.isAssignableFrom(f.getClass)) // note: false sorts first
53     }
54     SimpleFeatureConverter.infer(is, sft, hints, priority)
55   }
56 }
Line Stmt Id Pos Tree Symbol Tests Code
27 61677 1244 - 1663 Apply scala.collection.generic.GenMapFactory.apply scala.Predef.Map.apply[String, Class[_ <: org.locationtech.geomesa.convert2.SimpleFeatureConverterFactory]](scala.Predef.ArrowAssoc[String]("avro").->[Class[org.locationtech.geomesa.convert.avro.AvroConverterFactory]](classOf[org.locationtech.geomesa.convert.avro.AvroConverterFactory]), scala.Predef.ArrowAssoc[String]("json").->[Class[org.locationtech.geomesa.convert.json.JsonConverterFactory]](classOf[org.locationtech.geomesa.convert.json.JsonConverterFactory]), scala.Predef.ArrowAssoc[String]("csv").->[Class[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory]](classOf[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory]), scala.Predef.ArrowAssoc[String]("tsv").->[Class[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory]](classOf[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory]), scala.Predef.ArrowAssoc[String]("parquet").->[Class[org.locationtech.geomesa.convert.parquet.ParquetConverterFactory]](classOf[org.locationtech.geomesa.convert.parquet.ParquetConverterFactory]), scala.Predef.ArrowAssoc[String]("shp").->[Class[org.locationtech.geomesa.convert.shp.ShapefileConverterFactory]](classOf[org.locationtech.geomesa.convert.shp.ShapefileConverterFactory]), scala.Predef.ArrowAssoc[String]("xml").->[Class[org.locationtech.geomesa.convert.xml.XmlConverterFactory]](classOf[org.locationtech.geomesa.convert.xml.XmlConverterFactory]))
28 61670 1304 - 1346 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String]("avro").->[Class[org.locationtech.geomesa.convert.avro.AvroConverterFactory]](classOf[org.locationtech.geomesa.convert.avro.AvroConverterFactory])
29 61671 1352 - 1394 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String]("json").->[Class[org.locationtech.geomesa.convert.json.JsonConverterFactory]](classOf[org.locationtech.geomesa.convert.json.JsonConverterFactory])
30 61672 1400 - 1451 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String]("csv").->[Class[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory]](classOf[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory])
31 61673 1457 - 1508 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String]("tsv").->[Class[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory]](classOf[org.locationtech.geomesa.convert.text.DelimitedTextConverterFactory])
32 61674 1514 - 1559 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String]("parquet").->[Class[org.locationtech.geomesa.convert.parquet.ParquetConverterFactory]](classOf[org.locationtech.geomesa.convert.parquet.ParquetConverterFactory])
33 61675 1565 - 1612 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String]("shp").->[Class[org.locationtech.geomesa.convert.shp.ShapefileConverterFactory]](classOf[org.locationtech.geomesa.convert.shp.ShapefileConverterFactory])
34 61676 1618 - 1659 Apply scala.Predef.ArrowAssoc.-> scala.Predef.ArrowAssoc[String]("xml").->[Class[org.locationtech.geomesa.convert.xml.XmlConverterFactory]](classOf[org.locationtech.geomesa.convert.xml.XmlConverterFactory])
51 61678 2174 - 2183 Select java.util.Locale.US java.util.Locale.US
51 61679 2155 - 2184 Apply java.lang.String.toLowerCase format.toLowerCase(java.util.Locale.US)
51 61684 2142 - 2331 Apply scala.Option.map TypeAwareInference.this.mappings.get(format.toLowerCase(java.util.Locale.US)).map[scala.math.Ordering[org.locationtech.geomesa.convert2.SimpleFeatureConverterFactory]](((clas: Class[_ <: org.locationtech.geomesa.convert2.SimpleFeatureConverterFactory]) => scala.`package`.Ordering.by[org.locationtech.geomesa.convert2.SimpleFeatureConverterFactory, Boolean](((f: org.locationtech.geomesa.convert2.SimpleFeatureConverterFactory) => clas.isAssignableFrom(f.getClass()).unary_!))(math.this.Ordering.Boolean)))
52 61680 2286 - 2296 Apply java.lang.Object.getClass f.getClass()
52 61681 2263 - 2297 Select scala.Boolean.unary_! clas.isAssignableFrom(f.getClass()).unary_!
52 61682 2257 - 2257 Select scala.math.Ordering.Boolean math.this.Ordering.Boolean
52 61683 2206 - 2298 ApplyToImplicitArgs scala.math.Ordering.by scala.`package`.Ordering.by[org.locationtech.geomesa.convert2.SimpleFeatureConverterFactory, Boolean](((f: org.locationtech.geomesa.convert2.SimpleFeatureConverterFactory) => clas.isAssignableFrom(f.getClass()).unary_!))(math.this.Ordering.Boolean)
54 61685 2336 - 2390 Apply org.locationtech.geomesa.convert2.SimpleFeatureConverter.infer org.locationtech.geomesa.convert2.SimpleFeatureConverter.infer(is, sft, hints, priority)