I use this
var streamingczml = new Cesium.CzmlDataSource();
var pixelRange = 75;
var minimumClusterSize = 3;
var enabled = true;
streamingczml.clustering.enabled = enabled;
streamingczml.clustering.pixelRange = pixelRange;
streamingczml.clustering.minimumClusterSize = minimumClusterSize;
var czmlStreamUrl = ‘http://yoururl/streambaby.php’;
var czmlEventSource = new EventSource(czmlStreamUrl);
var quaternion: any;
var pi: number;
var lon: number;
var lat: number;
var center: any;
var hpr: any;
var mycog: number;
var heading: number;
var pitch: number;
var dataSources: any;
var roll: number;
var counter = 0;
pi = Cesium.Math.PI;
pitch = 0;
roll = 0.0;
var mabox = new Cesium.DistanceDisplayCondition(0, 15000);
var malabel = new Cesium.DistanceDisplayCondition(0, 150000);
czmlEventSource.addEventListener(“czml”, function (czmlUpdate: MessageEvent) { try {
var mamessag = JSON.parse(czmlUpdate.data)
//console.log(mydatasource.entities.getById(mamessag[1].id));
lat = mamessag[1].position.cartographicDegrees[1]
lon = mamessag[1].position.cartographicDegrees[0];
mycog = mamessag[1].properties.cog / 10;
center = new Cesium.Cartesian3.fromDegrees(lon, lat);
if (Cesium.Cartesian3.distance(dummyviewer.scene.camera.positionWC, center) < 50000) {
//console.log(Cesium.Cartesian3.distance(dummyviewer.scene.camera.positionWC, center) + “:” + mamessag[1].name);
heading = (pi * mycog) / 180;
hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
quaternion = new Cesium.Transforms.headingPitchRollQuaternion(center, hpr);
if (streamingczml.entities.getById(mamessag[1].id) == null) {
streamingczml.process(mamessag);
streamingczml.entities.getById(mamessag[1].id).model.distanceDisplayCondition = mabox;
streamingczml.entities.getById(mamessag[1].id).label.distanceDisplayCondition = malabel;
streamingczml.entities.getById(mamessag[1].id).model.scale = 8;
streamingczml.entities.getById(mamessag[1].id).model.asynchronous = true;
streamingczml.entities.getById(mamessag[1].id).model.minimumPixelSize = 4.0;
streamingczml.entities.getById(mamessag[1].id).model.shadows = false;
streamingczml.entities.getById(mamessag[1].id).model.heightReference = Cesium.HeightReference.CLAMP_TO_GROUND;
streamingczml.entities.getById(mamessag[1].id).label.eyeOffset = new Cesium.Cartesian3(0.0, 80.0, 0.0);
} else {
streamingczml.entities.getById(mamessag[1].id).label.eyeOffset = new Cesium.Cartesian3(0.0, 80.0, 0.0);
}
streamingczml.entities.getById(mamessag[1].id).position = Cesium.Cartesian3.fromDegrees(lon, lat);
tships = streamingczml.entities.values.length;
streamingczml.entities.getById(mamessag[1].id).orientation = quaternion;
} else {
if (streamingczml.entities.getById(mamessag[1].id) != null) {
var bla = streamingczml.entities.removeById(mamessag[1].id);
tships = streamingczml.entities.values.length;
}
}
} catch (t) {
console.error(t);
console.log(czmlUpdate.data)
}
});
this.viewer.dataSources.add(streamingczml);
The streaming php. I figure out the orientation in typescript so the orientation is a placeholder.
<?php
function czmlrow($mmsi,$name,$lon,$lat,$navstatus,$shiptype,$timestamp,$rot,$sog,$cog,$dimbow,$dimport,$dimstar,$dimstern,$draught) {
if ($rot == '') {
$rot .= '128';
}
if ($sog == '') {
$sog = '1023';
}
if ($cog == '') {
$cog = '3600';
}
if ($cog == ' ') {
$cog = '3600';
}
$length = 400000;
$width = 200000;
$depth = 100000;
if ($dimbow and $dimport and $dimstar and $dimstern) {
$length = ($dimbow + $dimstern)*10000;
$width = ($dimstar + $dimport)*10000;
$depth = ($width/2)*10000;
}
print "event: czml\n";
print "data: [ { \"id\":\"document\",";
print " \"name\":\"A Ships\",";
print " \"version\":\"1.0\"";
print "},";
$newname = str_replace('"','',$name);
print " {\"id\": \"$mmsi\",";
print " \"name\":\"$newname\",";
print " \"description\" :\"
Status: |
$navstatus |
Ship Type |
$shiptype |
AIS Type: |
A |
Reported at: |
$timestamp |
Rate of Turn: |
$rot |
Speed over Ground: |
$sog knots |
Course over Ground: |
$cog |
MMSI: |
$mmsi |
GPS to Bow: |
$dimbow |
GPS to Port: |
$dimport |
GPS to Starboard: |
$dimstar |
GPS to Stern: |
$dimstern |
Draught: |
$draught |
\",";
print "\"model\": {";
print " \"gltf\": \"http://localhost:4200/assets/models/myship.glb\",";
print " \"nodeTransformations\": {";
print " \"node0\": {";
print " \"scale\": {";
print " \"cartesian\": [";
print " $width, $length, $depth";
print " ]";
print " }";
print " }";
print " }";
print " },";
print " \"label\" : {\"fillColor\" : {\"rgba\" : [240, 98, 146, 255]},";
print " \"font\" : \"16pt Roboto Bold\",";
print " \"horizontalOrigin\" : \"CENTER\",";
print " \"style\" : \"FILL\",";
print " \"text\" : \"$newname\",";
print " \"showBackground\" : true,";
print " \"backgroundColor\" : { \"rgba\" : [38, 50, 56, 196] }";
print " },";
print " \"position\": { \"cartographicDegrees\": [$lon, $lat, 0] },";
print " \"properties\": {";
print " \"cog\": $cog,";
print " \"lon\": $lon,";
print " \"lat\": $lat";
print " },";
print " \"orientation\": {\"unitQuaternion\": [ -0.181377404434859,0.0902255173561898,0.102596391029801,0.973876570067245 ]}";
print "}]\n\n";
flush();
}
header('Cache-Control: no-cache');
header('Content-Type: text/event-stream');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Methods, Access-Control-Request-Headers, Cache-Control');
if ($_SERVER['REQUEST_METHOD'] !== 'OPTIONS') {
while(true)
{
$servername = "someip";
$username = "username";
$password = "password";
$today = date("Y-m-d H:i:s");
$yesterday = date("Y-m-d H:i:s", (strtotime ( '-5 second' , strtotime ( $today) ) ));
try {
$conn = new PDO("mysql:host=$servername;dbname=ships", $username, $password, array(PDO::ATTR_PERSISTENT => true));
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach($conn->query("SELECT AsText(Message5.LATLON), Message5.MMSI,Message5.NAME,Navstatus.NAVSTATUS, shiptype.TYPE, Message5.TIMESTAMP, Message5.SHIPTYPE, Message5.ROT, Message5.SOG, Message5.COG, Message5.DIMBOW, Message5.DIMPORT, Message5.DIMSTAR, Message5.DIMSTERN, Message5.DRAUGHT FROM Message5 LEFT JOIN Navstatus on Message5.POSTYPE = Navstatus.CODE LEFT JOIN shiptype on Message5.SHIPTYPE = shiptype.CODE WHERE Message5.LATLON IS NOT NULL AND Message5.TIMESTAMP > '$yesterday' ORDER BY Message5.TIMESTAMP DESC") as $row) {
$bla = preg_replace('/POINT\((.*)\)/','$1',$row["AsText(Message5.LATLON)"]);
$bla2 = explode(" ",$bla);
czmlrow($row["MMSI"],$row["NAME"],$bla2[0],$bla2[1],$row["NAVSTATUS"],$row["TYPE"],$row["TIMESTAMP"],$row["ROT"],$row["SOG"],$row["COG"],$row["DIMBOW"],$row["DIMPORT"],$row["DIMSTAR"],$row["DIMSTERN"],$row["DRAUGHT"]);
flush();
} // foreach
sleep(5);
$conn = null;
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
} // while
} // if
?>
``
sleep(x) will control your update interval.
Used here
http://www.deepship.ai/#/globe