@@ -7,6 +7,7 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
77import org.jetbrains.kotlinx.dataframe.geo.GeoDataFrame
88import org.jetbrains.kotlinx.dataframe.geo.geotools.toGeoDataFrame
99import org.jetbrains.kotlinx.dataframe.io.asUrl
10+ import java.io.File
1011import java.net.URL
1112
1213fun GeoDataFrame.Companion.readGeoJson (path : String ): GeoDataFrame <* > = readGeoJson(asUrl(path))
@@ -21,9 +22,30 @@ fun DataFrame.Companion.readGeoJson(path: String): GeoDataFrame<*> = GeoDataFram
2122
2223fun DataFrame.Companion.readGeoJson (url : URL ): GeoDataFrame <* > = GeoDataFrame .readGeoJson(url)
2324
24- fun GeoDataFrame.Companion.readShapefile (path : String ): GeoDataFrame <* > = readShapefile(asUrl(path))
25+ /* *
26+ * Examples:
27+ * ```
28+ * GeoDataFrame.readShapefile("simple_points")
29+ * GeoDataFrame.readShapefile("simple_points/simple_points.shp")
30+ * ```
31+ *
32+ * @param path path to *.shp or *.shp.gz file, or to a directory containing such a file
33+ */
34+ fun GeoDataFrame.Companion.readShapefile (path : String ): GeoDataFrame <* > {
35+ val url = resolveShapefileUrl(path)
36+ return readShapeFileImpl(url)
37+ }
2538
2639fun GeoDataFrame.Companion.readShapefile (url : URL ): GeoDataFrame <* > {
40+ val resolvedUrl = if (url.protocol == " file" ) {
41+ resolveShapefileUrl(url.path)
42+ } else {
43+ url
44+ }
45+ return readShapeFileImpl(resolvedUrl)
46+ }
47+
48+ private fun readShapeFileImpl (url : URL ): GeoDataFrame <* > {
2749 val dataStore = ShapefileDataStoreFactory ().createDataStore(url)
2850 try {
2951 return dataStore.featureSource.features.toGeoDataFrame()
@@ -32,6 +54,20 @@ fun GeoDataFrame.Companion.readShapefile(url: URL): GeoDataFrame<*> {
3254 }
3355}
3456
57+ private fun resolveShapefileUrl (path : String ): URL {
58+ val file = File (path)
59+ val shpFile = when {
60+ file.isDirectory -> findShapefileInDirectory(file)
61+ else -> file
62+ }
63+ return shpFile.toURI().toURL()
64+ }
65+
66+ private fun findShapefileInDirectory (dir : File ): File =
67+ File (dir, " ${dir.name} .shp" ).takeIf { it.exists() }
68+ ? : File (dir, " ${dir.name} .shp.gz" ).takeIf { it.exists() }
69+ ? : throw IllegalArgumentException (" No shapefile found in directory: ${dir.absolutePath} " )
70+
3571fun DataFrame.Companion.readShapefile (path : String ): GeoDataFrame <* > = GeoDataFrame .readShapefile(path)
3672
3773fun DataFrame.Companion.readShapefile (url : URL ): GeoDataFrame <* > = GeoDataFrame .readShapefile(url)
0 commit comments