diff --git a/api/android-sdk-core.api b/api/android-sdk-core.api index 0529e36..77458bd 100644 --- a/api/android-sdk-core.api +++ b/api/android-sdk-core.api @@ -303,6 +303,9 @@ public final class com.mapconductor.core.circle.CircleTileSet$Companion { public final boolean canTile(com.mapconductor.core.circle.CircleState); } public final class com.mapconductor.core.circle.CircleTiler { + public static final double MAX_LATITUDE; + public static final double TAP_STROKE_SLACK_PX; + public static final double TAP_SLACK_METERS; public com.mapconductor.core.circle.CircleTiler(com.mapconductor.core.circle.CircleTilingOptions); public com.mapconductor.core.circle.CircleTiler(com.mapconductor.core.circle.CircleTilingOptions, int, kotlin.jvm.internal.DefaultConstructorMarker); public final com.mapconductor.core.circle.CircleTilingOptions getOptions(); @@ -318,6 +321,8 @@ public final class com.mapconductor.core.circle.CircleTiler { public final java.lang.Object clear(kotlin.coroutines.Continuation); public com.mapconductor.core.circle.CircleTiler(); } +final class com.mapconductor.core.circle.CircleTiler$Companion { +} public final class com.mapconductor.core.circle.CircleTilerKt { public static final java.lang.String CIRCLE_TILE_LAYER_ID; public static final com.mapconductor.core.raster.RasterLayerState circleTileRasterLayerState(java.lang.String, int); @@ -1222,10 +1227,16 @@ public interface com.mapconductor.core.map.MapViewHolderInterface); public default com.mapconductor.core.features.GeoPoint fromScreenOffsetSync-k-4lQ0M(long); + public default com.mapconductor.core.projection.MapCoordinateSystem getCoordinateSystem(); + public default com.mapconductor.core.projection.ProjectedPoint toProjectedPoint(com.mapconductor.core.features.GeoPointInterface); + public default com.mapconductor.core.features.GeoPoint fromProjectedPoint(com.mapconductor.core.projection.ProjectedPoint); } public final class com.mapconductor.core.map.MapViewHolderInterface$DefaultImpls { public static java.lang.Object fromScreenOffset-3MmeM6k(com.mapconductor.core.map.MapViewHolderInterface, long, kotlin.coroutines.Continuation); public static com.mapconductor.core.features.GeoPoint fromScreenOffsetSync-k-4lQ0M(com.mapconductor.core.map.MapViewHolderInterface, long); + public static com.mapconductor.core.projection.MapCoordinateSystem getCoordinateSystem(com.mapconductor.core.map.MapViewHolderInterface); + public static com.mapconductor.core.projection.ProjectedPoint toProjectedPoint(com.mapconductor.core.map.MapViewHolderInterface, com.mapconductor.core.features.GeoPointInterface); + public static com.mapconductor.core.features.GeoPoint fromProjectedPoint(com.mapconductor.core.map.MapViewHolderInterface, com.mapconductor.core.projection.ProjectedPoint); } public abstract class com.mapconductor.core.map.MapViewHolderStoreBaseAsync extends com.mapconductor.core.map.StaticHolder> { public com.mapconductor.core.map.MapViewHolderStoreBaseAsync(); @@ -2850,6 +2861,22 @@ public final class com.mapconductor.core.projection.Earth { public static final double SEMI_MINOR_AXIS_METERS; public static final double ECCENTRICITY_SQUARED; } +public final class com.mapconductor.core.projection.MapCoordinateSystem { + public static final com.mapconductor.core.projection.MapCoordinateSystem$Companion Companion; + public com.mapconductor.core.projection.MapCoordinateSystem(java.lang.String, com.mapconductor.core.projection.ProjectedUnit); + public final java.lang.String getCode(); + public final com.mapconductor.core.projection.ProjectedUnit getUnit(); + public final java.lang.String component1(); + public final com.mapconductor.core.projection.ProjectedUnit component2(); + public final com.mapconductor.core.projection.MapCoordinateSystem copy(java.lang.String, com.mapconductor.core.projection.ProjectedUnit); + public java.lang.String toString(); + public int hashCode(); + public boolean equals(java.lang.Object); +} +public final class com.mapconductor.core.projection.MapCoordinateSystem$Companion { + public final com.mapconductor.core.projection.MapCoordinateSystem getWebMercator(); + public final com.mapconductor.core.projection.MapCoordinateSystem getWGS84(); +} public final class com.mapconductor.core.projection.ProjectedPoint { public com.mapconductor.core.projection.ProjectedPoint(double, double); public final double getX(); @@ -2861,6 +2888,14 @@ public final class com.mapconductor.core.projection.ProjectedPoint { public int hashCode(); public boolean equals(java.lang.Object); } +public final class com.mapconductor.core.projection.ProjectedUnit extends java.lang.Enum { + public static final com.mapconductor.core.projection.ProjectedUnit Meters; + public static final com.mapconductor.core.projection.ProjectedUnit Degrees; + public static final com.mapconductor.core.projection.ProjectedUnit Other; + public static com.mapconductor.core.projection.ProjectedUnit[] values(); + public static com.mapconductor.core.projection.ProjectedUnit valueOf(java.lang.String); + public static kotlin.enums.EnumEntries getEntries(); +} public interface com.mapconductor.core.projection.ProjectionInterface { public abstract com.mapconductor.core.projection.ProjectedPoint project(com.mapconductor.core.features.GeoPointInterface); public abstract com.mapconductor.core.features.GeoPointInterface unproject(com.mapconductor.core.projection.ProjectedPoint); diff --git a/src/main/java/com/mapconductor/core/map/MapViewHolderInterface.kt b/src/main/java/com/mapconductor/core/map/MapViewHolderInterface.kt index 2392ded..0948e68 100644 --- a/src/main/java/com/mapconductor/core/map/MapViewHolderInterface.kt +++ b/src/main/java/com/mapconductor/core/map/MapViewHolderInterface.kt @@ -3,6 +3,9 @@ package com.mapconductor.core.map import androidx.compose.ui.geometry.Offset import com.mapconductor.core.features.GeoPoint import com.mapconductor.core.features.GeoPointInterface +import com.mapconductor.core.projection.MapCoordinateSystem +import com.mapconductor.core.projection.ProjectedPoint +import com.mapconductor.core.projection.WebMercator interface MapViewHolderInterface { val mapView: ActualMapView @@ -36,4 +39,28 @@ interface MapViewHolderInterface { * 既定 null のまま黙って無反応にしない。 */ fun fromScreenOffsetSync(offset: Offset): GeoPoint? = null + + /** + * 地図エンジンが今使っている座標系。[toProjectedPoint] の値はこの座標系になる。 + * + * 既定は EPSG:3857 メートル。Mercator しか持たないエンジンと、地球儀表示中の + * エンジンはこのままでよい。地図ごとに座標系を持てるエンジン(ArcGIS など) + * だけが上書きする。 + */ + val coordinateSystem: MapCoordinateSystem + get() = MapCoordinateSystem.WebMercator + + /** + * 地理座標 → [coordinateSystem] の投影座標。**画面座標ではない。** + * + * 投影の定義域の外などで変換できないときは null。既定は EPSG:3857。 + */ + fun toProjectedPoint(position: GeoPointInterface): ProjectedPoint? = WebMercator.project(position) + + /** + * [coordinateSystem] の投影座標 → 地理座標。[toProjectedPoint] の逆。 + * + * 変換できないときは null。既定は EPSG:3857。 + */ + fun fromProjectedPoint(point: ProjectedPoint): GeoPoint? = GeoPoint.from(WebMercator.unproject(point)) } diff --git a/src/main/java/com/mapconductor/core/projection/MapCoordinateSystem.kt b/src/main/java/com/mapconductor/core/projection/MapCoordinateSystem.kt new file mode 100644 index 0000000..94a9171 --- /dev/null +++ b/src/main/java/com/mapconductor/core/projection/MapCoordinateSystem.kt @@ -0,0 +1,36 @@ +package com.mapconductor.core.projection + +/** + * 地図エンジンが地図を並べている座標系。**画面座標ではない。** + * + * [com.mapconductor.core.map.MapViewHolderInterface.toProjectedPoint] が返す + * [ProjectedPoint] がどの座標系の値かを示す。 + * + * - [code] は `"EPSG:3857"` のような識別子。エンジンが独自に定義した投影で、 + * EPSG などの番号を持たないときは null。 + * - [unit] は x / y の単位。 + * + * Mercator しか持たないエンジンと、地球儀表示中のエンジンは [WebMercator] + * (EPSG:3857 メートル)になる。 + */ +data class MapCoordinateSystem( + val code: String?, + val unit: ProjectedUnit, +) { + companion object { + /** EPSG:3857。x / y はメートル。 */ + val WebMercator = MapCoordinateSystem("EPSG:3857", ProjectedUnit.Meters) + + /** EPSG:4326。x が経度、y が緯度で、単位は度。 */ + val WGS84 = MapCoordinateSystem("EPSG:4326", ProjectedUnit.Degrees) + } +} + +/** [ProjectedPoint] の x / y の単位。 */ +enum class ProjectedUnit { + Meters, + Degrees, + + /** フィートなど上の 2 つ以外、またはエンジン独自の単位。 */ + Other, +} diff --git a/src/test/java/com/mapconductor/core/map/MapViewHolderProjectedPointTest.kt b/src/test/java/com/mapconductor/core/map/MapViewHolderProjectedPointTest.kt new file mode 100644 index 0000000..221a22c --- /dev/null +++ b/src/test/java/com/mapconductor/core/map/MapViewHolderProjectedPointTest.kt @@ -0,0 +1,57 @@ +package com.mapconductor.core.map + +import androidx.compose.ui.geometry.Offset +import com.mapconductor.core.features.GeoPoint +import com.mapconductor.core.features.GeoPointInterface +import com.mapconductor.core.projection.MapCoordinateSystem +import com.mapconductor.core.projection.ProjectedPoint +import com.mapconductor.core.projection.ProjectedUnit +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Test + +/** + * 投影座標の既定実装。上書きしないホルダは EPSG:3857 メートルを返す。 + */ +class MapViewHolderProjectedPointTest { + private class PlainHolder : MapViewHolderInterface { + override val mapView = Unit + override val map = Unit + + override fun toScreenOffset(position: GeoPointInterface): Offset? = null + } + + @Test + fun defaultCoordinateSystemIsWebMercator() { + val system = PlainHolder().coordinateSystem + assertEquals("EPSG:3857", system.code) + assertEquals(ProjectedUnit.Meters, system.unit) + assertEquals(MapCoordinateSystem.WebMercator, system) + } + + @Test + fun defaultProjectionIsEpsg3857Meters() { + // 東京駅。期待値は x = λ·a、y = a·ln(tan(π/4 + φ/2))(a = 6378137)。 + val point = PlainHolder().toProjectedPoint(GeoPoint(35.681236, 139.767125, 0.0)) + assertNotNull(point) + assertEquals(15558805.18, point!!.x, 0.01) + assertEquals(4256848.12, point.y, 0.01) + } + + @Test + fun defaultRoundTripReturnsTheSamePoint() { + val holder = PlainHolder() + val original = GeoPoint(-33.8688, 151.2093, 0.0) + val back = holder.fromProjectedPoint(holder.toProjectedPoint(original)!!) + assertNotNull(back) + assertEquals(original.latitude, back!!.latitude, 1e-9) + assertEquals(original.longitude, back.longitude, 1e-9) + } + + @Test + fun originMapsToNullIsland() { + val back = PlainHolder().fromProjectedPoint(ProjectedPoint(0.0, 0.0)) + assertEquals(0.0, back!!.latitude, 1e-12) + assertEquals(0.0, back.longitude, 1e-12) + } +}