Last active
November 24, 2021 17:11
-
-
Save neogeogre/c016484a1c364bce480df37975d02533 to your computer and use it in GitHub Desktop.
Sample to create read a geotools FeatureCollection with properties in java / kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
import org.geotools.feature.DefaultFeatureCollection | |
import org.geotools.feature.simple.SimpleFeatureBuilder | |
import org.geotools.feature.simple.SimpleFeatureTypeBuilder | |
import org.geotools.referencing.crs.DefaultGeographicCRS.WGS84 | |
import org.locationtech.jts.geom.Coordinate | |
import org.locationtech.jts.geom.GeometryFactory | |
import org.locationtech.jts.geom.Point | |
fun main() { | |
val typeBuilder = SimpleFeatureTypeBuilder() | |
typeBuilder.name = "points" | |
typeBuilder.crs = WGS84 | |
typeBuilder.add("Geometry", Point::class.java) | |
typeBuilder.add("blabla", Int::class.java) | |
typeBuilder.add("age", Int::class.java) | |
typeBuilder.add("stuff", String::class.java) | |
val type = typeBuilder.buildFeatureType() | |
val featureBuilder = SimpleFeatureBuilder(type) | |
featureBuilder.add(GeometryFactory().createPoint(Coordinate(5.0, 2.0))) | |
// respect properties order | |
featureBuilder.add(32) | |
featureBuilder.add(999) | |
featureBuilder.add("hello world") | |
val featureCollection = DefaultFeatureCollection("id-fc1", type) | |
featureCollection.add(featureBuilder.buildFeature("id-f1")) | |
val feature = featureCollection.features().next() | |
println(feature.getProperty("age").value) | |
println(feature.getProperty("stuff").value) | |
println(feature.getProperty("blabla").value) | |
println((feature.defaultGeometry as Point).coordinate) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import org.geotools.feature.FeatureIterator | |
import org.geotools.geojson.feature.FeatureJSON | |
import org.locationtech.jts.geom.MultiPolygon | |
import org.opengis.feature.Feature | |
import org.opengis.feature.simple.SimpleFeature | |
fun read() { | |
// https://datahub.io/core/geo-countries | |
val featureJson = this::class.java.getResource("/countries.geojson")?.readText()!! | |
val fc = FeatureJSON().readFeatureCollection(featureJson) | |
val features: FeatureIterator<Feature> = fc.features() | |
while(features.hasNext()) { | |
val feature = features.next() | |
val simpleFeature = (feature as SimpleFeature) | |
println(simpleFeature.getProperty("ISO_A3").value) | |
val multiPolygon = simpleFeature.defaultGeometry as MultiPolygon | |
(0 until multiPolygon.numGeometries).forEach { | |
println(multiPolygon.getGeometryN(it).coordinates.size) | |
} | |
} | |
println("over") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment