Document toolboxDocument toolbox

GPS Calculation Guide

Motivation

Many systems have data stored in GPS coordinates (latitude, longitude). To be able to integrate and showcase them in twinzo - Digital Twin platform, they need to be recalculated to local twinzo coordinates. This guide will show you how.

GPS items creation

In twinzo portal go to bookmark Places/GPS items (documentation here). Without knowing where North is on the layout, you need at least 3 GPS items with x, y, lat, lon Strong preference for the corners of the sector layout.

GPS items download

Here is a screenshot from Postman, showing how to get GPS items downloaded to your software. Three important parameters are Client, Branch and Api-Key. If you are missing any of them please ask your contact person to provide them.

GPS item data structure

In the coming steps, we will show you examples from our Kotlin language implementation of a data structure and calculation.

GpsItem( @SerializedName("id") var id: Int? = null, @SerializedName("lat") var latitude: Double? = null, @SerializedName("lon") var longitude: Double? = null, @SerializedName("timestamp") var timestamp: Long? = null, @SerializedName("x") var X: Float? = null, @SerializedName("y") var Y: Float? = null, @SerializedName("distance") var distance: Double? = null )
class MyPoint extends Point{ MyPoint({required double x, required double y}): super (x, y); double angleTo(MyPoint in2){ return Vector2(this.x as double, this.y as double).angleTo(Vector2(in2.x as double, in2.y as double)); } MyPoint centerOfLineTo(MyPoint in2){ return new MyPoint( x: ((this.x + in2.x) / 2) * 1.0, y: ((this.y + in2.y) / 2) * 1.0 ); } }

GPS coordinates recalculation to twinzo coordinates

Now, with the established data structure we are ready to continue with the trilateration.

Distance from GPS item (myGpsItem contains my position in lat, lon only)

fun calculateDistanceForGpsItems(myGpsItem: GpsItem, GpsBeacons: ArrayList<GpsItem>) = runBlocking { var result: ArrayList<GpsItem> = ArrayList() for (item in GpsBeacons) { item.distance = distanceToGpsItems(myGpsItem.latitude!!, myGpsItem.longitude!!, item.latitude!!, item.longitude!!) * 1000 result.add(item) } result } fun distanceToGpsItems(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val R = 6371000 // metres val fi1 = Math.toRadians(lat1) val fi2 = Math.toRadians(lat2) val deltafi = Math.toRadians(lat2 - lat1) val deltalambda = Math.toRadians(lon2 - lon1) val a = Math.sin(deltafi / 2) * Math.sin(deltafi / 2) + Math.cos(fi1) * Math.cos(fi2) * Math.sin(deltalambda / 2) * Math.sin(deltalambda / 2) val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) val distance = R * c return distance }

Positioning Item

A little bit of mathematics ahead of us.

Needed functions (getClosestTangentTo & __calculateWeightedAverageFromIntersects)

CalculatePosition

And finally, the calculatePosition function will give us the desired result.

This position, in combination with Sector from where you obtained GPS items is enough to provide data to twinzo - Digital Twin platform.

Related pages

If you encounter any issues or need assistance with using this product, please do not hesitate to reach out for support. Our team is here to help you resolve any problems and answer any questions you may have.
To create a support ticket, visit our support portal at https://partner.twinzo.eu/helpdesk/customer-care-1