Skip to content

Commit 4a1f94f

Browse files
Merge pull request #960 from Kotlin/geo
Fix IO closing & add new useful extensions
2 parents 0be9e2e + 63142c8 commit 4a1f94f

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

dataframe-geo/src/main/kotlin/org/jetbrains/kotlinx/dataframe/geo/io/read.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,25 @@ import java.net.URL
1212
fun GeoDataFrame.Companion.readGeoJson(path: String): GeoDataFrame<*> = readGeoJson(asURL(path))
1313

1414
fun GeoDataFrame.Companion.readGeoJson(url: URL): GeoDataFrame<*> =
15-
(FeatureJSON().readFeatureCollection(url.openStream()) as SimpleFeatureCollection).toGeoDataFrame()
15+
url.openStream().use { inputStream ->
16+
val featureCollection = FeatureJSON().readFeatureCollection(inputStream) as SimpleFeatureCollection
17+
featureCollection.toGeoDataFrame()
18+
}
1619

1720
fun DataFrame.Companion.readGeoJson(path: String): GeoDataFrame<*> = GeoDataFrame.readGeoJson(path)
1821

1922
fun DataFrame.Companion.readGeoJson(url: URL): GeoDataFrame<*> = GeoDataFrame.readGeoJson(url)
2023

2124
fun GeoDataFrame.Companion.readShapefile(path: String): GeoDataFrame<*> = readShapefile(asURL(path))
2225

23-
fun GeoDataFrame.Companion.readShapefile(url: URL): GeoDataFrame<*> =
24-
ShapefileDataStoreFactory().createDataStore(url).featureSource.features.toGeoDataFrame()
26+
fun GeoDataFrame.Companion.readShapefile(url: URL): GeoDataFrame<*> {
27+
val dataStore = ShapefileDataStoreFactory().createDataStore(url)
28+
try {
29+
return dataStore.featureSource.features.toGeoDataFrame()
30+
} finally {
31+
dataStore.dispose()
32+
}
33+
}
2534

2635
fun DataFrame.Companion.readShapefile(path: String): GeoDataFrame<*> = GeoDataFrame.readShapefile(path)
2736

dataframe-geo/src/main/kotlin/org/jetbrains/kotlinx/dataframe/geo/io/write.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fun GeoDataFrame<*>.writeShapefile(directory: File) {
5252
e.printStackTrace()
5353
transaction.rollback()
5454
} finally {
55+
dataStore.dispose()
5556
transaction.close()
5657
}
5758
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.jetbrains.kotlinx.dataframe.geo.jts
2+
3+
import org.locationtech.jts.geom.LineString
4+
import org.locationtech.jts.geom.MultiLineString
5+
import org.locationtech.jts.geom.MultiPoint
6+
import org.locationtech.jts.geom.MultiPolygon
7+
import org.locationtech.jts.geom.Point
8+
import org.locationtech.jts.geom.Polygon
9+
10+
/**
11+
* Converts a [Polygon] to a [MultiPolygon] by wrapping it in a MultiPolygon.
12+
*
13+
* @receiver Polygon to be converted.
14+
* @return A MultiPolygon containing the original Polygon.
15+
*/
16+
fun Polygon.toMultiPolygon(): MultiPolygon {
17+
val geometryFactory = this.factory
18+
return geometryFactory.createMultiPolygon(arrayOf(this))
19+
}
20+
21+
/**
22+
* Converts a [Point] to a [MultiPoint] by wrapping it in a MultiPoint.
23+
*
24+
* @receiver Point to be converted.
25+
* @return A MultiPoint containing the original Point.
26+
*/
27+
fun Point.toMultiPoint(): MultiPoint {
28+
val geometryFactory = this.factory
29+
return geometryFactory.createMultiPoint(arrayOf(this))
30+
}
31+
32+
/**
33+
* Converts a [LineString] to a [MultiLineString] by wrapping it in a MultiLineString.
34+
*
35+
* @receiver LineString to be converted.
36+
* @return A MultiLineString containing the original LineString.
37+
*/
38+
fun LineString.toMultiLineString(): MultiLineString {
39+
val geometryFactory = this.factory
40+
return geometryFactory.createMultiLineString(arrayOf(this))
41+
}

0 commit comments

Comments
 (0)