package com.mobimeo.gtfs import cats.effect._ import java.nio.file.Paths import com.mobimeo.gtfs.model.Stop import org.locationtech.jts.geom.Coordinate import org.locationtech.jts.algorithm.ConvexHull import org.locationtech.jts.geom.GeometryFactory import org.locationtech.jts.io.geojson._ object HullMain extends IOApp { def run(args: List[String]): IO[ExitCode] = args match { case file :: result :: _ => Blocker[IO].use { blocker => Gtfs[IO](Paths.get(file), blocker).use { gtfs => gtfs.read .stops[Stop] .collect { case Stop(_, _, _, _, Some(lat), Some(lon), _, _, _, _, _, _, _, _) => new Coordinate(lon, lat) } .compile .to(Array) .flatMap { coordinates => val factory = new GeometryFactory val writer = new GeoJsonWriter val hull = new ConvexHull(coordinates, factory) val geojson = writer.write(hull.getConvexHull()) fs2.Stream .emit(geojson) .covary[IO] .through(fs2.text.utf8Encode) .through(fs2.io.file.writeAll(Paths.get(result), blocker)) .compile .drain } .as(ExitCode.Success) } } case _ => IO(println("Files expected")).as(ExitCode.Error) } }