Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Created May 17, 2019 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobiasd/979420e25dc719e64247cf20adeb643d to your computer and use it in GitHub Desktop.
Save Dobiasd/979420e25dc719e64247cf20adeb643d to your computer and use it in GitHub Desktop.
package com.regions
import org.geotools.geojson.feature.FeatureJSON
import org.geotools.geometry.jts.JTS
import org.geotools.referencing.CRS
import org.locationtech.jts.geom.Polygon
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import java.nio.charset.StandardCharsets
@Component
class RegionStuff {
private val log = LoggerFactory.getLogger(this::class.java)
@Scheduled(fixedRateString = "1000")
fun testRegion() {
val json = FeatureJSON()
val geojsonString =
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[-162.1,69.5],[-111.8,46.8],[-71.4,69.5],[-162.1,69.5]]]}}]}"
val featureCollection = json.readFeatureCollection(
geojsonString.byteInputStream(StandardCharsets.UTF_8)
)
assert(featureCollection.size() == 1)
val polygon = featureCollection.features().next().defaultGeometryProperty.value as Polygon
// Project WGS84 to UTM
val sourceCrs = CRS.decode("EPSG:4326", true)
val targetCrs = if (CRS.getAxisOrder(sourceCrs) == CRS.AxisOrder.EAST_NORTH) {
CRS.decode("AUTO:42001,${polygon.centroid.x},${polygon.centroid.y}")
} else {
CRS.decode("AUTO:42001,${polygon.centroid.y},${polygon.centroid.x}")
}
val transform = CRS.findMathTransform(sourceCrs, targetCrs)
val projectedPolygon = JTS.transform(polygon, transform) as Polygon
log.info("original $polygon")
log.info("projected $projectedPolygon")
log.info("original-area ${polygon.area}")
log.info("projected-area ${projectedPolygon.area}")
}
}
@SpringBootApplication
@EnableScheduling
class RegionsApplication
fun main(args: Array<String>) {
runApplication<RegionsApplication>(*args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment