Skip to content

Commit 177bab9

Browse files
committed
refactor(library): Introduce Polyline and Polygon type aliases
This commit introduces `Polyline` and `Polygon` as type aliases for `List<LatLng>` to improve code readability and provide stronger semantic meaning. This change makes it explicit whether a list of coordinates represents a line or a closed area. The following files and classes have been updated to use these new type aliases: - `PolyUtil`: Functions now accept `Polyline` or `Polygon` instead of a generic `List<LatLng>`. - `SphericalUtil`: `computeLength` and `computeArea` now operate on `Polyline` and `Polygon` respectively. - `LineString`: The `coordinates` property is now of type `Polyline`. - `DataPolygon`: Boundary properties now use the `Polygon` type alias.
1 parent 02076cd commit 177bab9

File tree

5 files changed

+45
-29
lines changed

5 files changed

+45
-29
lines changed

library/src/main/java/com/google/maps/android/PolyUtil.kt

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.maps.android
1818

1919
import com.google.android.gms.maps.model.LatLng
20+
import com.google.maps.android.data.Polygon
21+
import com.google.maps.android.data.Polyline
2022
import com.google.maps.android.MathUtil.clamp
2123
import com.google.maps.android.MathUtil.hav
2224
import com.google.maps.android.MathUtil.havDistance
@@ -62,7 +64,7 @@ object PolyUtil {
6264
* @return `true` if the point is inside the polygon, `false` otherwise.
6365
*/
6466
@JvmStatic
65-
fun containsLocation(point: LatLng, polygon: List<LatLng>, geodesic: Boolean): Boolean {
67+
fun containsLocation(point: LatLng, polygon: Polygon, geodesic: Boolean): Boolean {
6668
return containsLocation(point.latitude, point.longitude, polygon, geodesic)
6769
}
6870

@@ -74,7 +76,7 @@ object PolyUtil {
7476
fun containsLocation(
7577
latitude: Double,
7678
longitude: Double,
77-
polygon: List<LatLng>,
79+
polygon: Polygon,
7880
geodesic: Boolean
7981
): Boolean {
8082
if (polygon.isEmpty()) {
@@ -122,7 +124,7 @@ object PolyUtil {
122124
@JvmOverloads
123125
fun isLocationOnEdge(
124126
point: LatLng,
125-
polygon: List<LatLng>,
127+
polygon: Polygon,
126128
geodesic: Boolean,
127129
tolerance: Double = DEFAULT_TOLERANCE
128130
): Boolean {
@@ -145,7 +147,7 @@ object PolyUtil {
145147
@JvmOverloads
146148
fun isLocationOnPath(
147149
point: LatLng,
148-
polyline: List<LatLng>,
150+
polyline: Polyline,
149151
geodesic: Boolean,
150152
tolerance: Double = DEFAULT_TOLERANCE
151153
): Boolean {
@@ -154,12 +156,12 @@ object PolyUtil {
154156

155157
private fun isLocationOnEdgeOrPath(
156158
point: LatLng,
157-
poly: List<LatLng>,
159+
polyline: Polyline,
158160
closed: Boolean,
159161
geodesic: Boolean,
160162
toleranceEarth: Double
161163
): Boolean {
162-
val idx = locationIndexOnEdgeOrPath(point, poly, closed, geodesic, toleranceEarth)
164+
val idx = locationIndexOnEdgeOrPath(point, polyline, closed, geodesic, toleranceEarth)
163165

164166
return (idx >= 0)
165167
}
@@ -183,7 +185,7 @@ object PolyUtil {
183185
@JvmOverloads
184186
fun locationIndexOnPath(
185187
point: LatLng,
186-
poly: List<LatLng>,
188+
poly: Polyline,
187189
geodesic: Boolean,
188190
tolerance: Double = DEFAULT_TOLERANCE
189191
): Int {
@@ -209,7 +211,7 @@ object PolyUtil {
209211
@JvmStatic
210212
fun locationIndexOnEdgeOrPath(
211213
point: LatLng,
212-
poly: List<LatLng>,
214+
poly: Polyline,
213215
closed: Boolean,
214216
geodesic: Boolean,
215217
toleranceEarth: Double
@@ -299,8 +301,8 @@ object PolyUtil {
299301
* simplified poly.
300302
* @return a simplified poly produced by the Douglas-Peucker algorithm
301303
*/
302-
@JvmStatic
303-
fun simplify(poly: List<LatLng>, tolerance: Double): List<LatLng> {
304+
@JvmStatic
305+
fun simplify(poly: Polyline, tolerance: Double): Polyline {
304306
require(poly.isNotEmpty()) { "Polyline must have at least 1 point" }
305307
require(tolerance > 0) { "Tolerance must be greater than zero" }
306308

@@ -339,13 +341,13 @@ object PolyUtil {
339341
* If this point is farther than the specified tolerance, it is kept, and the algorithm is
340342
* applied recursively to the two new segments.
341343
*
342-
* @param poly The polyline to be simplified.
344+
* @param polyline The polyline to be simplified.
343345
* @param tolerance The tolerance in meters.
344346
* @return A boolean array where `true` indicates that the point at the corresponding index
345347
* should be kept in the simplified polyline.
346348
*/
347-
private fun douglasPeucker(poly: List<LatLng>, tolerance: Double): BooleanArray {
348-
val n = poly.size
349+
private fun douglasPeucker(polyline: Polyline, tolerance: Double): BooleanArray {
350+
val n = polyline.size
349351
// We start with a boolean array that will mark the points to keep.
350352
// Initially, only the first and last points are marked for keeping.
351353
val keepPoint = BooleanArray(n) { false }
@@ -368,7 +370,7 @@ object PolyUtil {
368370
// For the current segment, we find the point that is farthest from the line
369371
// connecting the start and end points.
370372
for (idx in start + 1 until end) {
371-
val dist = distanceToLine(poly[idx], poly[start], poly[end])
373+
val dist = distanceToLine(polyline[idx], polyline[start], polyline[end])
372374
if (dist > maxDist) {
373375
maxDist = dist
374376
maxIdx = idx
@@ -393,13 +395,13 @@ object PolyUtil {
393395
* Returns true if the provided list of points is a closed polygon (i.e., the first and last
394396
* points are the same), and false if it is not
395397
*
396-
* @param poly polyline or polygon
398+
* @param polyline polyline or polygon
397399
* @return true if the provided list of points is a closed polygon (i.e., the first and last
398400
* points are the same), and false if it is not
399401
*/
400402
@JvmStatic
401-
fun isClosedPolygon(poly: List<LatLng>): Boolean {
402-
return poly.isNotEmpty() && poly.first() == poly.last()
403+
fun isClosedPolygon(polyline: Polyline): Boolean {
404+
return polyline.isNotEmpty() && polyline.first() == polyline.last()
403405
}
404406

405407
/**
@@ -447,7 +449,7 @@ object PolyUtil {
447449
* Decodes an encoded path string into a sequence of LatLngs.
448450
*/
449451
@JvmStatic
450-
fun decode(encodedPath: String): List<LatLng> {
452+
fun decode(encodedPath: String): Polyline {
451453
val len = encodedPath.length
452454
val path = mutableListOf<LatLng>()
453455
var index = 0
@@ -484,7 +486,7 @@ object PolyUtil {
484486
* Encodes a sequence of LatLngs into an encoded path string.
485487
*/
486488
@JvmStatic
487-
fun encode(path: List<LatLng>): String {
489+
fun encode(path: Polyline): String {
488490
var lastLat: Long = 0
489491
var lastLng: Long = 0
490492
val result = StringBuilder()

library/src/main/java/com/google/maps/android/SphericalUtil.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.google.maps.android
1818

1919
import com.google.android.gms.maps.model.LatLng
20+
import com.google.maps.android.data.Polygon
21+
import com.google.maps.android.data.Polyline
2022
import com.google.maps.android.MathUtil.EARTH_RADIUS
2123
import com.google.maps.android.MathUtil.arcHav
2224
import com.google.maps.android.MathUtil.havDistance
@@ -202,7 +204,7 @@ object SphericalUtil {
202204
* Returns the length of the given path, in meters, on Earth.
203205
*/
204206
@JvmStatic
205-
fun computeLength(path: List<LatLng>): Double {
207+
fun computeLength(path: Polyline): Double {
206208
if (path.size < 2) {
207209
return 0.0
208210
}
@@ -228,7 +230,7 @@ object SphericalUtil {
228230
* @return The path's area in square meters.
229231
*/
230232
@JvmStatic
231-
fun computeArea(path: List<LatLng>): Double {
233+
fun computeArea(path: Polygon): Double {
232234
return abs(computeSignedArea(path))
233235
}
234236

@@ -241,7 +243,7 @@ object SphericalUtil {
241243
* @return The loop's area in square meters.
242244
*/
243245
@JvmStatic
244-
fun computeSignedArea(path: List<LatLng>): Double {
246+
fun computeSignedArea(path: Polygon): Double {
245247
return computeSignedArea(path, EARTH_RADIUS)
246248
}
247249

@@ -251,7 +253,7 @@ object SphericalUtil {
251253
* Used by SphericalUtilTest.
252254
*/
253255
@JvmStatic
254-
fun computeSignedArea(path: List<LatLng>, radius: Double): Double {
256+
fun computeSignedArea(path: Polygon, radius: Double): Double {
255257
val size = path.size
256258
if (size < 3) {
257259
return 0.0

library/src/main/java/com/google/maps/android/data/DataPolygon.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ interface DataPolygon<T> : Geometry<T> {
2828
/**
2929
* Gets an array of outer boundary coordinates
3030
*/
31-
val outerBoundaryCoordinates: List<LatLng>
31+
val outerBoundaryCoordinates: Polygon
3232

3333
/**
3434
* Gets an array of arrays of inner boundary coordinates
3535
*/
36-
val innerBoundaryCoordinates: List<List<LatLng>>
36+
val innerBoundaryCoordinates: List<Polygon>
3737
}

library/src/main/java/com/google/maps/android/data/Geometry.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
*/
1616
package com.google.maps.android.data
1717

18+
import com.google.android.gms.maps.model.LatLng
19+
20+
/**
21+
* A Polyline is a list of LatLngs where each LatLng is a vertex of the line.
22+
*/
23+
typealias Polyline = List<LatLng>
24+
25+
/**
26+
* A Polygon is a list of LatLngs where each LatLng is a vertex of the polygon.
27+
*/
28+
typealias Polygon = List<LatLng>
29+
1830
/**
1931
* An abstraction that represents a Geometry object
2032
*

library/src/main/java/com/google/maps/android/data/LineString.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ import com.google.android.gms.maps.model.LatLng
2222
* [com.google.maps.android.data.kml.KmlLineString] and
2323
* [com.google.maps.android.data.geojson.GeoJsonLineString]
2424
*/
25-
open class LineString(coordinates: List<LatLng>) : Geometry<List<LatLng>> {
25+
open class LineString(coordinates: Polyline) : Geometry<Polyline> {
2626

27-
private val _coordinates: List<LatLng> = coordinates
27+
private val _coordinates: Polyline = coordinates
2828

2929
/**
3030
* Gets the coordinates of the LineString
3131
*/
32-
open val coordinates: List<LatLng>
32+
open val coordinates: Polyline
3333
get() = _coordinates
3434

3535
/**
@@ -40,7 +40,7 @@ open class LineString(coordinates: List<LatLng>) : Geometry<List<LatLng>> {
4040
/**
4141
* Gets the geometry object
4242
*/
43-
override val geometryObject: List<LatLng> = _coordinates
43+
override val geometryObject: Polyline = _coordinates
4444

4545
override fun equals(other: Any?): Boolean {
4646
if (this === other) return true

0 commit comments

Comments
 (0)