9.5. Using Converters ProgrammaticallyΒΆ

Converters and SimpleFeatureTypes can be imported through maven and used directly in code:

<dependency>
  <groupId>org.locationtech.geomesa</groupId>
  <!-- pull in all converters, or use a specific converter, e.g. geomesa-convert-json_2.12 -->
  <artifactId>geomesa-convert-all_2.12</artifactId>
</dependency>
import org.locationtech.geomesa.convert.ConverterConfigLoader
import org.locationtech.geomesa.convert2.SimpleFeatureConverter
import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypeLoader

val sft = SimpleFeatureTypeLoader.sftForName("example-csv").getOrElse {
  throw new RuntimeException("Could not load feature type")
}
val conf = ConverterConfigLoader.configForName("example-csv").getOrElse {
  throw new RuntimeException("Could not load converter definition")
}
val converter = SimpleFeatureConverter(sft, conf)
try {
  val is: InputStream = ??? // load your input data
  val features = converter.process(is)
  try {
    features.foreach(???) // do something with the conversion result
  } finally {
    features.close() // will also close the input stream
  }
} finally {
  converter.close() // clean up any resources associated with your converter
}