Skip to content

update for Document the code #3 #58

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
71 changes: 12 additions & 59 deletions geoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,21 @@
<body>
<div id="cesiumContainer"></div>
<script>
// Your Cesium ion access token here
Cesium.Ion.defaultAccessToken = '$accessToken'

// Create the viewer
const viewer = new Cesium.Viewer('cesiumContainer')

function interpolateColor(color1, color2, factor) {
const result = new Cesium.Color()
result.red = color1.red + factor * (color2.red - color1.red)
result.green =
color1.green + factor * (color2.green - color1.green)
result.green = color1.green + factor * (color2.green - color1.green)
result.blue = color1.blue + factor * (color2.blue - color1.blue)
result.alpha = '$visualType' == 'size' ? 0.2 :
color1.alpha + factor * (color2.alpha - color1.alpha)
result.alpha = '$visualType' == 'size' ? 0.2 : color1.alpha + factor * (color2.alpha - color1.alpha)
return result
}

function getColor(value, min, max) {
const factor = (value - min) / (max - min)
return interpolateColor(
Cesium.Color.BLUE,
Cesium.Color.RED,
factor
)
return interpolateColor(Cesium.Color.BLUE, Cesium.Color.RED, factor)
}

function getPixelSize(value, min, max) {
Expand All @@ -103,41 +94,29 @@

geoJsonData.features.forEach((feature) => {
const id = feature.properties.id
const time = Cesium.JulianDate.fromIso8601(
feature.properties.time
)
const time = Cesium.JulianDate.fromIso8601(feature.properties.time)
const value = feature.properties.value
const coordinates = feature.geometry.coordinates

if (!timeSeriesMap.has(id)) {
timeSeriesMap.set(id, [])
}
timeSeriesMap.get(id).push({ time, value, coordinates })

minValue = Math.min(minValue, value)
maxValue = Math.max(maxValue, value)
})

return { timeSeriesMap, minValue, maxValue }
}

function createTimeSeriesEntities(
timeSeriesData,
startTime,
stopTime
) {
const dataSource = new Cesium.CustomDataSource(
'AgentTorch Simulation'
)
function createTimeSeriesEntities(timeSeriesData, startTime, stopTime) {
const dataSource = new Cesium.CustomDataSource('AgentTorch Simulation')

for (const [id, timeSeries] of timeSeriesData.timeSeriesMap) {
const entity = new Cesium.Entity({
id: id,
availability: new Cesium.TimeIntervalCollection([
new Cesium.TimeInterval({
start: startTime,
stop: stopTime,
}),
new Cesium.TimeInterval({ start: startTime, stop: stopTime }),
]),
position: new Cesium.SampledPositionProperty(),
point: {
Expand All @@ -150,30 +129,13 @@
})

timeSeries.forEach(({ time, value, coordinates }) => {
const position = Cesium.Cartesian3.fromDegrees(
coordinates[0],
coordinates[1]
)
const position = Cesium.Cartesian3.fromDegrees(coordinates[0], coordinates[1])
entity.position.addSample(time, position)
entity.properties.value.addSample(time, value)
entity.point.color.addSample(
time,
getColor(
value,
timeSeriesData.minValue,
timeSeriesData.maxValue
)
)
entity.point.color.addSample(time, getColor(value, timeSeriesData.minValue, timeSeriesData.maxValue))

if ('$visualType' == 'size') {
entity.point.pixelSize.addSample(
time,
getPixelSize(
value,
timeSeriesData.minValue,
timeSeriesData.maxValue
)
)
entity.point.pixelSize.addSample(time, getPixelSize(value, timeSeriesData.minValue, timeSeriesData.maxValue))
}
})

Expand All @@ -183,27 +145,21 @@
return dataSource
}

// Example time-series GeoJSON data
const geoJsons = $data

const start = Cesium.JulianDate.fromIso8601('$startTime')
const stop = Cesium.JulianDate.fromIso8601('$stopTime')

viewer.clock.startTime = start.clone()
viewer.clock.stopTime = stop.clone()
viewer.clock.currentTime = start.clone()
viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP
viewer.clock.multiplier = 3600 // 1 hour per second
viewer.clock.multiplier = 3600

viewer.timeline.zoomTo(start, stop)

for (const geoJsonData of geoJsons) {
const timeSeriesData = processTimeSeriesData(geoJsonData)
const dataSource = createTimeSeriesEntities(
timeSeriesData,
start,
stop
)
const dataSource = createTimeSeriesEntities(timeSeriesData, start, stop)
viewer.dataSources.add(dataSource)
viewer.zoomTo(dataSource)
}
Expand All @@ -212,11 +168,9 @@
</html>
"""


def read_var(state, var):
return get_by_path(state, re.split("/", var))


class GeoPlot:
def __init__(self, config, options):
self.config = config
Expand All @@ -241,7 +195,6 @@ def render(self, state_trajectory):

for i in range(0, len(state_trajectory) - 1):
final_state = state_trajectory[i][-1]

coords = np.array(read_var(final_state, self.entity_position)).tolist()
values.append(
np.array(read_var(final_state, self.entity_property)).flatten().tolist()
Expand Down