Skip to content
New issue

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

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

Already on GitHub? # to your account

Ported turf measurement center function #120

Merged
merged 1 commit into from
Feb 12, 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 @@ -231,7 +231,7 @@ fun bbox(geometry: MultiPolygon) = computeBbox(geometry.coordAll())
* @return A [BoundingBox] that covers the geometry.
*/
@ExperimentalTurfApi
fun bbox(feature: Feature): BoundingBox? = computeBbox(feature.coordAll() ?: emptyList())
fun bbox(feature: Feature): BoundingBox = computeBbox(feature.coordAll() ?: emptyList())

/**
* Takes a feature collection and calculates a bbox that covers all features in the collection.
Expand Down Expand Up @@ -463,3 +463,28 @@ fun midpoint(point1: Position, point2: Position): Position {

return destination(point1, dist / 2, heading)
}

/**
* Takes any kind of [Feature] and returns the center point. It will create a [BoundingBox] around the given
* [Feature] and calculates the center point of it.
*
* @param feature the feature to find the center for
* @return A [Point] holding the center coordinates
*/
@ExperimentalTurfApi
fun center(feature: Feature): Point {
val ext = bbox(feature)
val x = (ext.southwest.longitude + ext.northeast.longitude) / 2
val y = (ext.southwest.latitude + ext.northeast.latitude) / 2
return Point(Position(longitude = x, latitude = y))
}

/**
* It overloads the center(feature: Feature) method.
*
* @param geometry the [Geometry] to find the center for
*/
@ExperimentalTurfApi
fun center(geometry: Geometry): Point {
return center(Feature(geometry = geometry))
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@file:Suppress("MagicNumber")

package io.github.dellisd.spatialk.turf

import io.github.dellisd.spatialk.geojson.BoundingBox
import io.github.dellisd.spatialk.geojson.LineString
import io.github.dellisd.spatialk.geojson.Feature
import io.github.dellisd.spatialk.geojson.Point
import io.github.dellisd.spatialk.geojson.Polygon
import io.github.dellisd.spatialk.geojson.Position
Expand Down Expand Up @@ -121,4 +121,24 @@ class TurfMeasurementTest {
assertDoubleEquals(-76.6311, midpoint.longitude, 0.0001)
assertDoubleEquals(42.2101, midpoint.latitude, 0.0001)
}

@Test
fun testCenterFromFeature() {
val geometry = Polygon.fromJson(readResource("measurement/area/other.json"))

val centerPoint = center(Feature(geometry))

assertDoubleEquals(-75.71805238723755, centerPoint.coordinates.longitude, 0.0001)
assertDoubleEquals(45.3811030151199, centerPoint.coordinates.latitude, 0.0001)
}

@Test
fun testCenterFromGeometry() {
val geometry = Polygon.fromJson(readResource("measurement/area/other.json"))

val centerPoint = center(geometry)

assertDoubleEquals(-75.71805238723755, centerPoint.coordinates.longitude, 0.0001)
assertDoubleEquals(45.3811030151199, centerPoint.coordinates.latitude, 0.0001)
}
}