Skip to content
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

Measurement - envelope #134

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -5,7 +5,7 @@ package io.github.dellisd.spatialk.geojson
*
* @property bbox An optional bounding box used to represent the limits of the object's geometry.
*/
interface GeoJson {
sealed interface GeoJson {
val bbox: BoundingBox?

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package io.github.dellisd.spatialk.turf
import io.github.dellisd.spatialk.geojson.BoundingBox
import io.github.dellisd.spatialk.geojson.Feature
import io.github.dellisd.spatialk.geojson.FeatureCollection
import io.github.dellisd.spatialk.geojson.GeoJson
import io.github.dellisd.spatialk.geojson.Geometry
import io.github.dellisd.spatialk.geojson.GeometryCollection
import io.github.dellisd.spatialk.geojson.LineString
Expand Down Expand Up @@ -652,3 +653,25 @@ fun greatCircle(start: Position, end: Position, pointCount: Int = 100, antimerid
}
}


/**
* Takes any [GeoJson] and returns a [Feature] containing a rectangular [Polygon] that encompasses all vertices.
* @param geoJson input containing any coordinates
* @return a rectangular [Polygon] feature that encompasses all vertices
*/
@ExperimentalTurfApi
fun envelope(geoJson: GeoJson): Feature {
val coordinates = when (geoJson) {
is Feature -> geoJson.coordAll()
is FeatureCollection -> geoJson.coordAll()
is GeometryCollection -> geoJson.coordAll()
is Geometry -> geoJson.coordAll()
}.orEmpty()

val bbox = geoJson.bbox ?: computeBbox(coordinates)

return Feature(
geometry = bboxPolygon(bbox),
bbox = bbox
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
package io.github.dellisd.spatialk.turf

import io.github.dellisd.spatialk.geojson.BoundingBox
import io.github.dellisd.spatialk.geojson.Feature
import io.github.dellisd.spatialk.geojson.LineString
import io.github.dellisd.spatialk.geojson.MultiLineString
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
import io.github.dellisd.spatialk.geojson.dsl.featureCollection
import io.github.dellisd.spatialk.geojson.dsl.geometryCollection
import io.github.dellisd.spatialk.geojson.dsl.lineString
import io.github.dellisd.spatialk.geojson.dsl.point
import io.github.dellisd.spatialk.geojson.dsl.polygon
import io.github.dellisd.spatialk.turf.utils.assertDoubleEquals
import io.github.dellisd.spatialk.turf.utils.readResource
Expand Down Expand Up @@ -186,4 +189,54 @@ class TurfMeasurementTest {
greatCircle(start, antipodal)
}
}

@Test
fun envelopeProcessesFeatureCollection() {
val fc = featureCollection {
feature(
geometry = point(102.0, 0.5)
)
feature(
geometry = lineString {
point(102.0, -10.0)
point(103.0, 1.0)
point(104.0, 0.0)
point(130.0, 4.0)
}
)
feature(
geometry = polygon {
ring {
point(102.0, -10.0)
point(103.0, 1.0)
point(104.0, 0.0)
point(130.0, 4.0)
point(20.0, 0.0)
point(101.0, 0.0)
point(101.0, 1.0)
point(100.0, 1.0)
point(100.0, 0.0)
}
}
)
}

val enveloped = envelope(fc)

assertIs<Polygon>(enveloped.geometry, "geometry type should be Polygon")
assertEquals(
listOf(
listOf(
Position(20.0, -10.0),
Position(130.0, -10.0),
Position(130.0, 4.0),
Position(20.0, 4.0),
Position(20.0, -10.0),
)
),
(enveloped.geometry as Polygon).coordinates,
"positions should be correct"
)
}

}