Skip to content

Commit e99fdeb

Browse files
committed
Generate lexicographically ordered IDs for writeGeojson features
1 parent ded3648 commit e99fdeb

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

dataframe-geo/src/main/kotlin/org/jetbrains/kotlinx/dataframe/geo/geotools/toSimpleFeatureCollection.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ fun GeoDataFrame<*>.toSimpleFeatureCollection(
4040
val featureCollection = ListFeatureCollection(featureType)
4141

4242
val featureBuilder = SimpleFeatureBuilder(featureType)
43-
43+
// if ID is present, SortedMap in DefaultFeatureCollection sorts rows by ID lexicographically
44+
// I couldn't disable writing it, so let's generate lexicographically sorted IDs
45+
val format = "f%0${df.rowsCount().toString().length}d"
4446
df.forEach { row ->
4547
val geometry = row["geometry"]
4648
featureBuilder.add(geometry)
4749
df.columnNames().filter { it != "geometry" }.forEach { colName ->
4850
featureBuilder.add(row[colName])
4951
}
50-
val feature: SimpleFeature = featureBuilder.buildFeature(null)
52+
val feature: SimpleFeature = featureBuilder.buildFeature(String.format(format, index()))
5153
featureCollection.add(feature)
5254
}
5355

dataframe-geo/src/test/kotlin/org/jetbrains/kotlinx/dataframe/geo/io/IOTest.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jetbrains.kotlinx.dataframe.geo.io
22

3+
import org.geotools.referencing.CRS
34
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
45
import org.jetbrains.kotlinx.dataframe.geo.GeoDataFrame
56
import org.jetbrains.kotlinx.dataframe.geo.toGeo
@@ -35,17 +36,18 @@ class IOTest {
3536
assert(geodf.crs == null)
3637
}
3738

38-
/*
39-
TODO: doesn't work for now - writers adds ids that breaks order when read features
4039
@Test
4140
fun writeGeoJson() {
4241
val tempFile = Files.createTempFile("simple_points", ".json").toFile()
4342
simplePointsGeoDf.writeGeoJson(tempFile)
4443

45-
assertEquals(simplePointsGeoDf, GeoDataFrame.readGeoJson(tempFile.toURI().toURL()))
44+
val loadedGeoDataFrame = GeoDataFrame.readGeoJson(tempFile.toURI().toURL())
45+
assertEquals(simplePointsGeoDf.df, loadedGeoDataFrame.df)
46+
// TODO: Doesn't work because of how equality between CRS is checked by geotools
47+
// assertEquals(simplePointsGeoDf, loadedGeoDataFrame)
4648

4749
tempFile.deleteOnExit()
48-
}*/
50+
}
4951

5052
@Test
5153
fun readShapefile() {

0 commit comments

Comments
 (0)