11package fi.hsl.jore4.mapmatching.service.matching.test
22
3+ import fi.hsl.jore4.mapmatching.util.GeoToolsUtils.transformCRS
34import org.geolatte.geom.jts.JTS
45import org.geotools.api.feature.simple.SimpleFeature
56import org.geotools.api.feature.simple.SimpleFeatureType
67import org.geotools.data.collection.ListFeatureCollection
78import org.geotools.feature.simple.SimpleFeatureBuilder
89import org.geotools.feature.simple.SimpleFeatureTypeBuilder
10+ import org.geotools.geometry.jts.Geometries
911import org.geotools.geopkg.FeatureEntry
1012import org.geotools.geopkg.GeoPackage
1113import org.geotools.referencing.crs.DefaultGeographicCRS
14+ import org.locationtech.jts.geom.Geometry
1215import org.locationtech.jts.geom.LineString
1316import java.io.File
1417
1518object GeoPackageUtils {
19+ private const val GEOMETRY_COLUMN_NAME = " geometry"
20+
1621 fun createGeoPackage (
1722 file : File ,
18- failedSegments : List <SegmentMatchFailure >
23+ failedSegments : List <SegmentMatchFailure >,
24+ isBufferPolygonInsteadOfLineString : Boolean
1925 ): GeoPackage {
2026 val geoPkg = GeoPackage (file)
2127 geoPkg.init ()
2228
2329 try {
2430 failedSegments.forEach { segment ->
2531 val featureTypeName: String = segment.routeId
26- val featureType: SimpleFeatureType = createFeatureTypeForFailedSegment(featureTypeName)
32+ val featureType: SimpleFeatureType =
33+ createFeatureTypeForFailedSegment(featureTypeName, isBufferPolygonInsteadOfLineString)
2734
2835 val entry = FeatureEntry ()
2936 entry.identifier = segment.routeId
30- entry.description = " Failed segment within map-matching"
37+ entry.description =
38+ if (isBufferPolygonInsteadOfLineString) {
39+ " Buffered geometry for failed ${segment.routeId} segment within map-matching"
40+ } else {
41+ " LineString for failed ${segment.routeId} segment within map-matching"
42+ }
3143
32- geoPkg.add(entry, createFeatureCollection(segment, featureType))
44+ geoPkg.add(entry, createFeatureCollection(segment, featureType, isBufferPolygonInsteadOfLineString ))
3345
3446 // Not sure, if this is really needed.
3547 geoPkg.createSpatialIndex(entry)
@@ -42,12 +54,16 @@ object GeoPackageUtils {
4254 }
4355 }
4456
45- private fun createFeatureTypeForFailedSegment (name : String ): SimpleFeatureType {
57+ private fun createFeatureTypeForFailedSegment (
58+ name : String ,
59+ isBufferPolygonInsteadOfLineString : Boolean
60+ ): SimpleFeatureType {
4661 val builder = SimpleFeatureTypeBuilder ()
4762 builder.name = name
48- builder.crs = DefaultGeographicCRS .WGS84
4963
50- builder.add(" geometry" , LineString ::class .java)
64+ val geomType = if (isBufferPolygonInsteadOfLineString) Geometries .POLYGON else Geometries .LINESTRING
65+
66+ builder.add(GEOMETRY_COLUMN_NAME , geomType.binding, DefaultGeographicCRS .WGS84 )
5167 builder.add(" length" , Double ::class .java)
5268 builder.add(" startStopId" , String ::class .java)
5369 builder.add(" endStopId" , String ::class .java)
@@ -61,23 +77,34 @@ object GeoPackageUtils {
6177
6278 private fun createFeatureCollection (
6379 failedSegment : SegmentMatchFailure ,
64- featureType : SimpleFeatureType
80+ featureType : SimpleFeatureType ,
81+ isBufferPolygonInsteadOfLineString : Boolean
6582 ): ListFeatureCollection {
66- val feature: SimpleFeature = createFeature(failedSegment, featureType)
83+ val feature: SimpleFeature = createFeature(failedSegment, featureType, isBufferPolygonInsteadOfLineString )
6784
6885 return ListFeatureCollection (featureType, feature)
6986 }
7087
7188 private fun createFeature (
7289 failedSegment : SegmentMatchFailure ,
73- type : SimpleFeatureType
90+ type : SimpleFeatureType ,
91+ isBufferPolygonInsteadOfLineString : Boolean
7492 ): SimpleFeature {
7593 val builder = SimpleFeatureBuilder (type)
7694
7795 failedSegment.run {
7896 val lineString: LineString = JTS .to(sourceRouteGeometry)
7997
80- builder.add(lineString)
98+ if (isBufferPolygonInsteadOfLineString) {
99+ val lineString3067: Geometry = transformCRS(lineString, 4326 , 3067 )
100+ val bufferPolygon3067: Geometry = lineString3067.buffer(bufferRadius.value)
101+ val bufferPolygon: Geometry = transformCRS(bufferPolygon3067, 3067 , 4326 )
102+
103+ builder.add(bufferPolygon)
104+ } else {
105+ builder.add(lineString)
106+ }
107+
81108 builder.add(sourceRouteLength)
82109 builder.add(startStopId)
83110 builder.add(endStopId)
0 commit comments