def CreateMISB(self): ''' Create MISB Video ''' ''' Only tested using DJI Data ''' # Create ProgressBar self.iface.messageBar().clearWidgets() progressMessageBar = self.iface.messageBar().createMessage("Creating video packets...") progress = QProgressBar() progress.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) progressMessageBar.layout().addWidget(progress) self.iface.messageBar().pushWidget(progressMessageBar, QGis.Info) QApplication.setOverrideCursor(Qt.WaitCursor) QApplication.processEvents() HFOV = self.sp_hfov.value() #VFOV = self.sp_vfov.value() index = self.cmb_telemetry.currentIndex() out_record = self.cmb_telemetry.itemData(index) rowCount = self.GetRows(out_record) progress.setMaximum(rowCount) d = {} with open(out_record) as csvfile: reader = csv.DictReader(csvfile) for row in reader: date_start = datetime.strptime(row["CUSTOM.updateTime"], '%Y/%m/%d %H:%M:%S.%f') break with open(out_record) as csvfile: reader = csv.DictReader(csvfile) for row in reader: for k in row: stripK = k.strip() stripV = row[k].strip() d[stripK] = stripV # We create the klv file for every moment sizeTotal = 0 bufferData = b'' cnt = 0 for k, v in d.items(): try: if k == "CUSTOM.updateTime": # We prevent it from failing in the exact times that don't have milliseconds try: date_end = datetime.strptime(v, '%Y/%m/%d %H:%M:%S.%f') except: date_end = datetime.strptime(v, '%Y/%m/%d %H:%M:%S') _bytes = datetime_to_bytes(date_end) _len = int_to_bytes(len(_bytes)) _bytes = _key2 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Platform Heading Angle if k == "OSD.yaw": OSD_yaw = float(v) _bytes = float_to_bytes(round(OSD_yaw, 4), _domain5, _range5) _len = int_to_bytes(len(_bytes)) _bytes = _key5 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Platform Pitch Angle if k == "OSD.pitch": OSD_pitch = 0.0 _bytes = float_to_bytes(round(OSD_pitch, 4), _domain6, _range6) _len = int_to_bytes(len(_bytes)) _bytes = _key6 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Platform Roll Angle if k == "OSD.roll": OSD_roll = 0.0 _bytes = float_to_bytes(round(OSD_roll, 4), _domain7, _range7) _len = int_to_bytes(len(_bytes)) _bytes = _key7 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Latitude if k == "OSD.latitude": OSD_latitude = float(v) _bytes = float_to_bytes(round(OSD_latitude, 4), _domain13, _range13) _len = int_to_bytes(len(_bytes)) _bytes = _key13 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Longitude if k == "OSD.longitude": OSD_longitude = float(v) _bytes = float_to_bytes(OSD_longitude, _domain14, _range14) _len = int_to_bytes(len(_bytes)) _bytes = _key14 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor True Altitude if k == "OSD.altitude [m]": OSD_altitude = float(v) _bytes = float_to_bytes(round(OSD_altitude, 4), _domain15, _range15) _len = int_to_bytes(len(_bytes)) _bytes = _key15 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Ellipsoid Height if k == "OSD.height [m]": OSD_height = float(v) _bytes = float_to_bytes(round(OSD_height, 4), _domain75, _range75) _len = int_to_bytes(len(_bytes)) _bytes = _key75 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Relative Azimuth Angle if k == "GIMBAL.yaw": #GIMBAL_yaw = float(v) GIMBAL_yaw = 0.0 _bytes = float_to_bytes(round(GIMBAL_yaw, 4), _domain18, _range18) _len = int_to_bytes(len(_bytes)) _bytes = _key18 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Relative Elevation Angle if k == "GIMBAL.pitch": GIMBAL_pitch = float(v) _bytes = float_to_bytes(round(GIMBAL_pitch, 4), _domain19, _range19) _len = int_to_bytes(len(_bytes)) _bytes = _key19 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Relative Roll Angle if k == "GIMBAL.roll": GIMBAL_roll = 0.0 _bytes = float_to_bytes(round(GIMBAL_roll, 4), _domain20, _range20) _len = int_to_bytes(len(_bytes)) _bytes = _key20 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes except Exception: print("Multiplexer error") continue try: # Diference time td = date_end - date_start end_path = self.klv_folder + "/%.1f.klv" % (round(td.total_seconds(), 1)) # CheckSum v = abs(hash(end_path)) % (10 ** 4) _bytes = int_to_bytes(v, 4) _len = int_to_bytes(len(_bytes)) _bytes = _key1 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Horizontal Field of View v = self.sp_hfov.value() _bytes = float_to_bytes(float(v), _domain16, _range16) _len = int_to_bytes(len(_bytes)) _bytes = _key16 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Sensor Vertical Field of View v = self.sp_vfov.value() _bytes = float_to_bytes(float(v), _domain17, _range17) _len = int_to_bytes(len(_bytes)) _bytes = _key17 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # TODO : Check these calculations # Slant Range anlge = 180 + (OSD_pitch + GIMBAL_pitch) slantRange = abs(OSD_altitude / (cos(radians(anlge)))) _bytes = float_to_bytes(round(slantRange, 4), _domain21, _range21) _len = int_to_bytes(len(_bytes)) _bytes = _key21 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Target Width # targetWidth = 0.0 targetWidth = 2.0 * slantRange * tan(radians(HFOV / 2.0)) _bytes = float_to_bytes(round(targetWidth, 4), _domain22, _range22) _len = int_to_bytes(len(_bytes)) _bytes = _key22 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Frame Center Latitude angle = 90 + (OSD_pitch + GIMBAL_pitch) tgHzDist = OSD_altitude * tan(radians(angle)) r_earth = 6371008.8 dy = tgHzDist * cos(radians(OSD_yaw)) framecenterlatitude = OSD_latitude + (dy / r_earth) * (180 / pi) _bytes = float_to_bytes(round(framecenterlatitude, 4), _domain23, _range23) _len = int_to_bytes(len(_bytes)) _bytes = _key23 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Frame Center Longitude dx = tgHzDist * sin(radians(OSD_yaw)) framecenterlongitude = OSD_longitude + (dx / r_earth) * (180 / pi) / cos(OSD_latitude * pi/180) _bytes = float_to_bytes(round(framecenterlongitude, 4), _domain24, _range24) _len = int_to_bytes(len(_bytes)) _bytes = _key24 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Frame Center Elevation frameCenterElevation = 0.0 _bytes = float_to_bytes(frameCenterElevation, _domain25, _range25) _len = int_to_bytes(len(_bytes)) _bytes = _key25 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # TODO : If we save the corners in the klv have a overflow # # CALCULATE CORNERS COORDINATES # sensor = (OSD_longitude, OSD_latitude, OSD_altitude) # frameCenter = (destPoint[0], destPoint[1], frameCenterElevation) # FOV = (VFOV, HFOV) # others = (OSD_yaw, GIMBAL_yaw, targetWidth, slantRange) # cornerPointUL, cornerPointUR, cornerPointLR, cornerPointLL = CornerEstimationWithoutOffsets(sensor=sensor, frameCenter=frameCenter, FOV=FOV, others=others) # # # Corner Latitude Point 1 (Full) # _bytes = float_to_bytes(round(cornerPointUL[0],4), _domain82, _range82) # _len = int_to_bytes(len(_bytes)) # _bytes = _key82 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # # # Corner Longitude Point 1 (Full) # _bytes = float_to_bytes(round(cornerPointUL[1],4), _domain83, _range83) # _len = int_to_bytes(len(_bytes)) # _bytes = _key83 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # # # Corner Latitude Point 2 (Full) # _bytes = float_to_bytes(round(cornerPointUR[0],4), _domain84, _range84) # _len = int_to_bytes(len(_bytes)) # _bytes = _key84 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # # # Corner Longitude Point 2 (Full) # _bytes = float_to_bytes(round(cornerPointUR[1],4), _domain85, _range85) # _len = int_to_bytes(len(_bytes)) # _bytes = _key85 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # # # Corner Latitude Point 3 (Full) # _bytes = float_to_bytes(round(cornerPointLR[0],4), _domain86, _range86) # _len = int_to_bytes(len(_bytes)) # _bytes = _key86 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # # # Corner Longitude Point 3 (Full) # _bytes = float_to_bytes(round(cornerPointLR[1],4), _domain87, _range87) # _len = int_to_bytes(len(_bytes)) # _bytes = _key87 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # # # Corner Latitude Point 4 (Full) # _bytes = float_to_bytes(round(cornerPointLL[0],4), _domain88, _range88) # _len = int_to_bytes(len(_bytes)) # _bytes = _key88 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # # # Corner Longitude Point 4 (Full) # _bytes = float_to_bytes(round(cornerPointLL[1],4), _domain89, _range89) # _len = int_to_bytes(len(_bytes)) # _bytes = _key89 + _len + _bytes # sizeTotal += len(_bytes) # bufferData += _bytes # Platform Pitch Angle (Full) _bytes = float_to_bytes(round(OSD_pitch, 4), _domain90, _range90) _len = int_to_bytes(len(_bytes)) _bytes = _key90 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # Platform Roll Angle (Full) _bytes = float_to_bytes(round(OSD_roll, 4), _domain91, _range91) _len = int_to_bytes(len(_bytes)) _bytes = _key91 + _len + _bytes sizeTotal += len(_bytes) bufferData += _bytes # set packet header writeData = cle writeData += int_to_bytes(sizeTotal) writeData += bufferData # Write packet f_write = open(end_path, "wb+") f_write.write(writeData) f_write.close() cnt += 1 progress.setValue(cnt) except Exception as e: print("Multiplexer error : " + str(e)) QApplication.restoreOverrideCursor() QApplication.processEvents() progress.setValue(rowCount) self.iface.messageBar().clearWidgets() # We add it to the manager _, name = os.path.split(self.video_file) self.parent.AddFileRowToManager(name, self.video_file, islocal=True, klv_folder=self.klv_folder) # Close dialog self.close() return
def CreateMISB(self): """ Create MISB Video """ """ Only tested using DJI Data """ # Create ProgressBar self.iface.messageBar().clearWidgets() progressMessageBar = self.iface.messageBar().createMessage( "Creating video packets...") progress = QProgressBar() progress.setAlignment(Qt.AlignLeft | Qt.AlignVCenter) progressMessageBar.layout().addWidget(progress) self.iface.messageBar().pushWidget(progressMessageBar, QGis.Info) QApplication.setOverrideCursor(Qt.WaitCursor) QApplication.processEvents() HFOV = self.sp_hfov.value() VFOV = self.sp_vfov.value() index = self.cmb_telemetry.currentIndex() out_record = self.cmb_telemetry.itemData(index) rowCount = self.GetRows(out_record) progress.setMaximum(rowCount) d = {} with open(out_record, encoding=encoding) as csvfile: reader = csv.DictReader(csvfile) for row in reader: date_start = datetime.strptime(row["CUSTOM.updateTime"], "%Y/%m/%d %H:%M:%S.%f") break with open(out_record, encoding=encoding) as csvfile: reader = csv.DictReader(csvfile) for row in reader: for k in row: stripK = k.strip() stripV = row[k].strip() d[stripK] = stripV # We create the klv file for every moment bufferData = b"" cnt = 0 for k, v in d.items(): try: if k == "CUSTOM.updateTime": # We prevent it from failing in the exact times # that don't have milliseconds try: date_end = datetime.strptime( v, "%Y/%m/%d %H:%M:%S.%f") except Exception: date_end = datetime.strptime( v, "%Y/%m/%d %H:%M:%S") _bytes = bytes( PrecisionTimeStamp( datetime_to_bytes(date_end))) bufferData += _bytes # Platform Heading Angle if k == "OSD.yaw": OSD_yaw = float(v) if OSD_yaw < 0: OSD_yaw = OSD_yaw + 360 _bytes = bytes(PlatformHeadingAngle(OSD_yaw)) bufferData += _bytes # Platform Pitch Angle if k == "OSD.pitch": OSD_pitch = float(v) _bytes = bytes(PlatformPitchAngle(OSD_pitch)) bufferData += _bytes # Platform Roll Angle if k == "OSD.roll": OSD_roll = float(v) _bytes = bytes(PlatformRollAngle(OSD_roll)) bufferData += _bytes # Sensor Latitude if k == "OSD.latitude": OSD_latitude = float(v) _bytes = bytes(SensorLatitude(OSD_latitude)) bufferData += _bytes # Sensor Longitude if k == "OSD.longitude": OSD_longitude = float(v) _bytes = bytes(SensorLongitude(OSD_longitude)) bufferData += _bytes # Sensor True Altitude if k == "OSD.altitude [m]": OSD_altitude = float(v) _bytes = bytes(SensorTrueAltitude(OSD_altitude)) bufferData += _bytes # Sensor Ellipsoid Height if k == "OSD.height [m]": OSD_height = float(v) _bytes = bytes( SensorEllipsoidHeightConversion(OSD_height)) bufferData += _bytes # Sensor Relative Azimuth Angle if k == "GIMBAL.yaw": # GIMBAL_yaw = float(v) GIMBAL_yaw = 0.0 _bytes = bytes( SensorRelativeAzimuthAngle(GIMBAL_yaw)) bufferData += _bytes # Sensor Relative Elevation Angle if k == "GIMBAL.pitch": GIMBAL_pitch = float(v) _bytes = bytes( SensorRelativeElevationAngle(GIMBAL_pitch)) bufferData += _bytes # Sensor Relative Roll Angle if k == "GIMBAL.roll": GIMBAL_roll = float(v) _bytes = bytes( SensorRelativeRollAngle(GIMBAL_roll)) bufferData += _bytes except Exception as e: qgsu.showUserAndLogMessage( QCoreApplication.translate( "Multiplexor", "Multiplexer error") + e, onlyLog=True, ) continue try: # Diference time td = date_end - date_start end_path = self.klv_folder + "/%.1f.klv" % (round( td.total_seconds(), 1)) # CheckSum v = abs(hash(end_path)) % (10**4) _bytes = bytes(Checksum(v)) bufferData += _bytes # Sensor Horizontal Field of View v = self.sp_hfov.value() _bytes = bytes(SensorHorizontalFieldOfView(float(v))) bufferData += _bytes # Sensor Vertical Field of View v = self.sp_vfov.value() _bytes = bytes(SensorVerticalFieldOfView(float(v))) bufferData += _bytes # TODO : Check these calculations # Slant Range anlge = 180 + (OSD_pitch + GIMBAL_pitch) slantRange = abs(OSD_altitude / (cos(radians(anlge)))) _bytes = bytes(SlantRange(slantRange)) bufferData += _bytes # Target Width # targetWidth = 0.0 targetWidth = 2.0 * slantRange * tan(radians(HFOV / 2.0)) try: _bytes = bytes(TargetWidth(targetWidth)) except Exception: _bytes = bytes(TargetWidth(0.0)) bufferData += _bytes # Frame Center Latitude angle = 90 + (OSD_pitch + GIMBAL_pitch) tgHzDist = OSD_altitude * tan(radians(angle)) dy = tgHzDist * cos(radians(OSD_yaw)) framecenterlatitude = OSD_latitude + degrees( (dy / EARTH_MEAN_RADIUS)) _bytes = bytes(FrameCenterLatitude(framecenterlatitude)) bufferData += _bytes # Frame Center Longitude dx = tgHzDist * sin(radians(OSD_yaw)) framecenterlongitude = OSD_longitude + degrees( (dx / EARTH_MEAN_RADIUS)) / cos(radians(OSD_latitude)) _bytes = bytes(FrameCenterLongitude(framecenterlongitude)) bufferData += _bytes # Frame Center Elevation frameCenterElevation = 0.0 _bytes = bytes(FrameCenterElevation(frameCenterElevation)) bufferData += _bytes # CALCULATE CORNERS COORDINATES # FIXME : If we add this values, the klv parse has a overflow # Probably the packets is not created correctly # sensor = (OSD_longitude, OSD_latitude, OSD_altitude) # frameCenter = (framecenterlongitude, framecenterlatitude, frameCenterElevation) # FOV = (VFOV, HFOV) # others = (OSD_yaw, GIMBAL_yaw, targetWidth, slantRange) # cornerPointUL, cornerPointUR, cornerPointLR, cornerPointLL = CornerEstimationWithoutOffsets(sensor=sensor, frameCenter=frameCenter, FOV=FOV, others=others) # # # Corner Latitude Point 1 (Full) CornerLatitudePoint1Full # _bytes = bytes(CornerLatitudePoint1Full(cornerPointUL[0])) # bufferData += _bytes # # # Corner Longitude Point 1 (Full) # _bytes = bytes(CornerLongitudePoint1Full(cornerPointUL[1])) # bufferData += _bytes # # # Corner Latitude Point 2 (Full) # _bytes = bytes(CornerLatitudePoint2Full(cornerPointUR[0])) # bufferData += _bytes # # # Corner Longitude Point 2 (Full) # _bytes = bytes(CornerLongitudePoint2Full(cornerPointUR[1])) # bufferData += _bytes # # # Corner Latitude Point 3 (Full) # _bytes = bytes(CornerLatitudePoint3Full(cornerPointLR[0])) # bufferData += _bytes # # # Corner Longitude Point 3 (Full) # _bytes = bytes(CornerLongitudePoint3Full(cornerPointLR[1])) # bufferData += _bytes # # # Corner Latitude Point 4 (Full) # _bytes = bytes(CornerLatitudePoint4Full(cornerPointLL[0])) # bufferData += _bytes # # # Corner Longitude Point 4 (Full) # _bytes = bytes(CornerLongitudePoint4Full(cornerPointLL[1])) # bufferData += _bytes # Platform Pitch Angle (Full) _bytes = bytes(PlatformPitchAngleFull(OSD_pitch)) bufferData += _bytes # Platform Roll Angle (Full) _bytes = bytes(PlatformRollAngleFull(OSD_roll)) bufferData += _bytes # set packet header writeData = UASLocalMetadataSet sizeTotal = len(bufferData) writeData += int_to_bytes(sizeTotal) writeData += bufferData # Write packet f_write = open(end_path, "wb+") f_write.write(writeData) f_write.close() cnt += 1 progress.setValue(cnt) except Exception as e: qgsu.showUserAndLogMessage( QCoreApplication.translate( "Multiplexor", "Multiplexer error") + str(e), onlyLog=True, ) QApplication.restoreOverrideCursor() QApplication.processEvents() progress.setValue(rowCount) self.iface.messageBar().clearWidgets() # We add it to the manager _, name = os.path.split(self.video_file) self.parent.AddFileRowToManager(name, self.video_file, islocal=True, klv_folder=self.klv_folder) # Close dialog self.close() return