Skip to content

Commit d127dc7

Browse files
fix IO closing
1 parent 0c5cb9b commit d127dc7

File tree

2 files changed

+13
-3
lines changed
  • dataframe-geo/src/main/kotlin/org/jetbrains/kotlinx/dataframe/geo/io

2 files changed

+13
-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
}

0 commit comments

Comments
 (0)