Skip to content

Fix IO closing & add new useful extensions #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ class GeoDataFrame<T : WithGeometry>(val df: DataFrame<T>, val crs: CoordinateRe
* @return A new `GeoDataFrame` with reprojected geometries and the specified CRS.
*/
fun applyCrs(targetCrs: CoordinateReferenceSystem): GeoDataFrame<T> {
if (crs == null) {
return GeoDataFrame(df, targetCrs)
}
if (targetCrs == this.crs) return this
// Use WGS 84 by default TODO
val sourceCRS: CoordinateReferenceSystem = this.crs
val sourceCRS: CoordinateReferenceSystem = this.crs ?: DEFAULT_CRS
val transform = CRS.findMathTransform(sourceCRS, targetCrs, true)
return GeoDataFrame(
df.update { geometry }.with { JTS.transform(it, transform) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ import java.net.URL
fun GeoDataFrame.Companion.readGeoJson(path: String): GeoDataFrame<*> = readGeoJson(asURL(path))

fun GeoDataFrame.Companion.readGeoJson(url: URL): GeoDataFrame<*> =
(FeatureJSON().readFeatureCollection(url.openStream()) as SimpleFeatureCollection).toGeoDataFrame()
url.openStream().use { inputStream ->
val featureCollection = FeatureJSON().readFeatureCollection(inputStream) as SimpleFeatureCollection
featureCollection.toGeoDataFrame()
}

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

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

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

fun GeoDataFrame.Companion.readShapefile(url: URL): GeoDataFrame<*> =
ShapefileDataStoreFactory().createDataStore(url).featureSource.features.toGeoDataFrame()
fun GeoDataFrame.Companion.readShapefile(url: URL): GeoDataFrame<*> {
val dataStore = ShapefileDataStoreFactory().createDataStore(url)
try {
return dataStore.featureSource.features.toGeoDataFrame()
} finally {
dataStore.dispose()
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fun GeoDataFrame<*>.writeShapefile(directory: File) {
e.printStackTrace()
transaction.rollback()
} finally {
dataStore.dispose()
transaction.close()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.jetbrains.kotlinx.dataframe.geo.jts

import org.locationtech.jts.geom.LineString
import org.locationtech.jts.geom.MultiLineString
import org.locationtech.jts.geom.MultiPoint
import org.locationtech.jts.geom.MultiPolygon
import org.locationtech.jts.geom.Point
import org.locationtech.jts.geom.Polygon

/**
* Converts a [Polygon] to a [MultiPolygon] by wrapping it in a MultiPolygon.
*
* @receiver Polygon to be converted.
* @return A MultiPolygon containing the original Polygon.
*/
fun Polygon.toMultiPolygon(): MultiPolygon {
val geometryFactory = this.factory
return geometryFactory.createMultiPolygon(arrayOf(this))
}

/**
* Converts a [Point] to a [MultiPoint] by wrapping it in a MultiPoint.
*
* @receiver Point to be converted.
* @return A MultiPoint containing the original Point.
*/
fun Point.toMultiPoint(): MultiPoint {
val geometryFactory = this.factory
return geometryFactory.createMultiPoint(arrayOf(this))
}

/**
* Converts a [LineString] to a [MultiLineString] by wrapping it in a MultiLineString.
*
* @receiver LineString to be converted.
* @return A MultiLineString containing the original LineString.
*/
fun LineString.toMultiLineString(): MultiLineString {
val geometryFactory = this.factory
return geometryFactory.createMultiLineString(arrayOf(this))
}
Loading