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

Fix visualization for geom_map #184

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
coord map fixed
  • Loading branch information
IKrukov-HORIS committed Aug 11, 2020
commit 7680cf4e1f53c166deae7720f526258e8c4fd707
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package jetbrains.datalore.plot.builder.coord

import jetbrains.datalore.base.gcommon.collect.ClosedRange
import jetbrains.datalore.plot.builder.coord.map.MercatorProjectionX
import jetbrains.datalore.plot.builder.coord.map.MercatorProjectionY

object CoordProviders {
Expand All @@ -23,21 +22,24 @@ object CoordProviders {
}

fun map(
ratio: Double,
xLim: ClosedRange<Double>? = null,
yLim: ClosedRange<Double>? = null
): CoordProvider {
// Mercator projection is cylindrical thus we don't really need 'projection X'
// return ProjectionCoordProvider.withProjectionY(
// MercatorProjectionY(),
// xLim,
// yLim
// )

return ProjectionCoordProvider.withProjectionXY(
MercatorProjectionX(),
return ProjectionCoordProvider.withProjectionY(
MercatorProjectionY(),
ratio,
xLim,
yLim
)

// return ProjectionCoordProvider.withProjectionXY(
// MercatorProjectionX(),
// MercatorProjectionY(),
// ratio,
// xLim,
// yLim
// )
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ import jetbrains.datalore.plot.builder.layout.axis.GuideBreaks
import jetbrains.datalore.plot.common.data.SeriesUtil

internal class ProjectionCoordProvider private constructor(
private val myProjectionX: Projection,
private val myProjectionY: Projection,
private val myProjectionX: Projection?,
private val myProjectionY: Projection?,
ratio: Double,
xLim: ClosedRange<Double>?,
yLim: ClosedRange<Double>?
)// square grid
// : FixedRatioCoordProvider(1.0, xLim, yLim) {
: CoordProviderBase(xLim, yLim) {
: FixedRatioCoordProvider(ratio, xLim, yLim) {

override fun buildAxisScaleX(
scaleProto: Scale<Double>,
domain: ClosedRange<Double>,
axisLength: Double,
breaks: GuideBreaks
): Scale<Double> {
return buildAxisScaleWithProjection(
myProjectionX,
scaleProto,
domain,
axisLength,
breaks
)
return if (myProjectionX != null) {
buildAxisScaleWithProjection(
myProjectionX,
scaleProto,
domain,
axisLength,
breaks
)
} else super.buildAxisScaleX(scaleProto, domain, axisLength, breaks)
}

override fun buildAxisScaleY(
Expand All @@ -45,66 +47,35 @@ internal class ProjectionCoordProvider private constructor(
axisLength: Double,
breaks: GuideBreaks
): Scale<Double> {
return buildAxisScaleWithProjection(
myProjectionY,
scaleProto,
domain,
axisLength,
breaks
)
return if (myProjectionY != null) {
buildAxisScaleWithProjection(
myProjectionY,
scaleProto,
domain,
axisLength,
breaks
)
} else super.buildAxisScaleY(scaleProto, domain, axisLength, breaks)
}


override fun adjustDomainsImpl(
xDomain: ClosedRange<Double>,
yDomain: ClosedRange<Double>,
displaySize: DoubleVector
): Pair<ClosedRange<Double>, ClosedRange<Double>> {
val dx = myProjectionX.apply(180.0) - myProjectionX.apply(-180.0)
val dy = myProjectionY.apply(85.0) - myProjectionY.apply(-85.0)
val orig_dx = 360.0
val orig_dy = 170.0
val ratio = (dy/dx ) / (orig_dy/orig_dx)

val prvd = FixedRatioCoordProvider( ratio, null, null )
return prvd.adjustDomains(xDomain, yDomain, displaySize)

// val newDomainX = ClosedRange( myProjectionX.apply(-180.0), myProjectionX.apply(180.0))
// val newDomainY = ClosedRange( myProjectionY.apply(-85.0), myProjectionY.apply(85.0))
// val prvd = FixedRatioCoordProvider( 1.0 , null, null )

// return prvd.adjustDomains(newDomainX, newDomainY, displaySize)
}

companion object {
// fun withProjectionY(
// projectionY: Projection,
// xLim: ClosedRange<Double>?,
// yLim: ClosedRange<Double>?
// ): CoordProvider {
// return ProjectionCoordProvider(
// null,
// projectionY,
// xLim,
// yLim
// )
// }

fun withProjectionXY(
projectionX: Projection,
fun withProjectionY(
projectionY: Projection,
ratio: Double,
xLim: ClosedRange<Double>?,
yLim: ClosedRange<Double>?
): CoordProvider {
return ProjectionCoordProvider(
projectionX,
null,
projectionY,
ratio,
xLim,
yLim
)
}

private fun buildAxisScaleWithProjection(
private fun buildAxisScaleWithProjection(
projection: Projection, scaleProto: Scale<Double>,
domain: ClosedRange<Double>, axisLength: Double, breaks: GuideBreaks
): Scale<Double> {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class CoordMapTest : jetbrains.datalore.plot.builder.coord.CoordTestBas
}

companion object {
private val PROVIDER = CoordProviders.map()
private val PROVIDER = CoordProviders.map(1.0)

private val DATA_SPAN = DoubleVector(10.0, 10.0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ class CoordConfig private constructor(name: String, options: Map<String, Any>) :
private fun createForName(name: String, options: Map<String, Any>): CoordConfig {
return CoordConfig(name, options)
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import jetbrains.datalore.plot.config.Option.CoordName.MAP
internal object CoordProto {

// option names
private const val X_LIM = "xlim"
private const val Y_LIM = "ylim"
private const val RATIO = "ratio"
const val X_LIM = "xlim"
const val Y_LIM = "ylim"
const val RATIO = "ratio"
private const val EXPAND = "expand" // todo
private const val ORIENTATION = "orientation" // todo
private const val PROJECTION = "projection" // todo
Expand All @@ -27,7 +27,7 @@ internal object CoordProto {
return when (coordName) {
CARTESIAN -> CoordProviders.cartesian(xLim, yLim)
FIXED -> CoordProviders.fixed(options.getDouble(RATIO) ?: 1.0, xLim, yLim)
MAP -> CoordProviders.map(xLim, yLim)
MAP -> CoordProviders.map(options.getDouble(RATIO) ?: 1.0, xLim, yLim)
else -> throw IllegalArgumentException("Unknown coordinate system name: '$coordName'")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GeomProtoClientSide(geomKind: GeomKind) : GeomProto(geomKind) {
GeomKind.RASTER,
GeomKind.IMAGE -> CoordProviders.fixed(1.0)

GeomKind.MAP -> CoordProviders.map()
// GeomKind.MAP -> CoordProviders.map()

else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package jetbrains.datalore.plot.config

import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.DataFrame
import jetbrains.datalore.plot.base.GeomKind
import jetbrains.datalore.plot.builder.assemble.GuideOptions
import jetbrains.datalore.plot.builder.assemble.TypedScaleProviderMap
import jetbrains.datalore.plot.builder.coord.CoordProvider
Expand All @@ -17,6 +18,10 @@ import jetbrains.datalore.plot.config.PlotConfigClientSideUtil.createGuideOption
import jetbrains.datalore.plot.config.theme.ThemeConfig
import jetbrains.datalore.plot.config.transform.PlotSpecTransform
import jetbrains.datalore.plot.config.transform.migration.MoveGeomPropertiesToLayerMigration
import jetbrains.datalore.base.gcommon.collect.ClosedRange
import jetbrains.datalore.plot.common.data.SeriesUtil.span
import jetbrains.datalore.base.spatial.MercatorUtils.getMercatorX
import jetbrains.datalore.base.spatial.MercatorUtils.getMercatorY

class PlotConfigClientSide private constructor(opts: Map<String, Any>) : PlotConfig(opts) {

Expand All @@ -28,20 +33,83 @@ class PlotConfigClientSide private constructor(opts: Map<String, Any>) : PlotCon
get() = true

init {
coordProvider = createCoordProvider()
guideOptionsMap = createGuideOptionsMap(this.scaleConfigs)
}

private fun createCoordProvider(): CoordProvider {
val coord = CoordConfig.create(get(COORD)!!)
var coordProvider = coord.coord
if (!hasOwn(COORD)) {
// if coord wasn't set explicitly then geom can provide its own preferred coord system
for (layerConfig in layerConfigs) {
val geomProtoClientSide = layerConfig.geomProto as GeomProtoClientSide
if (geomProtoClientSide.hasPreferredCoordinateSystem()) {
if (layerConfig.geomProto.geomKind == GeomKind.MAP) {
val data = layerConfig.combinedData
val varX = layerConfig.getVariableForAes(Aes.X)
val varY = layerConfig.getVariableForAes(Aes.Y)

if (varX != null && varY != null) {
val xRange = data.range(varX)
val yRange = data.range(varY)

val xLim = getRangeOrNull(CoordProto.X_LIM)
val yLim = getRangeOrNull(CoordProto.Y_LIM)

coordProvider = getMapCoordinateProvider(xRange!!, yRange!!, xLim, yLim)
}
} else if (geomProtoClientSide.hasPreferredCoordinateSystem()) {
coordProvider = geomProtoClientSide.preferredCoordinateSystem()
}
}
}
this.coordProvider = coordProvider
guideOptionsMap = createGuideOptionsMap(this.scaleConfigs)

return coordProvider
}

private fun getMapCoordinateProvider(
xDomain: ClosedRange<Double>,
yDomain: ClosedRange<Double>,
xLim: ClosedRange<Double>?,
yLim: ClosedRange<Double>?
): CoordProvider {
val projDX = span(
doProjection({ getMercatorX(it) }, xDomain)!!
)

val projDY = span(
doProjection({ getMercatorY(it) }, yDomain)!!
)

val dx = span(xDomain)
val dy = span(yDomain)

val ratio = (projDY / projDX) / (dy / dx)

@Suppress("NAME_SHADOWING")
val xLim = doProjection({ getMercatorX(it) }, xLim)
@Suppress("NAME_SHADOWING")
val yLim = doProjection({ getMercatorY(it) }, yLim)

val opts: MutableMap<String, Any> = mutableMapOf(
Option.Meta.NAME to Option.CoordName.MAP,
CoordProto.RATIO to ratio
)

if (xLim != null) {
opts[CoordProto.X_LIM] = xLim
}

if (yLim != null) {
opts[CoordProto.Y_LIM] = yLim
}


return CoordConfig.create(opts).coord
}

private fun doProjection(proj: ((Double) -> Double), range: ClosedRange<Double>?) = range?.let {
ClosedRange(proj(range.lowerEnd), proj(range.upperEnd))
}

override fun createLayerConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ open class PolygonWithCoordMapDemo : SimpleDemoBase() {
.color(constant(Color.DARK_MAGENTA))
.alpha(constant(0.5))
.build()
val coord = CoordProviders.map()


val coord = CoordProviders.map(1.0) // FIX ME!
.createCoordinateSystem(domainX, lengthX, domainY, lengthY)
val layer = jetbrains.datalore.plot.builder.SvgLayerRenderer(
aes,
Expand Down